Finish inventory provider

+ Support 1.9+ servers
This commit is contained in:
mmxw11 2017-09-25 21:49:43 +03:00
parent b3d3921f6e
commit 1c4e0051a0
2 changed files with 14 additions and 15 deletions

View File

@ -40,12 +40,10 @@ public class BukkitInvContainerUpdateTask implements Runnable {
for (InvItemStorage storage : items) { for (InvItemStorage storage : items) {
Object packet = provider.buildWindowClickPacket(p, storage); Object packet = provider.buildWindowClickPacket(p, storage);
boolean result = provider.sendPlayer(p, packet); boolean result = provider.sendPlayer(p, packet);
System.out.println("item result: " + result);
if (!result) { if (!result) {
break; break;
} }
} }
System.out.println("items: " + items);
items.clear(); items.clear();
} }
} finally { } finally {

View File

@ -29,6 +29,7 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
private boolean supported; private boolean supported;
// packet class // packet class
private Class<?> wclickPacketClass; private Class<?> wclickPacketClass;
private Object clickTypeEnum;
// Use for nms // Use for nms
private Method nmsItemMethod; private Method nmsItemMethod;
private Method ephandle; private Method ephandle;
@ -76,16 +77,19 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
} }
Object cinstance = null; Object cinstance = null;
try { try {
Object nmsItem = nmsItemMethod.invoke(null, itemstack);
// TODO: PASSTHROUGH PROTOCOLS? (Make InvItemStorage inheritance from Item?)
cinstance = wclickPacketClass.newInstance(); cinstance = wclickPacketClass.newInstance();
Object nmsItem = nmsItemMethod.invoke(null, itemstack);
ReflectionUtil.set(cinstance, "a", (int) storage.getWindowId()); ReflectionUtil.set(cinstance, "a", (int) storage.getWindowId());
ReflectionUtil.set(cinstance, "slot", (int) slotId); ReflectionUtil.set(cinstance, "slot", (int) slotId);
ReflectionUtil.set(cinstance, "button", 0); // shift + left mouse click ReflectionUtil.set(cinstance, "button", 0); // shift + left mouse click
ReflectionUtil.set(cinstance, "d", storage.getActionNumber()); ReflectionUtil.set(cinstance, "d", storage.getActionNumber());
ReflectionUtil.set(cinstance, "item", nmsItem); ReflectionUtil.set(cinstance, "item", nmsItem);
// TODO: THIS MUST BE AN ENUM on 1.9+ SERVERS!! int protocolId = ProtocolRegistry.SERVER_PROTOCOL;
ReflectionUtil.set(cinstance, "shift", 1); // mode if (protocolId == 47) {
ReflectionUtil.set(cinstance, "shift", 1);
} else if (protocolId >= 107) { // 1.9+
ReflectionUtil.set(cinstance, "shift", clickTypeEnum);
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -115,7 +119,7 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
private void scheduleTask(BukkitInvContainerUpdateTask utask) { private void scheduleTask(BukkitInvContainerUpdateTask utask) {
BukkitScheduler scheduler = Bukkit.getServer().getScheduler(); BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
Plugin instance = Bukkit.getServer().getPluginManager().getPlugin("ViaVersion"); Plugin instance = Bukkit.getServer().getPluginManager().getPlugin("ViaVersion");
scheduler.runTaskLater(instance, utask, 5); // 5 ticks later. scheduler.runTaskLater(instance, utask, 2); // 2 ticks later (possible double click action).
} }
private void setupReflection() { private void setupReflection() {
@ -124,6 +128,9 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
} }
try { try {
this.wclickPacketClass = NMSUtil.nms("PacketPlayInWindowClick"); this.wclickPacketClass = NMSUtil.nms("PacketPlayInWindowClick");
Class<?> eclassz = NMSUtil.nms("InventoryClickType");
Object[] constants = eclassz.getEnumConstants();
this.clickTypeEnum = constants[1]; // QUICK_MOVE
Class<?> citemStack = NMSUtil.obc("inventory.CraftItemStack"); Class<?> citemStack = NMSUtil.obc("inventory.CraftItemStack");
this.nmsItemMethod = citemStack.getDeclaredMethod("asNMSCopy", ItemStack.class); this.nmsItemMethod = citemStack.getDeclaredMethod("asNMSCopy", ItemStack.class);
} catch (Exception e) { } catch (Exception e) {
@ -152,15 +159,9 @@ public class BukkitInvContainerItemProvider extends InvContainerItemProvider {
private boolean isSupported() { private boolean isSupported() {
int protocolId = ProtocolRegistry.SERVER_PROTOCOL; int protocolId = ProtocolRegistry.SERVER_PROTOCOL;
if (protocolId == 47) { if (protocolId >= 47 && protocolId <= 316) {
return true; // 1.8 return true; // 1.8
} /**else if (protocolId >= 107 && protocolId <= 110) { }
return true; // 1.9
} else if (protocolId == 210) {
return true; // 1.10
} else if (protocolId == 315 || protocolId == 316) {
return true; // 1.11
}*/
// this is not needed on 1.12+ // this is not needed on 1.12+
return false; return false;
} }