From 5ac5b0ba99d1e6d0e0ba3d81fb47463f72958848 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Fri, 12 Mar 2021 11:16:00 +0000 Subject: [PATCH] Finalise key in generated enums --- .../minestom/server/entity/EntityType.java | 96 ++----------------- .../net/minestom/server/fluids/Fluid.java | 2 +- .../minestom/server/instance/block/Block.java | 2 +- .../net/minestom/server/item/Enchantment.java | 2 +- .../net/minestom/server/item/Material.java | 2 +- .../minestom/server/particle/Particle.java | 2 +- .../minestom/server/potion/PotionEffect.java | 2 +- .../minestom/server/potion/PotionType.java | 2 +- .../java/net/minestom/server/sound/Sound.java | 2 +- .../minestom/server/stat/StatisticType.java | 2 +- .../minestom/codegen/BasicEnumGenerator.java | 2 +- .../net/minestom/codegen/EnumGenerator.java | 20 ++-- .../codegen/blocks/BlockEnumGenerator.java | 2 +- .../entitytypes/EntityTypeEnumGenerator.java | 3 +- .../codegen/items/ItemEnumGenerator.java | 2 +- 15 files changed, 36 insertions(+), 107 deletions(-) diff --git a/src/autogenerated/java/net/minestom/server/entity/EntityType.java b/src/autogenerated/java/net/minestom/server/entity/EntityType.java index c5cf9ccb8..0f7c68b77 100644 --- a/src/autogenerated/java/net/minestom/server/entity/EntityType.java +++ b/src/autogenerated/java/net/minestom/server/entity/EntityType.java @@ -1,31 +1,11 @@ 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; -import net.minestom.server.entity.metadata.animal.BeeMeta; -import net.minestom.server.entity.metadata.animal.ChickenMeta; -import net.minestom.server.entity.metadata.animal.CowMeta; -import net.minestom.server.entity.metadata.animal.DonkeyMeta; -import net.minestom.server.entity.metadata.animal.FoxMeta; -import net.minestom.server.entity.metadata.animal.HoglinMeta; -import net.minestom.server.entity.metadata.animal.HorseMeta; -import net.minestom.server.entity.metadata.animal.LlamaMeta; -import net.minestom.server.entity.metadata.animal.MooshroomMeta; -import net.minestom.server.entity.metadata.animal.MuleMeta; -import net.minestom.server.entity.metadata.animal.OcelotMeta; -import net.minestom.server.entity.metadata.animal.PandaMeta; -import net.minestom.server.entity.metadata.animal.PigMeta; -import net.minestom.server.entity.metadata.animal.PolarBearMeta; -import net.minestom.server.entity.metadata.animal.RabbitMeta; -import net.minestom.server.entity.metadata.animal.SheepMeta; -import net.minestom.server.entity.metadata.animal.SkeletonHorseMeta; -import net.minestom.server.entity.metadata.animal.StriderMeta; -import net.minestom.server.entity.metadata.animal.TurtleMeta; -import net.minestom.server.entity.metadata.animal.ZombieHorseMeta; +import net.minestom.server.entity.metadata.animal.*; import net.minestom.server.entity.metadata.animal.tameable.CatMeta; import net.minestom.server.entity.metadata.animal.tameable.ParrotMeta; import net.minestom.server.entity.metadata.animal.tameable.WolfMeta; @@ -37,73 +17,15 @@ import net.minestom.server.entity.metadata.flying.PhantomMeta; import net.minestom.server.entity.metadata.golem.IronGolemMeta; import net.minestom.server.entity.metadata.golem.ShulkerMeta; import net.minestom.server.entity.metadata.golem.SnowGolemMeta; -import net.minestom.server.entity.metadata.item.EyeOfEnderMeta; -import net.minestom.server.entity.metadata.item.FireballMeta; -import net.minestom.server.entity.metadata.item.ItemEntityMeta; -import net.minestom.server.entity.metadata.item.SmallFireballMeta; -import net.minestom.server.entity.metadata.item.SnowballMeta; -import net.minestom.server.entity.metadata.item.ThrownEggMeta; -import net.minestom.server.entity.metadata.item.ThrownEnderPearlMeta; -import net.minestom.server.entity.metadata.item.ThrownExperienceBottleMeta; -import net.minestom.server.entity.metadata.item.ThrownPotionMeta; -import net.minestom.server.entity.metadata.minecart.ChestMinecartMeta; -import net.minestom.server.entity.metadata.minecart.CommandBlockMinecartMeta; -import net.minestom.server.entity.metadata.minecart.FurnaceMinecartMeta; -import net.minestom.server.entity.metadata.minecart.HopperMinecartMeta; -import net.minestom.server.entity.metadata.minecart.MinecartMeta; -import net.minestom.server.entity.metadata.minecart.SpawnerMinecartMeta; -import net.minestom.server.entity.metadata.minecart.TntMinecartMeta; -import net.minestom.server.entity.metadata.monster.BlazeMeta; -import net.minestom.server.entity.metadata.monster.CaveSpiderMeta; -import net.minestom.server.entity.metadata.monster.CreeperMeta; -import net.minestom.server.entity.metadata.monster.ElderGuardianMeta; -import net.minestom.server.entity.metadata.monster.EndermanMeta; -import net.minestom.server.entity.metadata.monster.EndermiteMeta; -import net.minestom.server.entity.metadata.monster.GiantMeta; -import net.minestom.server.entity.metadata.monster.GuardianMeta; -import net.minestom.server.entity.metadata.monster.PiglinBruteMeta; -import net.minestom.server.entity.metadata.monster.PiglinMeta; -import net.minestom.server.entity.metadata.monster.SilverfishMeta; -import net.minestom.server.entity.metadata.monster.SpiderMeta; -import net.minestom.server.entity.metadata.monster.VexMeta; -import net.minestom.server.entity.metadata.monster.WitherMeta; -import net.minestom.server.entity.metadata.monster.ZoglinMeta; -import net.minestom.server.entity.metadata.monster.raider.EvokerMeta; -import net.minestom.server.entity.metadata.monster.raider.IllusionerMeta; -import net.minestom.server.entity.metadata.monster.raider.PillagerMeta; -import net.minestom.server.entity.metadata.monster.raider.RavagerMeta; -import net.minestom.server.entity.metadata.monster.raider.VindicatorMeta; -import net.minestom.server.entity.metadata.monster.raider.WitchMeta; +import net.minestom.server.entity.metadata.item.*; +import net.minestom.server.entity.metadata.minecart.*; +import net.minestom.server.entity.metadata.monster.*; +import net.minestom.server.entity.metadata.monster.raider.*; import net.minestom.server.entity.metadata.monster.skeleton.SkeletonMeta; import net.minestom.server.entity.metadata.monster.skeleton.StrayMeta; import net.minestom.server.entity.metadata.monster.skeleton.WitherSkeletonMeta; -import net.minestom.server.entity.metadata.monster.zombie.DrownedMeta; -import net.minestom.server.entity.metadata.monster.zombie.HuskMeta; -import net.minestom.server.entity.metadata.monster.zombie.ZombieMeta; -import net.minestom.server.entity.metadata.monster.zombie.ZombieVillagerMeta; -import net.minestom.server.entity.metadata.monster.zombie.ZombifiedPiglinMeta; -import net.minestom.server.entity.metadata.other.AreaEffectCloudMeta; -import net.minestom.server.entity.metadata.other.ArmorStandMeta; -import net.minestom.server.entity.metadata.other.BoatMeta; -import net.minestom.server.entity.metadata.other.DragonFireballMeta; -import net.minestom.server.entity.metadata.other.EndCrystalMeta; -import net.minestom.server.entity.metadata.other.EnderDragonMeta; -import net.minestom.server.entity.metadata.other.EvokerFangsMeta; -import net.minestom.server.entity.metadata.other.ExperienceOrbMeta; -import net.minestom.server.entity.metadata.other.FallingBlockMeta; -import net.minestom.server.entity.metadata.other.FireworkRocketMeta; -import net.minestom.server.entity.metadata.other.FishingHookMeta; -import net.minestom.server.entity.metadata.other.ItemFrameMeta; -import net.minestom.server.entity.metadata.other.LeashKnotMeta; -import net.minestom.server.entity.metadata.other.LightningBoltMeta; -import net.minestom.server.entity.metadata.other.LlamaSpitMeta; -import net.minestom.server.entity.metadata.other.MagmaCubeMeta; -import net.minestom.server.entity.metadata.other.PaintingMeta; -import net.minestom.server.entity.metadata.other.PrimedTntMeta; -import net.minestom.server.entity.metadata.other.ShulkerBulletMeta; -import net.minestom.server.entity.metadata.other.SlimeMeta; -import net.minestom.server.entity.metadata.other.TraderLlamaMeta; -import net.minestom.server.entity.metadata.other.WitherSkullMeta; +import net.minestom.server.entity.metadata.monster.zombie.*; +import net.minestom.server.entity.metadata.other.*; import net.minestom.server.entity.metadata.villager.VillagerMeta; import net.minestom.server.entity.metadata.villager.WanderingTraderMeta; import net.minestom.server.entity.metadata.water.DolphinMeta; @@ -116,6 +38,8 @@ import net.minestom.server.registry.Registries; import net.minestom.server.utils.NamespaceID; import org.jetbrains.annotations.NotNull; +import java.util.function.BiFunction; + /** * //============================== * // AUTOGENERATED BY EnumGenerator @@ -354,7 +278,7 @@ public enum EntityType implements Keyed { @NotNull private final EntitySpawnType spawnType; - private Key key; + private final Key key; EntityType(@NotNull String namespaceID, double width, double height, @NotNull BiFunction metaConstructor, diff --git a/src/autogenerated/java/net/minestom/server/fluids/Fluid.java b/src/autogenerated/java/net/minestom/server/fluids/Fluid.java index 73d3b565d..7d73de35a 100644 --- a/src/autogenerated/java/net/minestom/server/fluids/Fluid.java +++ b/src/autogenerated/java/net/minestom/server/fluids/Fluid.java @@ -24,7 +24,7 @@ public enum Fluid implements Keyed { private final String namespaceID; - private Key key; + private final Key key; Fluid(String namespaceID) { this.namespaceID = namespaceID; diff --git a/src/autogenerated/java/net/minestom/server/instance/block/Block.java b/src/autogenerated/java/net/minestom/server/instance/block/Block.java index 337d1d1ad..c4788bac6 100644 --- a/src/autogenerated/java/net/minestom/server/instance/block/Block.java +++ b/src/autogenerated/java/net/minestom/server/instance/block/Block.java @@ -2501,7 +2501,7 @@ public enum Block implements Keyed { private List alternatives = new java.util.ArrayList<>(); - private Key key; + private final Key key; Block(@NotNull String namespaceID, short defaultID, double hardness, double resistance, boolean isAir, boolean isSolid, @Nullable NamespaceID blockEntity, diff --git a/src/autogenerated/java/net/minestom/server/item/Enchantment.java b/src/autogenerated/java/net/minestom/server/item/Enchantment.java index 6b3524707..faba57e8b 100644 --- a/src/autogenerated/java/net/minestom/server/item/Enchantment.java +++ b/src/autogenerated/java/net/minestom/server/item/Enchantment.java @@ -90,7 +90,7 @@ public enum Enchantment implements Keyed { private final String namespaceID; - private Key key; + private final Key key; Enchantment(String namespaceID) { this.namespaceID = namespaceID; diff --git a/src/autogenerated/java/net/minestom/server/item/Material.java b/src/autogenerated/java/net/minestom/server/item/Material.java index b6fdfb9a2..0a6f526ac 100644 --- a/src/autogenerated/java/net/minestom/server/item/Material.java +++ b/src/autogenerated/java/net/minestom/server/item/Material.java @@ -1975,7 +1975,7 @@ public enum Material implements Keyed { @Nullable private final Block correspondingBlock; - private Key key; + private final Key key; Material(@NotNull String namespaceID, int maxDefaultStackSize, @Nullable Block correspondingBlock) { diff --git a/src/autogenerated/java/net/minestom/server/particle/Particle.java b/src/autogenerated/java/net/minestom/server/particle/Particle.java index f62c5b600..378db4edc 100644 --- a/src/autogenerated/java/net/minestom/server/particle/Particle.java +++ b/src/autogenerated/java/net/minestom/server/particle/Particle.java @@ -158,7 +158,7 @@ public enum Particle implements Keyed { private final String namespaceID; - private Key key; + private final Key key; Particle(String namespaceID) { this.namespaceID = namespaceID; diff --git a/src/autogenerated/java/net/minestom/server/potion/PotionEffect.java b/src/autogenerated/java/net/minestom/server/potion/PotionEffect.java index 28af57370..826394ee1 100644 --- a/src/autogenerated/java/net/minestom/server/potion/PotionEffect.java +++ b/src/autogenerated/java/net/minestom/server/potion/PotionEffect.java @@ -78,7 +78,7 @@ public enum PotionEffect implements Keyed { private final String namespaceID; - private Key key; + private final Key key; PotionEffect(String namespaceID) { this.namespaceID = namespaceID; diff --git a/src/autogenerated/java/net/minestom/server/potion/PotionType.java b/src/autogenerated/java/net/minestom/server/potion/PotionType.java index 0c3a7a59f..375314472 100644 --- a/src/autogenerated/java/net/minestom/server/potion/PotionType.java +++ b/src/autogenerated/java/net/minestom/server/potion/PotionType.java @@ -100,7 +100,7 @@ public enum PotionType implements Keyed { private final String namespaceID; - private Key key; + private final Key key; PotionType(String namespaceID) { this.namespaceID = namespaceID; diff --git a/src/autogenerated/java/net/minestom/server/sound/Sound.java b/src/autogenerated/java/net/minestom/server/sound/Sound.java index af1f16e83..2394aff1a 100644 --- a/src/autogenerated/java/net/minestom/server/sound/Sound.java +++ b/src/autogenerated/java/net/minestom/server/sound/Sound.java @@ -1998,7 +1998,7 @@ public enum Sound implements Keyed { private final String namespaceID; - private Key key; + private final Key key; Sound(String namespaceID) { this.namespaceID = namespaceID; diff --git a/src/autogenerated/java/net/minestom/server/stat/StatisticType.java b/src/autogenerated/java/net/minestom/server/stat/StatisticType.java index f7ebd8836..fea4762ff 100644 --- a/src/autogenerated/java/net/minestom/server/stat/StatisticType.java +++ b/src/autogenerated/java/net/minestom/server/stat/StatisticType.java @@ -162,7 +162,7 @@ public enum StatisticType implements Keyed { private final String namespaceID; - private Key key; + private final Key key; StatisticType(String namespaceID) { this.namespaceID = namespaceID; diff --git a/src/generators/java/net/minestom/codegen/BasicEnumGenerator.java b/src/generators/java/net/minestom/codegen/BasicEnumGenerator.java index 6cbb94273..b51ae322a 100644 --- a/src/generators/java/net/minestom/codegen/BasicEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/BasicEnumGenerator.java @@ -121,7 +121,7 @@ public abstract class BasicEnumGenerator extends MinestomEnumGenerator code.addStatement("this.key = Key.key(this.namespaceID)")); generator.addMethod("key", new ParameterSpec[0], ClassName.get(Key.class), code -> code.addStatement("return this.key")); } diff --git a/src/generators/java/net/minestom/codegen/EnumGenerator.java b/src/generators/java/net/minestom/codegen/EnumGenerator.java index 6fd1479cb..c61f4d21d 100644 --- a/src/generators/java/net/minestom/codegen/EnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/EnumGenerator.java @@ -1,6 +1,7 @@ package net.minestom.codegen; import com.squareup.javapoet.*; +import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,7 +25,7 @@ public class EnumGenerator implements CodeGenerator { private List methods = new LinkedList<>(); private List staticFields = new LinkedList<>(); private List instances = new LinkedList<>(); - private List fields = new LinkedList<>(); + private List> fields = new LinkedList<>(); private List hardcodedFields = new LinkedList<>(); private List annotations = new LinkedList<>(); private String enumPackage; @@ -125,10 +126,15 @@ public class EnumGenerator implements CodeGenerator { } // normal fields - for (Field field : fields) { - enumClass.addField(FieldSpec.builder(field.type, field.name) - .addModifiers(Modifier.PRIVATE) - .build()); + for (Pair field : fields) { + FieldSpec.Builder builder = FieldSpec.builder(field.getLeft().type, field.getLeft().name) + .addModifiers(Modifier.PRIVATE); + + if (field.getRight()) { + builder.addModifiers(Modifier.FINAL); + } + + enumClass.addField(builder.build()); } // constructor @@ -182,8 +188,8 @@ public class EnumGenerator implements CodeGenerator { constructorEnds.add(constructorEnding); } - public void addField(TypeName type, String name) { - fields.add(new Field(type, name)); + public void addField(TypeName type, String name, boolean isFinal) { + fields.add(Pair.of(new Field(type, name), isFinal)); } public void addHardcodedField(TypeName type, String name, String value) { diff --git a/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java b/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java index 89437ae5f..168ea6611 100644 --- a/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java @@ -303,7 +303,7 @@ public class BlockEnumGenerator extends MinestomEnumGenerator { // implement Keyed generator.addSuperinterface(ClassName.get(Keyed.class)); - generator.addField(ClassName.get(Key.class), "key"); + generator.addField(ClassName.get(Key.class), "key", true); 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")); } diff --git a/src/generators/java/net/minestom/codegen/entitytypes/EntityTypeEnumGenerator.java b/src/generators/java/net/minestom/codegen/entitytypes/EntityTypeEnumGenerator.java index 007975c49..741c2c7e6 100644 --- a/src/generators/java/net/minestom/codegen/entitytypes/EntityTypeEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/entitytypes/EntityTypeEnumGenerator.java @@ -21,7 +21,6 @@ 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; @@ -183,7 +182,7 @@ public class EntityTypeEnumGenerator extends MinestomEnumGenerator code.addStatement("this.key = Key.key(this.namespaceID)")); generator.addMethod("key", new ParameterSpec[0], ClassName.get(Key.class), code -> code.addStatement("return this.key")); } diff --git a/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java b/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java index fba48697b..0c18693b2 100644 --- a/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java @@ -223,7 +223,7 @@ public class ItemEnumGenerator extends MinestomEnumGenerator { // implement Keyed generator.addSuperinterface(ClassName.get(Keyed.class)); - generator.addField(ClassName.get(Key.class), "key"); + generator.addField(ClassName.get(Key.class), "key", true); 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")); }