diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/template/EntityPacketRewriter1_99.java b/common/src/main/java/com/viaversion/viaversion/protocols/template/EntityPacketRewriter1_99.java index fa887cc74..7750da436 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/template/EntityPacketRewriter1_99.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/template/EntityPacketRewriter1_99.java @@ -20,7 +20,6 @@ package com.viaversion.viaversion.protocols.template; import com.viaversion.viaversion.api.minecraft.RegistryEntry; import com.viaversion.viaversion.api.minecraft.entities.EntityType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.version.Types1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21; @@ -51,23 +50,22 @@ final class EntityPacketRewriter1_99 extends EntityRewriter { + final int entityId = wrapper.passthrough(Types.INT); // Entity id + wrapper.passthrough(Types.BOOLEAN); // Hardcore + wrapper.passthrough(Types.STRING_ARRAY); // World List + wrapper.passthrough(Types.VAR_INT); // Max players + wrapper.passthrough(Types.VAR_INT); // View distance + wrapper.passthrough(Types.VAR_INT); // Simulation distance + wrapper.passthrough(Types.BOOLEAN); // Reduced debug info + wrapper.passthrough(Types.BOOLEAN); // Show death screen + wrapper.passthrough(Types.BOOLEAN); // Limited crafting + + final int dimensionId = wrapper.passthrough(Types.VAR_INT); + final String world = wrapper.passthrough(Types.STRING); + trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world); + + trackPlayer(wrapper.user(), entityId); }); protocol.registerClientbound(ClientboundPackets1_21.RESPAWN, wrapper -> { diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java index d855104bc..47d60bce3 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/BlockRewriter.java @@ -35,7 +35,6 @@ import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.util.MathUtil; @@ -68,83 +67,61 @@ public class BlockRewriter { } public void registerBlockEvent(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(positionType); // Location - map(Types.UNSIGNED_BYTE); // Action id - map(Types.UNSIGNED_BYTE); // Action param - map(Types.VAR_INT); // Block id - /!\ NOT BLOCK STATE - handler(wrapper -> { - if (protocol.getMappingData().getBlockMappings() == null) { - return; - } + if (protocol.getMappingData().getBlockMappings() == null) { + return; + } - int id = wrapper.get(Types.VAR_INT, 0); - int mappedId = protocol.getMappingData().getNewBlockId(id); - if (mappedId == -1) { - // Block (action) has been removed - wrapper.cancel(); - return; - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(positionType); // Location + wrapper.passthrough(Types.UNSIGNED_BYTE); // Action id + wrapper.passthrough(Types.UNSIGNED_BYTE); // Action param + final int blockId = wrapper.passthrough(Types.VAR_INT); + final int mappedId = protocol.getMappingData().getNewBlockId(blockId); + if (mappedId == -1) { + wrapper.cancel(); + return; + } - wrapper.set(Types.VAR_INT, 0, mappedId); - }); + if (blockId != mappedId) { + wrapper.set(Types.VAR_INT, 0, mappedId); } }); } public void registerBlockUpdate(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(positionType); - map(Types.VAR_INT); - handler(wrapper -> wrapper.set(Types.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(wrapper.get(Types.VAR_INT, 0)))); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(positionType); + + final int blockId = wrapper.read(Types.VAR_INT); + wrapper.write(Types.VAR_INT, protocol.getMappingData().getNewBlockStateId(blockId)); }); } public void registerChunkBlocksUpdate(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.INT); // 0 - Chunk X - map(Types.INT); // 1 - Chunk Z - handler(wrapper -> { - for (BlockChangeRecord record : wrapper.passthrough(Types.BLOCK_CHANGE_ARRAY)) { - record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); - } - }); + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.INT); // Chunk X + wrapper.passthrough(Types.INT); // Chunk Z + for (BlockChangeRecord record : wrapper.passthrough(Types.BLOCK_CHANGE_ARRAY)) { + record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); } }); } public void registerSectionBlocksUpdate(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.LONG); // Chunk position - map(Types.BOOLEAN); // Suppress light updates - handler(wrapper -> { - for (BlockChangeRecord record : wrapper.passthrough(Types.VAR_LONG_BLOCK_CHANGE_ARRAY)) { - record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); - } - }); + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.LONG); // Chunk position + wrapper.passthrough(Types.BOOLEAN); // Suppress light updates + for (BlockChangeRecord record : wrapper.passthrough(Types.VAR_LONG_BLOCK_CHANGE_ARRAY)) { + record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); } }); } public void registerSectionBlocksUpdate1_20(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.LONG); // Chunk position - handler(wrapper -> { - for (BlockChangeRecord record : wrapper.passthrough(Types.VAR_LONG_BLOCK_CHANGE_ARRAY)) { - record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); - } - }); + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.LONG); // Chunk position + for (BlockChangeRecord record : wrapper.passthrough(Types.VAR_LONG_BLOCK_CHANGE_ARRAY)) { + record.setBlockId(protocol.getMappingData().getNewBlockStateId(record.getBlockId())); } }); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java index a07aca3fb..64edbd692 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java @@ -33,7 +33,6 @@ import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.State; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; import com.viaversion.viaversion.util.ComponentUtil; @@ -66,17 +65,11 @@ public class ComponentRewriter implements com.v } public void registerBossEvent(final C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.UUID); - map(Types.VAR_INT); - handler(wrapper -> { - final int action = wrapper.get(Types.VAR_INT, 0); - if (action == 0 || action == 3) { - passthroughAndProcess(wrapper); - } - }); + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.UUID); + final int action = wrapper.passthrough(Types.VAR_INT); + if (action == 0 || action == 3) { + passthroughAndProcess(wrapper); } }); } @@ -112,24 +105,18 @@ public class ComponentRewriter implements com.v } public void registerLegacyOpenWindow(final C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.UNSIGNED_BYTE); // Id - map(Types.STRING); // Window Type - handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT))); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.UNSIGNED_BYTE); // Id + wrapper.passthrough(Types.STRING); // Window Type + processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT)); }); } public void registerOpenScreen(final C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.VAR_INT); // Id - map(Types.VAR_INT); // Window Type - handler(wrapper -> passthroughAndProcess(wrapper)); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.VAR_INT); // Id + wrapper.passthrough(Types.VAR_INT); // Window Type + passthroughAndProcess(wrapper); }); } @@ -222,23 +209,17 @@ public class ComponentRewriter implements com.v } public void registerPlayerCombatKill(final C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.VAR_INT); // Player ID - map(Types.INT); // Killer ID - handler(wrapper -> processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT))); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.VAR_INT); // Player ID + wrapper.passthrough(Types.INT); // Killer ID + processText(wrapper.user(), wrapper.passthrough(Types.COMPONENT)); }); } public void registerPlayerCombatKill1_20(final C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.VAR_INT); // Player ID - handler(wrapper -> passthroughAndProcess(wrapper)); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.VAR_INT); // Player ID + passthroughAndProcess(wrapper); }); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java index 63f0f456b..f8cbb0805 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/EntityRewriter.java @@ -38,6 +38,7 @@ import com.viaversion.viaversion.api.minecraft.entitydata.EntityData; import com.viaversion.viaversion.api.minecraft.entitydata.EntityDataType; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.rewriter.RewriterBase; @@ -260,14 +261,11 @@ public abstract class EntityRewriter { + wrapper.passthrough(Types.VAR_INT); // Entity ID + wrapper.passthrough(Types.UUID); // Entity UUID + wrapper.passthrough(Types.VAR_INT); // Entity Type + trackerHandler().handle(wrapper); }); } @@ -275,15 +273,15 @@ public abstract class EntityRewriter { int entityId = wrapper.get(Types.VAR_INT, 0); @@ -297,31 +295,22 @@ public abstract class EntityRewriter { - if (protocol.getMappingData() == null) { - return; - } + protocol.registerClientbound(packetType, wrapper -> { + final int entityId = wrapper.passthrough(Types.VAR_INT); + wrapper.passthrough(Types.UUID); // Entity UUID + final int entityTypeId = wrapper.passthrough(Types.VAR_INT); + wrapper.passthrough(Types.DOUBLE); // X + wrapper.passthrough(Types.DOUBLE); // Y + wrapper.passthrough(Types.DOUBLE); // Z + wrapper.passthrough(Types.BYTE); // Pitch + wrapper.passthrough(Types.BYTE); // Yaw + wrapper.passthrough(Types.BYTE); // Head yaw + final int data = wrapper.passthrough(Types.VAR_INT); - int entityId = wrapper.get(Types.VAR_INT, 0); - EntityType entityType = tracker(wrapper.user()).entityType(entityId); - if (entityType == fallingBlockType) { - wrapper.set(Types.VAR_INT, 2, protocol.getMappingData().getNewBlockStateId(wrapper.get(Types.VAR_INT, 2))); - } - }); + final EntityType entityType = trackAndRewrite(wrapper, entityTypeId, entityId); + if (protocol.getMappingData() != null && entityType == fallingBlockType) { + final int mappedBlockStateId = protocol.getMappingData().getNewBlockStateId(data); + wrapper.set(Types.VAR_INT, 2, mappedBlockStateId); } }); } @@ -388,12 +377,13 @@ public abstract class EntityRewriter { - final EntityTracker tracker = tracker(wrapper.user()); - final int entityId = wrapper.get(Types.INT, 0); - tracker.setClientEntityId(entityId); - tracker.addEntity(entityId, tracker.playerType()); - }; + return wrapper -> trackPlayer(wrapper.user(), wrapper.get(Types.INT, 0)); + } + + public void trackPlayer(final UserConnection connection, final int entityId) { + final EntityTracker tracker = tracker(connection); + tracker.setClientEntityId(entityId); + tracker.addEntity(entityId, tracker.playerType()); } /** @@ -533,6 +523,17 @@ public abstract class EntityRewriter { int entityId = wrapper.get(Types.VAR_INT, 0); int type = wrapper.get(Types.VAR_INT, 1); - - int newType = newEntityId(type); - if (newType != type) { - wrapper.set(Types.VAR_INT, 1, newType); - } - - EntityType entType = typeFromId(trackMappedType ? newType : type); - // Register Type ID - tracker(wrapper.user()).addEntity(entityId, entType); - + trackAndRewrite(wrapper, type, entityId); if (dataType != null) { handleEntityData(entityId, wrapper.get(dataType, 0), wrapper.user()); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java index 9adab4b66..256e1c679 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ItemRewriter.java @@ -145,13 +145,10 @@ public class ItemRewriter passthroughClientboundItem(wrapper)); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.UNSIGNED_BYTE); // Container id + wrapper.passthrough(Types.SHORT); // Slot id + passthroughClientboundItem(wrapper); }); } @@ -174,56 +171,42 @@ public class ItemRewriter passthroughClientboundItem(wrapper)); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.VAR_INT); // Entity ID + wrapper.passthrough(Types.VAR_INT); // Slot ID + passthroughClientboundItem(wrapper); }); } // 1.16+ public void registerSetEquipment(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.VAR_INT); // 0 - Entity ID + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.VAR_INT); // Entity ID - handler(wrapper -> { - byte slot; - do { - slot = wrapper.passthrough(Types.BYTE); - // & 0x7F into an extra variable if slot is needed - passthroughClientboundItem(wrapper); - } while (slot < 0); - }); - } + byte slot; + do { + slot = wrapper.passthrough(Types.BYTE); + // & 0x7F into an extra variable if slot is needed + passthroughClientboundItem(wrapper); + } while (slot < 0); }); } public void registerSetCreativeModeSlot(S packetType) { - protocol.registerServerbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.SHORT); // 0 - Slot - handler(wrapper -> passthroughServerboundItem(wrapper)); - } + protocol.registerServerbound(packetType, wrapper -> { + wrapper.passthrough(Types.SHORT); // Slot + passthroughServerboundItem(wrapper); }); } public void registerContainerClick(S packetType) { - protocol.registerServerbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.UNSIGNED_BYTE); // 0 - Container ID - map(Types.SHORT); // 1 - Slot - map(Types.BYTE); // 2 - Button - map(Types.SHORT); // 3 - Action number - map(Types.VAR_INT); // 4 - Mode - handler(wrapper -> passthroughServerboundItem(wrapper)); - } + protocol.registerServerbound(packetType, wrapper -> { + wrapper.passthrough(Types.UNSIGNED_BYTE); // Container ID + wrapper.passthrough(Types.SHORT); // Slot + wrapper.passthrough(Types.BYTE); // Button + wrapper.passthrough(Types.SHORT); // Action number + wrapper.passthrough(Types.VAR_INT); // Mode + passthroughServerboundItem(wrapper); }); } @@ -460,22 +443,18 @@ public class ItemRewriter { - Mappings mappings = protocol.getMappingData().getEnchantmentMappings(); - if (mappings == null) { - return; - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.UNSIGNED_BYTE); // Container id - short property = wrapper.passthrough(Types.SHORT); - if (property >= 4 && property <= 6) { // Enchantment id - short enchantmentId = (short) mappings.getNewId(wrapper.read(Types.SHORT)); - wrapper.write(Types.SHORT, enchantmentId); - } - }); + Mappings mappings = protocol.getMappingData().getEnchantmentMappings(); + if (mappings == null) { + return; + } + + short property = wrapper.passthrough(Types.SHORT); + if (property >= 4 && property <= 6) { // Enchantment id + short enchantmentId = (short) mappings.getNewId(wrapper.read(Types.SHORT)); + wrapper.write(Types.SHORT, enchantmentId); } }); } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java index 15ffbadc1..ca4e03ed0 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/SoundRewriter.java @@ -22,7 +22,6 @@ import com.viaversion.viaversion.api.minecraft.SoundEvent; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.type.Types; public class SoundRewriter { @@ -40,12 +39,9 @@ public class SoundRewriter { } public void registerSound(C packetType) { - protocol.registerClientbound(packetType, new PacketHandlers() { - @Override - public void register() { - map(Types.VAR_INT); // Sound id - handler(getSoundHandler()); - } + protocol.registerClientbound(packetType, wrapper -> { + wrapper.passthrough(Types.VAR_INT); // Sound id + getSoundHandler().handle(wrapper); }); }