mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-21 23:01:32 +01:00
Improve memory usage
This commit is contained in:
parent
65f92bf071
commit
a83482efb8
@ -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);
|
||||
});
|
||||
|
@ -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
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user