Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[SOLVED][1.18] Loot Modifiers Require "type"... but using any known ones causes error


izofar
 Share

Recommended Posts

In 1.16, LootModifierManager#deserializeModifier read:

private IGlobalLootModifier deserializeModifier(ResourceLocation location, JsonElement element) {
        if (!element.isJsonObject()) return null;
        JsonObject object = element.getAsJsonObject();
        ILootCondition[] lootConditions = GSON_INSTANCE.fromJson(object.get("conditions"), ILootCondition[].class);

        // For backward compatibility with the initial implementation, fall back to using the location as the type.
        // TODO: Remove fallback in 1.16
        ResourceLocation serializer = location;
        if (object.has("type"))
        {
            serializer = new ResourceLocation(JSONUtils.getAsString(object, "type"));
        }

        return ForgeRegistries.LOOT_MODIFIER_SERIALIZERS.getValue(serializer).read(location, object, lootConditions);
    }

Note the if(object.has(type")){} block, which is lacking from the 1.18 version of the class:

private IGlobalLootModifier deserializeModifier(ResourceLocation location, JsonElement element) {
        if (!element.isJsonObject()) return null;
        JsonObject object = element.getAsJsonObject();
        LootItemCondition[] lootConditions = GSON_INSTANCE.fromJson(object.get("conditions"), LootItemCondition[].class);

        ResourceLocation serializer = new ResourceLocation(GsonHelper.getAsString(object, "type"));

        return ForgeRegistries.LOOT_MODIFIER_SERIALIZERS.getValue(serializer).read(location, object, lootConditions);
    }

So, the "value" tag is required now for loot modifiers... fine. I added this to my loot modifer json files when I kept getting NullPointerExceptions. But when I use a valid "value" tag I get:

[15:27:02] [Render thread/ERROR]: Couldn't parse loot modifier examplemod:add_structure_loot
java.lang.NullPointerException: Cannot invoke "net.minecraftforge.common.loot.GlobalLootModifierSerializer.read(net.minecraft.resources.ResourceLocation, com.google.gson.JsonObject, net.minecraft.world.level.storage.loot.predicates.LootItemCondition[])" because the return value of "net.minecraftforge.registries.IForgeRegistry.getValue(net.minecraft.resources.ResourceLocation)" is null
        at net.minecraftforge.common.loot.LootModifierManager.deserializeModifier(LootModifierManager.java:133) ~[forge-1.18-38.0.14_mapped_official_1.18-recomp.jar%2376%2382!:?]
        at net.minecraftforge.common.loot.LootModifierManager.lambda$apply$0(LootModifierManager.java:115) ~[forge-1.18-38.0.14_mapped_official_1.18-recomp.jar%2376%2382!:?]
        at java.util.ArrayList.forEach(ArrayList.java:1511) ~[?:?]
        at net.minecraftforge.common.loot.LootModifierManager.apply(LootModifierManager.java:113) ~[forge-1.18-38.0.14_mapped_official_1.18-recomp.jar%2376%2382!:?]
        at net.minecraftforge.common.loot.LootModifierManager.apply(LootModifierManager.java:54) ~[forge-1.18-38.0.14_mapped_official_1.18-recomp.jar%2376%2382!:?]

My json file is:

{
  "type": "chest",
  "conditions": [
    {
      "condition": "forge:loot_table_id",
      "loot_table_id": "examplemod:chests/modstructure"
    }
  ]
}

I tried printing the contents of ForgeRegistries.LOOT_MODIFIER_SERIALIZERS to know which keys I can use, but got:

[15:26:13] [modloading-worker-0/INFO]: Printing LOOT_MODIFER_SERIALIZERS
[15:26:13] [modloading-worker-0/INFO]: Printed LOOT_MODIFER_SERIALIZERS

Is this a bug, given 1.18 is new, or am I not using these classes right?

Edited by izofar
Link to comment
Share on other sites

1 minute ago, diesieben07 said:

"minecraft:chest" is not a valid loot modifier registry name.

I got this from the link above, which says:

Quote

Optional type of the loot table. Must be one of empty if the loot table does not generate any loot, entity for loot an entity drops, block for loot a block drops, chest for a treasure chest, fishing for a fishing loot table, gift for a cat or villager gift, advancement_reward if it's used as a reward for an advancement, barter for loot from bartering with piglins, command for /execute, selector for predicate= in selectors, advancement_entity for entity predicates in advancements or generic if none of the above apply.

If these are not the valid names, then what are?

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

That is for loot tables. You are making a loot modifier.

You registered your GlobalLootModifierSerializer to the registry with a registry name. The "type" field tells the game which serializer to use.

Just noticed the first part. As for the second, thanks lad!

Link to comment
Share on other sites

  • izofar changed the title to [SOLVED][1.18] Loot Modifiers Require "type"... but using any known ones causes error

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.