Fix BlockTest immutability

This commit is contained in:
TheMode 2021-06-16 00:19:36 +02:00
parent babe72608a
commit 3324fd1e21
2 changed files with 24 additions and 21 deletions

View File

@ -62,7 +62,7 @@ class BlockRegistry {
JsonObject stateOverride = stateEntry.getValue().getAsJsonObject();
final int stateId = stateOverride.get("stateId").getAsInt();
final var propertyMap = getPropertyMap(query);
final Block block = new BlockTest(object, stateOverride);
final Block block = new BlockTest(object, stateOverride, propertyMap);
BLOCK_STATE_MAP.put(stateId, block);
propertyEntry.propertyMap.put(propertyMap, block);
});

View File

@ -15,16 +15,28 @@ class BlockTest implements Block {
private final Registry.BlockEntry registry;
private final Map<String, String> properties = new HashMap<>();
private NBTCompound compound;
private BlockHandler handler;
private final Map<String, String> properties;
private final NBTCompound compound;
private final BlockHandler handler;
BlockTest(Registry.BlockEntry registry) {
BlockTest(Registry.BlockEntry registry,
Map<String, String> properties,
NBTCompound compound,
BlockHandler handler) {
this.registry = registry;
this.properties = properties;
this.compound = compound;
this.handler = handler;
}
BlockTest(JsonObject jsonObject, JsonObject override) {
this(Registry.block(jsonObject, override));
BlockTest(Registry.BlockEntry registry,
Map<String, String> properties) {
this(registry, properties, null, null);
}
BlockTest(JsonObject jsonObject, JsonObject override,
Map<String, String> properties) {
this(Registry.block(jsonObject, override), properties);
}
@Override
@ -36,24 +48,19 @@ class BlockTest implements Block {
@Override
public @NotNull <T> Block withTag(@NotNull Tag<T> tag, @Nullable T value) {
var clone = shallowClone();
clone.compound = Objects.requireNonNullElseGet(clone.compound, NBTCompound::new);
tag.write(clone.compound, value);
return clone;
var block = new BlockTest(registry, properties, compound.deepClone(), handler);
tag.write(block.compound, value);
return block;
}
@Override
public @NotNull Block withNbt(@Nullable NBTCompound compound) {
var clone = shallowClone();
clone.compound = compound;
return clone;
return new BlockTest(registry, properties, compound, handler);
}
@Override
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
var clone = shallowClone();
clone.handler = handler;
return clone;
return new BlockTest(registry, properties, compound, handler);
}
@Override
@ -90,8 +97,4 @@ class BlockTest implements Block {
public boolean hasTag(@NotNull Tag<?> tag) {
return compound.containsKey(tag.getKey());
}
private @NotNull BlockTest shallowClone() {
return new BlockTest(registry);
}
}