diff --git a/src/main/java/us/myles/ViaVersion/Core.java b/src/main/java/us/myles/ViaVersion/Core.java index 9eed8619a..8a114e0f2 100644 --- a/src/main/java/us/myles/ViaVersion/Core.java +++ b/src/main/java/us/myles/ViaVersion/Core.java @@ -8,6 +8,7 @@ import io.netty.channel.socket.SocketChannel; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.handlers.ViaVersionInitializer; @@ -69,4 +70,22 @@ public class Core extends JavaPlugin { return null; } } + + public static ItemStack getHandItem(final ConnectionInfo info) { + try { + return Bukkit.getScheduler().callSyncMethod(getPlugin(Core.class), new Callable() { + @Override + public ItemStack call() throws Exception { + if (info.getPlayer() != null) { + return info.getPlayer().getItemInHand(); + } + return null; + } + }).get(10, TimeUnit.SECONDS); + } catch (Exception e) { + System.out.println("Error fetching hand item "); + e.printStackTrace(); + return null; + } + } } diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index d5aab2b3b..6368a7464 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -2,15 +2,14 @@ package us.myles.ViaVersion.transformers; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; -import us.myles.ViaVersion.CancelException; -import us.myles.ViaVersion.ConnectionInfo; -import us.myles.ViaVersion.PacketUtil; -import us.myles.ViaVersion.ReflectionUtil; +import org.bukkit.inventory.ItemStack; +import us.myles.ViaVersion.*; import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; public class IncomingTransformer { private final Channel channel; @@ -168,8 +167,23 @@ public class IncomingTransformer { int face = PacketUtil.readVarInt(input); output.writeByte(face); int hand = PacketUtil.readVarInt(input); - // write item in hand - output.writeShort(-1); + + ItemStack inHand = Core.getHandItem(info); + Object item = null; + try { + Method m = ReflectionUtil.obc("inventory.CraftItemStack").getDeclaredMethod("asNMSCopy", ItemStack.class); + item = m.invoke(null, inHand); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + PacketUtil.writeItem(item, output); short curX = input.readUnsignedByte(); output.writeByte(curX);