Jump to content

Recommended Posts

Posted

I'm trying out using global loot modifiers to modify the loot tables of some vanilla entities for my mod.  At first I thought I had it working fine: my custom item dropped for entities in the specified tag and didn't drop for entities outside of it.  Great.  The next day, though, while doing some testing in survival mode, I broke an oak log... and my custom item dropped for that too.  And then again for stone, granite, and coal ore.  Not great.

Can anyone tell me what I'm doing wrong?  Why my loot modifier, with conditions that seem to imply that it should only drop for certain entity types, is also triggering for every block under the sun?  I haven't found anything that derives from ILootCondition with a name that suggests it differentiates between blocks and entities, so I'm feeling stuck.

Here's the code for the loot modifier object:

package com.verdantartifice.primalmagic.common.loot.modifiers;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonObject;
import com.verdantartifice.primalmagic.common.items.ItemsPM;
import com.verdantartifice.primalmagic.common.util.ItemUtils;

import net.minecraft.item.ItemStack;
import net.minecraft.loot.ConstantRange;
import net.minecraft.loot.ItemLootEntry;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.LootTable;
import net.minecraft.loot.RandomValueRange;
import net.minecraft.loot.conditions.ILootCondition;
import net.minecraft.loot.functions.LootingEnchantBonus;
import net.minecraft.loot.functions.SetCount;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
import net.minecraftforge.common.loot.LootModifier;

/**
 * Global loot modifier that allows mobs in the conditioned tag to drop Bloody Flesh when killed.
 * 
 * @author Daedalus4096
 */
public class BloodyFleshModifier extends LootModifier {
    public BloodyFleshModifier(ILootCondition[] conditionsIn) {
        super(conditionsIn);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
        LootTable table = LootTable.builder().addLootPool(LootPool.builder().rolls(ConstantRange.of(1)).addEntry(ItemLootEntry.builder(ItemsPM.BLOODY_FLESH.get()).acceptFunction(SetCount.builder(RandomValueRange.of(0.0F, 1.0F))).acceptFunction(LootingEnchantBonus.builder(RandomValueRange.of(0.0F, 1.0F))))).build();
        List<ItemStack> lootList = new ArrayList<>();
        table.generate(context, lootList::add); // Use deprecated version to avoid calling modifyLoot and prevent infinite recursion
        return ItemUtils.mergeItemStackLists(generatedLoot, lootList);
    }

    public static class Serializer extends GlobalLootModifierSerializer<BloodyFleshModifier> {
        @Override
        public BloodyFleshModifier read(ResourceLocation location, JsonObject object, ILootCondition[] ailootcondition) {
            return new BloodyFleshModifier(ailootcondition);
        }

        @Override
        public JsonObject write(BloodyFleshModifier instance) {
            return this.makeConditions(instance.conditions);
        }
    }
}

And here's the JSON file for the modifier:

{
  "conditions": [
    {
      "condition": "minecraft:entity_properties",
      "predicate": {
        "type": "#primalmagic:drops_bloody_flesh"
      },
      "entity": "this"
    }
  ],
  "type": "primalmagic:bloody_flesh"
}

The GitHub repository can be found here if you need to look at anything else.  Thank you for your time.

Posted
3 hours ago, Daedalus4096 said:

Can anyone tell me what I'm doing wrong?

When you are breaking a block, the "this" entity in the condition is the player, and since the primalmagic:drops_bloody_flesh tag contains the player, the condition is met. You would have to add another condition that checks that an entity was actually killed.

  • Thanks 1
Posted (edited)

Also why are you building a new loot table every time?

Either:

a) build the loot table once and store it in a field
b) don't use a loot table at all and just go "derp, I can ask for a random integer from 0 to 1 and if 1 add a new item stack to the generated loot list." Because the loot table you're building is overkill for what you need.
c) put that loot table stuff in the loot table json file and deserialize the details so datapacks can override it

Edited by Draco18s
  • Thanks 1

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.

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.