Use map copies for possible properties

This commit is contained in:
themode 2021-12-19 01:05:30 +01:00 committed by TheMode
parent 089a90df52
commit 77a14c0750
2 changed files with 17 additions and 11 deletions

View File

@ -13,6 +13,7 @@ import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
final class BlockImpl implements Block {
@ -23,16 +24,17 @@ final class BlockImpl implements Block {
final var stateObject = (Map<String, Object>) object.get("states");
// Loop each state
var propertyEntry = new HashMap<Map<String, String>, Block>();
var unmodifiableEntries = Collections.unmodifiableMap(propertyEntry);
AtomicReference<Map<Map<String, String>, Block>> ref = new AtomicReference<>();
for (var stateEntry : stateObject.entrySet()) {
final String query = stateEntry.getKey();
final var stateOverride = (Map<String, Object>) stateEntry.getValue();
final var propertyMap = BlockUtils.parseProperties(query);
final Block block = new BlockImpl(Registry.block(namespace, object, stateOverride),
unmodifiableEntries, propertyMap, null, null);
ref, propertyMap, null, null);
BLOCK_STATE_MAP.set(block.stateId(), block);
propertyEntry.put(propertyMap, block);
}
ref.setPlain(Map.copyOf(propertyEntry));
// Register default state
final int defaultState = ((Number) object.get("defaultStateId")).intValue();
container.register(getState(defaultState));
@ -67,7 +69,7 @@ final class BlockImpl implements Block {
}
private final Registry.BlockEntry registry;
private final Map<Map<String, String>, Block> propertyEntry;
private final AtomicReference<Map<Map<String, String>, Block>> possibleProperties;
private final Map<String, String> properties;
private final NBTCompound nbt;
private final BlockHandler handler;
@ -75,12 +77,12 @@ final class BlockImpl implements Block {
private int hashCode; // Cache
BlockImpl(@NotNull Registry.BlockEntry registry,
@NotNull Map<Map<String, String>, Block> propertyEntry,
@NotNull AtomicReference<Map<Map<String, String>, Block>> possibleProperties,
@NotNull Map<String, String> properties,
@Nullable NBTCompound nbt,
@Nullable BlockHandler handler) {
this.registry = registry;
this.propertyEntry = propertyEntry;
this.possibleProperties = possibleProperties;
this.properties = properties;
this.nbt = nbt;
this.handler = handler;
@ -109,12 +111,12 @@ final class BlockImpl implements Block {
var temporaryNbt = new MutableNBTCompound(Objects.requireNonNullElse(nbt, NBTCompound.EMPTY));
tag.write(temporaryNbt, value);
final var finalNbt = temporaryNbt.getSize() > 0 ? NBT_CACHE.get(temporaryNbt.toCompound(), Function.identity()) : null;
return new BlockImpl(registry, propertyEntry, properties, finalNbt, handler);
return new BlockImpl(registry, possibleProperties, properties, finalNbt, handler);
}
@Override
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
return new BlockImpl(registry, propertyEntry, properties, nbt, handler);
return new BlockImpl(registry, possibleProperties, properties, nbt, handler);
}
@Override
@ -134,7 +136,7 @@ final class BlockImpl implements Block {
@Override
public @NotNull Collection<@NotNull Block> possibleStates() {
return propertyEntry.values();
return possibleProperties().values();
}
@Override
@ -147,6 +149,10 @@ final class BlockImpl implements Block {
return nbt != null ? tag.read(nbt) : null;
}
private Map<Map<String, String>, Block> possibleProperties() {
return possibleProperties.getPlain();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -178,10 +184,10 @@ final class BlockImpl implements Block {
private Block compute(Map<String, String> properties) {
if (this.properties.equals(properties)) return this;
Block block = propertyEntry.get(properties);
Block block = possibleProperties().get(properties);
if (block == null)
throw new IllegalArgumentException("Invalid properties: " + properties + " for block " + this);
return nbt == null && handler == null ? block :
new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
new BlockImpl(block.registry(), possibleProperties, block.properties(), nbt, handler);
}
}

View File

@ -533,7 +533,7 @@ public final class Registry {
private static Object readObject(JsonReader reader) throws IOException {
return switch (reader.peek()) {
case BEGIN_ARRAY -> {
ArrayList<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) list.add(readObject(reader));
reader.endArray();