diff --git a/src/main/java/net/minestom/server/registry/Registry.java b/src/main/java/net/minestom/server/registry/Registry.java index e8a0d6f01..cc8ed6779 100644 --- a/src/main/java/net/minestom/server/registry/Registry.java +++ b/src/main/java/net/minestom/server/registry/Registry.java @@ -3,11 +3,23 @@ package net.minestom.server.registry; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import net.minestom.server.instance.block.Block; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; public class Registry { - public static BlockEntry block(Block block) { - return new BlockEntry(new JsonObject()); + private static final Loader LOADER = new Loader(); + + public static BlockEntry block(@NotNull Block block) { + return loader().block(block.getName()); + } + + @ApiStatus.Internal + public static @NotNull Loader loader() { + return LOADER; } public static class BlockEntry extends Entry { @@ -49,7 +61,6 @@ public class Registry { } public static class Entry { - private final JsonObject json; private Entry(JsonObject json) { @@ -76,4 +87,31 @@ public class Registry { return json.get(name); } } + + public static class Loader { + private final RegistryMap blockRegistry = new RegistryMap<>(BlockEntry::new); + + public void loadBlocks(@NotNull JsonObject blocks) { + loadRegistry(blockRegistry, blocks); + } + + public BlockEntry block(String name) { + return blockRegistry.get(name); + } + + private void loadRegistry(RegistryMap map, JsonObject data) { + data.keySet().forEach(namespace -> { + final JsonObject value = data.get(namespace).getAsJsonObject(); + map.put(namespace, map.function.apply(value)); + }); + } + } + + private static class RegistryMap extends ConcurrentHashMap { + private final Function function; + + private RegistryMap(Function function) { + this.function = function; + } + } }