mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-05 23:11:36 +01:00
Start port codegen over to Javapoet
This commit is contained in:
parent
aac18a19f9
commit
31114415cf
@ -163,6 +163,9 @@ dependencies {
|
||||
lwjglRuntimeOnly "org.lwjgl:lwjgl-opengl::$lwjglNatives"
|
||||
lwjglRuntimeOnly "org.lwjgl:lwjgl-opengles::$lwjglNatives"
|
||||
lwjglRuntimeOnly "org.lwjgl:lwjgl-glfw::$lwjglNatives"
|
||||
|
||||
|
||||
generatorsImplementation("com.squareup:javapoet:1.13.0")
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 08dfbb32bdb0c0f6ee2c5229f92fef3132668405
|
||||
Subproject commit 472b3c041c58bd0a4397fb9144b1035682022cbe
|
@ -1,5 +1,7 @@
|
||||
// AUTOGENERATED by net.minestom.codegen.RegistriesGenerator
|
||||
package net.minestom.server.registry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.fluids.Fluid;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
@ -11,11 +13,13 @@ import net.minestom.server.potion.PotionType;
|
||||
import net.minestom.server.sound.Sound;
|
||||
import net.minestom.server.stat.StatisticType;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
// AUTOGENERATED
|
||||
public class Registries {
|
||||
/**
|
||||
* AUTOGENERATED
|
||||
*/
|
||||
public final class Registries {
|
||||
/**
|
||||
* Should only be used for internal code, please use the get* methods.
|
||||
*/
|
||||
@ -76,10 +80,10 @@ public class Registries {
|
||||
@Deprecated
|
||||
public static final HashMap<NamespaceID, Fluid> fluids = new HashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* Returns the corresponding Block matching the given id. Returns 'AIR' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static Block getBlock(String id) {
|
||||
return getBlock(NamespaceID.from(id));
|
||||
}
|
||||
@ -87,6 +91,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Block matching the given id. Returns 'AIR' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static Block getBlock(NamespaceID id) {
|
||||
return blocks.getOrDefault(id, Block.AIR);
|
||||
}
|
||||
@ -94,6 +99,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Material matching the given id. Returns 'AIR' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static Material getMaterial(String id) {
|
||||
return getMaterial(NamespaceID.from(id));
|
||||
}
|
||||
@ -101,6 +107,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Material matching the given id. Returns 'AIR' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static Material getMaterial(NamespaceID id) {
|
||||
return materials.getOrDefault(id, Material.AIR);
|
||||
}
|
||||
@ -108,6 +115,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Enchantment matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static Enchantment getEnchantment(String id) {
|
||||
return getEnchantment(NamespaceID.from(id));
|
||||
}
|
||||
@ -115,6 +123,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Enchantment matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static Enchantment getEnchantment(NamespaceID id) {
|
||||
return enchantments.get(id);
|
||||
}
|
||||
@ -122,6 +131,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding EntityType matching the given id. Returns 'PIG' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static EntityType getEntityType(String id) {
|
||||
return getEntityType(NamespaceID.from(id));
|
||||
}
|
||||
@ -129,6 +139,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding EntityType matching the given id. Returns 'PIG' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static EntityType getEntityType(NamespaceID id) {
|
||||
return entityTypes.getOrDefault(id, EntityType.PIG);
|
||||
}
|
||||
@ -136,6 +147,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Particle matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static Particle getParticle(String id) {
|
||||
return getParticle(NamespaceID.from(id));
|
||||
}
|
||||
@ -143,6 +155,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Particle matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static Particle getParticle(NamespaceID id) {
|
||||
return particles.get(id);
|
||||
}
|
||||
@ -150,6 +163,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding PotionType matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static PotionType getPotionType(String id) {
|
||||
return getPotionType(NamespaceID.from(id));
|
||||
}
|
||||
@ -157,6 +171,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding PotionType matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static PotionType getPotionType(NamespaceID id) {
|
||||
return potionTypes.get(id);
|
||||
}
|
||||
@ -164,6 +179,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding PotionEffect matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static PotionEffect getPotionEffect(String id) {
|
||||
return getPotionEffect(NamespaceID.from(id));
|
||||
}
|
||||
@ -171,6 +187,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding PotionEffect matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static PotionEffect getPotionEffect(NamespaceID id) {
|
||||
return potionEffects.get(id);
|
||||
}
|
||||
@ -178,6 +195,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Sound matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static Sound getSound(String id) {
|
||||
return getSound(NamespaceID.from(id));
|
||||
}
|
||||
@ -185,6 +203,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Sound matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static Sound getSound(NamespaceID id) {
|
||||
return sounds.get(id);
|
||||
}
|
||||
@ -192,6 +211,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding StatisticType matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static StatisticType getStatisticType(String id) {
|
||||
return getStatisticType(NamespaceID.from(id));
|
||||
}
|
||||
@ -199,6 +219,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding StatisticType matching the given id. Returns null if none match.
|
||||
*/
|
||||
@Nullable
|
||||
public static StatisticType getStatisticType(NamespaceID id) {
|
||||
return statisticTypes.get(id);
|
||||
}
|
||||
@ -206,6 +227,7 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Fluid matching the given id. Returns 'EMPTY' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static Fluid getFluid(String id) {
|
||||
return getFluid(NamespaceID.from(id));
|
||||
}
|
||||
@ -213,9 +235,8 @@ public class Registries {
|
||||
/**
|
||||
* Returns the corresponding Fluid matching the given id. Returns 'EMPTY' if none match.
|
||||
*/
|
||||
@NotNull
|
||||
public static Fluid getFluid(NamespaceID id) {
|
||||
return fluids.getOrDefault(id, Fluid.EMPTY);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.minestom.codegen;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.fluids.Fluid;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
@ -12,10 +13,15 @@ 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.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import java.io.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static net.minestom.codegen.MinestomEnumGenerator.DEFAULT_TARGET_PATH;
|
||||
@ -27,67 +33,47 @@ 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(),
|
||||
Enchantment.class.getCanonicalName(),
|
||||
EntityType.class.getCanonicalName(),
|
||||
Particle.class.getCanonicalName(),
|
||||
PotionType.class.getCanonicalName(),
|
||||
PotionEffect.class.getCanonicalName(),
|
||||
Sound.class.getCanonicalName(),
|
||||
StatisticType.class.getCanonicalName(),
|
||||
Fluid.class.getCanonicalName(),
|
||||
};
|
||||
private static final String[] defaults = {
|
||||
"AIR",
|
||||
"AIR",
|
||||
null,
|
||||
"PIG",
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
"EMPTY"
|
||||
private static final ImmutablePair<String, String>[] registries = new ImmutablePair[]{
|
||||
new ImmutablePair<>(Block.class.getCanonicalName(), "AIR"),
|
||||
new ImmutablePair<>(Material.class.getCanonicalName(), "AIR"),
|
||||
new ImmutablePair<>(Enchantment.class.getCanonicalName(), null),
|
||||
new ImmutablePair<>(EntityType.class.getCanonicalName(), "PIG"),
|
||||
new ImmutablePair<>(Particle.class.getCanonicalName(), null),
|
||||
new ImmutablePair<>(PotionType.class.getCanonicalName(), null),
|
||||
new ImmutablePair<>(PotionEffect.class.getCanonicalName(), null),
|
||||
new ImmutablePair<>(Sound.class.getCanonicalName(), null),
|
||||
new ImmutablePair<>(StatisticType.class.getCanonicalName(), null),
|
||||
new ImmutablePair<>(Fluid.class.getCanonicalName(), "EMPTY"),
|
||||
};
|
||||
|
||||
@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");
|
||||
TypeSpec.Builder registriesClass = TypeSpec.classBuilder(ClassName.get("net.minestom.server.registry", "Registries"))
|
||||
.addModifiers(Modifier.FINAL, Modifier.PUBLIC)
|
||||
.addJavadoc("AUTOGENERATED");
|
||||
|
||||
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");
|
||||
}
|
||||
FieldSpec[] fields = new FieldSpec[registries.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");
|
||||
for (int i = 0; i < registries.length; i++) {
|
||||
ClassName type = ClassName.bestGuess(registries[i].left);
|
||||
String simpleType = type.simpleName();
|
||||
|
||||
FieldSpec field = FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(HashMap.class), ClassName.get(NamespaceID.class), type), CodeGenerator.decapitalize(simpleType)+"s")
|
||||
.addModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.PUBLIC)
|
||||
.addJavadoc("Should only be used for internal code, please use the get* methods.")
|
||||
.addAnnotation(Deprecated.class)
|
||||
.initializer("new HashMap<>()")
|
||||
.build();
|
||||
fields[i] = field;
|
||||
registriesClass.addField(field);
|
||||
}
|
||||
|
||||
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];
|
||||
for (int i = 0; i < registries.length; i++) {
|
||||
ClassName type = ClassName.bestGuess(registries[i].left);
|
||||
String simpleType = type.simpleName();
|
||||
String defaultValue = registries[i].right;
|
||||
|
||||
// Example:
|
||||
/*
|
||||
@ -101,43 +87,60 @@ public class RegistriesGenerator implements CodeGenerator {
|
||||
return blocks.getOrDefault(id, AIR);
|
||||
}
|
||||
*/
|
||||
StringBuilder comment = new StringBuilder("/** Returns the corresponding ");
|
||||
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. */");
|
||||
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");
|
||||
ParameterSpec namespaceIDParam = ParameterSpec.builder(ClassName.get(NamespaceID.class), "id")
|
||||
.build();
|
||||
|
||||
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(");");
|
||||
CodeBlock.Builder code = CodeBlock.builder();
|
||||
Class<? extends Annotation> annotation;
|
||||
if(defaultValue != null) {
|
||||
annotation = NotNull.class;
|
||||
code.addStatement("return $N.getOrDefault($N, $T.$N)", fields[i], namespaceIDParam, type, defaultValue);
|
||||
} else {
|
||||
contents.append("get(id);");
|
||||
annotation = Nullable.class;
|
||||
code.addStatement("return $N.get($N)", fields[i], namespaceIDParam);
|
||||
}
|
||||
|
||||
contents.append("\n");
|
||||
// string variant
|
||||
ParameterSpec idParam = ParameterSpec.builder(ClassName.get(String.class), "id")
|
||||
.build();
|
||||
MethodSpec idMethod = MethodSpec.methodBuilder("get"+simpleType)
|
||||
.returns(type)
|
||||
.addAnnotation(annotation)
|
||||
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
||||
.addParameter(idParam)
|
||||
.addStatement("return get$N(NamespaceID.from($N))", simpleType, idParam)
|
||||
.addJavadoc(comment.toString())
|
||||
.build();
|
||||
registriesClass.addMethod(idMethod);
|
||||
|
||||
contents.append("\t}\n\n");
|
||||
// NamespaceID variant
|
||||
registriesClass.addMethod(MethodSpec.methodBuilder("get"+simpleType)
|
||||
.returns(type)
|
||||
.addAnnotation(annotation)
|
||||
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
||||
.addParameter(namespaceIDParam)
|
||||
.addCode(code.build())
|
||||
.addJavadoc(comment.toString())
|
||||
.build());
|
||||
}
|
||||
|
||||
contents.append("\n}\n");
|
||||
|
||||
return contents.toString();
|
||||
JavaFile file = JavaFile.builder("net.minestom.server.registry", registriesClass.build())
|
||||
.addFileComment("AUTOGENERATED by "+getClass().getCanonicalName())
|
||||
.indent(" ")
|
||||
.skipJavaLangImports(true)
|
||||
.build();
|
||||
StringWriter strWriter = new StringWriter();
|
||||
file.writeTo(strWriter);
|
||||
return strWriter.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
Loading…
Reference in New Issue
Block a user