Improve memory usage

This commit is contained in:
TheMode 2021-06-15 13:45:23 +02:00
parent 65f92bf071
commit a83482efb8
3 changed files with 18 additions and 24 deletions

View File

@ -43,8 +43,10 @@ class BlockRegistry {
blocks.entrySet().forEach(entry -> {
final String blockNamespace = entry.getKey();
final JsonObject blockObject = entry.getValue().getAsJsonObject();
final JsonObject stateObject = blockObject.remove("states").getAsJsonObject();
blockObject.remove("properties");
retrieveState(blockNamespace, blockObject);
retrieveState(blockNamespace, blockObject, stateObject);
final int defaultState = blockObject.get("defaultStateId").getAsInt();
final Block defaultBlock = getState(defaultState);
final int id = blockObject.get("id").getAsInt();
@ -53,24 +55,14 @@ class BlockRegistry {
});
}
private static void retrieveState(String namespace, JsonObject object) {
final JsonObject states = object.getAsJsonObject("states");
private static void retrieveState(String namespace, JsonObject object, JsonObject stateObject) {
PropertyEntry propertyEntry = new PropertyEntry();
states.entrySet().forEach(stateEntry -> {
stateObject.entrySet().forEach(stateEntry -> {
final String query = stateEntry.getKey();
JsonObject stateObject = object.deepCopy();
stateObject.remove("states");
stateObject.remove("properties");
JsonObject stateOverride = stateEntry.getValue().getAsJsonObject();
stateOverride.entrySet().forEach(entry -> stateObject.add(entry.getKey(), entry.getValue()));
final int stateId = stateOverride.get("stateId").getAsInt();
final var propertyMap = getPropertyMap(query);
final Block block = new BlockTest(stateObject);
final Block block = new BlockTest(object, stateOverride);
BLOCK_STATE_MAP.put(stateId, block);
propertyEntry.propertyMap.put(propertyMap, block);
});

View File

@ -23,8 +23,8 @@ class BlockTest implements Block {
this.registry = registry;
}
BlockTest(JsonObject jsonObject) {
this(Registry.block(jsonObject));
BlockTest(JsonObject jsonObject, JsonObject override) {
this(Registry.block(jsonObject, override));
}
@Override

View File

@ -8,14 +8,15 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.io.InputStreamReader;
import java.util.Objects;
@ApiStatus.Internal
public class Registry {
protected static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
public static BlockEntry block(@NotNull JsonObject jsonObject) {
return new BlockEntry(jsonObject);
public static BlockEntry block(@NotNull JsonObject jsonObject, JsonObject override) {
return new BlockEntry(jsonObject, override);
}
public static JsonObject load(Resource resource) {
@ -36,8 +37,8 @@ public class Registry {
}
public static class BlockEntry extends Entry {
private BlockEntry(JsonObject json) {
super(json);
private BlockEntry(JsonObject main, JsonObject override) {
super(main, override);
}
public String namespace() {
@ -86,10 +87,11 @@ public class Registry {
}
public static class Entry {
private final JsonObject json;
private final JsonObject main, override;
private Entry(JsonObject json) {
this.json = json;
private Entry(JsonObject main, JsonObject override) {
this.main = main;
this.override = override;
}
public String getString(String name) {
@ -109,7 +111,7 @@ public class Registry {
}
protected JsonElement element(String name) {
return json.get(name);
return Objects.requireNonNullElseGet(override.get(name), () -> main.get(name));
}
}
}