|
3 | 3 | import com.google.gson.Gson;
|
4 | 4 | import com.google.gson.GsonBuilder;
|
5 | 5 | import com.google.gson.JsonObject;
|
6 |
| -import com.squareup.javapoet.AnnotationSpec; |
7 |
| -import com.squareup.javapoet.ClassName; |
8 |
| -import com.squareup.javapoet.FieldSpec; |
9 |
| -import com.squareup.javapoet.JavaFile; |
10 |
| -import com.squareup.javapoet.TypeSpec; |
11 |
| -import org.jetbrains.annotations.ApiStatus; |
| 6 | +import com.squareup.javapoet.*; |
12 | 7 | import org.jetbrains.annotations.NotNull;
|
13 | 8 | import org.slf4j.Logger;
|
14 | 9 | import org.slf4j.LoggerFactory;
|
15 | 10 |
|
| 11 | +import javax.lang.model.SourceVersion; |
16 | 12 | import javax.lang.model.element.Modifier;
|
17 | 13 | import java.io.File;
|
| 14 | +import java.io.IOException; |
18 | 15 | import java.io.InputStream;
|
19 | 16 | import java.io.InputStreamReader;
|
20 |
| -import java.util.HashMap; |
21 |
| -import java.util.Map; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Locale; |
22 | 19 |
|
23 |
| -import static net.minestom.codegen.MinestomCodeGenerator.DEFAULT_INDENT; |
24 |
| -import static net.minestom.codegen.MinestomCodeGenerator.extractNamespaces; |
25 |
| - |
26 |
| -@ApiStatus.Internal |
27 |
| -public class CodeGenerator implements CodeExporter { |
| 20 | +public class CodeGenerator { |
28 | 21 | protected static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
|
29 | 22 | private static final Logger LOGGER = LoggerFactory.getLogger(CodeGenerator.class);
|
| 23 | + |
30 | 24 | private final File outputFolder;
|
31 | 25 |
|
32 |
| - public CodeGenerator(@NotNull File outputFolder) { |
| 26 | + public CodeGenerator(File outputFolder) { |
33 | 27 | this.outputFolder = outputFolder;
|
34 | 28 | }
|
35 | 29 |
|
36 | 30 | public void generate(InputStream resourceFile, String packageName, String typeName, String loaderName, String generatedName) {
|
37 | 31 | if (resourceFile == null) {
|
38 |
| - LOGGER.error("Failed to find resource file for {}", typeName); |
| 32 | + LOGGER.error("Failed to find resource file for " + typeName); |
39 | 33 | return;
|
40 | 34 | }
|
41 | 35 | ClassName typeClass = ClassName.get(packageName, typeName);
|
42 | 36 | ClassName loaderClass = ClassName.get(packageName, loaderName);
|
43 | 37 |
|
44 |
| - JsonObject json = GSON.fromJson(new InputStreamReader(resourceFile), JsonObject.class); |
| 38 | + JsonObject json; |
| 39 | + json = GSON.fromJson(new InputStreamReader(resourceFile), JsonObject.class); |
45 | 40 | ClassName materialsCN = ClassName.get(packageName, generatedName);
|
46 | 41 | // BlockConstants class
|
47 | 42 | TypeSpec.Builder blockConstantsClass = TypeSpec.interfaceBuilder(materialsCN)
|
48 | 43 | // Add @SuppressWarnings("unused")
|
49 | 44 | .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unused").build())
|
50 | 45 | .addJavadoc("Code autogenerated, do not edit!");
|
51 | 46 |
|
52 |
| - Map<String, String> replacementOptions = new HashMap<>(); |
53 |
| - replacementOptions.put("minecraft:", ""); |
54 |
| - replacementOptions.put(".", "_"); |
55 | 47 | // Use data
|
56 | 48 | json.keySet().forEach(namespace -> {
|
57 |
| - final String constantName = extractNamespaces(namespace, replacementOptions); |
| 49 | + final String constantName = namespace |
| 50 | + .replace("minecraft:", "") |
| 51 | + .replace(".", "_") |
| 52 | + .toUpperCase(Locale.ROOT); |
58 | 53 | blockConstantsClass.addField(
|
59 | 54 | FieldSpec.builder(typeClass, constantName)
|
60 | 55 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
|
61 | 56 | .initializer(
|
| 57 | + // TypeClass.STONE = MaterialLoader.fromNamespaceId("minecraft:stone") |
62 | 58 | "$T.get($S)",
|
63 | 59 | loaderClass,
|
64 | 60 | namespace
|
65 | 61 | )
|
66 | 62 | .build()
|
67 | 63 | );
|
68 | 64 | });
|
69 |
| - writeFile( |
70 |
| - JavaFile.builder(packageName, blockConstantsClass.build()) |
71 |
| - .indent(DEFAULT_INDENT) |
| 65 | + writeFiles( |
| 66 | + List.of(JavaFile.builder(packageName, blockConstantsClass.build()) |
| 67 | + .indent(" ") |
| 68 | + .skipJavaLangImports(true) |
| 69 | + .build()), |
| 70 | + outputFolder); |
| 71 | + } |
| 72 | + |
| 73 | + public void generateKeys(InputStream resourceFile, String packageName, String typeName, String generatedName) { |
| 74 | + if (resourceFile == null) { |
| 75 | + LOGGER.error("Failed to find resource file for " + typeName); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + ClassName typeClass = ClassName.bestGuess(packageName + "." + typeName); // Use bestGuess to handle nested class |
| 80 | + ClassName registryKeyClass = ClassName.get("net.minestom.server.registry", "DynamicRegistry", "Key"); |
| 81 | + ParameterizedTypeName typedRegistryKeyClass = ParameterizedTypeName.get(registryKeyClass, typeClass); |
| 82 | + |
| 83 | + JsonObject json; |
| 84 | + json = GSON.fromJson(new InputStreamReader(resourceFile), JsonObject.class); |
| 85 | + ClassName materialsCN = ClassName.get(packageName, generatedName); |
| 86 | + // BlockConstants class |
| 87 | + TypeSpec.Builder blockConstantsClass = TypeSpec.interfaceBuilder(materialsCN) |
| 88 | + // Add @SuppressWarnings("unused") |
| 89 | + .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unused").build()) |
| 90 | + .addJavadoc("Code autogenerated, do not edit!"); |
| 91 | + |
| 92 | + // Use data |
| 93 | + json.keySet().forEach(namespace -> { |
| 94 | + String constantName = namespace |
| 95 | + .replace("minecraft:", "") |
| 96 | + .replace(".", "_") |
| 97 | + .toUpperCase(Locale.ROOT); |
| 98 | + if (!SourceVersion.isName(constantName)) { |
| 99 | + constantName = "_" + constantName; |
| 100 | + } |
| 101 | + blockConstantsClass.addField( |
| 102 | + FieldSpec.builder(typedRegistryKeyClass, constantName) |
| 103 | + .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) |
| 104 | + .initializer( |
| 105 | + // TypeClass.STONE = NamespaceID.from("minecraft:stone") |
| 106 | + "$T.of($S)", |
| 107 | + registryKeyClass, |
| 108 | + namespace |
| 109 | + ) |
| 110 | + .build() |
| 111 | + ); |
| 112 | + }); |
| 113 | + writeFiles( |
| 114 | + List.of(JavaFile.builder(packageName, blockConstantsClass.build()) |
| 115 | + .indent(" ") |
72 | 116 | .skipJavaLangImports(true)
|
73 |
| - .build(), |
74 |
| - outputFolder |
75 |
| - ); |
| 117 | + .build()), |
| 118 | + outputFolder); |
| 119 | + } |
| 120 | + |
| 121 | + private void writeFiles(@NotNull List<JavaFile> fileList, File outputFolder) { |
| 122 | + for (JavaFile javaFile : fileList) { |
| 123 | + try { |
| 124 | + javaFile.writeTo(outputFolder); |
| 125 | + } catch (IOException e) { |
| 126 | + LOGGER.error("An error occured while writing source code to the file system.", e); |
| 127 | + } |
| 128 | + } |
76 | 129 | }
|
77 | 130 | }
|
0 commit comments