2021-06-03 12:11:42 +02:00
|
|
|
package net.minestom.codegen.potion;
|
|
|
|
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.squareup.javapoet.*;
|
|
|
|
import net.minestom.codegen.MinestomCodeGenerator;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import javax.lang.model.element.Modifier;
|
2021-06-11 17:47:26 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2021-06-03 12:11:42 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
public final class PotionTypeGenerator extends MinestomCodeGenerator {
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(PotionTypeGenerator.class);
|
2021-06-06 14:22:32 +02:00
|
|
|
private final InputStream potionsFile;
|
2021-06-03 12:11:42 +02:00
|
|
|
private final File outputFolder;
|
|
|
|
|
2021-06-06 14:22:32 +02:00
|
|
|
public PotionTypeGenerator(@Nullable InputStream potionsFile, @NotNull File outputFolder) {
|
2021-06-03 12:11:42 +02:00
|
|
|
this.potionsFile = potionsFile;
|
|
|
|
this.outputFolder = outputFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generate() {
|
2021-06-06 14:22:32 +02:00
|
|
|
if (potionsFile == null) {
|
2021-06-03 12:11:42 +02:00
|
|
|
LOGGER.error("Failed to find potions.json.");
|
|
|
|
LOGGER.error("Stopped code generation for potions.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!outputFolder.exists() && !outputFolder.mkdirs()) {
|
|
|
|
LOGGER.error("Output folder for code generation does not exist and could not be created.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Important classes we use alot
|
|
|
|
ClassName namespaceIDClassName = ClassName.get("net.minestom.server.utils", "NamespaceID");
|
|
|
|
ClassName registriesClassName = ClassName.get("net.minestom.server.registry", "Registries");
|
|
|
|
|
2021-06-11 17:47:26 +02:00
|
|
|
JsonArray potions = GSON.fromJson(new InputStreamReader(potionsFile), JsonArray.class);
|
2021-06-03 12:11:42 +02:00
|
|
|
ClassName potionTypeClassName = ClassName.get("net.minestom.server.potion", "PotionType");
|
|
|
|
|
|
|
|
// Particle
|
|
|
|
TypeSpec.Builder potionTypeClass = TypeSpec.enumBuilder(potionTypeClassName)
|
|
|
|
.addSuperinterface(ClassName.get("net.kyori.adventure.key", "Keyed"))
|
|
|
|
.addModifiers(Modifier.PUBLIC).addJavadoc("AUTOGENERATED by " + getClass().getSimpleName());
|
|
|
|
|
|
|
|
potionTypeClass.addField(
|
|
|
|
FieldSpec.builder(namespaceIDClassName, "id")
|
|
|
|
.addModifiers(Modifier.PRIVATE, Modifier.FINAL).addAnnotation(NotNull.class).build()
|
|
|
|
);
|
|
|
|
// static field
|
|
|
|
potionTypeClass.addField(
|
|
|
|
FieldSpec.builder(ArrayTypeName.of(potionTypeClassName), "VALUES")
|
|
|
|
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
|
|
|
|
.initializer("values()")
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
|
|
|
|
potionTypeClass.addMethod(
|
|
|
|
MethodSpec.constructorBuilder()
|
|
|
|
.addParameter(ParameterSpec.builder(namespaceIDClassName, "id").addAnnotation(NotNull.class).build())
|
|
|
|
.addStatement("this.id = id")
|
|
|
|
.addStatement("$T.potionTypes.put(id, this)", registriesClassName)
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
// Override key method (adventure)
|
|
|
|
potionTypeClass.addMethod(
|
|
|
|
MethodSpec.methodBuilder("key")
|
|
|
|
.returns(ClassName.get("net.kyori.adventure.key", "Key"))
|
|
|
|
.addAnnotation(Override.class)
|
|
|
|
.addAnnotation(NotNull.class)
|
|
|
|
.addStatement("return this.id")
|
|
|
|
.addModifiers(Modifier.PUBLIC)
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
// getId method
|
|
|
|
potionTypeClass.addMethod(
|
|
|
|
MethodSpec.methodBuilder("getId")
|
|
|
|
.returns(TypeName.SHORT)
|
|
|
|
.addStatement("return (short) ordinal()")
|
|
|
|
.addModifiers(Modifier.PUBLIC)
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
// getNamespaceID method
|
|
|
|
potionTypeClass.addMethod(
|
|
|
|
MethodSpec.methodBuilder("getNamespaceID")
|
|
|
|
.returns(namespaceIDClassName)
|
|
|
|
.addAnnotation(NotNull.class)
|
|
|
|
.addStatement("return this.id")
|
|
|
|
.addModifiers(Modifier.PUBLIC)
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
// fromId Method
|
|
|
|
potionTypeClass.addMethod(
|
|
|
|
MethodSpec.methodBuilder("fromId")
|
|
|
|
.returns(potionTypeClassName)
|
|
|
|
.addAnnotation(Nullable.class)
|
|
|
|
.addParameter(TypeName.SHORT, "id")
|
|
|
|
.beginControlFlow("if(id >= 0 && id < VALUES.length)")
|
|
|
|
.addStatement("return VALUES[id]")
|
|
|
|
.endControlFlow()
|
|
|
|
.addStatement("return null")
|
|
|
|
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
// toString method
|
|
|
|
potionTypeClass.addMethod(
|
|
|
|
MethodSpec.methodBuilder("toString")
|
|
|
|
.addAnnotation(NotNull.class)
|
|
|
|
.addAnnotation(Override.class)
|
|
|
|
.returns(String.class)
|
|
|
|
// this resolves to [Namespace]
|
|
|
|
.addStatement("return \"[\" + this.id + \"]\"")
|
|
|
|
.addModifiers(Modifier.PUBLIC)
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Use data
|
|
|
|
for (JsonElement p : potions) {
|
|
|
|
JsonObject potion = p.getAsJsonObject();
|
|
|
|
|
|
|
|
String potionName = potion.get("name").getAsString();
|
|
|
|
|
|
|
|
potionTypeClass.addEnumConstant(potionName, TypeSpec.anonymousClassBuilder(
|
|
|
|
"$T.from($S)",
|
|
|
|
namespaceIDClassName,
|
|
|
|
potion.get("id").getAsString()
|
|
|
|
).build()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write files to outputFolder
|
|
|
|
writeFiles(
|
|
|
|
Collections.singletonList(
|
|
|
|
JavaFile.builder("net.minestom.server.potion", potionTypeClass.build())
|
|
|
|
.indent(" ")
|
|
|
|
.skipJavaLangImports(true)
|
|
|
|
.build()
|
|
|
|
),
|
|
|
|
outputFolder
|
|
|
|
);
|
|
|
|
}
|
2021-06-06 04:48:43 +02:00
|
|
|
}
|