Jump to content

Recommended Posts

Posted

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!

 

Posted

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?

Posted

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?

Posted
13 minutes ago, diesieben07 said:

Resources in src/main/resources are copied into the build output folder when running the game, if you modify them while the game is running you have to rebuild.

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.