Load categories dynamically from json. Closes #26
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
package exopandora.worldhandler.gui.category;
|
package exopandora.worldhandler.gui.category;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -27,6 +30,15 @@ public class Category
|
|||||||
{
|
{
|
||||||
public static IForgeRegistry<Category> REGISTRY;
|
public static IForgeRegistry<Category> REGISTRY;
|
||||||
public static final ResourceKey<Registry<Category>> REGISTRY_KEY = ResourceKey.createRegistryKey(new ResourceLocation(Main.MODID, "category"));
|
public static final ResourceKey<Registry<Category>> REGISTRY_KEY = ResourceKey.createRegistryKey(new ResourceLocation(Main.MODID, "category"));
|
||||||
|
public static final Map<String, List<String>> 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<ResourceLocation> contents;
|
private final List<ResourceLocation> contents;
|
||||||
|
|
||||||
@@ -45,11 +57,6 @@ public class Category
|
|||||||
this(Lists.newArrayList(contents));
|
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)
|
public Category add(int index, ResourceLocation content)
|
||||||
{
|
{
|
||||||
this.contents.add(Math.min(index, this.getSize()), content);
|
this.contents.add(Math.min(index, this.getSize()), content);
|
||||||
@@ -91,13 +98,16 @@ public class Category
|
|||||||
{
|
{
|
||||||
if(event.getRegistryKey().equals(REGISTRY_KEY))
|
if(event.getRegistryKey().equals(REGISTRY_KEY))
|
||||||
{
|
{
|
||||||
RegistryHelper.register(event, REGISTRY_KEY, "main", () -> new Category("main", "containers", "multiplayer"));
|
for(Entry<String, List<String>> entry : UsercontentLoader.CATEGORIES.entrySet())
|
||||||
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, entry.getKey(), () ->
|
||||||
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"));
|
var keys = entry.getValue().stream()
|
||||||
RegistryHelper.register(event, REGISTRY_KEY, "player", () -> new Category("player", "experience", "advancements"));
|
.map(key -> new ResourceLocation(Main.MODID, key))
|
||||||
RegistryHelper.register(event, REGISTRY_KEY, "scoreboard", () -> new Category("scoreboard_objectives", "scoreboard_teams", "scoreboard_players"));
|
.collect(Collectors.toList());
|
||||||
|
return new Category(keys);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for(UsercontentConfig config : UsercontentLoader.CONFIGS)
|
for(UsercontentConfig config : UsercontentLoader.CONFIGS)
|
||||||
{
|
{
|
||||||
@@ -115,7 +125,7 @@ public class Category
|
|||||||
{
|
{
|
||||||
if(!Categories.isRegistered(tab.getCategory()))
|
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
|
else
|
||||||
{
|
{
|
||||||
@@ -123,4 +133,20 @@ public class Category
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class CategoriesBuilder
|
||||||
|
{
|
||||||
|
private final Map<String, List<String>> categories = new HashMap<String, List<String>>();
|
||||||
|
|
||||||
|
public CategoriesBuilder add(String category, String... contents)
|
||||||
|
{
|
||||||
|
this.categories.put(category, Collections.unmodifiableList(Lists.newArrayList(contents)));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, List<String>> build()
|
||||||
|
{
|
||||||
|
return Collections.unmodifiableMap(categories);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package exopandora.worldhandler.usercontent;
|
package exopandora.worldhandler.usercontent;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -16,6 +19,7 @@ import javax.script.ScriptEngine;
|
|||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||||
|
|
||||||
|
import com.google.common.reflect.TypeToken;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
@@ -24,6 +28,7 @@ import com.google.gson.stream.JsonReader;
|
|||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
import exopandora.worldhandler.WorldHandler;
|
import exopandora.worldhandler.WorldHandler;
|
||||||
|
import exopandora.worldhandler.gui.category.Category;
|
||||||
import exopandora.worldhandler.gui.widget.button.EnumIcon;
|
import exopandora.worldhandler.gui.widget.button.EnumIcon;
|
||||||
import exopandora.worldhandler.usercontent.model.Action;
|
import exopandora.worldhandler.usercontent.model.Action;
|
||||||
import exopandora.worldhandler.usercontent.model.ArgumentType;
|
import exopandora.worldhandler.usercontent.model.ArgumentType;
|
||||||
@@ -36,6 +41,7 @@ import net.minecraft.resources.ResourceLocation;
|
|||||||
public class UsercontentLoader
|
public class UsercontentLoader
|
||||||
{
|
{
|
||||||
public static final List<UsercontentConfig> CONFIGS = new ArrayList<UsercontentConfig>();
|
public static final List<UsercontentConfig> CONFIGS = new ArrayList<UsercontentConfig>();
|
||||||
|
public static final Map<String, List<String>> CATEGORIES = new HashMap<String, List<String>>(Category.DEFAULT_CATEGORIES);
|
||||||
|
|
||||||
public static void load(Path path)
|
public static void load(Path path)
|
||||||
{
|
{
|
||||||
@@ -63,7 +69,21 @@ public class UsercontentLoader
|
|||||||
.registerTypeAdapter(JsonWidget.Type.class, new EnumTypeAdapter<JsonWidget.Type>(JsonWidget.Type.class))
|
.registerTypeAdapter(JsonWidget.Type.class, new EnumTypeAdapter<JsonWidget.Type>(JsonWidget.Type.class))
|
||||||
.registerTypeAdapter(Action.Type.class, new EnumTypeAdapter<Action.Type>(Action.Type.class))
|
.registerTypeAdapter(Action.Type.class, new EnumTypeAdapter<Action.Type>(Action.Type.class))
|
||||||
.registerTypeAdapter(JsonMenu.Type.class, new EnumTypeAdapter<JsonMenu.Type>(JsonMenu.Type.class))
|
.registerTypeAdapter(JsonMenu.Type.class, new EnumTypeAdapter<JsonMenu.Type>(JsonMenu.Type.class))
|
||||||
|
.setPrettyPrinting()
|
||||||
.create();
|
.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<Map<String, List<String>>>() {}.getType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
try(FileOutputStream outputStream = new FileOutputStream(categories.toFile()))
|
||||||
|
{
|
||||||
|
IOUtils.write(gson.toJson(UsercontentLoader.CATEGORIES), outputStream, Charset.defaultCharset());
|
||||||
|
}
|
||||||
|
|
||||||
final List<Path> folders = Files.list(path)
|
final List<Path> folders = Files.list(path)
|
||||||
.filter(Files::isDirectory)
|
.filter(Files::isDirectory)
|
||||||
.filter(Files::isReadable)
|
.filter(Files::isReadable)
|
||||||
|
|||||||
Reference in New Issue
Block a user