Jump to content

[1.16.4] Global Loot Modifiers for Mobs


ChonkoTheGreat

Recommended Posts

How does one add items to vanilla mob loot tables with global loot modifiers? I already managed to make it work with blocks, but mobs don't seem to work.

Here's my code:

LootAdditionModifier:

public class LootAdditionModifier extends LootModifier {
	private final Item addition;

    protected LootAdditionModifier(ILootCondition[] conditionsIn, Item addition) {
        super(conditionsIn);
        this.addition = addition;
    }

    @Nonnull
    @Override
    protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
        // generatedLoot is the loot that would be dropped, if we wouldn't add or replace
        // anything!
        generatedLoot.add(new ItemStack(addition, 1));
        return generatedLoot;
    }

    public static class Serializer extends GlobalLootModifierSerializer<LootAdditionModifier> {

        @Override
        public LootAdditionModifier read(ResourceLocation name, JsonObject object, ILootCondition[] conditionsIn) {
            Item addition = ForgeRegistries.ITEMS.getValue(
                    new ResourceLocation(JSONUtils.getString(object, "addition")));
            return new LootAdditionModifier(conditionsIn, addition);
        }

        @Override
        public JsonObject write(LootAdditionModifier instance) {
            JsonObject json = makeConditions(instance.conditions);
            json.addProperty("addition", ForgeRegistries.ITEMS.getKey(instance.addition).toString());
            return json;
        }
    }
}

LootStructureAdditionModifier:

public class LootStructureAdditionModifier extends LootModifier{
	 private final Item addition;

	    protected LootStructureAdditionModifier(ILootCondition[] conditionsIn, Item addition) {
	        super(conditionsIn);
	        this.addition = addition;
	    }

	    @Nonnull
	    @Override
	    protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
	        // generatedLoot is the loot that would be dropped, if we wouldn't add or replace
	        // anything!
	        if(context.getRandom().nextFloat() > 1.0) {
	            generatedLoot.add(new ItemStack(addition, 1));
	        }
	        return generatedLoot;
	    }

	    public static class Serializer extends GlobalLootModifierSerializer<LootStructureAdditionModifier> {

	        @Override
	        public LootStructureAdditionModifier read(ResourceLocation name, JsonObject object, ILootCondition[] conditionsIn) {
	            Item addition = ForgeRegistries.ITEMS.getValue(
	                    new ResourceLocation(JSONUtils.getString(object, "addition")));
	            return new LootStructureAdditionModifier(conditionsIn, addition);
	        }

	        @Override
	        public JsonObject write(LootStructureAdditionModifier instance) {
	            JsonObject json = makeConditions(instance.conditions);
	            json.addProperty("addition", ForgeRegistries.ITEMS.getKey(instance.addition).toString());
	            return json;
	        }
	    }
	}

The global_loot_modifiers.json:

{
  "replace": false,
  "entries": [
    "mikesfancycontent:test"
  ]
}

And the test.json:

{
  "conditions": [
    {
      "condition": "minecraft:entity_properties",
      "entity": "this",
      "predicate": {
        "type": "minecraft:stray"
      }
    }
  ],
  "addition": "mikesfancycontent:frost_berries"
}

What do I have to change to make it work for mob drops. Thanks in advance.

Link to comment
Share on other sites

4 hours ago, ChonkoTheGreat said:
mikesfancycontent:test

This is not the registry name of your loot modifier serializser(s) is it?

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

They sure do, I made sure of it.
Kinda surprised it isn't part of the test mod that went along with it in the Forge repo source, but whatever.

I wrote it and tested it. So I know it works.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

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.