From 4179e5b91e4b02e2c6fd80bc5f814ee49ccd3ba7 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Fri, 5 Mar 2021 18:49:38 +0000 Subject: [PATCH] allow obtaining enum values from registries using adventure key --- .../minestom/server/registry/Registries.java | 81 +++++++++++++++++++ .../net/minestom/codegen/AllGenerators.java | 1 - .../minestom/codegen/RegistriesGenerator.java | 12 +++ .../minestom/server/utils/NamespaceID.java | 5 ++ 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/autogenerated/java/net/minestom/server/registry/Registries.java b/src/autogenerated/java/net/minestom/server/registry/Registries.java index b4ab492cd..b38f4c8a4 100644 --- a/src/autogenerated/java/net/minestom/server/registry/Registries.java +++ b/src/autogenerated/java/net/minestom/server/registry/Registries.java @@ -2,6 +2,7 @@ package net.minestom.server.registry; import java.util.HashMap; +import net.kyori.adventure.key.Key; import net.minestom.server.entity.EntityType; import net.minestom.server.fluids.Fluid; import net.minestom.server.instance.block.Block; @@ -96,6 +97,14 @@ public final class Registries { return blocks.getOrDefault(id, Block.AIR); } + /** + * Returns the corresponding Block matching the given key. Returns 'AIR' if none match. + */ + @NotNull + public static Block getBlock(Key key) { + return getBlock(NamespaceID.from(key)); + } + /** * Returns the corresponding Material matching the given id. Returns 'AIR' if none match. */ @@ -112,6 +121,14 @@ public final class Registries { return materials.getOrDefault(id, Material.AIR); } + /** + * Returns the corresponding Material matching the given key. Returns 'AIR' if none match. + */ + @NotNull + public static Material getMaterial(Key key) { + return getMaterial(NamespaceID.from(key)); + } + /** * Returns the corresponding Enchantment matching the given id. Returns null if none match. */ @@ -128,6 +145,14 @@ public final class Registries { return enchantments.get(id); } + /** + * Returns the corresponding Enchantment matching the given key. Returns null if none match. + */ + @Nullable + public static Enchantment getEnchantment(Key key) { + return getEnchantment(NamespaceID.from(key)); + } + /** * Returns the corresponding EntityType matching the given id. Returns null if none match. */ @@ -144,6 +169,14 @@ public final class Registries { return entityTypes.get(id); } + /** + * Returns the corresponding EntityType matching the given key. Returns null if none match. + */ + @Nullable + public static EntityType getEntityType(Key key) { + return getEntityType(NamespaceID.from(key)); + } + /** * Returns the corresponding Particle matching the given id. Returns null if none match. */ @@ -160,6 +193,14 @@ public final class Registries { return particles.get(id); } + /** + * Returns the corresponding Particle matching the given key. Returns null if none match. + */ + @Nullable + public static Particle getParticle(Key key) { + return getParticle(NamespaceID.from(key)); + } + /** * Returns the corresponding PotionType matching the given id. Returns null if none match. */ @@ -176,6 +217,14 @@ public final class Registries { return potionTypes.get(id); } + /** + * Returns the corresponding PotionType matching the given key. Returns null if none match. + */ + @Nullable + public static PotionType getPotionType(Key key) { + return getPotionType(NamespaceID.from(key)); + } + /** * Returns the corresponding PotionEffect matching the given id. Returns null if none match. */ @@ -192,6 +241,14 @@ public final class Registries { return potionEffects.get(id); } + /** + * Returns the corresponding PotionEffect matching the given key. Returns null if none match. + */ + @Nullable + public static PotionEffect getPotionEffect(Key key) { + return getPotionEffect(NamespaceID.from(key)); + } + /** * Returns the corresponding Sound matching the given id. Returns null if none match. */ @@ -208,6 +265,14 @@ public final class Registries { return sounds.get(id); } + /** + * Returns the corresponding Sound matching the given key. Returns null if none match. + */ + @Nullable + public static Sound getSound(Key key) { + return getSound(NamespaceID.from(key)); + } + /** * Returns the corresponding StatisticType matching the given id. Returns null if none match. */ @@ -224,6 +289,14 @@ public final class Registries { return statisticTypes.get(id); } + /** + * Returns the corresponding StatisticType matching the given key. Returns null if none match. + */ + @Nullable + public static StatisticType getStatisticType(Key key) { + return getStatisticType(NamespaceID.from(key)); + } + /** * Returns the corresponding Fluid matching the given id. Returns 'EMPTY' if none match. */ @@ -239,4 +312,12 @@ public final class Registries { public static Fluid getFluid(NamespaceID id) { return fluids.getOrDefault(id, Fluid.EMPTY); } + + /** + * Returns the corresponding Fluid matching the given key. Returns 'EMPTY' if none match. + */ + @NotNull + public static Fluid getFluid(Key key) { + return getFluid(NamespaceID.from(key)); + } } diff --git a/src/generators/java/net/minestom/codegen/AllGenerators.java b/src/generators/java/net/minestom/codegen/AllGenerators.java index cc1ad65f1..d63856e81 100644 --- a/src/generators/java/net/minestom/codegen/AllGenerators.java +++ b/src/generators/java/net/minestom/codegen/AllGenerators.java @@ -16,7 +16,6 @@ import java.io.IOException; public class AllGenerators { public static void main(String[] args) throws IOException { - args = new String[]{"1.16.5", "src/autogenerated/java"}; BlockEnumGenerator.main(args); ItemEnumGenerator.main(args); // must be done after block PotionEnumGenerator.main(args); diff --git a/src/generators/java/net/minestom/codegen/RegistriesGenerator.java b/src/generators/java/net/minestom/codegen/RegistriesGenerator.java index fdcbba675..b96c2eb46 100644 --- a/src/generators/java/net/minestom/codegen/RegistriesGenerator.java +++ b/src/generators/java/net/minestom/codegen/RegistriesGenerator.java @@ -1,6 +1,7 @@ package net.minestom.codegen; import com.squareup.javapoet.*; +import net.kyori.adventure.key.Key; import net.minestom.server.entity.EntityType; import net.minestom.server.fluids.Fluid; import net.minestom.server.instance.block.Block; @@ -101,6 +102,7 @@ public class RegistriesGenerator implements CodeGenerator { ParameterSpec namespaceIDParam = ParameterSpec.builder(ClassName.get(NamespaceID.class), "id") .build(); + ParameterSpec keyIDParam = ParameterSpec.builder(ClassName.get(Key.class), "key").build(); CodeBlock.Builder code = CodeBlock.builder(); Class annotation; @@ -134,6 +136,16 @@ public class RegistriesGenerator implements CodeGenerator { .addCode(code.build()) .addJavadoc(comment.toString()) .build()); + + // Key variant + registriesClass.addMethod(MethodSpec.methodBuilder("get" + simpleType) + .returns(type) + .addAnnotation(annotation) + .addModifiers(Modifier.STATIC, Modifier.PUBLIC) + .addParameter(keyIDParam) + .addStatement("return get$N(NamespaceID.from($N))", simpleType, keyIDParam) + .addJavadoc(comment.toString().replace(" id.", " key.")) + .build()); } JavaFile file = JavaFile.builder("net.minestom.server.registry", registriesClass.build()) diff --git a/src/main/java/net/minestom/server/utils/NamespaceID.java b/src/main/java/net/minestom/server/utils/NamespaceID.java index 9c22c76b2..b4706349f 100644 --- a/src/main/java/net/minestom/server/utils/NamespaceID.java +++ b/src/main/java/net/minestom/server/utils/NamespaceID.java @@ -1,6 +1,7 @@ package net.minestom.server.utils; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; +import net.kyori.adventure.key.Key; import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -62,6 +63,10 @@ public class NamespaceID implements CharSequence { return from(getDomain(id), getPath(id)); } + public static NamespaceID from(Key key) { + return from(key.asString()); + } + private NamespaceID(@NotNull String path) { final int index = path.indexOf(':'); if (index < 0) {