From a7191bcf12b09b506289388b11f2a519a200c4b5 Mon Sep 17 00:00:00 2001 From: Exopandora Date: Sun, 9 Jul 2023 13:07:49 +0200 Subject: [PATCH] Load categories dynamically from json. Closes #26 --- .../worldhandler/gui/category/Category.java | 54 ++++++++++++++----- .../usercontent/UsercontentLoader.java | 20 +++++++ 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/main/java/exopandora/worldhandler/gui/category/Category.java b/src/main/java/exopandora/worldhandler/gui/category/Category.java index e4feaa9..884b7e8 100644 --- a/src/main/java/exopandora/worldhandler/gui/category/Category.java +++ b/src/main/java/exopandora/worldhandler/gui/category/Category.java @@ -1,7 +1,10 @@ package exopandora.worldhandler.gui.category; -import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -27,6 +30,15 @@ public class Category { public static IForgeRegistry REGISTRY; public static final ResourceKey> REGISTRY_KEY = ResourceKey.createRegistryKey(new ResourceLocation(Main.MODID, "category")); + public static final Map> DEFAULT_CATEGORIES = new CategoriesBuilder() + .add("main", "main", "containers", "multiplayer") + .add("entities", "summon", "butcher") + .add("items", "custom_item", "enchantment", "recipes") + .add("blocks", "edit_blocks", "sign_editor", "note_editor") + .add("world", "world", "gamerules", "locate") + .add("player", "player", "experience", "advancements") + .add("scoreboard", "scoreboard_objectives", "scoreboard_teams", "scoreboard_players") + .build(); private final List contents; @@ -45,11 +57,6 @@ public class Category this(Lists.newArrayList(contents)); } - public Category(String... keys) - { - this(Arrays.stream(keys).map(key -> new ResourceLocation(Main.MODID, key)).collect(Collectors.toList())); - } - public Category add(int index, ResourceLocation content) { this.contents.add(Math.min(index, this.getSize()), content); @@ -91,13 +98,16 @@ public class Category { if(event.getRegistryKey().equals(REGISTRY_KEY)) { - RegistryHelper.register(event, REGISTRY_KEY, "main", () -> new Category("main", "containers", "multiplayer")); - RegistryHelper.register(event, REGISTRY_KEY, "entities", () -> new Category("summon", "butcher")); - RegistryHelper.register(event, REGISTRY_KEY, "items", () -> new Category("custom_item", "enchantment", "recipes")); - RegistryHelper.register(event, REGISTRY_KEY, "blocks", () -> new Category("edit_blocks", "sign_editor", "note_editor")); - RegistryHelper.register(event, REGISTRY_KEY, "world", () -> new Category("world", "gamerules", "locate")); - RegistryHelper.register(event, REGISTRY_KEY, "player", () -> new Category("player", "experience", "advancements")); - RegistryHelper.register(event, REGISTRY_KEY, "scoreboard", () -> new Category("scoreboard_objectives", "scoreboard_teams", "scoreboard_players")); + for(Entry> entry : UsercontentLoader.CATEGORIES.entrySet()) + { + RegistryHelper.register(event, REGISTRY_KEY, entry.getKey(), () -> + { + var keys = entry.getValue().stream() + .map(key -> new ResourceLocation(Main.MODID, key)) + .collect(Collectors.toList()); + return new Category(keys); + }); + } for(UsercontentConfig config : UsercontentLoader.CONFIGS) { @@ -115,7 +125,7 @@ public class Category { if(!Categories.isRegistered(tab.getCategory())) { - RegistryHelper.register(event, REGISTRY_KEY, tab.getCategory(), () -> new Category(id)); + RegistryHelper.register(event, REGISTRY_KEY, tab.getCategory(), () -> new Category(new ResourceLocation(Main.MODID, id))); } else { @@ -123,4 +133,20 @@ public class Category } } } + + private static class CategoriesBuilder + { + private final Map> categories = new HashMap>(); + + public CategoriesBuilder add(String category, String... contents) + { + this.categories.put(category, Collections.unmodifiableList(Lists.newArrayList(contents))); + return this; + } + + public Map> build() + { + return Collections.unmodifiableMap(categories); + } + } } diff --git a/src/main/java/exopandora/worldhandler/usercontent/UsercontentLoader.java b/src/main/java/exopandora/worldhandler/usercontent/UsercontentLoader.java index fd4d7d4..ce23c58 100644 --- a/src/main/java/exopandora/worldhandler/usercontent/UsercontentLoader.java +++ b/src/main/java/exopandora/worldhandler/usercontent/UsercontentLoader.java @@ -1,12 +1,15 @@ package exopandora.worldhandler.usercontent; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @@ -16,6 +19,7 @@ import javax.script.ScriptEngine; import org.apache.commons.io.IOUtils; import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory; +import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; @@ -24,6 +28,7 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import exopandora.worldhandler.WorldHandler; +import exopandora.worldhandler.gui.category.Category; import exopandora.worldhandler.gui.widget.button.EnumIcon; import exopandora.worldhandler.usercontent.model.Action; import exopandora.worldhandler.usercontent.model.ArgumentType; @@ -36,6 +41,7 @@ import net.minecraft.resources.ResourceLocation; public class UsercontentLoader { public static final List CONFIGS = new ArrayList(); + public static final Map> CATEGORIES = new HashMap>(Category.DEFAULT_CATEGORIES); public static void load(Path path) { @@ -63,7 +69,21 @@ public class UsercontentLoader .registerTypeAdapter(JsonWidget.Type.class, new EnumTypeAdapter(JsonWidget.Type.class)) .registerTypeAdapter(Action.Type.class, new EnumTypeAdapter(Action.Type.class)) .registerTypeAdapter(JsonMenu.Type.class, new EnumTypeAdapter(JsonMenu.Type.class)) + .setPrettyPrinting() .create(); + final Path categories = path.resolve("categories.json"); + + if(Files.exists(categories) && Files.isRegularFile(categories) && Files.isReadable(categories)) + { + String fileContents = UsercontentLoader.readFile(categories); + UsercontentLoader.CATEGORIES.putAll(gson.fromJson(fileContents, new TypeToken>>() {}.getType())); + } + + try(FileOutputStream outputStream = new FileOutputStream(categories.toFile())) + { + IOUtils.write(gson.toJson(UsercontentLoader.CATEGORIES), outputStream, Charset.defaultCharset()); + } + final List folders = Files.list(path) .filter(Files::isDirectory) .filter(Files::isReadable)