From 28e483267d5919a1cc7e78299a5abc955f829a57 Mon Sep 17 00:00:00 2001 From: Shevchik Date: Thu, 6 Mar 2014 21:32:24 +0400 Subject: [PATCH] Rewrite the nms part, fix nope error completely --- src/com/cnaude/chairs/core/Chairs.java | 42 ++++++----------- src/com/cnaude/chairs/core/PlayerSitData.java | 47 +++++++------------ src/com/cnaude/chairs/core/api/ChairsAPI.java | 18 +++++++ .../listeners/TryUnsitEventListener.java | 22 ++------- .../vehiclearrow/GetVehicleArrowClass.java | 20 -------- .../cnaude/chairs/vehiclearrow/NMSAccess.java | 47 +++++++++++++++++++ .../vehiclearrow/NMSChairsArrowInterface.java | 17 +++++++ .../vehiclearrow/nms164/VehicleArrow.java | 22 --------- .../vehiclearrow/nms172/CraftChairsArrow.java | 30 ++++++++++++ .../vehiclearrow/nms172/NMSChairsArrow.java | 42 +++++++++++++++++ .../vehiclearrow/nms172/VehicleArrow.java | 22 --------- 11 files changed, 189 insertions(+), 140 deletions(-) create mode 100644 src/com/cnaude/chairs/core/api/ChairsAPI.java delete mode 100644 src/com/cnaude/chairs/vehiclearrow/GetVehicleArrowClass.java create mode 100644 src/com/cnaude/chairs/vehiclearrow/NMSAccess.java create mode 100644 src/com/cnaude/chairs/vehiclearrow/NMSChairsArrowInterface.java delete mode 100644 src/com/cnaude/chairs/vehiclearrow/nms164/VehicleArrow.java create mode 100644 src/com/cnaude/chairs/vehiclearrow/nms172/CraftChairsArrow.java create mode 100644 src/com/cnaude/chairs/vehiclearrow/nms172/NMSChairsArrow.java delete mode 100644 src/com/cnaude/chairs/vehiclearrow/nms172/VehicleArrow.java diff --git a/src/com/cnaude/chairs/core/Chairs.java b/src/com/cnaude/chairs/core/Chairs.java index 0295ff7..9787ed3 100644 --- a/src/com/cnaude/chairs/core/Chairs.java +++ b/src/com/cnaude/chairs/core/Chairs.java @@ -1,7 +1,6 @@ package com.cnaude.chairs.core; import java.io.File; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -9,24 +8,21 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.ChatColor; -import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Arrow; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.util.Vector; import com.cnaude.chairs.commands.ChairsCommand; import com.cnaude.chairs.commands.ChairsIgnoreList; +import com.cnaude.chairs.core.api.ChairsAPI; import com.cnaude.chairs.listeners.NANLoginListener; import com.cnaude.chairs.listeners.TrySitEventListener; import com.cnaude.chairs.listeners.TryUnsitEventListener; import com.cnaude.chairs.sitaddons.ChairEffects; import com.cnaude.chairs.sitaddons.CommandRestrict; -import com.cnaude.chairs.vehiclearrow.GetVehicleArrowClass; +import com.cnaude.chairs.vehiclearrow.NMSAccess; public class Chairs extends JavaPlugin { public ChairEffects chairEffects; @@ -54,28 +50,16 @@ public class Chairs extends JavaPlugin { public PlayerSitData getPlayerSitData() { return psitdata; } - private Class vehiclearrowclass; - protected Class getVehicleArrowClass() { - return vehiclearrowclass; + private NMSAccess nmsaccess = new NMSAccess(); + protected NMSAccess getNMSAccess() { + return nmsaccess; } - GetVehicleArrowClass genvehiclearrow = new GetVehicleArrowClass(); - @Override public void onEnable() { log = this.getLogger(); - //load vehiclearrowclass try { - World world = getServer().getWorlds().get(0); - Arrow arrow = world.spawnArrow(new Location(world, 0, 0, 0), new Vector(0, 0, 0), 0, 0); - String arrowclass = arrow.getClass().getName(); - Method getHandle; - getHandle = arrow.getClass().getDeclaredMethod("getHandle"); - getHandle.setAccessible(true); - Class entityarrow = getHandle.invoke(arrow).getClass(); - Class craftserver = getServer().getClass(); - vehiclearrowclass = genvehiclearrow.getVehicleArrowClass(arrowclass, entityarrow, craftserver); - arrow.remove(); + nmsaccess.setupVehicleArrow(); } catch (Exception e) { e.printStackTrace(); log.severe("Failed to generate VehicleArrow class, exiting"); @@ -100,24 +84,26 @@ public class Chairs extends JavaPlugin { getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); getServer().getPluginManager().registerEvents(new CommandRestrict(this), this); getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList)); + new ChairsAPI(getPlayerSitData()); } @Override public void onDisable() { for (Player player : getServer().getOnlinePlayers()) { if (psitdata.isSitting(player)) { - psitdata.unSitPlayer(player, false, true); + psitdata.unsitPlayerNow(player); } } if (ignoreList != null) { ignoreList.save(); } - chairEffects.cancelHealing(); - chairEffects.cancelPickup(); - chairEffects = null; + if (chairEffects != null) { + chairEffects.cancelHealing(); + chairEffects.cancelPickup(); + chairEffects = null; + } log = null; - genvehiclearrow = null; - vehiclearrowclass = null; + nmsaccess = null; psitdata = null; } diff --git a/src/com/cnaude/chairs/core/PlayerSitData.java b/src/com/cnaude/chairs/core/PlayerSitData.java index 23ec015..4f7d083 100644 --- a/src/com/cnaude/chairs/core/PlayerSitData.java +++ b/src/com/cnaude/chairs/core/PlayerSitData.java @@ -1,9 +1,6 @@ package com.cnaude.chairs.core; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.HashMap; import org.bukkit.Bukkit; @@ -11,7 +8,6 @@ import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.util.Vector; public class PlayerSitData { @@ -28,14 +24,6 @@ public class PlayerSitData { public boolean isSitting(Player player) { return sit.containsKey(player.getName()); } - public boolean isAroowOccupied(Entity entity) { - for (Entity usedentity : sit.values()) { - if (usedentity.getEntityId() == entity.getEntityId()) { - return true; - } - } - return false; - } public boolean isBlockOccupied(Block block) { return sitblock.containsKey(block); } @@ -70,8 +58,7 @@ public class PlayerSitData { },1000,1000); sittask.put(player.getName(), task); } - public void reSitPlayer(final Player player) - { + public void reSitPlayer(final Player player) { try { final Entity prevarrow = sit.get(player.getName()); sit.remove(player.getName()); @@ -91,31 +78,31 @@ public class PlayerSitData { } } 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 ,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 = plugin.getVehicleArrowClass().getDeclaredConstructor(Bukkit.getServer().getClass(), nmsarrow.getClass()); - ctor.setAccessible(true); - Object vehiclearrow = ctor.newInstance(Bukkit.getServer(), nmsarrow); - bukkitEntityField.set(nmsarrow, vehiclearrow); + Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc); arrow.setPassenger(player); return arrow; } - public void unSitPlayer(final Player player, boolean restoreposition, boolean correctleaveposition) { + public void unsitPlayerNormal(Player player) { + unsitPlayer(player, false, true, false); + } + public void unsitPlayerForce(Player player) { + unsitPlayer(player, true, true, false); + } + public void unsitPlayerNow(Player player) { + unsitPlayer(player, true, false, true); + } + private void unsitPlayer(final Player player, boolean eject, boolean restoreposition, boolean correctleaveposition) { final Entity arrow = sit.get(player.getName()); sit.remove(player.getName()); - player.eject(); + if (eject) { + player.eject(); + } arrow.remove(); final Location tploc = sitstopteleportloc.get(player.getName()); if (restoreposition) { - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() - { + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { @Override - public void run() - { + public void run() { player.teleport(tploc); player.setSneaking(false); } diff --git a/src/com/cnaude/chairs/core/api/ChairsAPI.java b/src/com/cnaude/chairs/core/api/ChairsAPI.java new file mode 100644 index 0000000..7a9bf89 --- /dev/null +++ b/src/com/cnaude/chairs/core/api/ChairsAPI.java @@ -0,0 +1,18 @@ +package com.cnaude.chairs.core.api; + +import org.bukkit.entity.Player; + +import com.cnaude.chairs.core.PlayerSitData; + +public class ChairsAPI { + + private static PlayerSitData pdata; + public ChairsAPI(PlayerSitData pdata) { + ChairsAPI.pdata = pdata; + } + + public static boolean isSitting(Player player) { + return pdata.isSitting(player); + } + +} diff --git a/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java b/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java index c53786b..17e913e 100644 --- a/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java +++ b/src/com/cnaude/chairs/listeners/TryUnsitEventListener.java @@ -1,8 +1,5 @@ package com.cnaude.chairs.listeners; -import java.util.HashSet; - -import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -28,7 +25,7 @@ public class TryUnsitEventListener implements Listener { public void onPlayerQuit(PlayerQuitEvent event) { Player player = event.getPlayer(); if (plugin.getPlayerSitData().isSitting(player)) { - plugin.getPlayerSitData().unSitPlayer(player, false, true); + plugin.getPlayerSitData().unsitPlayerNow(player); } } @@ -44,27 +41,16 @@ public class TryUnsitEventListener implements Listener { public void onPlayerDeath(PlayerDeathEvent event) { Player player = event.getEntity(); if (plugin.getPlayerSitData().isSitting(player)) { - plugin.getPlayerSitData().unSitPlayer(player, false, false); + plugin.getPlayerSitData().unsitPlayerNow(player); } } - private HashSet queueUnsit = new HashSet(); @EventHandler(priority=EventPriority.LOWEST) public void onExitVehicle(VehicleExitEvent e) { if (e.getVehicle().getPassenger() instanceof Player) { final Player player = (Player) e.getVehicle().getPassenger(); if (plugin.getPlayerSitData().isSitting(player)) { - e.setCancelled(true); - if (!queueUnsit.contains(player.getName())) { - queueUnsit.add(player.getName()); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - @Override - public void run() { - queueUnsit.remove(player.getName()); - plugin.getPlayerSitData().unSitPlayer(player, true, false); - } - }); - } + plugin.getPlayerSitData().unsitPlayerNormal(player); } } } @@ -74,7 +60,7 @@ public class TryUnsitEventListener implements Listener { Block b = event.getBlock(); if (plugin.getPlayerSitData().isBlockOccupied(b)) { Player player = plugin.getPlayerSitData().getPlayerOnChair(b); - plugin.getPlayerSitData().unSitPlayer(player, true, false); + plugin.getPlayerSitData().unsitPlayerForce(player); } } diff --git a/src/com/cnaude/chairs/vehiclearrow/GetVehicleArrowClass.java b/src/com/cnaude/chairs/vehiclearrow/GetVehicleArrowClass.java deleted file mode 100644 index 93c5f94..0000000 --- a/src/com/cnaude/chairs/vehiclearrow/GetVehicleArrowClass.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.cnaude.chairs.vehiclearrow; - -import org.bukkit.Bukkit; - -public class GetVehicleArrowClass { - - private String pkgname = this.getClass().getPackage().getName(); - private String vehiclearrowclassname = "VehicleArrow"; - public Class getVehicleArrowClass(String arrowclass, Class entityarrow, Class craftserver) throws Exception { - String packageName = Bukkit.getServer().getClass().getPackage().getName(); - String nmspackageversion = packageName.substring(packageName.lastIndexOf('.') + 1); - if (nmspackageversion.equals("v1_7_R1")) { - return Class.forName(pkgname+"."+"nms172"+"."+vehiclearrowclassname); - } else if (nmspackageversion.equals("v1_6_R3")) { - return Class.forName(pkgname+"."+"nms164"+"."+vehiclearrowclassname); - } - throw new Exception("ChairsReloaded is not compatible with your server version"); - } - -} diff --git a/src/com/cnaude/chairs/vehiclearrow/NMSAccess.java b/src/com/cnaude/chairs/vehiclearrow/NMSAccess.java new file mode 100644 index 0000000..099d158 --- /dev/null +++ b/src/com/cnaude/chairs/vehiclearrow/NMSAccess.java @@ -0,0 +1,47 @@ +package com.cnaude.chairs.vehiclearrow; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Arrow; + +public class NMSAccess { + + private Class nmsArrowClass; + + public void setupVehicleArrow() throws Exception { + String pkgname = getClass().getPackage().getName(); + String packageName = Bukkit.getServer().getClass().getPackage().getName(); + String nmspackageversion = packageName.substring(packageName.lastIndexOf('.') + 1); + switch (nmspackageversion) { + case "v1_7_R1": { + nmsArrowClass = Class.forName(pkgname+"."+"nms172"+".NMSChairsArrow"); + return; + } + case "v1_6_R3": { + nmsArrowClass = Class.forName(pkgname+"."+"nms164"+".NMSChairsArrow"); + return; + } + } + throw new Exception("ChairsReloaded is not compatible with your server version"); + } + + public Arrow spawnArrow(Location location) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { + World world = location.getWorld(); + Method getHandle = world.getClass().getDeclaredMethod("getHandle"); + getHandle.setAccessible(true); + Object nmsworld = getHandle.invoke(world); + Constructor ct = nmsArrowClass.getConstructor(nmsworld.getClass().getSuperclass()); + ct.setAccessible(true); + NMSChairsArrowInterface vehiclearrow = (NMSChairsArrowInterface) ct.newInstance(nmsworld); + vehiclearrow.setArrowLocation(location); + vehiclearrow.addToWorld(); + vehiclearrow.setBukkitEntity(Bukkit.getServer()); + return vehiclearrow.getBukkitArrow(); + } + +} diff --git a/src/com/cnaude/chairs/vehiclearrow/NMSChairsArrowInterface.java b/src/com/cnaude/chairs/vehiclearrow/NMSChairsArrowInterface.java new file mode 100644 index 0000000..2c5aac8 --- /dev/null +++ b/src/com/cnaude/chairs/vehiclearrow/NMSChairsArrowInterface.java @@ -0,0 +1,17 @@ +package com.cnaude.chairs.vehiclearrow; + +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.entity.Arrow; + +public interface NMSChairsArrowInterface { + + public void setBukkitEntity(Server server); + + public Arrow getBukkitArrow(); + + public void setArrowLocation(Location location); + + public void addToWorld(); + +} diff --git a/src/com/cnaude/chairs/vehiclearrow/nms164/VehicleArrow.java b/src/com/cnaude/chairs/vehiclearrow/nms164/VehicleArrow.java deleted file mode 100644 index 9bde3c1..0000000 --- a/src/com/cnaude/chairs/vehiclearrow/nms164/VehicleArrow.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.cnaude.chairs.vehiclearrow.nms164; - -import net.minecraft.server.v1_6_R3.EntityArrow; - -import org.bukkit.craftbukkit.v1_6_R3.CraftServer; -import org.bukkit.craftbukkit.v1_6_R3.entity.CraftArrow; -import org.bukkit.entity.Vehicle; - -public class VehicleArrow extends CraftArrow implements Vehicle { - - public VehicleArrow(CraftServer server, EntityArrow entity) { - super(server, entity); - } - - @Override - public void remove() { - if (isEmpty()) { - super.remove(); - } - } - -} diff --git a/src/com/cnaude/chairs/vehiclearrow/nms172/CraftChairsArrow.java b/src/com/cnaude/chairs/vehiclearrow/nms172/CraftChairsArrow.java new file mode 100644 index 0000000..66411ba --- /dev/null +++ b/src/com/cnaude/chairs/vehiclearrow/nms172/CraftChairsArrow.java @@ -0,0 +1,30 @@ +package com.cnaude.chairs.vehiclearrow.nms172; + +import net.minecraft.server.v1_7_R1.EntityArrow; + +import org.bukkit.craftbukkit.v1_7_R1.CraftServer; +import org.bukkit.craftbukkit.v1_7_R1.entity.CraftArrow; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Vehicle; + +import com.cnaude.chairs.core.api.ChairsAPI; + +public class CraftChairsArrow extends CraftArrow implements Vehicle { + + public CraftChairsArrow(CraftServer server, EntityArrow entity) { + super(server, entity); + } + + @Override + public void remove() { + Entity passenger = getPassenger(); + if (passenger != null && passenger instanceof Player) { + if (ChairsAPI.isSitting((Player) passenger)) { + return; + } + } + super.remove(); + } + +} diff --git a/src/com/cnaude/chairs/vehiclearrow/nms172/NMSChairsArrow.java b/src/com/cnaude/chairs/vehiclearrow/nms172/NMSChairsArrow.java new file mode 100644 index 0000000..ceb9592 --- /dev/null +++ b/src/com/cnaude/chairs/vehiclearrow/nms172/NMSChairsArrow.java @@ -0,0 +1,42 @@ +package com.cnaude.chairs.vehiclearrow.nms172; + +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.craftbukkit.v1_7_R1.CraftServer; +import org.bukkit.entity.Arrow; + +import com.cnaude.chairs.vehiclearrow.NMSChairsArrowInterface; + +import net.minecraft.server.v1_7_R1.EntityArrow; +import net.minecraft.server.v1_7_R1.World; + +public class NMSChairsArrow extends EntityArrow implements NMSChairsArrowInterface { + + public NMSChairsArrow(World world) { + super(world); + } + + @Override + public void h() { + } + + public void setBukkitEntity(Server server) { + bukkitEntity = new CraftChairsArrow((CraftServer) server, this); + } + + @Override + public Arrow getBukkitArrow() { + return (Arrow) bukkitEntity; + } + + @Override + public void setArrowLocation(Location location) { + setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); + } + + @Override + public void addToWorld() { + world.addEntity(this); + } + +} diff --git a/src/com/cnaude/chairs/vehiclearrow/nms172/VehicleArrow.java b/src/com/cnaude/chairs/vehiclearrow/nms172/VehicleArrow.java deleted file mode 100644 index 13d1a60..0000000 --- a/src/com/cnaude/chairs/vehiclearrow/nms172/VehicleArrow.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.cnaude.chairs.vehiclearrow.nms172; - -import net.minecraft.server.v1_7_R1.EntityArrow; - -import org.bukkit.craftbukkit.v1_7_R1.CraftServer; -import org.bukkit.craftbukkit.v1_7_R1.entity.CraftArrow; -import org.bukkit.entity.Vehicle; - -public class VehicleArrow extends CraftArrow implements Vehicle { - - public VehicleArrow(CraftServer server, EntityArrow entity) { - super(server, entity); - } - - @Override - public void remove() { - if (isEmpty()) { - super.remove(); - } - } - -}