implement Keyed in generated enums

This commit is contained in:
Kieran Wallbanks 2021-03-05 16:03:01 +00:00
parent d419bf9401
commit b6dd2fa8f1
16 changed files with 179 additions and 31 deletions

View File

@ -1,6 +1,8 @@
package net.minestom.server.entity;
import java.util.function.BiFunction;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.entity.metadata.EntityMeta;
import net.minestom.server.entity.metadata.PlayerMeta;
import net.minestom.server.entity.metadata.ambient.BatMeta;
@ -120,7 +122,7 @@ import org.jetbrains.annotations.NotNull;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum EntityType {
public enum EntityType implements Keyed {
AREA_EFFECT_CLOUD("minecraft:area_effect_cloud", 6.0, 0.5, AreaEffectCloudMeta::new, EntitySpawnType.BASE),
ARMOR_STAND("minecraft:armor_stand", 0.5, 1.975, ArmorStandMeta::new, EntitySpawnType.LIVING),
@ -352,6 +354,8 @@ public enum EntityType {
@NotNull
private final EntitySpawnType spawnType;
private Key key;
EntityType(@NotNull String namespaceID, double width, double height,
@NotNull BiFunction<Entity, Metadata, EntityMeta> metaConstructor,
@NotNull EntitySpawnType spawnType) {
@ -361,6 +365,7 @@ public enum EntityType {
this.metaConstructor = metaConstructor;
this.spawnType = spawnType;
Registries.entityTypes.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public short getId() {
@ -393,4 +398,8 @@ public enum EntityType {
}
return null;
}
public Key key() {
return this.key;
}
}

View File

@ -1,5 +1,7 @@
package net.minestom.server.fluids;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -9,7 +11,7 @@ import net.minestom.server.utils.NamespaceID;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum Fluid {
public enum Fluid implements Keyed {
EMPTY("minecraft:empty"),
FLOWING_WATER("minecraft:flowing_water"),
@ -20,11 +22,14 @@ public enum Fluid {
LAVA("minecraft:lava");
private String namespaceID;
private final String namespaceID;
private Key key;
Fluid(String namespaceID) {
this.namespaceID = namespaceID;
Registries.fluids.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public int getId() {
@ -35,6 +40,10 @@ public enum Fluid {
return namespaceID;
}
public Key key() {
return this.key;
}
public static Fluid fromId(int id) {
if (id >= 0 && id < values().length) {
return values()[id];

View File

@ -2,6 +2,8 @@ package net.minestom.server.instance.block;
import java.util.Arrays;
import java.util.List;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.instance.block.states.AcaciaButton;
import net.minestom.server.instance.block.states.AcaciaDoor;
import net.minestom.server.instance.block.states.AcaciaFence;
@ -481,7 +483,7 @@ import org.jetbrains.annotations.Nullable;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum Block {
public enum Block implements Keyed {
AIR("minecraft:air", (short) 0, 0.0, 0.0, true, false, null, true),
STONE("minecraft:stone", (short) 1, 1.5, 6.0, false, true, null, true),
@ -2480,25 +2482,27 @@ public enum Block {
}
@NotNull
private String namespaceID;
private final String namespaceID;
private short defaultID;
private final short defaultID;
private double hardness;
private final double hardness;
private double resistance;
private final double resistance;
private boolean isAir;
private final boolean isAir;
private boolean isSolid;
private final boolean isSolid;
@Nullable
private NamespaceID blockEntity;
private final NamespaceID blockEntity;
private boolean singleState;
private final boolean singleState;
private List<BlockAlternative> alternatives = new java.util.ArrayList<>();
private Key key;
Block(@NotNull String namespaceID, short defaultID, double hardness, double resistance,
boolean isAir, boolean isSolid, @Nullable NamespaceID blockEntity,
boolean singleState) {
@ -2514,6 +2518,7 @@ public enum Block {
addBlockAlternative(new BlockAlternative(defaultID));
}
Registries.blocks.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public short getBlockId() {
@ -2586,4 +2591,8 @@ public enum Block {
public static Block fromStateId(short blockStateId) {
return BlockArray.blocks[blockStateId];
}
public Key key() {
return this.key;
}
}

View File

@ -1,5 +1,7 @@
package net.minestom.server.item;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -9,7 +11,7 @@ import net.minestom.server.utils.NamespaceID;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum Enchantment {
public enum Enchantment implements Keyed {
PROTECTION("minecraft:protection"),
FIRE_PROTECTION("minecraft:fire_protection"),
@ -86,11 +88,14 @@ public enum Enchantment {
VANISHING_CURSE("minecraft:vanishing_curse");
private String namespaceID;
private final String namespaceID;
private Key key;
Enchantment(String namespaceID) {
this.namespaceID = namespaceID;
Registries.enchantments.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public int getId() {
@ -101,6 +106,10 @@ public enum Enchantment {
return namespaceID;
}
public Key key() {
return this.key;
}
public static Enchantment fromId(int id) {
if (id >= 0 && id < values().length) {
return values()[id];

View File

@ -1,5 +1,7 @@
package net.minestom.server.item;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.instance.block.Block;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -12,7 +14,7 @@ import org.jetbrains.annotations.Nullable;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum Material {
public enum Material implements Keyed {
AIR("minecraft:air", 64, Block.AIR),
STONE("minecraft:stone", 64, Block.STONE),
@ -1966,12 +1968,14 @@ public enum Material {
RESPAWN_ANCHOR("minecraft:respawn_anchor", 64, Block.RESPAWN_ANCHOR);
@NotNull
private String namespaceID;
private final String namespaceID;
private int maxDefaultStackSize;
private final int maxDefaultStackSize;
@Nullable
private Block correspondingBlock;
private final Block correspondingBlock;
private Key key;
Material(@NotNull String namespaceID, int maxDefaultStackSize,
@Nullable Block correspondingBlock) {
@ -1979,6 +1983,7 @@ public enum Material {
this.maxDefaultStackSize = maxDefaultStackSize;
this.correspondingBlock = correspondingBlock;
Registries.materials.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public short getId() {
@ -2083,4 +2088,8 @@ public enum Material {
}
return isFood();
}
public Key key() {
return this.key;
}
}

View File

@ -1,5 +1,7 @@
package net.minestom.server.particle;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -9,7 +11,7 @@ import net.minestom.server.utils.NamespaceID;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum Particle {
public enum Particle implements Keyed {
AMBIENT_ENTITY_EFFECT("minecraft:ambient_entity_effect"),
ANGRY_VILLAGER("minecraft:angry_villager"),
@ -154,11 +156,14 @@ public enum Particle {
WHITE_ASH("minecraft:white_ash");
private String namespaceID;
private final String namespaceID;
private Key key;
Particle(String namespaceID) {
this.namespaceID = namespaceID;
Registries.particles.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public int getId() {
@ -169,6 +174,10 @@ public enum Particle {
return namespaceID;
}
public Key key() {
return this.key;
}
public static Particle fromId(int id) {
if (id >= 0 && id < values().length) {
return values()[id];

View File

@ -1,5 +1,7 @@
package net.minestom.server.potion;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -9,7 +11,7 @@ import net.minestom.server.utils.NamespaceID;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum PotionEffect {
public enum PotionEffect implements Keyed {
SPEED("minecraft:speed"),
SLOWNESS("minecraft:slowness"),
@ -74,11 +76,14 @@ public enum PotionEffect {
HERO_OF_THE_VILLAGE("minecraft:hero_of_the_village");
private String namespaceID;
private final String namespaceID;
private Key key;
PotionEffect(String namespaceID) {
this.namespaceID = namespaceID;
Registries.potionEffects.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public int getId() {
@ -89,6 +94,10 @@ public enum PotionEffect {
return namespaceID;
}
public Key key() {
return this.key;
}
public static PotionEffect fromId(int id) {
if (id >= 0 && id < values().length + 1) {
return values()[id - 1];

View File

@ -1,5 +1,7 @@
package net.minestom.server.potion;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -9,7 +11,7 @@ import net.minestom.server.utils.NamespaceID;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum PotionType {
public enum PotionType implements Keyed {
EMPTY("minecraft:empty"),
WATER("minecraft:water"),
@ -96,11 +98,14 @@ public enum PotionType {
LONG_SLOW_FALLING("minecraft:long_slow_falling");
private String namespaceID;
private final String namespaceID;
private Key key;
PotionType(String namespaceID) {
this.namespaceID = namespaceID;
Registries.potionTypes.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public int getId() {
@ -111,6 +116,10 @@ public enum PotionType {
return namespaceID;
}
public Key key() {
return this.key;
}
public static PotionType fromId(int id) {
if (id >= 0 && id < values().length) {
return values()[id];

View File

@ -1,5 +1,7 @@
package net.minestom.server.sound;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -9,7 +11,7 @@ import net.minestom.server.utils.NamespaceID;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum Sound {
public enum Sound implements Keyed {
AMBIENT_CAVE("minecraft:ambient.cave"),
AMBIENT_BASALT_DELTAS_ADDITIONS("minecraft:ambient.basalt_deltas.additions"),
@ -1994,11 +1996,14 @@ public enum Sound {
ENTITY_ZOMBIE_VILLAGER_STEP("minecraft:entity.zombie_villager.step");
private String namespaceID;
private final String namespaceID;
private Key key;
Sound(String namespaceID) {
this.namespaceID = namespaceID;
Registries.sounds.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public int getId() {
@ -2009,6 +2014,10 @@ public enum Sound {
return namespaceID;
}
public Key key() {
return this.key;
}
public static Sound fromId(int id) {
if (id >= 0 && id < values().length) {
return values()[id];

View File

@ -1,5 +1,7 @@
package net.minestom.server.stat;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -9,7 +11,7 @@ import net.minestom.server.utils.NamespaceID;
* //==============================
*/
@SuppressWarnings({"deprecation"})
public enum StatisticType {
public enum StatisticType implements Keyed {
LEAVE_GAME("minecraft:leave_game"),
PLAY_ONE_MINUTE("minecraft:play_one_minute"),
@ -158,11 +160,14 @@ public enum StatisticType {
INTERACT_WITH_SMITHING_TABLE("minecraft:interact_with_smithing_table");
private String namespaceID;
private final String namespaceID;
private Key key;
StatisticType(String namespaceID) {
this.namespaceID = namespaceID;
Registries.statisticTypes.put(NamespaceID.from(namespaceID), this);
this.key = Key.key(this.namespaceID);
}
public int getId() {
@ -173,6 +178,10 @@ public enum StatisticType {
return namespaceID;
}
public Key key() {
return this.key;
}
public static StatisticType fromId(int id) {
if (id >= 0 && id < values().length) {
return values()[id];

View File

@ -16,6 +16,7 @@ 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);

View File

@ -3,6 +3,8 @@ package net.minestom.codegen;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.squareup.javapoet.*;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@ -116,6 +118,12 @@ public abstract class BasicEnumGenerator extends MinestomEnumGenerator<BasicEnum
generator.appendToConstructor(code -> {
code.addStatement("$T." + CodeGenerator.decapitalize(getClassName()) + "s.put($T.from($N), this)", registriesClass, NamespaceID.class, "namespaceID");
});
// implement Keyed
generator.addSuperinterface(ClassName.get(Keyed.class));
generator.addField(ClassName.get(Key.class), "key");
generator.appendToConstructor(code -> code.addStatement("this.key = Key.key(this.namespaceID)"));
generator.addMethod("key", new ParameterSpec[0], ClassName.get(Key.class), code -> code.addStatement("return this.key"));
}
@Override

View File

@ -20,9 +20,11 @@ public class EnumGenerator implements CodeGenerator {
private final String enumName;
private ParameterSpec[] parameters;
private List<TypeName> superinterfaces = new LinkedList<>();
private List<Method> methods = new LinkedList<>();
private List<Field> fields = new LinkedList<>();
private List<Field> staticFields = new LinkedList<>();
private List<Instance> instances = new LinkedList<>();
private List<Field> fields = new LinkedList<>();
private List<Field> hardcodedFields = new LinkedList<>();
private List<AnnotationSpec> annotations = new LinkedList<>();
private String enumPackage;
@ -35,6 +37,10 @@ public class EnumGenerator implements CodeGenerator {
this.enumName = enumName;
}
public void addSuperinterface(TypeName typeNames) {
superinterfaces.add(typeNames);
}
public void setParams(ParameterSpec... parameters) {
this.parameters = parameters;
}
@ -52,7 +58,7 @@ public class EnumGenerator implements CodeGenerator {
}
public void addStaticField(TypeName type, String name, String value) {
fields.add(new Field(type, name, value));
staticFields.add(new Field(type, name, value));
}
public void addInstance(String name, Object... parameters) {
@ -86,6 +92,9 @@ public class EnumGenerator implements CodeGenerator {
enumClass.addEnumConstant(instance.name, arguments);
}
// add superinterfaces
enumClass.addSuperinterfaces(superinterfaces);
if (staticBlock != null) {
enumClass.addStaticBlock(staticBlock);
}
@ -100,7 +109,7 @@ public class EnumGenerator implements CodeGenerator {
.build());
}
for (Field field : fields) {
for (Field field : staticFields) {
enumClass.addField(FieldSpec.builder(field.type, field.name)
.initializer("$L", field.value)
.addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC)
@ -115,6 +124,13 @@ public class EnumGenerator implements CodeGenerator {
.build());
}
// normal fields
for (Field field : fields) {
enumClass.addField(FieldSpec.builder(field.type, field.name)
.addModifiers(Modifier.PRIVATE)
.build());
}
// constructor
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
for (int i = 0; i < parameters.length; i++) {
@ -166,6 +182,10 @@ public class EnumGenerator implements CodeGenerator {
constructorEnds.add(constructorEnding);
}
public void addField(TypeName type, String name) {
fields.add(new Field(type, name));
}
public void addHardcodedField(TypeName type, String name, String value) {
hardcodedFields.add(new Field(type, name, value));
}
@ -210,6 +230,10 @@ public class EnumGenerator implements CodeGenerator {
private String name;
private String value;
public Field(TypeName type, String name) {
this(type, name, null);
}
public Field(TypeName type, String name, String value) {
this.type = type;
this.name = name;

View File

@ -5,6 +5,8 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.squareup.javapoet.*;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.codegen.EnumGenerator;
import net.minestom.codegen.MinestomEnumGenerator;
import net.minestom.codegen.PrismarinePaths;
@ -298,6 +300,12 @@ public class BlockEnumGenerator extends MinestomEnumGenerator<BlockContainer> {
.endControlFlow()
.addStatement("$T.blocks.put($T.from(namespaceID), this)", Registries.class, NamespaceID.class);
});
// implement Keyed
generator.addSuperinterface(ClassName.get(Keyed.class));
generator.addField(ClassName.get(Key.class), "key");
generator.appendToConstructor(code -> code.addStatement("this.key = Key.key(this.namespaceID)"));
generator.addMethod("key", new ParameterSpec[0], ClassName.get(Key.class), code -> code.addStatement("return this.key"));
}
@Override

View File

@ -4,6 +4,8 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.squareup.javapoet.*;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.codegen.ConstructorLambda;
import net.minestom.codegen.EnumGenerator;
import net.minestom.codegen.MinestomEnumGenerator;
@ -19,6 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.lang.model.element.TypeElement;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
@ -177,6 +180,12 @@ public class EntityTypeEnumGenerator extends MinestomEnumGenerator<EntityTypeCon
.endControlFlow()
.addStatement("return null");
});
// implement Keyed
generator.addSuperinterface(ClassName.get(Keyed.class));
generator.addField(ClassName.get(Key.class), "key");
generator.appendToConstructor(code -> code.addStatement("this.key = Key.key(this.namespaceID)"));
generator.addMethod("key", new ParameterSpec[0], ClassName.get(Key.class), code -> code.addStatement("return this.key"));
}
@Override

View File

@ -4,6 +4,8 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.squareup.javapoet.*;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.minestom.codegen.EnumGenerator;
import net.minestom.codegen.MinestomEnumGenerator;
import net.minestom.codegen.PrismarinePaths;
@ -218,6 +220,12 @@ public class ItemEnumGenerator extends MinestomEnumGenerator<ItemContainer> {
.endControlFlow()
.addStatement("return isFood()");
});
// implement Keyed
generator.addSuperinterface(ClassName.get(Keyed.class));
generator.addField(ClassName.get(Key.class), "key");
generator.appendToConstructor(code -> code.addStatement("this.key = Key.key(this.namespaceID)"));
generator.addMethod("key", new ParameterSpec[0], ClassName.get(Key.class), code -> code.addStatement("return this.key"));
}
@Override