Automatically generate dye colors. (#1051)

This commit is contained in:
Articdive 2022-05-10 20:03:23 +02:00 committed by GitHub
parent e4c1c9fe8d
commit e4de03ab1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 243 additions and 70 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.codegen;
import net.minestom.codegen.color.DyeColorGenerator;
import net.minestom.codegen.fluid.FluidGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,6 +17,12 @@ public class Generators {
return;
}
File outputFolder = new File(args[0]);
// Generate DyeColors
new DyeColorGenerator(resource("dye_colors.json"), outputFolder).generate();
var generator = new CodeGenerator(outputFolder);
generator.generate(resource("blocks.json"), "net.minestom.server.instance.block", "Block", "BlockImpl", "Blocks");
generator.generate(resource("items.json"), "net.minestom.server.item", "Material", "MaterialImpl", "Materials");
@ -29,6 +36,7 @@ public class Generators {
// Generate fluids
new FluidGenerator(resource("fluids.json"), outputFolder).generate();
// TODO: Generate attributes
// new AttributeGenerator(
// new File(inputFolder, targetVersion + "_attributes.json"),

View File

@ -0,0 +1,146 @@
package net.minestom.codegen.color;
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.Comparator;
import java.util.List;
import java.util.stream.StreamSupport;
public class DyeColorGenerator extends MinestomCodeGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(DyeColorGenerator.class);
private final InputStream dyeColorsFile;
private final File outputFolder;
public DyeColorGenerator(@Nullable InputStream dyeColorsFile, @NotNull File outputFolder) {
this.dyeColorsFile = dyeColorsFile;
this.outputFolder = outputFolder;
}
@Override
public void generate() {
if (dyeColorsFile == null) {
LOGGER.error("Failed to find dye_colors.json.");
LOGGER.error("Stopped code generation for dye colors.");
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 colorCN = ClassName.get("net.minestom.server.color", "Color");
JsonArray dyeColors = GSON.fromJson(new InputStreamReader(dyeColorsFile), JsonArray.class);
ClassName dyeColorCN = ClassName.get("net.minestom.server.color", "DyeColor");
// Dye Color Enum
TypeSpec.Builder dyeColorEnum = TypeSpec.enumBuilder(dyeColorCN)
.addSuperinterface(ClassName.get("net.kyori.adventure.util", "RGBLike"))
.addModifiers(Modifier.PUBLIC).addJavadoc("AUTOGENERATED by " + getClass().getSimpleName());
// Fields
dyeColorEnum.addFields(
List.of(
FieldSpec.builder(colorCN, "textureDiffuseColor", Modifier.PRIVATE, Modifier.FINAL).build(),
FieldSpec.builder(colorCN, "textColor", Modifier.PRIVATE, Modifier.FINAL).build(),
FieldSpec.builder(colorCN, "fireworkColor", Modifier.PRIVATE, Modifier.FINAL).build(),
FieldSpec.builder(TypeName.INT, "mapColorId", Modifier.PRIVATE, Modifier.FINAL).build()
)
);
// Methods
dyeColorEnum.addMethods(
List.of(
// Constructor
MethodSpec.constructorBuilder()
.addParameters(
List.of(
ParameterSpec.builder(colorCN, "textureDiffuseColor").addAnnotation(NotNull.class).build(),
ParameterSpec.builder(colorCN, "textColor").addAnnotation(NotNull.class).build(),
ParameterSpec.builder(colorCN, "fireworkColor").addAnnotation(NotNull.class).build(),
ParameterSpec.builder(TypeName.INT, "mapColorId").build()
)
)
.addStatement("this.textureDiffuseColor = textureDiffuseColor")
.addStatement("this.textColor = textColor")
.addStatement("this.fireworkColor = fireworkColor")
.addStatement("this.mapColorId = mapColorId")
.build(),
MethodSpec.methodBuilder("color")
.addModifiers(Modifier.PUBLIC)
.returns(colorCN.annotated(AnnotationSpec.builder(NotNull.class).build()))
.addStatement("return this.textureDiffuseColor")
.build(),
MethodSpec.methodBuilder("textColor")
.addModifiers(Modifier.PUBLIC)
.returns(colorCN.annotated(AnnotationSpec.builder(NotNull.class).build()))
.addStatement("return this.textColor")
.build(),
MethodSpec.methodBuilder("fireworkColor")
.addModifiers(Modifier.PUBLIC)
.returns(colorCN.annotated(AnnotationSpec.builder(NotNull.class).build()))
.addStatement("return this.fireworkColor")
.build(),
MethodSpec.methodBuilder("red")
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.INT)
.addAnnotation(Override.class)
.addStatement("return this.textureDiffuseColor.red()")
.build(),
MethodSpec.methodBuilder("green")
.returns(TypeName.INT)
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addStatement("return this.textureDiffuseColor.green()")
.build(),
MethodSpec.methodBuilder("blue")
.returns(TypeName.INT)
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addStatement("return this.textureDiffuseColor.blue()")
.build(),
MethodSpec.methodBuilder("mapColorId")
.addModifiers(Modifier.PUBLIC)
.returns(TypeName.INT)
.addStatement("return this.mapColorId")
.build()
)
);
// Use data
for (JsonObject dyeColorObject : StreamSupport.stream(dyeColors.spliterator(), true).map(JsonElement::getAsJsonObject).sorted(Comparator.comparingInt(o -> o.get("id").getAsInt())).toList()) {
String dyeColorName = dyeColorObject.get("name").getAsString();
dyeColorEnum.addEnumConstant(toConstant(dyeColorName), TypeSpec.anonymousClassBuilder(
"new $T(0x$L), new $T(0x$L), new $T(0x$L), $L",
colorCN, Integer.toString(dyeColorObject.get("textureDiffuseColor").getAsInt(), 16),
colorCN, Integer.toString(dyeColorObject.get("textColor").getAsInt(), 16),
colorCN, Integer.toString(dyeColorObject.get("fireworkColor").getAsInt(), 16),
dyeColorObject.get("mapColorId").getAsInt()
).build()
);
}
// Write files to outputFolder
writeFiles(
Collections.singletonList(
JavaFile.builder("net.minestom.server.color", dyeColorEnum.build())
.indent(" ")
.skipJavaLangImports(true)
.build()
),
outputFolder
);
}
}

View File

@ -7,7 +7,7 @@ adventure = "4.10.1"
kotlin = "1.6.20"
hydrazine = "1.7.2"
dependencyGetter = "v1.0.1"
minestomData = "895581d464"
minestomData = "3e211f3953"
hephaistos = "2.4.4"
jetbrainsAnnotations = "23.0.0"

View File

@ -0,0 +1,88 @@
package net.minestom.server.color;
import net.kyori.adventure.util.RGBLike;
import org.jetbrains.annotations.NotNull;
/**
* AUTOGENERATED by DyeColorGenerator
*/
public enum DyeColor implements RGBLike {
WHITE(new Color(0xf9fffe), new Color(0xffffff), new Color(0xf0f0f0), 8),
ORANGE(new Color(0xf9801d), new Color(0xff681f), new Color(0xeb8844), 15),
MAGENTA(new Color(0xc74ebd), new Color(0xff00ff), new Color(0xc354cd), 16),
LIGHT_BLUE(new Color(0x3ab3da), new Color(0x9ac0cd), new Color(0x6689d3), 17),
YELLOW(new Color(0xfed83d), new Color(0xffff00), new Color(0xdecf2a), 18),
LIME(new Color(0x80c71f), new Color(0xbfff00), new Color(0x41cd34), 19),
PINK(new Color(0xf38baa), new Color(0xff69b4), new Color(0xd88198), 20),
GRAY(new Color(0x474f52), new Color(0x808080), new Color(0x434343), 21),
LIGHT_GRAY(new Color(0x9d9d97), new Color(0xd3d3d3), new Color(0xababab), 22),
CYAN(new Color(0x169c9c), new Color(0xffff), new Color(0x287697), 23),
PURPLE(new Color(0x8932b8), new Color(0xa020f0), new Color(0x7b2fbe), 24),
BLUE(new Color(0x3c44aa), new Color(0xff), new Color(0x253192), 25),
BROWN(new Color(0x835432), new Color(0x8b4513), new Color(0x51301a), 26),
GREEN(new Color(0x5e7c16), new Color(0xff00), new Color(0x3b511a), 27),
RED(new Color(0xb02e26), new Color(0xff0000), new Color(0xb3312c), 28),
BLACK(new Color(0x1d1d21), new Color(0x0), new Color(0x1e1b1b), 29);
private final Color textureDiffuseColor;
private final Color textColor;
private final Color fireworkColor;
private final int mapColorId;
DyeColor(@NotNull Color textureDiffuseColor, @NotNull Color textColor,
@NotNull Color fireworkColor, int mapColorId) {
this.textureDiffuseColor = textureDiffuseColor;
this.textColor = textColor;
this.fireworkColor = fireworkColor;
this.mapColorId = mapColorId;
}
public @NotNull Color color() {
return this.textureDiffuseColor;
}
public @NotNull Color textColor() {
return this.textColor;
}
public @NotNull Color fireworkColor() {
return this.fireworkColor;
}
@Override
public int red() {
return this.textureDiffuseColor.red();
}
@Override
public int green() {
return this.textureDiffuseColor.green();
}
@Override
public int blue() {
return this.textureDiffuseColor.blue();
}
public int mapColorId() {
return this.mapColorId;
}
}

View File

@ -1,69 +0,0 @@
package net.minestom.server.color;
import net.kyori.adventure.util.RGBLike;
import org.jetbrains.annotations.NotNull;
/**
* Color values for dyes, wool and cloth items.
*/
public enum DyeColor implements RGBLike {
WHITE(new Color(0xF9FFFE), new Color(0xF0F0F0)),
ORANGE(new Color(0xF9801D), new Color(0xEB8844)),
MAGENTA(new Color(0xC74EBD), new Color(0xC354CD)),
LIGHT_BLUE(new Color(0x3AB3DA), new Color(0x6689D3)),
YELLOW(new Color(0xFED83D), new Color(0xDECF2A)),
LIME(new Color(0x80C71F), new Color(0x41CD34)),
PINK(new Color(0xF38BAA), new Color(0xD88198)),
GRAY(new Color(0x474F52), new Color(0x434343)),
LIGHT_GRAY(new Color(0x9D9D97), new Color(0xABABAB)),
CYAN(new Color(0x169C9C), new Color(0x287697)),
PURPLE(new Color(0x8932B8), new Color(0x7B2FBE)),
BLUE(new Color(0x3C44AA), new Color(0x253192)),
BROWN(new Color(0x835432), new Color(0x51301A)),
GREEN(new Color(0x5E7C16), new Color(0x3B511A)),
RED(new Color(0xB02E26), new Color(0xB3312C)),
BLACK(new Color(0x1D1D21), new Color(0x1E1B1B));
private final Color color;
private final Color firework;
DyeColor(Color color, Color firework) {
this.color = color;
this.firework = firework;
}
/**
* Gets the color that this dye represents.
*
* @return The {@link Color} that this dye represents
*/
@NotNull
public Color getColor() {
return this.color;
}
/**
* Gets the firework color that this dye represents.
*
* @return The {@link Color} that this dye represents
*/
@NotNull
public Color getFireworkColor() {
return this.firework;
}
@Override
public int red() {
return this.color.red();
}
@Override
public int green() {
return this.color.green();
}
@Override
public int blue() {
return this.color.blue();
}
}