From e6f97895c9f67ac8620ee349675bb1d5a930da5d Mon Sep 17 00:00:00 2001 From: Shevchik Date: Sat, 14 Dec 2013 22:20:46 +0400 Subject: [PATCH] Remove duplicate code --- src/com/cnaude/chairs/Chairs.java | 46 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/com/cnaude/chairs/Chairs.java b/src/com/cnaude/chairs/Chairs.java index 3196271..6fa4e9b 100644 --- a/src/com/cnaude/chairs/Chairs.java +++ b/src/com/cnaude/chairs/Chairs.java @@ -3,6 +3,7 @@ package com.cnaude.chairs; import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; @@ -118,20 +119,9 @@ public class Chairs extends JavaPlugin { Block block = sitlocation.getBlock(); sitstopteleportloc.put(player.getName(), player.getLocation()); player.teleport(sitlocation); - player.setSneaking(false); Location arrowloc = block.getLocation().add(0.5, 0 , 0.5); - Entity arrow = player.getWorld().spawnArrow(arrowloc, new Vector(0, 0.1 ,0), 0, 0); - Method getHandleMethod = arrow.getClass().getDeclaredMethod("getHandle"); - getHandleMethod.setAccessible(true); - Object nmsarrow = getHandleMethod.invoke(arrow); - Field bukkitEntityField = nmsarrow.getClass().getSuperclass().getDeclaredField("bukkitEntity"); - bukkitEntityField.setAccessible(true); - Constructor ctor = vehiclearrowclass.getDeclaredConstructor(this.getServer().getClass(), nmsarrow.getClass()); - ctor.setAccessible(true); - Object vehiclearrow = ctor.newInstance(this.getServer(), nmsarrow); - bukkitEntityField.set(nmsarrow, vehiclearrow); - arrow.setPassenger(player); - sit.put(player.getName(), (Entity) vehiclearrow); + Entity arrow = sitPlayerOnArrow(player, arrowloc); + sit.put(player.getName(), arrow); sitblock.put(block, player.getName()); sitblockbr.put(player.getName(), block); startReSitTask(player); @@ -139,7 +129,6 @@ public class Chairs extends JavaPlugin { e.printStackTrace(); } } - protected void startReSitTask(final Player player) { int task = @@ -159,18 +148,8 @@ public class Chairs extends JavaPlugin { sit.remove(player.getName()); player.eject(); Block block = sitblockbr.get(player.getName()); - Location arrowloc = block.getLocation().add(0.5, 0 , 0.5); - Entity arrow = player.getWorld().spawnArrow(arrowloc, new Vector(0, 0.1 ,0), 0, 0); - Method getHandleMethod = arrow.getClass().getDeclaredMethod("getHandle"); - getHandleMethod.setAccessible(true); - Object nmsarrow = getHandleMethod.invoke(arrow); - Field bukkitEntityField = nmsarrow.getClass().getSuperclass().getDeclaredField("bukkitEntity"); - bukkitEntityField.setAccessible(true); - Constructor ctor = vehiclearrowclass.getDeclaredConstructor(this.getServer().getClass(), nmsarrow.getClass()); - ctor.setAccessible(true); - Object vehiclearrow = ctor.newInstance(this.getServer(), nmsarrow); - bukkitEntityField.set(nmsarrow, vehiclearrow); - arrow.setPassenger(player); + Location arrowloc = block.getLocation().add(0.5, 0 , 0.5); + Entity arrow = sitPlayerOnArrow(player, arrowloc); sit.put(player.getName(), arrow); Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() { @@ -183,6 +162,21 @@ public class Chairs extends JavaPlugin { e.printStackTrace(); } } + private Entity sitPlayerOnArrow(Player player, Location arrowloc) throws NoSuchMethodException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException + { + Entity arrow = player.getWorld().spawnArrow(arrowloc, new Vector(0, 0.1 ,0), 0, 0); + Method getHandleMethod = arrow.getClass().getDeclaredMethod("getHandle"); + getHandleMethod.setAccessible(true); + Object nmsarrow = getHandleMethod.invoke(arrow); + Field bukkitEntityField = nmsarrow.getClass().getSuperclass().getDeclaredField("bukkitEntity"); + bukkitEntityField.setAccessible(true); + Constructor ctor = vehiclearrowclass.getDeclaredConstructor(this.getServer().getClass(), nmsarrow.getClass()); + ctor.setAccessible(true); + Object vehiclearrow = ctor.newInstance(this.getServer(), nmsarrow); + bukkitEntityField.set(nmsarrow, vehiclearrow); + arrow.setPassenger(player); + return arrow; + } protected void unSitPlayer(final Player player, boolean ignoretp) { final Entity arrow = sit.get(player.getName());