Skip to content

Commit 9cb9a92

Browse files
authored
1.21-rc1 (#26)
* feat: 1.21 new dynamic registries, new tag paths, generate game constants * chore: 1.21-pre2 * feat: generate all tags from client, 1.21-rc1, support generating snbt
1 parent 407b206 commit 9cb9a92

16 files changed

+103
-125
lines changed

DataGenerator/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ apply {
1212
group = "net.minestom"
1313
version = rootProject.version
1414

15-
//repositories {
16-
// mavenCentral()
17-
//}
18-
1915
dependencies {
2016
implementation(libs.gson)
2117
implementation(libs.bundles.logging)

DataGenerator/src/main/java/net/minestom/datagen/DataGen.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonElement;
6+
import net.minestom.generators.tags.GenericTagGenerator;
57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79

@@ -10,12 +12,15 @@
1012
import java.nio.file.Files;
1113
import java.nio.file.Path;
1214
import java.nio.file.StandardOpenOption;
15+
import java.util.List;
1316

1417
public class DataGen {
1518
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
1619
private static final Logger LOGGER = LoggerFactory.getLogger(DataGen.class);
1720
private static Path OUTPUT = Path.of("../MinestomData/");
1821

22+
private static final List<String> TAG_TYPES = List.of("banner_pattern", "block", "cat_variant", "damage_type", "enchantment", "entity_type", "fluid", "game_event", "instrument", "item", "painting_variant", "worldgen/biome");
23+
1924
public static void main(String[] args) throws Exception {
2025
if (args.length > 0) {
2126
OUTPUT = Path.of(args[0]);
@@ -26,26 +31,38 @@ public static void main(String[] args) throws Exception {
2631

2732
LOGGER.info("Generation starting...");
2833
for (var type : DataGenType.values()) {
29-
generate(type, type.getGenerator());
34+
generate(type.getFileName(), type.getGenerator());
35+
}
36+
for (var tag : TAG_TYPES) {
37+
if (tag.contains("/")) { // Slice off worldgen from worldgen/biome
38+
tag = tag.substring(tag.lastIndexOf("/") + 1);
39+
}
40+
generate("tags/" + tag, new GenericTagGenerator(tag));
3041
}
3142
LOGGER.info("Generation done!");
3243
}
3344

34-
public static void generate(DataGenType type, DataGenerator generator) {
35-
final var path = OUTPUT.resolve(type.getFileName() + ".json");
45+
public static void generate(String fileName, DataGenerator generator) {
3646
try {
47+
Object result = generator.generate();
48+
final var path = OUTPUT.resolve(fileName + (result instanceof JsonElement ? ".json" : ".snbt"));
49+
3750
// Ensure that the directory exists
3851
if (!Files.exists(path)) {
3952
Files.createDirectories(path.getParent());
4053
}
4154
try (BufferedWriter writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
4255
try {
43-
GSON.toJson(generator.generate(), writer);
56+
if (result instanceof JsonElement) {
57+
GSON.toJson(result, writer);
58+
} else {
59+
writer.write(result.toString());
60+
}
4461
} catch (Exception e) {
4562
throw new RuntimeException(e);
4663
}
4764
}
48-
} catch (IOException e) {
65+
} catch (Exception e) {
4966
e.printStackTrace();
5067
}
5168
}

DataGenerator/src/main/java/net/minestom/datagen/DataGenType.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import java.util.List;
1111

1212
public enum DataGenType {
13+
CONSTANTS("constants", new MinecraftConstantGenerator()),
14+
1315
ATTRIBUTES("attributes", new AttributeGenerator()),
1416
BIOMES("biomes", new BiomeGenerator()),
1517
BLOCKS("blocks", new BlockGenerator()),
1618
COMMAND_ARGUMENTS("command_arguments", new CommandArgumentGenerator()),
1719
CUSTOM_STATISTICS("custom_statistics", new CustomStatisticGenerator()),
1820
DYE_COLORS("dye_colors", new DyeColorGenerator()),
19-
ENCHANTMENTS("enchantments", new EnchantmentGenerator()),
2021
ENTITIES("entities", new EntityGenerator()),
2122
FLUIDS("fluids", new FluidGenerator()),
2223
GAME_EVENTS("game_events", new GameEventGenerator()),
@@ -31,11 +32,7 @@ public enum DataGenType {
3132
VILLAGER_TYPES("villager_types", new VillagerTypeGenerator()),
3233
RECIPE_TYPE("recipe_types", new RecipeTypeGenerator()),
3334

34-
BLOCK_TAGS("tags/block_tags", new BlockTagGenerator()),
35-
ENTITY_TYPE_TAGS("tags/entity_type_tags", new EntityTypeTagGenerator()),
36-
FLUID_TAGS("tags/fluid_tags", new FluidTagGenerator()),
37-
GAMEEVENT_TAGS("tags/gameplay_tags", new GameEventTagGenerator()),
38-
ITEM_TAGS("tags/item_tags", new ItemTagGenerator()),
35+
// Tags are specified as a special case in datagen
3936

4037
DIMENSION_TYPES("dimension_types", new GenericResourceGenerator("dimension_type")),
4138
CHAT_TYPES("chat_types", new GenericResourceGenerator("chat_type")),
@@ -44,6 +41,9 @@ public enum DataGenType {
4441
WOLF_VARIANTS("wolf_variants", new GenericResourceGenerator("wolf_variant")),
4542
TRIM_MATERIALS("trim_materials", new GenericResourceGenerator("trim_material")),
4643
TRIM_PATTERNS("trim_patterns", new GenericResourceGenerator("trim_pattern")),
44+
ENCHANTMENTS("enchantments", new GenericResourceGenerator("enchantment", List.of(), true)),
45+
PAINTING_VARIANTS("painting_variants", new GenericResourceGenerator("painting_variant")),
46+
JUKEBOX_SONGS("jukebox_songs", new GenericResourceGenerator("jukebox_song")),
4747

4848
BLOCK_LOOT_TABLES("loot_tables/block_loot_tables", new BlockLootTableGenerator()),
4949
CHEST_LOOT_TABLES("loot_tables/chest_loot_tables", new ChestLootTableGenerator()),

DataGenerator/src/main/java/net/minestom/datagen/DataGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.minestom.datagen;
22

3-
import com.google.gson.JsonElement;
43
import com.google.gson.JsonObject;
54
import com.google.gson.stream.JsonReader;
65
import net.minecraft.SharedConstants;
@@ -40,7 +39,7 @@ public abstract class DataGenerator {
4039
}
4140
}
4241

43-
public abstract JsonElement generate() throws Exception;
42+
public abstract Object/*JsonElement, String*/ generate() throws Exception;
4443

4544
protected void addDefaultable(JsonObject jsonObject, String key, boolean value, boolean defaultValue) {
4645
if (value != defaultValue) jsonObject.addProperty(key, value);

DataGenerator/src/main/java/net/minestom/generators/DyeColorGenerator.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public JsonArray generate() {
1414
JsonObject dyeColor = new JsonObject();
1515
dyeColor.addProperty("id", dC.getId());
1616
dyeColor.addProperty("name", dC.name());
17-
dyeColor.addProperty("textureDiffuseColor", convertTextureDiffuseColors(dC.getTextureDiffuseColors()));
17+
dyeColor.addProperty("textureDiffuseColor", dC.getTextureDiffuseColor() & 0xFFFFFF);
1818
dyeColor.addProperty("textColor", dC.getTextColor());
1919
dyeColor.addProperty("fireworkColor", dC.getFireworkColor());
2020
dyeColor.addProperty("mapColorId", dC.getMapColor().id);
@@ -23,11 +23,4 @@ public JsonArray generate() {
2323
return dyeColors;
2424
}
2525

26-
27-
private static int convertTextureDiffuseColors(float[] colorArray) {
28-
int red = Math.round(colorArray[0] * 255.0f) << 16;
29-
int green = Math.round(colorArray[1] * 255.0f) << 8;
30-
int blue = Math.round(colorArray[2] * 255.0f);
31-
return red + green + blue;
32-
}
3326
}

DataGenerator/src/main/java/net/minestom/generators/EnchantmentGenerator.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

DataGenerator/src/main/java/net/minestom/generators/GenericResourceGenerator.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,41 @@
33
import com.google.gson.Gson;
44
import com.google.gson.JsonElement;
55
import com.google.gson.JsonObject;
6+
import com.mojang.serialization.Dynamic;
7+
import com.mojang.serialization.JsonOps;
8+
import net.minecraft.nbt.NbtOps;
9+
import net.minecraft.nbt.SnbtPrinterTagVisitor;
10+
import net.minecraft.nbt.Tag;
611
import net.minestom.datagen.DataGenerator;
712
import net.minestom.utils.ResourceUtils;
813
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
915

16+
import java.util.ArrayList;
1017
import java.util.List;
1118
import java.util.Scanner;
19+
import java.util.function.Function;
1220

1321
public class GenericResourceGenerator extends DataGenerator {
1422

1523
private static final Gson gson = new Gson();
1624

1725
private final String name;
1826
private final List<String> exclusions;
27+
private final boolean snbt;
1928

2029
public GenericResourceGenerator(@NotNull String name) {
21-
this(name, List.of());
30+
this(name, List.of(), false);
2231
}
2332

24-
public GenericResourceGenerator(@NotNull String name, @NotNull List<String> exclusions) {
33+
public GenericResourceGenerator(@NotNull String name, @NotNull List<String> exclusions, boolean snbt) {
2534
this.name = "data/minecraft/" + name + "/";
2635
this.exclusions = exclusions;
36+
this.snbt = snbt;
2737
}
2838

2939
@Override
30-
public JsonElement generate() throws Exception {
40+
public Object generate() throws Exception {
3141
var result = new JsonObject();
3242

3343
// get all files from the damage types directory
@@ -49,10 +59,28 @@ public JsonElement generate() throws Exception {
4959
var key = "minecraft:" + fileName.substring(0, fileName.length() - 5);
5060
var jsonObject = gson.fromJson(content.toString(), JsonObject.class);
5161
exclusions.forEach(jsonObject::remove);
62+
5263
result.add(key, jsonObject);
5364
}
5465
}
5566

67+
if (snbt) {
68+
Tag tag = Dynamic.convert(JsonOps.INSTANCE, NbtOps.INSTANCE, result);
69+
return new SnbtPrinterTagVisitor(" ", 0, new ArrayList<>()).visit(tag);
70+
}
5671
return result;
5772
}
73+
74+
75+
private @Nullable Function<JsonObject, JsonObject> transformer() {
76+
if (name.contains("enchantment")) {
77+
return obj -> {
78+
JsonObject result = new JsonObject();
79+
Tag tag = Dynamic.convert(JsonOps.INSTANCE, NbtOps.INSTANCE, obj);
80+
var snbt = new SnbtPrinterTagVisitor("", 0, new ArrayList<>()).visit(tag);
81+
result.addProperty("raw", snbt);
82+
return result;
83+
};
84+
} else return null;
85+
}
5886
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.minestom.generators;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import net.minecraft.SharedConstants;
6+
import net.minecraft.server.packs.PackType;
7+
import net.minestom.datagen.DataGenerator;
8+
9+
public class MinecraftConstantGenerator extends DataGenerator {
10+
11+
@Override
12+
public JsonElement generate() throws Exception {
13+
var version = SharedConstants.getCurrentVersion();
14+
JsonObject obj = new JsonObject();
15+
obj.addProperty("name", version.getId());
16+
obj.addProperty("friendly_name", version.getName());
17+
obj.addProperty("protocol", version.getProtocolVersion());
18+
obj.addProperty("world", version.getDataVersion().getVersion());
19+
obj.addProperty("resourcepack", version.getPackVersion(PackType.CLIENT_RESOURCES));
20+
obj.addProperty("datapack", version.getPackVersion(PackType.SERVER_DATA));
21+
return obj;
22+
}
23+
24+
}

DataGenerator/src/main/java/net/minestom/generators/tags/BlockTagGenerator.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

DataGenerator/src/main/java/net/minestom/generators/tags/EntityTypeTagGenerator.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)