Rewrite the nms part, fix nope error completely

This commit is contained in:
Shevchik 2014-03-06 21:32:24 +04:00
parent 805131ce85
commit 28e483267d
11 changed files with 189 additions and 140 deletions

View File

@ -1,7 +1,6 @@
package com.cnaude.chairs.core; package com.cnaude.chairs.core;
import java.io.File; import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -9,24 +8,21 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import com.cnaude.chairs.commands.ChairsCommand; import com.cnaude.chairs.commands.ChairsCommand;
import com.cnaude.chairs.commands.ChairsIgnoreList; import com.cnaude.chairs.commands.ChairsIgnoreList;
import com.cnaude.chairs.core.api.ChairsAPI;
import com.cnaude.chairs.listeners.NANLoginListener; import com.cnaude.chairs.listeners.NANLoginListener;
import com.cnaude.chairs.listeners.TrySitEventListener; import com.cnaude.chairs.listeners.TrySitEventListener;
import com.cnaude.chairs.listeners.TryUnsitEventListener; import com.cnaude.chairs.listeners.TryUnsitEventListener;
import com.cnaude.chairs.sitaddons.ChairEffects; import com.cnaude.chairs.sitaddons.ChairEffects;
import com.cnaude.chairs.sitaddons.CommandRestrict; import com.cnaude.chairs.sitaddons.CommandRestrict;
import com.cnaude.chairs.vehiclearrow.GetVehicleArrowClass; import com.cnaude.chairs.vehiclearrow.NMSAccess;
public class Chairs extends JavaPlugin { public class Chairs extends JavaPlugin {
public ChairEffects chairEffects; public ChairEffects chairEffects;
@ -54,28 +50,16 @@ public class Chairs extends JavaPlugin {
public PlayerSitData getPlayerSitData() { public PlayerSitData getPlayerSitData() {
return psitdata; return psitdata;
} }
private Class<?> vehiclearrowclass; private NMSAccess nmsaccess = new NMSAccess();
protected Class<?> getVehicleArrowClass() { protected NMSAccess getNMSAccess() {
return vehiclearrowclass; return nmsaccess;
} }
GetVehicleArrowClass genvehiclearrow = new GetVehicleArrowClass();
@Override @Override
public void onEnable() { public void onEnable() {
log = this.getLogger(); log = this.getLogger();
//load vehiclearrowclass
try { try {
World world = getServer().getWorlds().get(0); nmsaccess.setupVehicleArrow();
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();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
log.severe("Failed to generate VehicleArrow class, exiting"); 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 TryUnsitEventListener(this), this);
getServer().getPluginManager().registerEvents(new CommandRestrict(this), this); getServer().getPluginManager().registerEvents(new CommandRestrict(this), this);
getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList)); getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList));
new ChairsAPI(getPlayerSitData());
} }
@Override @Override
public void onDisable() { public void onDisable() {
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
if (psitdata.isSitting(player)) { if (psitdata.isSitting(player)) {
psitdata.unSitPlayer(player, false, true); psitdata.unsitPlayerNow(player);
} }
} }
if (ignoreList != null) { if (ignoreList != null) {
ignoreList.save(); ignoreList.save();
} }
chairEffects.cancelHealing(); if (chairEffects != null) {
chairEffects.cancelPickup(); chairEffects.cancelHealing();
chairEffects = null; chairEffects.cancelPickup();
chairEffects = null;
}
log = null; log = null;
genvehiclearrow = null; nmsaccess = null;
vehiclearrowclass = null;
psitdata = null; psitdata = null;
} }

View File

@ -1,9 +1,6 @@
package com.cnaude.chairs.core; package com.cnaude.chairs.core;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -11,7 +8,6 @@ import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
public class PlayerSitData { public class PlayerSitData {
@ -28,14 +24,6 @@ public class PlayerSitData {
public boolean isSitting(Player player) { public boolean isSitting(Player player) {
return sit.containsKey(player.getName()); 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) { public boolean isBlockOccupied(Block block) {
return sitblock.containsKey(block); return sitblock.containsKey(block);
} }
@ -70,8 +58,7 @@ public class PlayerSitData {
},1000,1000); },1000,1000);
sittask.put(player.getName(), task); sittask.put(player.getName(), task);
} }
public void reSitPlayer(final Player player) public void reSitPlayer(final Player player) {
{
try { try {
final Entity prevarrow = sit.get(player.getName()); final Entity prevarrow = sit.get(player.getName());
sit.remove(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 { 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); Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc);
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);
arrow.setPassenger(player); arrow.setPassenger(player);
return arrow; 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()); final Entity arrow = sit.get(player.getName());
sit.remove(player.getName()); sit.remove(player.getName());
player.eject(); if (eject) {
player.eject();
}
arrow.remove(); arrow.remove();
final Location tploc = sitstopteleportloc.get(player.getName()); final Location tploc = sitstopteleportloc.get(player.getName());
if (restoreposition) { if (restoreposition) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
{
@Override @Override
public void run() public void run() {
{
player.teleport(tploc); player.teleport(tploc);
player.setSneaking(false); player.setSneaking(false);
} }

View File

@ -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);
}
}

View File

@ -1,8 +1,5 @@
package com.cnaude.chairs.listeners; package com.cnaude.chairs.listeners;
import java.util.HashSet;
import org.bukkit.Bukkit;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -28,7 +25,7 @@ public class TryUnsitEventListener implements Listener {
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (plugin.getPlayerSitData().isSitting(player)) { 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) { public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity(); Player player = event.getEntity();
if (plugin.getPlayerSitData().isSitting(player)) { if (plugin.getPlayerSitData().isSitting(player)) {
plugin.getPlayerSitData().unSitPlayer(player, false, false); plugin.getPlayerSitData().unsitPlayerNow(player);
} }
} }
private HashSet<String> queueUnsit = new HashSet<String>();
@EventHandler(priority=EventPriority.LOWEST) @EventHandler(priority=EventPriority.LOWEST)
public void onExitVehicle(VehicleExitEvent e) { public void onExitVehicle(VehicleExitEvent e) {
if (e.getVehicle().getPassenger() instanceof Player) { if (e.getVehicle().getPassenger() instanceof Player) {
final Player player = (Player) e.getVehicle().getPassenger(); final Player player = (Player) e.getVehicle().getPassenger();
if (plugin.getPlayerSitData().isSitting(player)) { if (plugin.getPlayerSitData().isSitting(player)) {
e.setCancelled(true); plugin.getPlayerSitData().unsitPlayerNormal(player);
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);
}
});
}
} }
} }
} }
@ -74,7 +60,7 @@ public class TryUnsitEventListener implements Listener {
Block b = event.getBlock(); Block b = event.getBlock();
if (plugin.getPlayerSitData().isBlockOccupied(b)) { if (plugin.getPlayerSitData().isBlockOccupied(b)) {
Player player = plugin.getPlayerSitData().getPlayerOnChair(b); Player player = plugin.getPlayerSitData().getPlayerOnChair(b);
plugin.getPlayerSitData().unSitPlayer(player, true, false); plugin.getPlayerSitData().unsitPlayerForce(player);
} }
} }

View File

@ -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");
}
}

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}
}