From 37742c4593af2f1c38c96416b011ccb27d9fe935 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Tue, 8 Jul 2025 19:21:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E6=B3=A8=E5=86=8C=E8=A1=A8?= =?UTF-8?q?=E4=B8=8Ebytebuffer=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/registry/MappedRegistry.java | 24 ++++++++++++------- .../craftengine/core/registry/Registry.java | 3 +++ .../core/util/FriendlyByteBuf.java | 22 +++++++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/net/momirealms/craftengine/core/registry/MappedRegistry.java b/core/src/main/java/net/momirealms/craftengine/core/registry/MappedRegistry.java index 219c1d5b7..85be047f2 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/registry/MappedRegistry.java +++ b/core/src/main/java/net/momirealms/craftengine/core/registry/MappedRegistry.java @@ -9,8 +9,9 @@ import java.util.*; public class MappedRegistry implements WritableRegistry { private final ResourceKey> key; - private final Map> byId = new HashMap<>(2048); - private final Map, Holder.Reference> byResourceKey = new HashMap<>(2048); + private final Map> byResourceLocation = new HashMap<>(512); + private final Map, Holder.Reference> byResourceKey = new HashMap<>(512); + private final List> byId = new ArrayList<>(512); public MappedRegistry(ResourceKey> key) { this.key = key; @@ -27,12 +28,13 @@ public class MappedRegistry implements WritableRegistry { if (!key.registry().equals(this.key.location())) { throw new IllegalStateException(key + " is not allowed to be registered in " + this.key); } - if (this.byId.containsKey(key.location())) { + if (this.byResourceLocation.containsKey(key.location())) { throw new IllegalStateException("Adding duplicate key '" + key + "' to registry"); } else { Holder.Reference reference = this.byResourceKey.computeIfAbsent(key, k -> Holder.Reference.create(this, k)); this.byResourceKey.put(key, reference); - this.byId.put(key.location(), reference); + this.byResourceLocation.put(key.location(), reference); + this.byId.add(reference); return reference; } } @@ -52,7 +54,7 @@ public class MappedRegistry implements WritableRegistry { @Override public Optional> get(Key id) { - return Optional.ofNullable(this.byId.get(id)); + return Optional.ofNullable(this.byResourceLocation.get(id)); } @Override @@ -63,10 +65,16 @@ public class MappedRegistry implements WritableRegistry { @Nullable @Override public T getValue(@Nullable Key id) { - Holder.Reference reference = this.byId.get(id); + Holder.Reference reference = this.byResourceLocation.get(id); return getValueFromNullable(reference); } + @Override + public @Nullable T getValue(int id) { + if (id < 0 || id >= this.byId.size()) return null; + return getValueFromNullable(this.byId.get(id)); + } + @Nullable private static T getValueFromNullable(@Nullable Holder.Reference entry) { return entry != null ? entry.value() : null; @@ -74,7 +82,7 @@ public class MappedRegistry implements WritableRegistry { @Override public Set keySet() { - return Collections.unmodifiableSet(this.byId.keySet()); + return Collections.unmodifiableSet(this.byResourceLocation.keySet()); } @Override @@ -84,7 +92,7 @@ public class MappedRegistry implements WritableRegistry { @Override public boolean containsKey(Key id) { - return this.byId.containsKey(id); + return this.byResourceLocation.containsKey(id); } @Override diff --git a/core/src/main/java/net/momirealms/craftengine/core/registry/Registry.java b/core/src/main/java/net/momirealms/craftengine/core/registry/Registry.java index 970d47b15..e61adefe7 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/registry/Registry.java +++ b/core/src/main/java/net/momirealms/craftengine/core/registry/Registry.java @@ -18,6 +18,9 @@ public interface Registry extends Holder.Owner { @Nullable T getValue(@Nullable Key id); + @Nullable + T getValue(int id); + Set keySet(); Set, T>> entrySet(); diff --git a/core/src/main/java/net/momirealms/craftengine/core/util/FriendlyByteBuf.java b/core/src/main/java/net/momirealms/craftengine/core/util/FriendlyByteBuf.java index 3d9baffca..244858169 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/util/FriendlyByteBuf.java +++ b/core/src/main/java/net/momirealms/craftengine/core/util/FriendlyByteBuf.java @@ -10,6 +10,7 @@ import io.netty.handler.codec.EncoderException; import io.netty.util.ByteProcessor; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; +import net.momirealms.craftengine.core.registry.Registry; import net.momirealms.craftengine.core.world.BlockPos; import net.momirealms.sparrow.nbt.NBT; import net.momirealms.sparrow.nbt.Tag; @@ -58,16 +59,28 @@ public class FriendlyByteBuf extends ByteBuf { public void writeCollection(Collection collection, Writer writer) { this.writeVarInt(collection.size()); - for(T t0 : collection) { + for (T t0 : collection) { writer.accept(this, t0); } - } public BlockPos readBlockPos() { return BlockPos.of(this.readLong()); } + public OptionalInt readOptionalVarInt() { + int i = this.readVarInt(); + return i == 0 ? OptionalInt.empty() : OptionalInt.of(i - 1); + } + + public void writeOptionalVarInt(OptionalInt optionalInt) { + if (optionalInt.isPresent()) { + this.writeVarInt(optionalInt.getAsInt() + 1); + } else { + this.writeVarInt(0); + } + } + public int readContainerId() { return VersionHelper.isOrAbove1_21_2() ? this.readVarInt() : this.readUnsignedByte(); } @@ -331,6 +344,11 @@ public class FriendlyByteBuf extends ByteBuf { return byteArray; } + public T readById(Registry registry) { + int id = this.readVarInt(); + return registry.getValue(id); + } + public int readVarInt() { int value = 0; int shift = 0;