mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-31 21:48:08 +01:00
allow obtaining enum values from registries using adventure key
This commit is contained in:
parent
89a962de9e
commit
4179e5b91e
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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<? extends Annotation> 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())
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user