Skip to content

Commit

Permalink
feat: generate file for builtin armor trim materials and patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed Jan 30, 2024
1 parent 17ba1b9 commit 5ff405e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public enum DataGenType {
GAMEEVENT_TAGS("tags/gameplay_tags", new GameEventTagGenerator()),
ITEM_TAGS("tags/item_tags", new ItemTagGenerator()),

DAMAGE_TYPES("damage_types", new DamageTypeGenerator()),
DAMAGE_TYPES("damage_types", new GenericResourceGenerator("damage_type")),

TRIM_MATERIALS("trim_materials", new GenericResourceGenerator("trim_material")),
TRIM_PATTERNS("trim_patterns", new GenericResourceGenerator("trim_pattern")),

BLOCK_LOOT_TABLES("loot_tables/block_loot_tables", new BlockLootTableGenerator()),
CHEST_LOOT_TABLES("loot_tables/chest_loot_tables", new ChestLootTableGenerator()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@
import com.google.gson.JsonObject;
import net.minestom.datagen.DataGenerator;
import net.minestom.utils.ResourceUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Scanner;

public class DamageTypeGenerator extends DataGenerator {
public class GenericResourceGenerator extends DataGenerator {

private static final String DAMAGE_TYPE_DIR = "data/minecraft/damage_type/";
private static final Gson gson = new Gson();

private final String name;

public GenericResourceGenerator(@NotNull String name) {
this.name = "data/minecraft/" + name + "/";
}

@Override
public JsonElement generate() throws Exception {
var damageTypesJson = new JsonObject();
var result = new JsonObject();

// get all files from the biomes directory
var files = ResourceUtils.getResourceListing(
net.minecraft.server.MinecraftServer.class, DAMAGE_TYPE_DIR);
// get all files from the damage types directory
var files = ResourceUtils.getResourceListing(net.minecraft.server.MinecraftServer.class, name);

for (String fileName : files) {
var file = net.minecraft.server.MinecraftServer.class
.getClassLoader()
.getResourceAsStream(DAMAGE_TYPE_DIR + fileName);
.getResourceAsStream(name + fileName);
var scanner = new Scanner(file);
var content = new StringBuilder();
while (scanner.hasNextLine()) {
Expand All @@ -34,12 +39,12 @@ public JsonElement generate() throws Exception {

// only collect valid files
if (content.length() > 0 && fileName.endsWith(".json")) {
var biomeKey = "minecraft:" + fileName.substring(0, fileName.length() - 5);
var key = "minecraft:" + fileName.substring(0, fileName.length() - 5);
var jsonObject = gson.fromJson(content.toString(), JsonObject.class);
damageTypesJson.add(biomeKey, jsonObject);
result.add(key, jsonObject);
}
}

return damageTypesJson;
return result;
}
}

0 comments on commit 5ff405e

Please sign in to comment.