Micro optimize block properties map

This commit is contained in:
TheMode 2021-07-23 15:44:53 +02:00
parent 8734478126
commit 11b2426629
2 changed files with 15 additions and 23 deletions

View File

@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@ -33,27 +32,19 @@ final class BlockImpl implements Block {
@Nullable BlockHandler handler) {
this.registry = registry;
this.propertyEntry = propertyEntry;
this.properties = Collections.unmodifiableMap(properties);
this.properties = properties;
this.nbt = nbt;
this.handler = handler;
}
BlockImpl(@NotNull Registry.BlockEntry registry,
@NotNull BlockLoader.PropertyEntry propertyEntry,
@NotNull Map<String, String> properties) {
this(registry, propertyEntry, properties, null, null);
}
@Override
public @NotNull Block withProperty(@NotNull String property, @NotNull String value) {
var properties = new HashMap<>(this.properties);
properties.put(property, value);
properties.replace(property, value);
Block block = propertyEntry.getProperties(properties);
if (block == null)
throw new IllegalArgumentException("Invalid property: " + property + ":" + value);
if (nbt != null || handler != null)
return new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
return block;
return compute(block);
}
@Override
@ -67,14 +58,12 @@ final class BlockImpl implements Block {
block = propertyEntry.getProperties(properties);
} else {
var newProperties = new HashMap<>(this.properties);
newProperties.putAll(properties);
newProperties.replaceAll((key, value) -> Objects.requireNonNullElse(properties.get(key), value));
block = propertyEntry.getProperties(newProperties);
}
if (block == null)
throw new IllegalArgumentException("Invalid properties: " + properties);
if (nbt != null || handler != null)
return new BlockImpl(block.registry(), propertyEntry, block.properties(), nbt, handler);
return block;
return compute(block);
}
@Override
@ -92,7 +81,7 @@ final class BlockImpl implements Block {
@Override
public boolean hasNbt() {
return nbt != null && nbt.getSize() > 0;
return nbt != null;
}
@Override
@ -112,8 +101,11 @@ final class BlockImpl implements Block {
@Override
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
if (nbt == null)
return null;
return tag.read(nbt);
return nbt != null ? tag.read(nbt) : null;
}
private Block compute(Block original) {
return nbt == null && handler == null ? original :
new BlockImpl(original.registry(), propertyEntry, original.properties(), nbt, handler);
}
}

View File

@ -59,7 +59,7 @@ class BlockLoader {
final JsonObject blockObject = entry.getValue().getAsJsonObject();
final JsonObject stateObject = blockObject.remove("states").getAsJsonObject();
retrieveState(blockNamespace, blockObject, stateObject);
retrieveState(blockObject, stateObject);
final int defaultState = blockObject.get("defaultStateId").getAsInt();
final Block defaultBlock = getState(defaultState);
final int id = blockObject.get("id").getAsInt();
@ -68,14 +68,14 @@ class BlockLoader {
});
}
private static void retrieveState(String namespace, JsonObject object, JsonObject stateObject) {
private static void retrieveState(JsonObject object, JsonObject stateObject) {
PropertyEntry propertyEntry = new PropertyEntry();
stateObject.entrySet().forEach(stateEntry -> {
final String query = stateEntry.getKey();
JsonObject stateOverride = stateEntry.getValue().getAsJsonObject();
final int stateId = stateOverride.get("stateId").getAsInt();
final var propertyMap = BlockUtils.parseProperties(query);
final Block block = new BlockImpl(Registry.block(object, stateOverride), propertyEntry, propertyMap);
final Block block = new BlockImpl(Registry.block(object, stateOverride), propertyEntry, propertyMap, null, null);
BLOCK_STATE_MAP.put(stateId, block);
propertyEntry.map.put(propertyMap, block);
});