From a5bd8abe93902392bf15239d96be9dba7e10f9ab Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 27 Jan 2024 13:48:08 +0100 Subject: [PATCH] Resize registry data array to actual size once collected --- .../viaversion/ViaVersionPlugin.java | 2 +- .../rewriter/EntityPacketRewriter1_20_5.java | 92 +++++++++---------- .../viaversion/rewriter/meta/MetaFilter.java | 7 +- .../viaversion/scheduler/TaskScheduler.java | 4 +- 4 files changed, 52 insertions(+), 53 deletions(-) diff --git a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java index 5f7e8c5b3..87c67b684 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java @@ -162,7 +162,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform @Override public PlatformTask runRepeatingAsync(final Runnable runnable, final long ticks) { if (FOLIA) { - return new BukkitViaTaskTask(Via.getManager().getScheduler().schedule(runnable, ticks * 50, TimeUnit.MILLISECONDS)); + return new BukkitViaTaskTask(Via.getManager().getScheduler().scheduleRepeating(runnable, 0, ticks * 50, TimeUnit.MILLISECONDS)); } return new BukkitViaTask(getServer().getScheduler().runTaskTimerAsynchronously(this, runnable, 0, ticks)); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java index 7d53d5bd0..27b488cb6 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_5to1_20_3/rewriter/EntityPacketRewriter1_20_5.java @@ -55,46 +55,47 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { - final CompoundTag registryData = wrapper.read(Type.COMPOUND_TAG); - cacheDimensionData(wrapper.user(), registryData); - trackBiomeSize(wrapper.user(), registryData); + protocol.registerClientbound(ClientboundConfigurationPackets1_20_3.REGISTRY_DATA, wrapper -> { + final CompoundTag registryData = wrapper.read(Type.COMPOUND_TAG); + cacheDimensionData(wrapper.user(), registryData); + trackBiomeSize(wrapper.user(), registryData); - for (final Map.Entry entry : registryData.entrySet()) { - final CompoundTag entryTag = (CompoundTag) entry.getValue(); - final StringTag typeTag = entryTag.get("type"); - final ListTag valueTag = entryTag.get("value"); - RegistryEntry[] registryEntries = new RegistryEntry[valueTag.size()]; - boolean requiresDummyValues = false; - for (final Tag tag : valueTag) { - final CompoundTag compoundTag = (CompoundTag) tag; - final StringTag nameTag = compoundTag.get("name"); - final int id = ((NumberTag) compoundTag.get("id")).asInt(); - if (id >= registryEntries.length) { - // It was previously possible to have arbitrary ids - registryEntries = Arrays.copyOf(registryEntries, Math.max(registryEntries.length * 2, id + 1)); - requiresDummyValues = true; - } - - registryEntries[id] = new RegistryEntry(nameTag.getValue(), compoundTag.get("element")); - } - - if (requiresDummyValues) { - fill(registryEntries); - } - - final PacketWrapper registryPacket = wrapper.create(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA); - registryPacket.write(Type.STRING, typeTag.getValue()); - registryPacket.write(Type.REGISTRY_ENTRY_ARRAY, registryEntries); - registryPacket.send(Protocol1_20_5To1_20_3.class); + for (final Map.Entry entry : registryData.entrySet()) { + final CompoundTag entryTag = (CompoundTag) entry.getValue(); + final StringTag typeTag = entryTag.get("type"); + final ListTag valueTag = entryTag.get("value"); + RegistryEntry[] registryEntries = new RegistryEntry[valueTag.size()]; + boolean requiresDummyValues = false; + int entriesLength = registryEntries.length; + for (final Tag tag : valueTag) { + final CompoundTag compoundTag = (CompoundTag) tag; + final StringTag nameTag = compoundTag.get("name"); + final int id = ((NumberTag) compoundTag.get("id")).asInt(); + entriesLength = Math.max(entriesLength, id + 1); + if (id >= registryEntries.length) { + // It was previously possible to have arbitrary ids + registryEntries = Arrays.copyOf(registryEntries, Math.max(registryEntries.length * 2, id + 1)); + requiresDummyValues = true; } - wrapper.cancel(); - }); + registryEntries[id] = new RegistryEntry(nameTag.getValue(), compoundTag.get("element")); + } + + if (requiresDummyValues) { + // Truncate and replace null values + if (registryEntries.length != entriesLength) { + registryEntries = Arrays.copyOf(registryEntries, entriesLength); + } + replaceNullValues(registryEntries); + } + + final PacketWrapper registryPacket = wrapper.create(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA); + registryPacket.write(Type.STRING, typeTag.getValue()); + registryPacket.write(Type.REGISTRY_ENTRY_ARRAY, registryEntries); + registryPacket.send(Protocol1_20_5To1_20_3.class); } + + wrapper.cancel(); }); protocol.registerClientbound(ClientboundPackets1_20_3.JOIN_GAME, new PacketHandlers() { @@ -126,17 +127,12 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter { - final String dimensionKey = wrapper.read(Type.STRING); - final DimensionData data = tracker(wrapper.user()).dimensionData(dimensionKey); - wrapper.write(Type.VAR_INT, data.id()); - }); - map(Type.STRING); // World - handler(worldDataTrackerHandlerByKey1_20_5(0)); // Tracks world height and name for chunk data and entity (un)tracking - } + protocol.registerClientbound(ClientboundPackets1_20_3.RESPAWN, wrapper -> { + final String dimensionKey = wrapper.read(Type.STRING); + final DimensionData data = tracker(wrapper.user()).dimensionData(dimensionKey); + wrapper.write(Type.VAR_INT, data.id()); + wrapper.passthrough(Type.STRING); // World + worldDataTrackerHandlerByKey1_20_5(0).handle(wrapper); // Tracks world height and name for chunk data and entity (un)tracking }); protocol.registerClientbound(ClientboundPackets1_20_3.ENTITY_EFFECT, wrapper -> { @@ -169,7 +165,7 @@ public final class EntityPacketRewriter1_20_5 extends EntityRewriter