From fe77df1434f8ae22d93405eb51fb685207f3e7f5 Mon Sep 17 00:00:00 2001 From: Myles Date: Sun, 6 Mar 2016 12:25:54 +0000 Subject: [PATCH] Fix brewing #141 --- .../transformers/IncomingTransformer.java | 49 ++++++++++--------- .../transformers/OutgoingTransformer.java | 22 ++++++++- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index ae39f2b8d..ecbb6e7a7 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -12,11 +12,8 @@ import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.slot.ItemSlotRewriter; import us.myles.ViaVersion.util.PacketUtil; -import us.myles.ViaVersion.util.ReflectionUtil; import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; public class IncomingTransformer { private final ConnectionInfo info; @@ -105,28 +102,32 @@ public class IncomingTransformer { byte button = input.readByte(); short action = input.readShort(); - byte mode = input.readByte(); - // if the action is on an elytra armour slot - if (slot == 45 && windowID == 0) { - try { - Class setSlot = ReflectionUtil.nms("PacketPlayOutSetSlot"); - Constructor setSlotConstruct = setSlot.getDeclaredConstructor(int.class, int.class, ReflectionUtil.nms("ItemStack")); - // properly construct - Object setSlotPacket = setSlotConstruct.newInstance(windowID, slot, null); - info.getChannel().pipeline().writeAndFlush(setSlotPacket); // slot is empty - slot = -999; // we're evil, they'll throw item on the ground - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } + int mode = input.readByte(); + // if the action is on an elytra armour slot + boolean throwItem = (slot == 45 && windowID == 0); + + if (info.getOpenWindow() != null && windowID > 0) { + if (info.getOpenWindow().equals("minecraft:brewing_stand")) { + if (slot == 4) { + // throw + throwItem = true; + } + if (slot > 4) + slot = (short) (slot - 1); + } + } + if (throwItem) { + ByteBuf buf = info.getChannel().alloc().buffer(); + PacketUtil.writeVarInt(PacketType.PLAY_SET_SLOT.getNewPacketID(), buf); + buf.writeByte(windowID); + buf.writeShort(slot); + buf.writeShort(-1); // empty + info.sendRawPacket(buf); + // Continue the packet simulating throw + mode = 0; + button = 0; + slot = -999; } output.writeByte(windowID); output.writeShort(slot); diff --git a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java index ef0fe1847..929a944b1 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java @@ -418,7 +418,7 @@ public class OutgoingTransformer { writeString(type, output); writeString(fixJson(windowTitle), output); int slots = input.readUnsignedByte(); - if(type.equals("minecraft:brewing_stand")){ + if (type.equals("minecraft:brewing_stand")) { slots = slots + 1; // new slot } output.writeByte(slots); @@ -433,6 +433,12 @@ public class OutgoingTransformer { output.writeByte(windowId); short slot = input.readShort(); + if (info.getOpenWindow() != null) { + if (info.getOpenWindow().equals("minecraft:brewing_stand")) { + if (slot >= 4) + slot = (short) (slot + 1); + } + } output.writeShort(slot); ItemSlotRewriter.rewrite1_8To1_9(input, output); return; @@ -442,10 +448,22 @@ public class OutgoingTransformer { output.writeByte(windowId); short count = input.readShort(); - output.writeShort(count); + boolean brewing = false; + if (info.getOpenWindow() != null && windowId > 0) { + if (info.getOpenWindow().equals("minecraft:brewing_stand")) { + brewing = true; + } + } + output.writeShort(brewing ? (count + 1) : count); for (int i = 0; i < count; i++) { + ItemSlotRewriter.rewrite1_8To1_9(input, output); + + // write "fuel" slot + if(brewing && i == 3){ + output.writeShort(-1); // empty slot + } } return; }