From f865a7b9ddd956779ae80712a6dff2b10b8a4e19 Mon Sep 17 00:00:00 2001 From: TheMode Date: Fri, 30 Jul 2021 15:29:05 +0200 Subject: [PATCH] Reduce registry boilerplate --- .../server/entity/EntityTypeImpl.java | 22 +++++----------- .../minestom/server/item/EnchantmentImpl.java | 22 +++++----------- .../minestom/server/item/MaterialImpl.java | 22 +++++----------- .../server/particle/ParticleImpl.java | 26 +++++++------------ .../server/potion/PotionEffectImpl.java | 22 +++++----------- .../server/potion/PotionTypeImpl.java | 26 +++++++------------ .../minestom/server/registry/Registry.java | 15 ++++++++++- .../minestom/server/sound/SoundEventImpl.java | 26 +++++++------------ .../server/statistic/StatisticTypeImpl.java | 26 +++++++------------ src/test/java/demo/PlayerInit.java | 4 +-- 10 files changed, 76 insertions(+), 135 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/EntityTypeImpl.java b/src/main/java/net/minestom/server/entity/EntityTypeImpl.java index 293724dbc..103e1f09c 100644 --- a/src/main/java/net/minestom/server/entity/EntityTypeImpl.java +++ b/src/main/java/net/minestom/server/entity/EntityTypeImpl.java @@ -1,6 +1,5 @@ package net.minestom.server.entity; -import com.google.gson.JsonObject; import net.minestom.server.entity.metadata.EntityMeta; import net.minestom.server.entity.metadata.PlayerMeta; import net.minestom.server.entity.metadata.ambient.BatMeta; @@ -47,25 +46,26 @@ import java.util.Map; import java.util.function.BiFunction; final class EntityTypeImpl implements EntityType { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.ENTITIES, + (container, namespace, object) -> container.register(new EntityTypeImpl(Registry.entity(namespace, object, null)))); private static final Map> ENTITY_META_SUPPLIER = createMetaMap(); private static final Map ACCELERATION_MAP = createAccelerationMap(); private static final Map DRAG_MAP = createDragMap(); static EntityType get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static EntityType getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static EntityType getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); + return CONTAINER.values(); } static EntityMeta createMeta(EntityType entityType, Entity entity, Metadata metadata) { @@ -80,16 +80,6 @@ final class EntityTypeImpl implements EntityType { return DRAG_MAP.getOrDefault(namespace, 0.02); } - static { - // Load data from file - JsonObject entities = Registry.load(Registry.Resource.ENTITIES); - entities.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject entityObject = entry.getValue().getAsJsonObject(); - LOADER.register(new EntityTypeImpl(Registry.entity(namespace, entityObject, null))); - }); - } - private static Map> createMetaMap() { Map> supplier = new HashMap<>(); supplier.put("minecraft:area_effect_cloud", AreaEffectCloudMeta::new); diff --git a/src/main/java/net/minestom/server/item/EnchantmentImpl.java b/src/main/java/net/minestom/server/item/EnchantmentImpl.java index 9e510aa24..e885ff0b2 100644 --- a/src/main/java/net/minestom/server/item/EnchantmentImpl.java +++ b/src/main/java/net/minestom/server/item/EnchantmentImpl.java @@ -1,38 +1,28 @@ package net.minestom.server.item; -import com.google.gson.JsonObject; import net.minestom.server.registry.Registry; import org.jetbrains.annotations.NotNull; import java.util.Collection; final class EnchantmentImpl implements Enchantment { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.ENCHANTMENTS, + (container, namespace, object) -> container.register(new EnchantmentImpl(Registry.enchantment(namespace, object, null)))); static Enchantment get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static Enchantment getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static Enchantment getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); - } - - static { - // Load data from file - JsonObject enchantments = Registry.load(Registry.Resource.ENCHANTMENTS); - enchantments.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject enchantmentObject = entry.getValue().getAsJsonObject(); - LOADER.register(new EnchantmentImpl(Registry.enchantment(namespace, enchantmentObject, null))); - }); + return CONTAINER.values(); } private final Registry.EnchantmentEntry registry; diff --git a/src/main/java/net/minestom/server/item/MaterialImpl.java b/src/main/java/net/minestom/server/item/MaterialImpl.java index 2d00a61b8..252ca71bc 100644 --- a/src/main/java/net/minestom/server/item/MaterialImpl.java +++ b/src/main/java/net/minestom/server/item/MaterialImpl.java @@ -1,38 +1,28 @@ package net.minestom.server.item; -import com.google.gson.JsonObject; import net.minestom.server.registry.Registry; import org.jetbrains.annotations.NotNull; import java.util.Collection; final class MaterialImpl implements Material { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.ITEMS, + (container, namespace, object) -> container.register(new MaterialImpl(Registry.material(namespace, object, null)))); static Material get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static Material getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static Material getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); - } - - static { - // Load data from file - JsonObject materials = Registry.load(Registry.Resource.ITEMS); - materials.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject materialObject = entry.getValue().getAsJsonObject(); - LOADER.register(new MaterialImpl(Registry.material(namespace, materialObject, null))); - }); + return CONTAINER.values(); } private final Registry.MaterialEntry registry; diff --git a/src/main/java/net/minestom/server/particle/ParticleImpl.java b/src/main/java/net/minestom/server/particle/ParticleImpl.java index 0a0dec6ca..55198b7d2 100644 --- a/src/main/java/net/minestom/server/particle/ParticleImpl.java +++ b/src/main/java/net/minestom/server/particle/ParticleImpl.java @@ -1,6 +1,5 @@ package net.minestom.server.particle; -import com.google.gson.JsonObject; import net.minestom.server.registry.Registry; import net.minestom.server.utils.NamespaceID; import org.jetbrains.annotations.NotNull; @@ -8,33 +7,26 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; final class ParticleImpl implements Particle { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.PARTICLES, + (loader, namespace, object) -> { + final int id = object.get("id").getAsInt(); + loader.register(new ParticleImpl(NamespaceID.from(namespace), id)); + }); static Particle get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static Particle getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static Particle getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); - } - - static { - // Load data from file - JsonObject particles = Registry.load(Registry.Resource.PARTICLES); - particles.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject object = entry.getValue().getAsJsonObject(); - final int id = object.get("id").getAsInt(); - LOADER.register(new ParticleImpl(NamespaceID.from(namespace), id)); - }); + return CONTAINER.values(); } private final NamespaceID namespaceID; diff --git a/src/main/java/net/minestom/server/potion/PotionEffectImpl.java b/src/main/java/net/minestom/server/potion/PotionEffectImpl.java index 74770abae..358e309cd 100644 --- a/src/main/java/net/minestom/server/potion/PotionEffectImpl.java +++ b/src/main/java/net/minestom/server/potion/PotionEffectImpl.java @@ -1,38 +1,28 @@ package net.minestom.server.potion; -import com.google.gson.JsonObject; import net.minestom.server.registry.Registry; import org.jetbrains.annotations.NotNull; import java.util.Collection; final class PotionEffectImpl implements PotionEffect { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.POTION_EFFECTS, + (container, namespace, object) -> container.register(new PotionEffectImpl(Registry.potionEffect(namespace, object, null)))); static PotionEffect get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static PotionEffect getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static PotionEffect getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); - } - - static { - // Load data from file - JsonObject potionEffects = Registry.load(Registry.Resource.POTION_EFFECTS); - potionEffects.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject object = entry.getValue().getAsJsonObject(); - LOADER.register(new PotionEffectImpl(Registry.potionEffect(namespace, object, null))); - }); + return CONTAINER.values(); } private final Registry.PotionEffectEntry registry; diff --git a/src/main/java/net/minestom/server/potion/PotionTypeImpl.java b/src/main/java/net/minestom/server/potion/PotionTypeImpl.java index 017cb6d58..c1822573f 100644 --- a/src/main/java/net/minestom/server/potion/PotionTypeImpl.java +++ b/src/main/java/net/minestom/server/potion/PotionTypeImpl.java @@ -1,6 +1,5 @@ package net.minestom.server.potion; -import com.google.gson.JsonObject; import net.minestom.server.registry.Registry; import net.minestom.server.utils.NamespaceID; import org.jetbrains.annotations.NotNull; @@ -8,33 +7,26 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; final class PotionTypeImpl implements PotionType { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.POTION_TYPES, + (loader, namespace, object) -> { + final int id = object.get("id").getAsInt(); + loader.register(new PotionTypeImpl(NamespaceID.from(namespace), id)); + }); static PotionType get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static PotionType getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static PotionType getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); - } - - static { - // Load data from file - JsonObject potionTypes = Registry.load(Registry.Resource.POTION_TYPES); - potionTypes.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject object = entry.getValue().getAsJsonObject(); - final int id = object.get("id").getAsInt(); - LOADER.register(new PotionTypeImpl(NamespaceID.from(namespace), id)); - }); + return CONTAINER.values(); } private final NamespaceID namespaceID; diff --git a/src/main/java/net/minestom/server/registry/Registry.java b/src/main/java/net/minestom/server/registry/Registry.java index c3d89baf9..c9ae4f6f3 100644 --- a/src/main/java/net/minestom/server/registry/Registry.java +++ b/src/main/java/net/minestom/server/registry/Registry.java @@ -49,7 +49,7 @@ public class Registry { return GSON.fromJson(new InputStreamReader(resourceStream), JsonObject.class); } - public static class Loader { + public static class Container { // Maps do not need to be thread-safe as they are fully populated // in the static initializer, should not be modified during runtime @@ -58,6 +58,15 @@ public class Registry { // id -> registry data private final Int2ObjectMap idMap = new Int2ObjectOpenHashMap<>(); + public Container(Resource resource, Loader loader) { + final JsonObject objects = Registry.load(resource); + objects.entrySet().forEach(entry -> { + final String namespace = entry.getKey(); + final JsonObject object = entry.getValue().getAsJsonObject(); + loader.accept(this, namespace, object); + }); + } + public T get(@NotNull String namespace) { return namespaceMap.get(namespace); } @@ -78,6 +87,10 @@ public class Registry { idMap.put(value.id(), value); namespaceMap.put(value.name(), value); } + + public interface Loader { + void accept(Container container, String namespace, JsonObject object); + } } public enum Resource { diff --git a/src/main/java/net/minestom/server/sound/SoundEventImpl.java b/src/main/java/net/minestom/server/sound/SoundEventImpl.java index bc48e27c1..d173bd0b5 100644 --- a/src/main/java/net/minestom/server/sound/SoundEventImpl.java +++ b/src/main/java/net/minestom/server/sound/SoundEventImpl.java @@ -1,6 +1,5 @@ package net.minestom.server.sound; -import com.google.gson.JsonObject; import net.minestom.server.registry.Registry; import net.minestom.server.utils.NamespaceID; import org.jetbrains.annotations.NotNull; @@ -8,33 +7,26 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; final class SoundEventImpl implements SoundEvent { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.SOUNDS, + (container, namespace, object) -> { + final int id = object.get("id").getAsInt(); + container.register(new SoundEventImpl(NamespaceID.from(namespace), id)); + }); static SoundEvent get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static SoundEvent getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static SoundEvent getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); - } - - static { - // Load data from file - JsonObject sounds = Registry.load(Registry.Resource.SOUNDS); - sounds.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject object = entry.getValue().getAsJsonObject(); - final int id = object.get("id").getAsInt(); - LOADER.register(new SoundEventImpl(NamespaceID.from(namespace), id)); - }); + return CONTAINER.values(); } private final NamespaceID namespaceID; diff --git a/src/main/java/net/minestom/server/statistic/StatisticTypeImpl.java b/src/main/java/net/minestom/server/statistic/StatisticTypeImpl.java index aa6da6ea5..5c9746258 100644 --- a/src/main/java/net/minestom/server/statistic/StatisticTypeImpl.java +++ b/src/main/java/net/minestom/server/statistic/StatisticTypeImpl.java @@ -1,6 +1,5 @@ package net.minestom.server.statistic; -import com.google.gson.JsonObject; import net.minestom.server.registry.Registry; import net.minestom.server.utils.NamespaceID; import org.jetbrains.annotations.NotNull; @@ -8,33 +7,26 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; final class StatisticTypeImpl implements StatisticType { - private static final Registry.Loader LOADER = new Registry.Loader<>(); + private static final Registry.Container CONTAINER = new Registry.Container<>(Registry.Resource.STATISTICS, + (container, namespace, object) -> { + final int id = object.get("id").getAsInt(); + container.register(new StatisticTypeImpl(NamespaceID.from(namespace), id)); + }); static StatisticType get(@NotNull String namespace) { - return LOADER.get(namespace); + return CONTAINER.get(namespace); } static StatisticType getSafe(@NotNull String namespace) { - return LOADER.getSafe(namespace); + return CONTAINER.getSafe(namespace); } static StatisticType getId(int id) { - return LOADER.getId(id); + return CONTAINER.getId(id); } static Collection values() { - return LOADER.values(); - } - - static { - // Load data from file - JsonObject statistics = Registry.load(Registry.Resource.STATISTICS); - statistics.entrySet().forEach(entry -> { - final String namespace = entry.getKey(); - final JsonObject object = entry.getValue().getAsJsonObject(); - final int id = object.get("id").getAsInt(); - LOADER.register(new StatisticTypeImpl(NamespaceID.from(namespace), id)); - }); + return CONTAINER.values(); } private final NamespaceID namespaceID; diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 6fdccb69c..aa833918b 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -5,6 +5,8 @@ import demo.generator.NoiseTestGenerator; import net.kyori.adventure.text.Component; import net.minestom.server.MinecraftServer; import net.minestom.server.adventure.audience.Audiences; +import net.minestom.server.coordinate.Pos; +import net.minestom.server.coordinate.Vec; import net.minestom.server.entity.Entity; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.ItemEntity; @@ -30,8 +32,6 @@ import net.minestom.server.item.Material; import net.minestom.server.monitoring.BenchmarkManager; import net.minestom.server.monitoring.TickMonitor; import net.minestom.server.utils.MathUtils; -import net.minestom.server.coordinate.Pos; -import net.minestom.server.coordinate.Vec; import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.world.DimensionType;