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

View File

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

View File

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