NamespaceID->Resource in Registries.java

This commit is contained in:
jglrxavpok 2020-06-30 12:53:00 +02:00
parent fad0373cb2
commit bf6313659e
18 changed files with 386 additions and 6 deletions

View File

@ -2,6 +2,9 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.entity;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum EntityType {
AREA_EFFECT_CLOUD("minecraft:area_effect_cloud"),
@ -112,6 +115,7 @@ public enum EntityType {
EntityType(String namespaceID) {
this.namespaceID = namespaceID;
Registries.entityTypes.put(NamespaceID.from(namespaceID), this);
}
public int getId() {

View File

@ -2,6 +2,9 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.instance;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum Biome {
OCEAN("minecraft:ocean"),
@ -84,6 +87,7 @@ public enum Biome {
Biome(String namespaceID) {
this.namespaceID = namespaceID;
Registries.biomes.put(NamespaceID.from(namespaceID), this);
}
public int getId() {

View File

@ -2,6 +2,7 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.instance.block;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
import java.util.List;
import java.util.ArrayList;
@ -1136,6 +1137,7 @@ public enum Block {
if(singleState) {
addBlockAlternative(new BlockAlternative(defaultID));
}
Registries.blocks.put(NamespaceID.from(namespaceID), this);
}
public short getBlockId() {

View File

@ -2,6 +2,9 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.item;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum Enchantment {
PROTECTION("minecraft:protection"),
@ -46,6 +49,7 @@ public enum Enchantment {
Enchantment(String namespaceID) {
this.namespaceID = namespaceID;
Registries.enchantments.put(NamespaceID.from(namespaceID), this);
}
public int getId() {

View File

@ -3,6 +3,9 @@
//==============================
package net.minestom.server.item;
import net.minestom.server.instance.block.Block;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum Material {
AIR("minecraft:air", 0, Block.AIR),
@ -898,6 +901,7 @@ public enum Material {
this.namespaceID = namespaceID;
this.maxDefaultStackSize = maxDefaultStackSize;
this.correspondingBlock = correspondingBlock;
Registries.materials.put(NamespaceID.from(namespaceID), this);
}
public short getId() {

View File

@ -2,6 +2,9 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.particle;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum Particle {
AMBIENT_ENTITY_EFFECT("minecraft:ambient_entity_effect"),
@ -71,6 +74,7 @@ public enum Particle {
Particle(String namespaceID) {
this.namespaceID = namespaceID;
Registries.particles.put(NamespaceID.from(namespaceID), this);
}
public int getId() {

View File

@ -2,6 +2,9 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.potion;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum PotionType {
EMPTY("minecraft:empty"),
@ -52,6 +55,7 @@ public enum PotionType {
PotionType(String namespaceID) {
this.namespaceID = namespaceID;
Registries.potionTypes.put(NamespaceID.from(namespaceID), this);
}
public int getId() {

View File

@ -0,0 +1,145 @@
package net.minestom.server.registry;
import java.util.HashMap;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.instance.block.Block;
import net.minestom.server.item.Material;
import net.minestom.server.instance.Biome;
import net.minestom.server.item.Enchantment;
import net.minestom.server.entity.EntityType;
import net.minestom.server.particle.Particle;
import net.minestom.server.potion.PotionType;
import net.minestom.server.sound.Sound;
import net.minestom.server.stat.StatisticType;
// AUTOGENERATED
public class Registries {
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, Block> blocks = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, Material> materials = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, Biome> biomes = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, Enchantment> enchantments = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, EntityType> entityTypes = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, Particle> particles = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, PotionType> potionTypes = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, Sound> sounds = new HashMap<>();
/** Should only be used for internal code, please use the get* methods. */
@Deprecated
public static final HashMap<NamespaceID, StatisticType> statisticTypes = new HashMap<>();
/** Returns the corresponding Block matching the given id. Returns 'AIR' if none match. */
public static Block getBlock(String id) {
return getBlock(NamespaceID.from(id));
}
/** Returns the corresponding Block matching the given id. Returns 'AIR' if none match. */
public static Block getBlock(NamespaceID id) {
return blocks.getOrDefault(id, Block.AIR);
}
/** Returns the corresponding Material matching the given id. Returns 'AIR' if none match. */
public static Material getMaterial(String id) {
return getMaterial(NamespaceID.from(id));
}
/** Returns the corresponding Material matching the given id. Returns 'AIR' if none match. */
public static Material getMaterial(NamespaceID id) {
return materials.getOrDefault(id, Material.AIR);
}
/** Returns the corresponding Biome matching the given id. Returns null if none match. */
public static Biome getBiome(String id) {
return getBiome(NamespaceID.from(id));
}
/** Returns the corresponding Biome matching the given id. Returns null if none match. */
public static Biome getBiome(NamespaceID id) {
return biomes.get(id);
}
/** Returns the corresponding Enchantment matching the given id. Returns null if none match. */
public static Enchantment getEnchantment(String id) {
return getEnchantment(NamespaceID.from(id));
}
/** Returns the corresponding Enchantment matching the given id. Returns null if none match. */
public static Enchantment getEnchantment(NamespaceID id) {
return enchantments.get(id);
}
/** Returns the corresponding EntityType matching the given id. Returns 'PIG' if none match. */
public static EntityType getEntityType(String id) {
return getEntityType(NamespaceID.from(id));
}
/** Returns the corresponding EntityType matching the given id. Returns 'PIG' if none match. */
public static EntityType getEntityType(NamespaceID id) {
return entityTypes.getOrDefault(id, EntityType.PIG);
}
/** Returns the corresponding Particle matching the given id. Returns null if none match. */
public static Particle getParticle(String id) {
return getParticle(NamespaceID.from(id));
}
/** Returns the corresponding Particle matching the given id. Returns null if none match. */
public static Particle getParticle(NamespaceID id) {
return particles.get(id);
}
/** Returns the corresponding PotionType matching the given id. Returns null if none match. */
public static PotionType getPotionType(String id) {
return getPotionType(NamespaceID.from(id));
}
/** Returns the corresponding PotionType matching the given id. Returns null if none match. */
public static PotionType getPotionType(NamespaceID id) {
return potionTypes.get(id);
}
/** Returns the corresponding Sound matching the given id. Returns null if none match. */
public static Sound getSound(String id) {
return getSound(NamespaceID.from(id));
}
/** Returns the corresponding Sound matching the given id. Returns null if none match. */
public static Sound getSound(NamespaceID id) {
return sounds.get(id);
}
/** Returns the corresponding StatisticType matching the given id. Returns null if none match. */
public static StatisticType getStatisticType(String id) {
return getStatisticType(NamespaceID.from(id));
}
/** Returns the corresponding StatisticType matching the given id. Returns null if none match. */
public static StatisticType getStatisticType(NamespaceID id) {
return statisticTypes.get(id);
}
}

View File

@ -2,6 +2,9 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.sound;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum Sound {
AMBIENT_CAVE("minecraft:ambient.cave"),
@ -819,6 +822,7 @@ public enum Sound {
Sound(String namespaceID) {
this.namespaceID = namespaceID;
Registries.sounds.put(NamespaceID.from(namespaceID), this);
}
public int getId() {

View File

@ -2,6 +2,9 @@
// AUTOGENERATED BY EnumGenerator
//==============================
package net.minestom.server.stat;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
@SuppressWarnings({"deprecation"})
public enum StatisticType {
LEAVE_GAME("minecraft:leave_game"),
@ -80,6 +83,7 @@ public enum StatisticType {
StatisticType(String namespaceID) {
this.namespaceID = namespaceID;
Registries.statisticTypes.put(NamespaceID.from(namespaceID), this);
}
public int getId() {

View File

@ -24,5 +24,6 @@ public class AllGenerators {
ParticleEnumGenerator.main(args);
StatsEnumGenerator.main(args);
BiomesEnumGenerator.main(args);
RegistriesGenerator.main(args);
}
}

View File

@ -2,6 +2,7 @@ package net.minestom.codegen;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
import java.io.File;
@ -63,9 +64,14 @@ public abstract class BasicEnumGenerator extends MinestomEnumGenerator<BasicEnum
@Override
protected void prepare(EnumGenerator generator) {
generator.addClassAnnotation("@SuppressWarnings({\"deprecation\"})");
generator.addImport(Registries.class.getCanonicalName());
generator.addImport(NamespaceID.class.getCanonicalName());
generator.setParams("String namespaceID");
generator.addMethod("getId", "()", "int", "return ordinal();");
generator.addMethod("getNamespaceID", "()", "String", "return namespaceID;");
generator.appendToConstructor("Registries."+CodeGenerator.decapitalize(getClassName())+"s.put(NamespaceID.from(namespaceID), this);");
}
@Override

View File

@ -13,4 +13,8 @@ public interface CodeGenerator {
*/
String generate() throws IOException;
static String decapitalize(String text) {
char first = text.charAt(0);
return ""+Character.toLowerCase(first)+text.substring(1);
}
}

View File

@ -0,0 +1,182 @@
package net.minestom.codegen;
import net.minestom.server.entity.EntityType;
import net.minestom.server.instance.Biome;
import net.minestom.server.instance.block.Block;
import net.minestom.server.item.Enchantment;
import net.minestom.server.item.Material;
import net.minestom.server.particle.Particle;
import net.minestom.server.potion.PotionType;
import net.minestom.server.registry.ResourceGatherer;
import net.minestom.server.sound.Sound;
import net.minestom.server.stat.StatisticType;
import net.minestom.server.utils.NamespaceID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.HashMap;
import static net.minestom.codegen.MinestomEnumGenerator.DEFAULT_TARGET_PATH;
/**
* Generates the Registries class, which contains methods to get items/blocks/biomes/etc. from a NamespaceID
*/
public class RegistriesGenerator implements CodeGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistriesGenerator.class);
// Order is important!
private static final String[] types = {
Block.class.getCanonicalName(),
Material.class.getCanonicalName(),
Biome.class.getCanonicalName(),
Enchantment.class.getCanonicalName(),
EntityType.class.getCanonicalName(),
Particle.class.getCanonicalName(),
PotionType.class.getCanonicalName(),
Sound.class.getCanonicalName(),
StatisticType.class.getCanonicalName(),
};
private static final String[] defaults = {
"AIR",
"AIR",
null,
null,
"PIG",
null,
null,
null,
null
};
@Override
public String generate() throws IOException {
StringBuilder contents = new StringBuilder(
"package net.minestom.server.registry;\n\n" +
"import " + HashMap.class.getCanonicalName() + ";\n" +
"import " + NamespaceID.class.getCanonicalName() + ";\n");
for(String type : types) {
contents.append("import ").append(type).append(";\n");
}
contents.append("\n// AUTOGENERATED\npublic class Registries {\n");
if(types.length != defaults.length) {
throw new Error("types.length != defaults.length");
}
// Hashmaps
for (int i = 0; i < types.length; i++) {
String type = types[i];
String simpleType = type.substring(type.lastIndexOf('.') + 1);
contents.append("\t/** Should only be used for internal code, please use the get* methods. */\n");
contents.append("\t@Deprecated\n");
contents.append('\t');
contents.append("public static final HashMap<NamespaceID, ").append(simpleType).append("> ").append(CodeGenerator.decapitalize(simpleType)).append('s');
contents.append(" = new HashMap<>();").append("\n\n");
}
contents.append('\n');
// accessor methods
for (int i = 0; i < types.length; i++) {
String type = types[i];
String simpleType = type.substring(type.lastIndexOf('.') + 1);
String defaultValue = defaults[i];
// Example:
/*
/** Returns 'AIR' if none match
public static Block getBlock(String id) {
return getBlock(NamespaceID.from(id));
}
/** Returns 'AIR' if none match
public static Block getBlock(NamespaceID id) {
return blocks.getOrDefault(id, AIR);
}
*/
StringBuilder comment = new StringBuilder("/** Returns the corresponding ");
comment.append(simpleType).append(" matching the given id. Returns ");
if(defaultValue != null) {
comment.append('\'').append(defaultValue).append('\'');
} else {
comment.append("null");
}
comment.append(" if none match. */");
// String variant
contents.append('\t').append(comment).append("\n");
contents.append('\t');
contents.append("public static ").append(simpleType).append(" get").append(simpleType).append("(String id) {\n");
contents.append("\t\t").append("return get").append(simpleType).append("(NamespaceID.from(id));\n");
contents.append("\t}\n\n");
// NamespaceID variant
contents.append('\t').append(comment).append("\n");
contents.append('\t');
contents.append("public static ").append(simpleType).append(" get").append(simpleType).append("(NamespaceID id) {\n");
contents.append("\t\t").append("return ").append(CodeGenerator.decapitalize(simpleType)).append("s.");
if(defaultValue != null) {
contents.append("getOrDefault(id, ").append(simpleType).append('.').append(defaultValue).append(");");
} else {
contents.append("get(id);");
}
contents.append("\n");
contents.append("\t}\n\n");
}
contents.append("\n}\n");
return contents.toString();
}
public static void main(String[] args) throws IOException {
// copy-pasted from BlockEnumGenerator, to stay consistent in the order of arguments
String targetVersion;
if(args.length < 1) {
System.err.println("Usage: <MC version> [target folder]");
return;
}
targetVersion = args[0];
try {
ResourceGatherer.ensureResourcesArePresent(targetVersion, null); // TODO
} catch (IOException e) {
e.printStackTrace();
}
String targetPart = DEFAULT_TARGET_PATH;
if(args.length >= 2) {
targetPart = args[1];
}
File targetFolder = new File(targetPart);
if(!targetFolder.exists()) {
targetFolder.mkdirs();
}
new RegistriesGenerator().generateTo(targetFolder);
}
private void generateTo(File targetFolder) throws IOException {
String code = generate();
String folder = "net/minestom/server/registry";
File parentFolder = new File(targetFolder, folder);
if(!parentFolder.exists()) {
parentFolder.mkdirs();
}
LOGGER.debug("Writing to file: "+parentFolder+"/Registries.java");
try(Writer writer = new BufferedWriter(new FileWriter(new File(parentFolder, "Registries.java")))) {
writer.write(code);
}
}
}

View File

@ -9,6 +9,7 @@ import net.minestom.codegen.EnumGenerator;
import net.minestom.codegen.MinestomEnumGenerator;
import net.minestom.codegen.PrismarinePaths;
import net.minestom.server.instance.block.BlockAlternative;
import net.minestom.server.registry.Registries;
import net.minestom.server.registry.ResourceGatherer;
import net.minestom.server.utils.NamespaceID;
import org.slf4j.Logger;
@ -240,6 +241,7 @@ public class BlockEnumGenerator extends MinestomEnumGenerator<BlockContainer> {
protected void prepare(EnumGenerator generator) {
String className = getClassName();
generator.addClassAnnotation("@SuppressWarnings({\"deprecation\"})");
generator.addImport(Registries.class.getCanonicalName());
generator.addImport(NamespaceID.class.getCanonicalName());
generator.addImport(List.class.getCanonicalName());
generator.addImport(ArrayList.class.getCanonicalName());
@ -272,6 +274,7 @@ public class BlockEnumGenerator extends MinestomEnumGenerator<BlockContainer> {
generator.appendToConstructor("if(singleState) {");
generator.appendToConstructor("\taddBlockAlternative(new BlockAlternative(defaultID));");
generator.appendToConstructor("}");
generator.appendToConstructor("Registries.blocks.put(NamespaceID.from(namespaceID), this);");
}
@Override

View File

@ -10,6 +10,7 @@ import net.minestom.codegen.PrismarinePaths;
import net.minestom.codegen.blocks.*;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockAlternative;
import net.minestom.server.registry.Registries;
import net.minestom.server.registry.ResourceGatherer;
import net.minestom.server.utils.NamespaceID;
import org.slf4j.Logger;
@ -130,7 +131,12 @@ public class ItemEnumGenerator extends MinestomEnumGenerator<ItemContainer> {
protected void prepare(EnumGenerator generator) {
String className = getClassName();
generator.addImport(Block.class.getCanonicalName());
generator.addImport(Registries.class.getCanonicalName());
generator.addImport(NamespaceID.class.getCanonicalName());
generator.addClassAnnotation("@SuppressWarnings({\"deprecation\"})");
generator.setParams("String namespaceID", "int maxDefaultStackSize", "Block correspondingBlock");
generator.appendToConstructor("Registries.materials.put(NamespaceID.from(namespaceID), this);");
generator.addMethod("getId", "()", "short", "return (short)ordinal();");
generator.addMethod("getName", "()", "String", "return namespaceID;");
generator.addMethod("getMaxDefaultStackSize", "()", "int", "return maxDefaultStackSize;");

View File

@ -6,6 +6,7 @@ import net.minestom.server.gamedata.loottables.LootTableEntryType;
import net.minestom.server.gamedata.loottables.LootTableFunction;
import net.minestom.server.gamedata.loottables.LootTableManager;
import net.minestom.server.item.Material;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID;
import java.util.List;
@ -17,7 +18,6 @@ public class ItemType implements LootTableEntryType {
@Override
public LootTable.Entry create(LootTableManager lootTableManager, String name, List<Condition> conditions, List<LootTable.Entry> children, boolean expand, List<LootTableFunction> functions, int weight, int quality) {
NamespaceID itemID = NamespaceID.from(name);
// TODO: handle non-vanilla IDs ?
return new ItemEntry(this, Material.valueOf(itemID.getPath().toUpperCase()), weight, quality, functions, conditions);
return new ItemEntry(this, Registries.getMaterial(itemID), weight, quality, functions, conditions);
}
}

View File

@ -11,6 +11,7 @@ import net.minestom.server.item.attribute.AttributeSlot;
import net.minestom.server.item.attribute.ItemAttribute;
import net.minestom.server.network.packet.PacketReader;
import net.minestom.server.potion.PotionType;
import net.minestom.server.registry.Registries;
import java.util.ArrayList;
import java.util.UUID;
@ -81,8 +82,7 @@ public class NbtReaderUtils {
if (stringName.equals("Potion")) {
String potionId = reader.readShortSizedString();
potionId = potionId.replace("minecraft:", "").toUpperCase();
PotionType potionType = PotionType.valueOf(potionId);
PotionType potionType = Registries.getPotionType(potionId);
item.addPotionType(potionType);
@ -109,8 +109,7 @@ public class NbtReaderUtils {
String id = reader.readShortSizedString();
// Convert id
id = id.replace("minecraft:", "").toUpperCase();
Enchantment enchantment = Enchantment.valueOf(id);
Enchantment enchantment = Registries.getEnchantment(id);
if (isEnchantment) {
item.setEnchantment(enchantment, lvl);