From 1c1ec10a0aef9ea621181254767da2fdd6f0cbae Mon Sep 17 00:00:00 2001 From: KennyTV <28825609+KennyTV@users.noreply.github.com> Date: Wed, 11 Dec 2019 12:03:30 +0100 Subject: [PATCH 1/2] Common methods for block rewriting --- .../api/rewriters/BlockRewriter.java | 129 ++++++++++++++++ .../packets/WorldPackets.java | 107 ++------------ .../packets/PlayerPackets.java | 138 +++++++++--------- .../packets/WorldPackets.java | 95 ++---------- 4 files changed, 217 insertions(+), 252 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java new file mode 100644 index 000000000..f1ad56850 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java @@ -0,0 +1,129 @@ +package us.myles.ViaVersion.api.rewriters; + +import us.myles.ViaVersion.api.PacketWrapper; +import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; +import us.myles.ViaVersion.api.minecraft.Position; +import us.myles.ViaVersion.api.minecraft.item.Item; +import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.remapper.PacketHandler; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.packets.State; + +// If any of these methods become outdated, just create a new rewriter overriding the methods +public class BlockRewriter { + private final Protocol protocol; + private final BlockIdRewriteFunction blockStateRewriter; + private final BlockIdRewriteFunction blockRewriter; + private final Type positionType; + + public BlockRewriter(Protocol protocol, Type positionType, BlockIdRewriteFunction blockStateRewriter, BlockIdRewriteFunction blockRewriter) { + this.protocol = protocol; + this.positionType = positionType; + this.blockStateRewriter = blockStateRewriter; + this.blockRewriter = blockRewriter; + } + + public void registerBlockAction(int oldPacketId, int newPacketId) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(positionType); // Location + map(Type.UNSIGNED_BYTE); // Action id + map(Type.UNSIGNED_BYTE); // Action param + map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE + handler(wrapper -> wrapper.set(Type.VAR_INT, 0, blockRewriter.rewrite(wrapper.get(Type.VAR_INT, 0)))); + } + }); + } + + public void registerBlockChange(int oldPacketId, int newPacketId) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(positionType); + map(Type.VAR_INT); + handler(wrapper -> wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(wrapper.get(Type.VAR_INT, 0)))); + } + }); + } + + public void registerMultiBlockChange(int oldPacketId, int newPacketId) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Chunk X + map(Type.INT); // 1 - Chunk Z + map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records + handler(wrapper -> { + for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { + record.setBlockId(blockStateRewriter.rewrite(record.getBlockId())); + } + }); + } + }); + } + + public void registerAcknowledgePlayerDigging(int oldPacketId, int newPacketId) { + // Same exact handler + registerBlockChange(oldPacketId, newPacketId); + } + + public void registerEffect(int oldPacketId, int newPacketId, int playRecordId, int blockBreakId, ItemRewriter.ItemIdRewriteFunction itemIdRewriteFunction) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // Effect Id + map(positionType); // Location + map(Type.INT); // Data + handler(wrapper -> { + int id = wrapper.get(Type.INT, 0); + int data = wrapper.get(Type.INT, 1); + if (id == playRecordId) { // Play record + wrapper.set(Type.INT, 1, itemIdRewriteFunction.rewrite(data)); + } else if (id == blockBreakId) { // Block break + block break sound + wrapper.set(Type.INT, 1, blockStateRewriter.rewrite(data)); + } + }); + } + }); + } + + public void registerSpawnParticle(Type coordType, int oldPacketId, int newPacketId, int blockId, int fallingDustId, int itemId, + ItemRewriter.RewriteFunction itemRewriteFunction, Type itemType) { + protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Particle ID + map(Type.BOOLEAN); // 1 - Long Distance + map(coordType); // 2 - X + map(coordType); // 3 - Y + map(coordType); // 4 - Z + map(Type.FLOAT); // 5 - Offset X + map(Type.FLOAT); // 6 - Offset Y + map(Type.FLOAT); // 7 - Offset Z + map(Type.FLOAT); // 8 - Particle Data + map(Type.INT); // 9 - Particle Count + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + int id = wrapper.get(Type.INT, 0); + if (id == blockId || id == fallingDustId) { + int data = wrapper.passthrough(Type.VAR_INT); + wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(data)); + } else if (id == itemId) { + // Has to be like this, until we make *everything* object oriented inside of each protocol :( + itemRewriteFunction.rewrite(wrapper.passthrough(itemType)); + } + } + }); + } + }); + } + + @FunctionalInterface + public interface BlockIdRewriteFunction { + + int rewrite(int itemId); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java index 805bea0c3..1540f235f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java @@ -1,12 +1,12 @@ package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.BlockRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13_1to1_13.Protocol1_13_1To1_13; @@ -16,6 +16,8 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class WorldPackets { public static void register(Protocol protocol) { + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13_1To1_13::getNewBlockStateId, Protocol1_13_1To1_13::getNewBlockId); + //Chunk protocol.registerOutgoing(State.PLAY, 0x22, 0x22, new PacketRemapper() { @Override @@ -37,81 +39,17 @@ public class WorldPackets { } }); - // Block Action - protocol.registerOutgoing(State.PLAY, 0x0A, 0x0A, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.POSITION); // Location - map(Type.UNSIGNED_BYTE); // Action id - map(Type.UNSIGNED_BYTE); // Action param - map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.set(Type.VAR_INT, 0, Protocol1_13_1To1_13.getNewBlockId(wrapper.get(Type.VAR_INT, 0))); - } - }); - } - }); + // Block action + blockRewriter.registerBlockAction(0x0A, 0x0A); // Block Change - protocol.registerOutgoing(State.PLAY, 0xB, 0xB, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.POSITION); - map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.VAR_INT, 0); - - wrapper.set(Type.VAR_INT, 0, Protocol1_13_1To1_13.getNewBlockStateId(id)); - } - }); - } - }); + blockRewriter.registerBlockChange(0xB, 0xB); // Multi Block Change - protocol.registerOutgoing(State.PLAY, 0xF, 0xF, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Chunk X - map(Type.INT); // 1 - Chunk Z - map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - // Convert ids - for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - int id = record.getBlockId(); - record.setBlockId(Protocol1_13_1To1_13.getNewBlockStateId(id)); - } - } - }); - } - }); + blockRewriter.registerMultiBlockChange(0xF, 0xF); // Effect packet - protocol.registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // Effect Id - map(Type.POSITION); // Location - map(Type.INT); // Data - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - int data = wrapper.get(Type.INT, 1); - if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, InventoryPackets.getNewItemId(data)); - } else if (id == 2001) { // Block break + block break sound - wrapper.set(Type.INT, 1, Protocol1_13_1To1_13.getNewBlockStateId(data)); - } - } - }); - } - }); + blockRewriter.registerEffect(0x23, 0x23, 1010, 2001, InventoryPackets::getNewItemId); //join game protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() { @@ -150,33 +88,6 @@ public class WorldPackets { }); //spawn particle - protocol.registerOutgoing(State.PLAY, 0x24, 0x24, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Particle ID - map(Type.BOOLEAN); // 1 - Long Distance - map(Type.FLOAT); // 2 - X - map(Type.FLOAT); // 3 - Y - map(Type.FLOAT); // 4 - Z - map(Type.FLOAT); // 5 - Offset X - map(Type.FLOAT); // 6 - Offset Y - map(Type.FLOAT); // 7 - Offset Z - map(Type.FLOAT); // 8 - Particle Data - map(Type.INT); // 9 - Particle Count - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - if (id == 3 || id == 20) { - int data = wrapper.passthrough(Type.VAR_INT); - wrapper.set(Type.VAR_INT, 0, Protocol1_13_1To1_13.getNewBlockStateId(data)); - } else if (id == 27) { - InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM)); - } - } - }); - } - }); + blockRewriter.registerSpawnParticle(Type.FLOAT, 0x24, 0x24, 3, 20, 27, InventoryPackets::toClient, Type.FLAT_ITEM); } - } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java index b097ad0b0..b1c70e761 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/PlayerPackets.java @@ -14,78 +14,78 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class PlayerPackets { - public static void register(Protocol protocol) { - // Respawn - protocol.registerOutgoing(State.PLAY, 0x3A, 0x3B, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); - int dimensionId = wrapper.get(Type.INT, 0); - clientWorld.setEnvironment(dimensionId); - } - }); - create(new ValueCreator() { - @Override - public void write(PacketWrapper wrapper) throws Exception { - wrapper.write(Type.LONG, 0L); // Level Seed - } - }); - } - }); + public static void register(Protocol protocol) { + // Respawn + protocol.registerOutgoing(State.PLAY, 0x3A, 0x3B, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + ClientWorld clientWorld = wrapper.user().get(ClientWorld.class); + int dimensionId = wrapper.get(Type.INT, 0); + clientWorld.setEnvironment(dimensionId); + } + }); + create(new ValueCreator() { + @Override + public void write(PacketWrapper wrapper) throws Exception { + wrapper.write(Type.LONG, 0L); // Level Seed + } + }); + } + }); - // Join Game - protocol.registerOutgoing(State.PLAY, 0x25, 0x26, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Entity ID - map(Type.UNSIGNED_BYTE); // 1 - Gamemode - map(Type.INT); // 2 - Dimension + // Join Game + protocol.registerOutgoing(State.PLAY, 0x25, 0x26, new PacketRemapper() { + @Override + public void registerMap() { + map(Type.INT); // 0 - Entity ID + map(Type.UNSIGNED_BYTE); // 1 - Gamemode + map(Type.INT); // 2 - Dimension - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - Entity1_15Types.EntityType entType = Entity1_15Types.EntityType.PLAYER; - EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class); - tracker.addEntity(wrapper.get(Type.INT, 0), entType); - } - }); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - // Store the player - ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); - int dimensionId = wrapper.get(Type.INT, 1); - clientChunks.setEnvironment(dimensionId); + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + Entity1_15Types.EntityType entType = Entity1_15Types.EntityType.PLAYER; + EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class); + tracker.addEntity(wrapper.get(Type.INT, 0), entType); + } + }); + handler(new PacketHandler() { + @Override + public void handle(PacketWrapper wrapper) throws Exception { + // Store the player + ClientWorld clientChunks = wrapper.user().get(ClientWorld.class); + int dimensionId = wrapper.get(Type.INT, 1); + clientChunks.setEnvironment(dimensionId); - // Register Type ID - EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class); - int entityId = wrapper.get(Type.INT, 0); - tracker.addEntity(entityId, Entity1_15Types.EntityType.PLAYER); - } - }); - create(new ValueCreator() { - @Override - public void write(PacketWrapper wrapper) throws Exception { - wrapper.write(Type.LONG, 0L); // Level Seed - } - }); + // Register Type ID + EntityTracker1_15 tracker = wrapper.user().get(EntityTracker1_15.class); + int entityId = wrapper.get(Type.INT, 0); + tracker.addEntity(entityId, Entity1_15Types.EntityType.PLAYER); + } + }); + create(new ValueCreator() { + @Override + public void write(PacketWrapper wrapper) throws Exception { + wrapper.write(Type.LONG, 0L); // Level Seed + } + }); - map(Type.UNSIGNED_BYTE); // 3 - Max Players - map(Type.STRING); // 4 - Level Type - map(Type.VAR_INT); // 5 - View Distance - map(Type.BOOLEAN); // 6 - Reduce Debug Info + map(Type.UNSIGNED_BYTE); // 3 - Max Players + map(Type.STRING); // 4 - Level Type + map(Type.VAR_INT); // 5 - View Distance + map(Type.BOOLEAN); // 6 - Reduce Debug Info - create(new ValueCreator() { - @Override - public void write(PacketWrapper wrapper) throws Exception { - wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn()); // Show Death Screen - } - }); - } - }); - } + create(new ValueCreator() { + @Override + public void write(PacketWrapper wrapper) throws Exception { + wrapper.write(Type.BOOLEAN, !Via.getConfig().is1_15InstantRespawn()); // Show Death Screen + } + }); + } + }); + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java index a3578e146..f070b4663 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/packets/WorldPackets.java @@ -1,12 +1,12 @@ package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.packets; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.BlockRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type; @@ -17,75 +17,19 @@ import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; public class WorldPackets { public static void register(Protocol protocol) { - // Acknowledge player digging - protocol.registerOutgoing(State.PLAY, 0x5C, 0x08, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.POSITION1_14); - map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int blockState = wrapper.get(Type.VAR_INT, 0); - wrapper.set(Type.VAR_INT, 0, Protocol1_15To1_14_4.getNewBlockStateId(blockState)); - } - }); - } - }); + BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14, Protocol1_15To1_14_4::getNewBlockStateId, Protocol1_15To1_14_4::getNewBlockId); - // Block Action - protocol.registerOutgoing(State.PLAY, 0x0A, 0x0B, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.POSITION1_14); // Location - map(Type.UNSIGNED_BYTE); // Action id - map(Type.UNSIGNED_BYTE); // Action param - map(Type.VAR_INT); // Block id - /!\ NOT BLOCK STATE - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - wrapper.set(Type.VAR_INT, 0, Protocol1_15To1_14_4.getNewBlockId(wrapper.get(Type.VAR_INT, 0))); - } - }); - } - }); + // Block action + blockRewriter.registerBlockAction(0x0A, 0x0B); // Block Change - protocol.registerOutgoing(State.PLAY, 0x0B, 0x0C, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.POSITION1_14); - map(Type.VAR_INT); - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.VAR_INT, 0); - - wrapper.set(Type.VAR_INT, 0, Protocol1_15To1_14_4.getNewBlockStateId(id)); - } - }); - } - }); + blockRewriter.registerBlockChange(0x0B, 0x0C); // Multi Block Change - protocol.registerOutgoing(State.PLAY, 0x0F, 0x10, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Chunk X - map(Type.INT); // 1 - Chunk Z - map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - // Convert ids - for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - int id = record.getBlockId(); - record.setBlockId(Protocol1_15To1_14_4.getNewBlockStateId(id)); - } - } - }); - } - }); + blockRewriter.registerMultiBlockChange(0x0F, 0x10); + + // Acknowledge player digging + blockRewriter.registerAcknowledgePlayerDigging(0x5C, 0x08); // Chunk Data protocol.registerOutgoing(State.PLAY, 0x21, 0x22, new PacketRemapper() { @@ -135,26 +79,7 @@ public class WorldPackets { }); // Effect - protocol.registerOutgoing(State.PLAY, 0x22, 0x23, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // Effect Id - map(Type.POSITION1_14); // Location - map(Type.INT); // Data - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - int data = wrapper.get(Type.INT, 1); - if (id == 1010) { // Play record - wrapper.set(Type.INT, 1, InventoryPackets.getNewItemId(data)); - } else if (id == 2001) { // Block break + block break sound - wrapper.set(Type.INT, 1, data = Protocol1_15To1_14_4.getNewBlockStateId(data)); - } - } - }); - } - }); + blockRewriter.registerEffect(0x22, 0x23, 1010, 2001, InventoryPackets::getNewItemId); // Spawn Particle protocol.registerOutgoing(State.PLAY, 0x23, 0x24, new PacketRemapper() { From 5d974858fe4fad930545b2abc80a5b029f0b9592 Mon Sep 17 00:00:00 2001 From: KennyTV <28825609+KennyTV@users.noreply.github.com> Date: Fri, 20 Dec 2019 17:53:14 +0100 Subject: [PATCH 2/2] Some more cleanup --- .../api/rewriters/BlockRewriter.java | 46 ++++++++------- .../api/rewriters/IdRewriteFunction.java | 7 +++ .../api/rewriters/ItemRewriter.java | 8 +-- .../packets/WorldPackets.java | 58 ++----------------- 4 files changed, 38 insertions(+), 81 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/rewriters/IdRewriteFunction.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java index f1ad56850..3b628d5d3 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/BlockRewriter.java @@ -1,11 +1,9 @@ package us.myles.ViaVersion.api.rewriters; -import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.protocol.Protocol; -import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; @@ -13,11 +11,11 @@ import us.myles.ViaVersion.packets.State; // If any of these methods become outdated, just create a new rewriter overriding the methods public class BlockRewriter { private final Protocol protocol; - private final BlockIdRewriteFunction blockStateRewriter; - private final BlockIdRewriteFunction blockRewriter; + private final IdRewriteFunction blockStateRewriter; + private final IdRewriteFunction blockRewriter; private final Type positionType; - public BlockRewriter(Protocol protocol, Type positionType, BlockIdRewriteFunction blockStateRewriter, BlockIdRewriteFunction blockRewriter) { + public BlockRewriter(Protocol protocol, Type positionType, IdRewriteFunction blockStateRewriter, IdRewriteFunction blockRewriter) { this.protocol = protocol; this.positionType = positionType; this.blockStateRewriter = blockStateRewriter; @@ -69,7 +67,7 @@ public class BlockRewriter { registerBlockChange(oldPacketId, newPacketId); } - public void registerEffect(int oldPacketId, int newPacketId, int playRecordId, int blockBreakId, ItemRewriter.ItemIdRewriteFunction itemIdRewriteFunction) { + public void registerEffect(int oldPacketId, int newPacketId, int playRecordId, int blockBreakId, IdRewriteFunction itemIdRewriteFunction) { protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { @Override public void registerMap() { @@ -91,6 +89,11 @@ public class BlockRewriter { public void registerSpawnParticle(Type coordType, int oldPacketId, int newPacketId, int blockId, int fallingDustId, int itemId, ItemRewriter.RewriteFunction itemRewriteFunction, Type itemType) { + registerSpawnParticle(coordType, oldPacketId, newPacketId, blockId, fallingDustId, itemId, null, itemRewriteFunction, itemType); + } + + public void registerSpawnParticle(Type coordType, int oldPacketId, int newPacketId, int blockId, int fallingDustId, int itemId, + IdRewriteFunction particleRewriteFunction, ItemRewriter.RewriteFunction itemRewriteFunction, Type itemType) { protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { @Override public void registerMap() { @@ -104,26 +107,25 @@ public class BlockRewriter { map(Type.FLOAT); // 7 - Offset Z map(Type.FLOAT); // 8 - Particle Data map(Type.INT); // 9 - Particle Count - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - if (id == blockId || id == fallingDustId) { - int data = wrapper.passthrough(Type.VAR_INT); - wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(data)); - } else if (id == itemId) { - // Has to be like this, until we make *everything* object oriented inside of each protocol :( - itemRewriteFunction.rewrite(wrapper.passthrough(itemType)); + handler(wrapper -> { + int id = wrapper.get(Type.INT, 0); + if (id == -1) return; + if (id == blockId || id == fallingDustId) { + int data = wrapper.passthrough(Type.VAR_INT); + wrapper.set(Type.VAR_INT, 0, blockStateRewriter.rewrite(data)); + } else if (id == itemId) { + // Has to be like this, until we make *everything* object oriented inside of each protocol :( + itemRewriteFunction.rewrite(wrapper.passthrough(itemType)); + } + + if (particleRewriteFunction != null) { + int newId = particleRewriteFunction.rewrite(id); + if (newId != id) { + wrapper.set(Type.INT, 0, newId); } } }); } }); } - - @FunctionalInterface - public interface BlockIdRewriteFunction { - - int rewrite(int itemId); - } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/IdRewriteFunction.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/IdRewriteFunction.java new file mode 100644 index 000000000..05075e3f3 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/IdRewriteFunction.java @@ -0,0 +1,7 @@ +package us.myles.ViaVersion.api.rewriters; + +@FunctionalInterface +public interface IdRewriteFunction { + + int rewrite(int id); +} \ No newline at end of file diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java index 8314c03a1..32be6c86f 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/ItemRewriter.java @@ -85,7 +85,7 @@ public class ItemRewriter { }); } - public void registerSetCooldown(int oldPacketId, int newPacketId, ItemIdRewriteFunction itemIDRewriteFunction) { + public void registerSetCooldown(int oldPacketId, int newPacketId, IdRewriteFunction itemIDRewriteFunction) { protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { @Override public void registerMap() { @@ -120,10 +120,4 @@ public class ItemRewriter { void rewrite(Item item); } - - @FunctionalInterface - public interface ItemIdRewriteFunction { - - int rewrite(int itemId); - } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 5e9cd678f..390c00cf2 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -5,7 +5,6 @@ import com.github.steveice10.opennbt.tag.builtin.LongArrayTag; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.entities.Entity1_14Types; -import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.BlockFace; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; @@ -14,6 +13,7 @@ import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.remapper.ValueCreator; +import us.myles.ViaVersion.api.rewriters.BlockRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type; @@ -39,6 +39,7 @@ public class WorldPackets { } public static void register(final Protocol protocol) { + BlockRewriter blockRewriter = new BlockRewriter(protocol, null, Protocol1_14To1_13_2::getNewBlockStateId, Protocol1_14To1_13_2::getNewBlockId); // Block Break Animation protocol.registerOutgoing(State.PLAY, 0x08, 0x08, new PacketRemapper() { @@ -107,24 +108,7 @@ public class WorldPackets { }); // Multi Block Change - protocol.registerOutgoing(State.PLAY, 0x0F, 0x0F, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Chunk X - map(Type.INT); // 1 - Chunk Z - map(Type.BLOCK_CHANGE_RECORD_ARRAY); // 2 - Records - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - // Convert ids - for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) { - int id = record.getBlockId(); - record.setBlockId(Protocol1_14To1_13_2.getNewBlockStateId(id)); - } - } - }); - } - }); + blockRewriter.registerMultiBlockChange(0x0F, 0x0F); // Explosion protocol.registerOutgoing(State.PLAY, 0x1E, 0x1C, new PacketRemapper() { @@ -302,39 +286,9 @@ public class WorldPackets { } }); - // Spawn Particle - protocol.registerOutgoing(State.PLAY, 0x24, 0x23, new PacketRemapper() { - @Override - public void registerMap() { - map(Type.INT); // 0 - Particle ID - map(Type.BOOLEAN); // 1 - Long Distance - map(Type.FLOAT); // 2 - X - map(Type.FLOAT); // 3 - Y - map(Type.FLOAT); // 4 - Z - map(Type.FLOAT); // 5 - Offset X - map(Type.FLOAT); // 6 - Offset Y - map(Type.FLOAT); // 7 - Offset Z - map(Type.FLOAT); // 8 - Particle Data - map(Type.INT); // 9 - Particle Count - handler(new PacketHandler() { - @Override - public void handle(PacketWrapper wrapper) throws Exception { - int id = wrapper.get(Type.INT, 0); - if (id == 3 || id == 20) { - int data = wrapper.passthrough(Type.VAR_INT); - wrapper.set(Type.VAR_INT, 0, Protocol1_14To1_13_2.getNewBlockStateId(data)); - } else if (id == 27) { - InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); - } - - int newId = MetadataRewriter1_14To1_13_2.getNewParticleId(id); - if (newId != id) { - wrapper.set(Type.INT, 0, newId); - } - } - }); - } - }); + // Spawn particle + blockRewriter.registerSpawnParticle(Type.FLOAT, 0x24, 0x23, 3, 20, 27, + MetadataRewriter1_14To1_13_2::getNewParticleId, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM); // Join Game protocol.registerOutgoing(State.PLAY, 0x25, 0x25, new PacketRemapper() {