mirror of https://github.com/Minestom/Minestom.git
79 lines
3.2 KiB
Java
79 lines
3.2 KiB
Java
package net.minestom.codegen.item;
|
|
|
|
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;
|
|
import java.io.File;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
public final class EnchantmentGenerator extends MinestomCodeGenerator {
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(EnchantmentGenerator.class);
|
|
private final InputStream enchantmentsFile;
|
|
private final File outputFolder;
|
|
|
|
public EnchantmentGenerator(@Nullable InputStream enchantmentsFile, @NotNull File outputFolder) {
|
|
this.enchantmentsFile = enchantmentsFile;
|
|
this.outputFolder = outputFolder;
|
|
}
|
|
|
|
@Override
|
|
public void generate() {
|
|
if (enchantmentsFile == null) {
|
|
LOGGER.error("Failed to find enchantments.json.");
|
|
LOGGER.error("Stopped code generation for enchantments.");
|
|
return;
|
|
}
|
|
if (!outputFolder.exists() && !outputFolder.mkdirs()) {
|
|
LOGGER.error("Output folder for code generation does not exist and could not be created.");
|
|
return;
|
|
}
|
|
ClassName enchantmentCN = ClassName.get("net.minestom.server.item", "Enchantment");
|
|
|
|
JsonObject items;
|
|
items = GSON.fromJson(new InputStreamReader(enchantmentsFile), JsonObject.class);
|
|
ClassName materialsCN = ClassName.get("net.minestom.server.item", "EnchantmentConstants");
|
|
// BlockConstants class
|
|
TypeSpec.Builder blockConstantsClass = TypeSpec.interfaceBuilder(materialsCN)
|
|
// Add @SuppressWarnings("unused")
|
|
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unused").build())
|
|
.addJavadoc("AUTOGENERATED by " + getClass().getSimpleName());
|
|
|
|
// Use data
|
|
items.keySet().forEach(namespace -> {
|
|
final String constantName = namespace.replace("minecraft:", "").toUpperCase(Locale.ROOT);
|
|
blockConstantsClass.addField(
|
|
FieldSpec.builder(enchantmentCN, constantName)
|
|
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
|
|
.initializer(
|
|
// Material.STONE = Material.fromNamespaceId("minecraft:stone")
|
|
"$T.fromNamespaceId($S)",
|
|
enchantmentCN,
|
|
namespace
|
|
)
|
|
.build()
|
|
);
|
|
});
|
|
writeFiles(
|
|
List.of(
|
|
JavaFile.builder("net.minestom.server.item", blockConstantsClass.build())
|
|
.indent(" ")
|
|
.skipJavaLangImports(true)
|
|
.build()
|
|
),
|
|
outputFolder
|
|
);
|
|
}
|
|
}
|