Everything posted by RisingInIris2017
-
[1.16.5] How to load and hot reload data from JSON
Oh I got it. I'm using IDEA, so if I want to reload these JSONs, I have to make a datapack and put it into `/run`? Are there some more easier ways?
-
[1.16.5] How to load and hot reload data from JSON
Thanks for your kind-hearted explanation! I tried your method and now it can read JSONs from correct ResourceLocation. private void jsonRulesReader (AddReloadListenerEvent event) { event.addListener(new JsonReloadListener((new GsonBuilder()).create(), "rules") { @Override protected void apply(Map<ResourceLocation, JsonElement> objectIn, IResourceManager resourceManagerIn, IProfiler profilerIn) { objectIn.forEach((resourceLocation, ruleJsonElement) -> { try { JsonObject jsonObject = JSONUtils.getJsonObject(ruleJsonElement, "rule"); LOGGER.warn(jsonObject); } catch (IllegalArgumentException | JsonParseException e) { LOGGER.error("Parsing error loading custom rules. Message: {}", e.getMessage()); } }); } }); } But I have a new problem: it cannot reload data when I type `/reload` command. To be more specific, I can reproduce this issue by following steps: 1. Start Minecraft and see it loading my mod's datapack. 2. After it finishes loading, modify a JSON. 3. Execute the `/reload` command. 4. In console log I find that it prints the version of JSON before it was modified. 5. Restart Minecraft, then I can see the modified JSON is loaded. This is not the behavior I want. I need it to be reloaded everytime the `/reload` command is executed. So how should I modify my codes to achieve that goal?
-
[1.16.5] How to load and hot reload data from JSON
I found that if I `new JsonReloadListener`, I should pass two arguments to two parameters of its constructor, the first one is a `Gson`, while second one is a `String`. I read the code of AdvancementManager, it passes `(new GsonBuilder()).create()` to the first parameter, and passes String `"advancement"` to the second one. So I wrote these codes: private void jsonRulesReader (AddReloadListenerEvent event) { event.addListener(new JsonReloadListener((new GsonBuilder()).create(), "some String") { @Override protected void apply(Map<ResourceLocation, JsonElement> objectIn, IResourceManager resourceManagerIn, IProfiler profilerIn) { "something need to do" } }); } I wonder what the second parameters mean. What will that string be? Will it become a part of a certain `ResourceLocation`, or a part of file name?
-
[1.16.5] How to load and hot reload data from JSON
To put more specifically, I expect my mod has such a behavior: 1. When Minecraft instance(both client and server) start, load some data from a JSON file. 2. Using these data to do what it should do, until vanilla `/reload` command is executed. 3. Reload new data from that JSON file. 4. Continue to do what it should do. I wrote these codes in my mod's main class: @Mod("spellbind") public class Spellbind { public static final String MOD_ID = "spellbind"; public static final String MOD_NAME = "Spellbind"; private static final Logger LOGGER = LogManager.getLogger(); public Spellbind() { final IEventBus forgeBus = MinecraftForge.EVENT_BUS; forgeBus.addListener(this::jsonRulesReader); } // Read JSON logic // Read JSON from datapack, so that modpack makers can set their own "spells". private void jsonRulesReader (AddReloadListenerEvent event) { event.addListener("something missing"); } Then I did find something missing. I had no idea about how to use the `AddReloadListenerEvent`. I searched the Internet and get this: I tried to use the `JsonReloadListener`, but I found I did not understand it, neither. I do read many Forge forum posts and codes(from other mods and Minecraft vanilla itself), but I still feel difficult. You are highly appreciated if you could explain what code I should write to achieve my goal. Thanks in advance!
IPS spam blocked by CleanTalk.