Added registry loader

This commit is contained in:
TheMode 2021-06-11 20:13:20 +02:00
parent 08684d075a
commit 9dffc7658c

View File

@ -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<BlockEntry> blockRegistry = new RegistryMap<>(BlockEntry::new);
public void loadBlocks(@NotNull JsonObject blocks) {
loadRegistry(blockRegistry, blocks);
}
public BlockEntry block(String name) {
return blockRegistry.get(name);
}
private <T extends Entry> void loadRegistry(RegistryMap<T> 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<T extends Entry> extends ConcurrentHashMap<String, T> {
private final Function<JsonObject, T> function;
private RegistryMap(Function<JsonObject, T> function) {
this.function = function;
}
}
}