9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-27 02:49:15 +00:00

增强注册表与bytebuffer功能

This commit is contained in:
XiaoMoMi
2025-07-08 19:21:46 +08:00
parent b19a1390a6
commit 37742c4593
3 changed files with 39 additions and 10 deletions

View File

@@ -9,8 +9,9 @@ import java.util.*;
public class MappedRegistry<T> implements WritableRegistry<T> {
private final ResourceKey<? extends Registry<T>> key;
private final Map<Key, Holder.Reference<T>> byId = new HashMap<>(2048);
private final Map<ResourceKey<T>, Holder.Reference<T>> byResourceKey = new HashMap<>(2048);
private final Map<Key, Holder.Reference<T>> byResourceLocation = new HashMap<>(512);
private final Map<ResourceKey<T>, Holder.Reference<T>> byResourceKey = new HashMap<>(512);
private final List<Holder.Reference<T>> byId = new ArrayList<>(512);
public MappedRegistry(ResourceKey<? extends Registry<T>> key) {
this.key = key;
@@ -27,12 +28,13 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
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<T> 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<T> implements WritableRegistry<T> {
@Override
public Optional<Holder.Reference<T>> 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<T> implements WritableRegistry<T> {
@Nullable
@Override
public T getValue(@Nullable Key id) {
Holder.Reference<T> reference = this.byId.get(id);
Holder.Reference<T> 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> T getValueFromNullable(@Nullable Holder.Reference<T> entry) {
return entry != null ? entry.value() : null;
@@ -74,7 +82,7 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
@Override
public Set<Key> keySet() {
return Collections.unmodifiableSet(this.byId.keySet());
return Collections.unmodifiableSet(this.byResourceLocation.keySet());
}
@Override
@@ -84,7 +92,7 @@ public class MappedRegistry<T> implements WritableRegistry<T> {
@Override
public boolean containsKey(Key id) {
return this.byId.containsKey(id);
return this.byResourceLocation.containsKey(id);
}
@Override

View File

@@ -18,6 +18,9 @@ public interface Registry<T> extends Holder.Owner<T> {
@Nullable
T getValue(@Nullable Key id);
@Nullable
T getValue(int id);
Set<Key> keySet();
Set<Map.Entry<ResourceKey<T>, T>> entrySet();

View File

@@ -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 <T> void writeCollection(Collection<T> collection, Writer<T> 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> T readById(Registry<T> registry) {
int id = this.readVarInt();
return registry.getValue(id);
}
public int readVarInt() {
int value = 0;
int shift = 0;