From 68807be880b8fefab7628ce7199809aa70a832f8 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 9 Apr 2024 12:37:58 +0200 Subject: [PATCH] Track banner patterns --- .../Protocol1_20_3To1_20_5.java | 4 +- .../rewriter/EntityPacketRewriter1_20_5.java | 129 +++++++++--------- 2 files changed, 69 insertions(+), 64 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java index 63ecef13..7a4b7230 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/Protocol1_20_3To1_20_5.java @@ -74,8 +74,6 @@ public final class Protocol1_20_3To1_20_5 extends BackwardsProtocol tagRewriter = new TagRewriter<>(this); @@ -89,7 +87,7 @@ public final class Protocol1_20_3To1_20_5 extends BackwardsProtocol wrapper.user().get(RegistryDataStorage.class).registryData().clear()); + registerClientbound(ClientboundPackets1_20_5.START_CONFIGURATION, wrapper -> wrapper.user().get(RegistryDataStorage.class).clear()); final SoundRewriter soundRewriter = new SoundRewriter<>(this); soundRewriter.register1_19_3Sound(ClientboundPackets1_20_5.SOUND); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java index feb6a8aa..93ca0c7f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java @@ -42,6 +42,7 @@ import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.data.Attribute import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundConfigurationPackets1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPacket1_20_5; import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.ClientboundPackets1_20_5; +import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.storage.BannerPatternStorage; import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.MathUtil; import java.util.HashMap; @@ -78,69 +79,75 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { - final String registryKey = Key.stripMinecraftNamespace(wrapper.read(Type.STRING)); - // TODO Track banner pattern/material ids - if (registryKey.equals("wolf_variant") || registryKey.equals("banner_pattern") || registryKey.equals("banner_material")) { - wrapper.cancel(); - return; - } + protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, wrapper -> { + wrapper.cancel(); - // Track data - final RegistryEntry[] entries = wrapper.read(Type.REGISTRY_ENTRY_ARRAY); - final RegistryDataStorage registryDataStorage = wrapper.user().get(RegistryDataStorage.class); - if (registryKey.equals("worldgen/biome")) { - tracker(wrapper.user()).setBiomesSent(entries.length); - } else if (registryKey.equals("dimension_type")) { - final Map dimensionDataMap = new HashMap<>(entries.length); - final String[] keys = new String[entries.length]; - for (int i = 0; i < entries.length; i++) { - final RegistryEntry entry = entries[i]; - Preconditions.checkNotNull(entry.tag(), "Server unexpectedly sent null dimension data for " + entry.key()); - - final String dimensionKey = Key.stripMinecraftNamespace(entry.key()); - final CompoundTag tag = (CompoundTag) entry.tag(); - updateDimensionTypeData(tag); - dimensionDataMap.put(dimensionKey, new DimensionDataImpl(i, tag)); - keys[i] = dimensionKey; - } - registryDataStorage.setDimensionKeys(keys); - tracker(wrapper.user()).setDimensions(dimensionDataMap); - } - - // Write to old format - final CompoundTag registryTag = new CompoundTag(); - final ListTag entriesTag = new ListTag<>(CompoundTag.class); - registryTag.putString("type", registryKey); - registryTag.put("value", entriesTag); - for (int i = 0; i < entries.length; i++) { - final RegistryEntry entry = entries[i]; - Preconditions.checkNotNull(entry.tag(), "Server unexpectedly sent null registry data entry for " + entry.key()); - - if (registryKey.equals("trim_pattern")) { - final CompoundTag patternTag = (CompoundTag) entry.tag(); - final StringTag templateItem = patternTag.getStringTag("template_item"); - if (Protocol1_20_5To1_20_3.MAPPINGS.itemId(templateItem.getValue()) == -1) { - // Skip new items - continue; - } - } - - final CompoundTag entryCompoundTag = new CompoundTag(); - entryCompoundTag.putString("name", entry.key()); - entryCompoundTag.putInt("id", i); - entryCompoundTag.put("element", entry.tag()); - entriesTag.add(entryCompoundTag); - } - - // Store and send together with the rest later - registryDataStorage.registryData().put(registryKey, registryTag); - wrapper.cancel(); - }); + final String registryKey = Key.stripMinecraftNamespace(wrapper.read(Type.STRING)); + if (registryKey.equals("wolf_variant")) { + // There's only one wolf variant now + return; } + + final RegistryDataStorage registryDataStorage = wrapper.user().get(RegistryDataStorage.class); + final RegistryEntry[] entries = wrapper.read(Type.REGISTRY_ENTRY_ARRAY); + // Track banner pattern and material ids for conversion in items + if (registryKey.equals("banner_pattern")) { + final BannerPatternStorage bannerStorage = new BannerPatternStorage(); + wrapper.user().put(bannerStorage); + for (int i = 0; i < entries.length; i++) { + bannerStorage.bannerPatterns().put(i, entries[i].key()); + } + return; + } + + // Track biome and dimension data + if (registryKey.equals("worldgen/biome")) { + tracker(wrapper.user()).setBiomesSent(entries.length); + } else if (registryKey.equals("dimension_type")) { + final Map dimensionDataMap = new HashMap<>(entries.length); + final String[] keys = new String[entries.length]; + for (int i = 0; i < entries.length; i++) { + final RegistryEntry entry = entries[i]; + Preconditions.checkNotNull(entry.tag(), "Server unexpectedly sent null dimension data for " + entry.key()); + + final String dimensionKey = Key.stripMinecraftNamespace(entry.key()); + final CompoundTag tag = (CompoundTag) entry.tag(); + updateDimensionTypeData(tag); + dimensionDataMap.put(dimensionKey, new DimensionDataImpl(i, tag)); + keys[i] = dimensionKey; + } + registryDataStorage.setDimensionKeys(keys); + tracker(wrapper.user()).setDimensions(dimensionDataMap); + } + + // Write to old format + final boolean isTrimPattern = registryKey.equals("trim_pattern"); + final CompoundTag registryTag = new CompoundTag(); + final ListTag entriesTag = new ListTag<>(CompoundTag.class); + registryTag.putString("type", registryKey); + registryTag.put("value", entriesTag); + for (int i = 0; i < entries.length; i++) { + final RegistryEntry entry = entries[i]; + Preconditions.checkNotNull(entry.tag(), "Server unexpectedly sent null registry data entry for " + entry.key()); + + if (isTrimPattern) { + final CompoundTag patternTag = (CompoundTag) entry.tag(); + final StringTag templateItem = patternTag.getStringTag("template_item"); + if (Protocol1_20_5To1_20_3.MAPPINGS.itemId(templateItem.getValue()) == -1) { + // Skip new items + continue; + } + } + + final CompoundTag entryCompoundTag = new CompoundTag(); + entryCompoundTag.putString("name", entry.key()); + entryCompoundTag.putInt("id", i); + entryCompoundTag.put("element", entry.tag()); + entriesTag.add(entryCompoundTag); + } + + // Store and send together with the rest later + registryDataStorage.registryData().put(registryKey, registryTag); }); protocol.registerClientbound(ClientboundPackets1_20_5.JOIN_GAME, new PacketHandlers() {