From d89a34cca313fd2fd2809184719f212866b11b66 Mon Sep 17 00:00:00 2001 From: Myles Date: Sun, 27 Mar 2016 19:13:19 +0100 Subject: [PATCH] Allow packets to be written to the server using passthrouh ID, shouldn't break anything I also updated comment on why interaction is half broken --- .../myles/ViaVersion/api/PacketWrapper.java | 20 ++++ .../ViaVersion/handlers/ViaDecodeHandler.java | 14 ++- .../ViaVersion/handlers/ViaEncodeHandler.java | 10 +- .../packets/WorldPackets.java | 100 ++++++++++++------ 4 files changed, 104 insertions(+), 40 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java b/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java index d7d17ed5c..08e4597dc 100644 --- a/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java +++ b/src/main/java/us/myles/ViaVersion/api/PacketWrapper.java @@ -10,6 +10,7 @@ import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.TypeConverter; import us.myles.ViaVersion.exception.InformativeException; +import us.myles.ViaVersion.handlers.ViaDecodeHandler; import java.io.IOException; import java.util.ArrayList; @@ -263,4 +264,23 @@ public class PacketWrapper { this.readableObjects.addAll(packetValues); this.packetValues.clear(); } + + public void sendToServer() throws Exception { + if (!isCancelled()) { + ByteBuf output = inputBuffer == null ? Unpooled.buffer() : inputBuffer.alloc().buffer(); + Type.VAR_INT.write(output, ViaDecodeHandler.PASSTHROUGH_ID); // Pass through + + writeToBuffer(output); + + boolean mark = false; + for (String s : user().getChannel().pipeline().names()) { + if (mark) { + user().getChannel().pipeline().context(user().getChannel().pipeline().get(s)).fireChannelRead(output); + return; + } + if (s.equalsIgnoreCase("decompress")) + mark = true; + } + } + } } diff --git a/src/main/java/us/myles/ViaVersion/handlers/ViaDecodeHandler.java b/src/main/java/us/myles/ViaVersion/handlers/ViaDecodeHandler.java index 862ba80ee..95532b996 100644 --- a/src/main/java/us/myles/ViaVersion/handlers/ViaDecodeHandler.java +++ b/src/main/java/us/myles/ViaVersion/handlers/ViaDecodeHandler.java @@ -7,7 +7,6 @@ import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.exception.CancelException; -import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.util.PipelineUtil; @@ -19,6 +18,7 @@ public class ViaDecodeHandler extends ByteToMessageDecoder { private final ByteToMessageDecoder minecraftDecoder; private final UserConnection info; + public static int PASSTHROUGH_ID = 1000; public ViaDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { this.info = info; @@ -37,10 +37,14 @@ public class ViaDecodeHandler extends ByteToMessageDecoder { // Transform ByteBuf newPacket = ctx.alloc().buffer(); try { - PacketWrapper wrapper = new PacketWrapper(id, bytebuf, info); - ProtocolInfo protInfo = info.get(ProtocolInfo.class); - protInfo.getPipeline().transform(Direction.INCOMING, protInfo.getState(), wrapper); - wrapper.writeToBuffer(newPacket); + if (id == ViaDecodeHandler.PASSTHROUGH_ID) { + newPacket.writeBytes(bytebuf); + } else { + PacketWrapper wrapper = new PacketWrapper(id, bytebuf, info); + ProtocolInfo protInfo = info.get(ProtocolInfo.class); + protInfo.getPipeline().transform(Direction.INCOMING, protInfo.getState(), wrapper); + wrapper.writeToBuffer(newPacket); + } bytebuf.clear(); bytebuf = newPacket; diff --git a/src/main/java/us/myles/ViaVersion/handlers/ViaEncodeHandler.java b/src/main/java/us/myles/ViaVersion/handlers/ViaEncodeHandler.java index 44c0d67f3..299bf9691 100644 --- a/src/main/java/us/myles/ViaVersion/handlers/ViaEncodeHandler.java +++ b/src/main/java/us/myles/ViaVersion/handlers/ViaEncodeHandler.java @@ -7,7 +7,6 @@ import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.exception.CancelException; -import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.util.PipelineUtil; @@ -48,11 +47,12 @@ public class ViaEncodeHandler extends MessageToByteEncoder { // Transform ByteBuf oldPacket = bytebuf.copy(); bytebuf.clear(); + try { - PacketWrapper wrapper = new PacketWrapper(id, oldPacket, info); - ProtocolInfo protInfo = info.get(ProtocolInfo.class); - protInfo.getPipeline().transform(Direction.OUTGOING, protInfo.getState(), wrapper); - wrapper.writeToBuffer(bytebuf); + PacketWrapper wrapper = new PacketWrapper(id, oldPacket, info); + ProtocolInfo protInfo = info.get(ProtocolInfo.class); + protInfo.getPipeline().transform(Direction.OUTGOING, protInfo.getState(), wrapper); + wrapper.writeToBuffer(bytebuf); } catch (Exception e) { bytebuf.clear(); throw e; diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java index 70335de9b..28d59265c 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/WorldPackets.java @@ -80,12 +80,12 @@ public class WorldPackets { } wrapper.set(Type.STRING, 0, newname); wrapper.write(Type.VAR_INT, catid); // Write Category ID - if(effect != null && effect.isBreaksound()) { + if (effect != null && effect.isBreaksound()) { EntityTracker tracker = wrapper.user().get(EntityTracker.class); int x = wrapper.passthrough(Type.INT); //Position X int y = wrapper.passthrough(Type.INT); //Position Y int z = wrapper.passthrough(Type.INT); //Position Z - if(tracker.interactedBlockRecently((int)Math.floor(x/8.0),(int)Math.floor(y/8.0),(int)Math.floor(z/8.0))) { + if (tracker.interactedBlockRecently((int) Math.floor(x / 8.0), (int) Math.floor(y / 8.0), (int) Math.floor(z / 8.0))) { wrapper.cancel(); return; } @@ -228,19 +228,6 @@ public class WorldPackets { protocol.registerIncoming(State.PLAY, -1, 0x1D, new PacketRemapper() { @Override public void registerMap() { - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - Long last = tracker.getLastPlaceBlock(); - if (last != -1) { - if ((wrapper.user().getReceivedPackets() - last) < 5) { - wrapper.cancel(); - } - tracker.setLastPlaceBlock(-1L); - } - } - }); handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { @@ -276,6 +263,46 @@ public class WorldPackets { wrapper.write(Type.BYTE, (byte) 0); } }); + /* + + The thing i've discovered is when using an item in air, it needs to send 2 packets. + I believe the issue is that this needs to be flipped with the packet above while still + sending block info. + + Otherwise no idea, the disadvantage: Interact does not get fired if you right click + special items. (there's quite a few...) + + */ +// handler(new PacketHandler() { +// @Override +// public void handle(PacketWrapper wrapper) throws Exception { +// if(wrapper.isCancelled()) return; +// EntityTracker tracker = wrapper.user().get(EntityTracker.class); +// if(tracker.isBlocking()) return; +// +// Long last = tracker.getLastPlaceBlock(); +// if (last != -1) { +// if ((wrapper.user().getReceivedPackets() - last) < 3) { +// tracker.setLastPlaceBlock(-1L); +// return; +// } +// tracker.setLastPlaceBlock(-1L); +// } +// final Item item = wrapper.get(Type.ITEM, 0); +// wrapper.create(0x08, new ValueCreator() { +// @Override +// public void write(PacketWrapper wrapper) throws Exception { +// wrapper.write(Type.POSITION, new Position(1L, 1L, 1L)); +// wrapper.write(Type.BYTE, (byte) 2); +// wrapper.write(Type.ITEM, item); // hand +// +// wrapper.write(Type.UNSIGNED_BYTE, (short) 1); +// wrapper.write(Type.UNSIGNED_BYTE, (short) 1); +// wrapper.write(Type.UNSIGNED_BYTE, (short) 1); +// } +// }).sendToServer(); +// } +// }); } }); @@ -322,11 +349,10 @@ public class WorldPackets { special = special || ArmorType.isArmor(m); // Don't send data if special if (special && m != Material.AIR) { - wrapper.set(Type.POSITION, 0, new Position(-1L, -1L, -1L)); - wrapper.set(Type.BYTE, 0, (byte) 255); + EntityTracker tracker = wrapper.user().get(EntityTracker.class); + tracker.setLastPlaceBlock(wrapper.user().getReceivedPackets()); } - EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.setLastPlaceBlock(wrapper.user().getReceivedPackets()); + } } } @@ -336,21 +362,35 @@ public class WorldPackets { handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - int face = wrapper.get(Type.BYTE,0); - if(face == 255) + int face = wrapper.get(Type.BYTE, 0); + if (face == 255) return; Position p = wrapper.get(Type.POSITION, 0); - long x = p.getX(); long y = p.getY(); long z = p.getZ(); - switch(face) { - case 0: y--; break; - case 1: y++; break; - case 2: z--; break; - case 3: z++; break; - case 4: x--; break; - case 5: x++; break; + long x = p.getX(); + long y = p.getY(); + long z = p.getZ(); + switch (face) { + case 0: + y--; + break; + case 1: + y++; + break; + case 2: + z--; + break; + case 3: + z++; + break; + case 4: + x--; + break; + case 5: + x++; + break; } EntityTracker tracker = wrapper.user().get(EntityTracker.class); - tracker.addBlockInteraction(new Position(x,y,z)); + tracker.addBlockInteraction(new Position(x, y, z)); } });