diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagType.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/RegistryType.java similarity index 76% rename from common/src/main/java/us/myles/ViaVersion/api/rewriters/TagType.java rename to common/src/main/java/us/myles/ViaVersion/api/rewriters/RegistryType.java index f11f2b5be..711bcc1b2 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagType.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/RegistryType.java @@ -1,6 +1,6 @@ package us.myles.ViaVersion.api.rewriters; -public enum TagType { +public enum RegistryType { BLOCK, ITEM, diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/StatisticsRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/StatisticsRewriter.java new file mode 100644 index 000000000..2ed3b52c4 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/StatisticsRewriter.java @@ -0,0 +1,104 @@ +package us.myles.ViaVersion.api.rewriters; + +import org.jetbrains.annotations.Nullable; +import us.myles.ViaVersion.api.protocol.ClientboundPacketType; +import us.myles.ViaVersion.api.protocol.Protocol; +import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.type.Type; + +public class StatisticsRewriter { + private final Protocol protocol; + private final IdRewriteFunction blockRewriter; + private final IdRewriteFunction itemRewriter; + private final IdRewriteFunction entityRewriter; + private final IdRewriteFunction categoryIdRewriter; + + public StatisticsRewriter(Protocol protocol, + @Nullable IdRewriteFunction blockRewriter, @Nullable IdRewriteFunction itemRewriter, @Nullable IdRewriteFunction entityRewriter, + @Nullable IdRewriteFunction categoryIdRewriter) { + this.protocol = protocol; + this.blockRewriter = blockRewriter; + this.itemRewriter = itemRewriter; + this.entityRewriter = entityRewriter; + this.categoryIdRewriter = categoryIdRewriter; + } + + public StatisticsRewriter(Protocol protocol, @Nullable IdRewriteFunction blockRewriter, @Nullable IdRewriteFunction itemRewriter, @Nullable IdRewriteFunction entityRewriter) { + this(protocol, blockRewriter, itemRewriter, entityRewriter, null); + } + + public void register(ClientboundPacketType packetType) { + protocol.registerOutgoing(packetType, new PacketRemapper() { + @Override + public void registerMap() { + handler(wrapper -> { + int size = wrapper.passthrough(Type.VAR_INT); + int newSize = size; + for (int i = 0; i < size; i++) { + int categoryId = wrapper.read(Type.VAR_INT); + int statisticId = wrapper.read(Type.VAR_INT); + int value = wrapper.read(Type.VAR_INT); + // Rewrite category id + if (categoryIdRewriter != null) { + categoryId = categoryIdRewriter.rewrite(categoryId); + if (categoryId == -1) { + // Remove entry + newSize--; + continue; + } + } + + wrapper.write(Type.VAR_INT, categoryId); + + RegistryType type = getRegistryTypeForStatistic(categoryId); + IdRewriteFunction statisticsRewriter; + // Rewrite the block/item/entity id + if (type != null && (statisticsRewriter = getRewriter(type)) != null) { + wrapper.write(Type.VAR_INT, statisticsRewriter.rewrite(statisticId)); + } else { + wrapper.write(Type.VAR_INT, statisticId); + } + + wrapper.write(Type.VAR_INT, value); + } + + if (newSize != size) { + wrapper.set(Type.VAR_INT, 0, newSize); + } + }); + } + }); + } + + @Nullable + protected IdRewriteFunction getRewriter(RegistryType type) { + switch (type) { + case BLOCK: + return blockRewriter; + case ITEM: + return itemRewriter; + case ENTITY: + return entityRewriter; + } + throw new IllegalArgumentException("Unknown registry type in statistics packet: " + type); + } + + @Nullable + public RegistryType getRegistryTypeForStatistic(int statisticsId) { + switch (statisticsId) { + case 0: + return RegistryType.BLOCK; + case 1: + case 2: + case 3: + case 4: + case 5: + return RegistryType.ITEM; + case 6: + case 7: + return RegistryType.ENTITY; + default: + return null; + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java index b7aa557ec..49afcefc9 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java @@ -32,18 +32,18 @@ public class TagRewriter { /** * Adds an empty tag (since the client crashes if a checked tag is not registered.) */ - public void addEmptyTag(TagType tagType, String id) { + public void addEmptyTag(RegistryType tagType, String id) { getNewTags(tagType).add(new TagData(id, EMPTY_ARRAY)); } - public void addEmptyTags(TagType tagType, String... ids) { + public void addEmptyTags(RegistryType tagType, String... ids) { List tagList = getNewTags(tagType); for (String id : ids) { tagList.add(new TagData(id, EMPTY_ARRAY)); } } - public void addTag(TagType tagType, String id, int... oldIds) { + public void addTag(RegistryType tagType, String id, int... oldIds) { List newTags = getNewTags(tagType); IdRewriteFunction rewriteFunction = getRewriter(tagType); for (int i = 0; i < oldIds.length; i++) { @@ -108,7 +108,7 @@ public class TagRewriter { } } - private List getNewTags(TagType tagType) { + private List getNewTags(RegistryType tagType) { switch (tagType) { case BLOCK: return newBlockTags; @@ -122,7 +122,7 @@ public class TagRewriter { } } - private IdRewriteFunction getRewriter(TagType tagType) { + private IdRewriteFunction getRewriter(RegistryType tagType) { switch (tagType) { case BLOCK: return blockRewriter; diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java index 54e6b1cbc..40f566787 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java @@ -8,6 +8,7 @@ import us.myles.ViaVersion.api.remapper.PacketHandler; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ComponentRewriter; import us.myles.ViaVersion.api.rewriters.SoundRewriter; +import us.myles.ViaVersion.api.rewriters.StatisticsRewriter; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; @@ -29,7 +30,7 @@ public class Protocol1_14To1_13_2 extends Protocol MappingData.soundMappings.getNewId(id)).registerSound(ClientboundPackets1_13.SOUND); + new StatisticsRewriter(this, Protocol1_14To1_13_2::getNewBlockId, + InventoryPackets::getNewItemId, metadataRewriter::getNewEntityId).register(ClientboundPackets1_13.STATISTICS); ComponentRewriter componentRewriter = new ComponentRewriter1_14(this); componentRewriter.registerChatMessage(ClientboundPackets1_13.CHAT_MESSAGE); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java index eec98fab9..19e59fe68 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java @@ -4,9 +4,10 @@ import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; +import us.myles.ViaVersion.api.rewriters.RegistryType; import us.myles.ViaVersion.api.rewriters.SoundRewriter; +import us.myles.ViaVersion.api.rewriters.StatisticsRewriter; import us.myles.ViaVersion.api.rewriters.TagRewriter; -import us.myles.ViaVersion.api.rewriters.TagType; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; @@ -28,7 +29,7 @@ public class Protocol1_15To1_14_4 extends Protocol MappingData.soundMappings.getNewId(id)); soundRewriter.registerSound(ClientboundPackets1_16.SOUND); soundRewriter.registerSound(ClientboundPackets1_16.ENTITY_SOUND); @@ -76,14 +80,14 @@ public class Protocol1_16_2To1_16_1 extends Protocol - tagRewriter.addEmptyTags(TagType.ITEM, "minecraft:soul_fire_base_blocks", "minecraft:furnace_materials", "minecraft:crimson_stems", + tagRewriter.addEmptyTags(RegistryType.ITEM, "minecraft:soul_fire_base_blocks", "minecraft:furnace_materials", "minecraft:crimson_stems", "minecraft:gold_ores", "minecraft:piglin_loved", "minecraft:piglin_repellents", "minecraft:creeper_drop_music_discs", "minecraft:logs_that_burn", "minecraft:stone_tool_materials", "minecraft:warped_stems"); - tagRewriter.addEmptyTags(TagType.BLOCK, "minecraft:infiniburn_nether", "minecraft:crimson_stems", + tagRewriter.addEmptyTags(RegistryType.BLOCK, "minecraft:infiniburn_nether", "minecraft:crimson_stems", "minecraft:wither_summon_base_blocks", "minecraft:infiniburn_overworld", "minecraft:piglin_repellents", "minecraft:hoglin_repellents", "minecraft:prevent_mob_spawning_inside", "minecraft:wart_blocks", "minecraft:stone_pressure_plates", "minecraft:nylium", "minecraft:gold_ores", "minecraft:pressure_plates", diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index ac55f174c..7b695b11f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -9,9 +9,10 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.remapper.PacketRemapper; import us.myles.ViaVersion.api.rewriters.ComponentRewriter; +import us.myles.ViaVersion.api.rewriters.RegistryType; import us.myles.ViaVersion.api.rewriters.SoundRewriter; +import us.myles.ViaVersion.api.rewriters.StatisticsRewriter; import us.myles.ViaVersion.api.rewriters.TagRewriter; -import us.myles.ViaVersion.api.rewriters.TagType; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14; @@ -50,6 +51,9 @@ public class Protocol1_16To1_15_2 extends Protocol