2020-06-30 12:53:00 +02:00
|
|
|
package net.minestom.codegen;
|
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
import com.squareup.javapoet.*;
|
2020-06-30 12:53:00 +02:00
|
|
|
import net.minestom.server.entity.EntityType;
|
2020-06-30 20:38:42 +02:00
|
|
|
import net.minestom.server.fluids.Fluid;
|
2020-06-30 12:53:00 +02:00
|
|
|
import net.minestom.server.instance.block.Block;
|
|
|
|
import net.minestom.server.item.Enchantment;
|
|
|
|
import net.minestom.server.item.Material;
|
2021-02-23 17:29:21 +01:00
|
|
|
import net.minestom.server.particle.Particle;
|
2020-10-15 05:29:48 +02:00
|
|
|
import net.minestom.server.potion.PotionEffect;
|
2020-06-30 12:53:00 +02:00
|
|
|
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;
|
2020-10-25 17:54:10 +01:00
|
|
|
import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2020-06-30 12:53:00 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
import javax.lang.model.element.Modifier;
|
2021-02-23 17:29:21 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-10-25 17:54:10 +01:00
|
|
|
import java.lang.annotation.Annotation;
|
2020-10-26 15:50:52 +01:00
|
|
|
import java.util.Collections;
|
2020-06-30 12:53:00 +02:00
|
|
|
import java.util.HashMap;
|
2020-10-26 15:50:52 +01:00
|
|
|
import java.util.List;
|
2020-06-30 12:53:00 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
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),
|
2021-02-23 17:29:21 +01:00
|
|
|
new ImmutablePair<>(EntityType.class.getCanonicalName(), null),
|
|
|
|
new ImmutablePair<>(Particle.class.getCanonicalName(), null),
|
2020-10-25 17:54:10 +01:00
|
|
|
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"),
|
2020-06-30 12:53:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
2020-10-26 15:50:52 +01:00
|
|
|
public List<JavaFile> generate() throws IOException {
|
2020-10-25 17:54:10 +01:00
|
|
|
TypeSpec.Builder registriesClass = TypeSpec.classBuilder(ClassName.get("net.minestom.server.registry", "Registries"))
|
|
|
|
.addModifiers(Modifier.FINAL, Modifier.PUBLIC)
|
|
|
|
.addJavadoc("AUTOGENERATED");
|
2020-06-30 12:53:00 +02:00
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
FieldSpec[] fields = new FieldSpec[registries.length];
|
2020-06-30 12:53:00 +02:00
|
|
|
|
|
|
|
// Hashmaps
|
2020-10-25 17:54:10 +01:00
|
|
|
for (int i = 0; i < registries.length; i++) {
|
|
|
|
ClassName type = ClassName.bestGuess(registries[i].left);
|
|
|
|
String simpleType = type.simpleName();
|
|
|
|
|
2021-02-23 17:29:21 +01:00
|
|
|
FieldSpec field = FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(HashMap.class), ClassName.get(NamespaceID.class), type), CodeGenerator.decapitalize(simpleType) + "s")
|
2020-10-25 17:54:10 +01:00
|
|
|
.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);
|
2020-06-30 12:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// accessor methods
|
2020-10-25 17:54:10 +01:00
|
|
|
for (int i = 0; i < registries.length; i++) {
|
|
|
|
ClassName type = ClassName.bestGuess(registries[i].left);
|
|
|
|
String simpleType = type.simpleName();
|
|
|
|
String defaultValue = registries[i].right;
|
2020-06-30 12:53:00 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
*/
|
2020-10-25 17:54:10 +01:00
|
|
|
StringBuilder comment = new StringBuilder("Returns the corresponding ");
|
2020-06-30 12:53:00 +02:00
|
|
|
comment.append(simpleType).append(" matching the given id. Returns ");
|
2020-10-15 05:29:48 +02:00
|
|
|
if (defaultValue != null) {
|
2020-06-30 12:53:00 +02:00
|
|
|
comment.append('\'').append(defaultValue).append('\'');
|
|
|
|
} else {
|
|
|
|
comment.append("null");
|
|
|
|
}
|
2020-10-25 17:54:10 +01:00
|
|
|
comment.append(" if none match.");
|
2020-06-30 12:53:00 +02:00
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
ParameterSpec namespaceIDParam = ParameterSpec.builder(ClassName.get(NamespaceID.class), "id")
|
|
|
|
.build();
|
2020-06-30 12:53:00 +02:00
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
CodeBlock.Builder code = CodeBlock.builder();
|
|
|
|
Class<? extends Annotation> annotation;
|
2021-02-23 17:29:21 +01:00
|
|
|
if (defaultValue != null) {
|
2020-10-25 17:54:10 +01:00
|
|
|
annotation = NotNull.class;
|
|
|
|
code.addStatement("return $N.getOrDefault($N, $T.$N)", fields[i], namespaceIDParam, type, defaultValue);
|
2020-06-30 12:53:00 +02:00
|
|
|
} else {
|
2020-10-25 17:54:10 +01:00
|
|
|
annotation = Nullable.class;
|
|
|
|
code.addStatement("return $N.get($N)", fields[i], namespaceIDParam);
|
2020-06-30 12:53:00 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
// string variant
|
|
|
|
ParameterSpec idParam = ParameterSpec.builder(ClassName.get(String.class), "id")
|
|
|
|
.build();
|
2021-02-23 17:29:21 +01:00
|
|
|
MethodSpec idMethod = MethodSpec.methodBuilder("get" + simpleType)
|
2020-10-25 17:54:10 +01:00
|
|
|
.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);
|
2020-06-30 12:53:00 +02:00
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
// NamespaceID variant
|
2021-02-23 17:29:21 +01:00
|
|
|
registriesClass.addMethod(MethodSpec.methodBuilder("get" + simpleType)
|
2020-10-25 17:54:10 +01:00
|
|
|
.returns(type)
|
|
|
|
.addAnnotation(annotation)
|
|
|
|
.addModifiers(Modifier.STATIC, Modifier.PUBLIC)
|
|
|
|
.addParameter(namespaceIDParam)
|
|
|
|
.addCode(code.build())
|
|
|
|
.addJavadoc(comment.toString())
|
|
|
|
.build());
|
2020-06-30 12:53:00 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:54:10 +01:00
|
|
|
JavaFile file = JavaFile.builder("net.minestom.server.registry", registriesClass.build())
|
2021-02-23 17:29:21 +01:00
|
|
|
.addFileComment("AUTOGENERATED by " + getClass().getCanonicalName())
|
2020-10-25 17:54:10 +01:00
|
|
|
.indent(" ")
|
|
|
|
.skipJavaLangImports(true)
|
|
|
|
.build();
|
2020-10-26 15:50:52 +01:00
|
|
|
return Collections.singletonList(file);
|
2020-06-30 12:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
// copy-pasted from BlockEnumGenerator, to stay consistent in the order of arguments
|
|
|
|
String targetVersion;
|
2020-10-15 05:29:48 +02:00
|
|
|
if (args.length < 1) {
|
2020-06-30 12:53:00 +02:00
|
|
|
System.err.println("Usage: <MC version> [target folder]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
targetVersion = args[0];
|
|
|
|
|
|
|
|
try {
|
2020-09-25 18:39:43 +02:00
|
|
|
ResourceGatherer.ensureResourcesArePresent(targetVersion); // TODO
|
2020-06-30 12:53:00 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
String targetPart = DEFAULT_TARGET_PATH;
|
2020-10-15 05:29:48 +02:00
|
|
|
if (args.length >= 2) {
|
2020-06-30 12:53:00 +02:00
|
|
|
targetPart = args[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
File targetFolder = new File(targetPart);
|
2020-10-15 05:29:48 +02:00
|
|
|
if (!targetFolder.exists()) {
|
2020-06-30 12:53:00 +02:00
|
|
|
targetFolder.mkdirs();
|
|
|
|
}
|
|
|
|
|
|
|
|
new RegistriesGenerator().generateTo(targetFolder);
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:50:52 +01:00
|
|
|
@Override
|
|
|
|
public Logger getLogger() {
|
|
|
|
return LOGGER;
|
2020-06-30 12:53:00 +02:00
|
|
|
}
|
|
|
|
}
|