package org.cyclops.cyclopscore.advancement.criterion;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.advancements.critereon.AbstractCriterionInstance;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import org.cyclops.cyclopscore.Reference;

/**
 * Trigger for when an item of a given mod is obtained.
 * @author rubensworks
 */
public class ModItemObtainedTrigger extends BaseCriterionTrigger<ItemStack, ModItemObtainedTrigger.Instance> {
    public ModItemObtainedTrigger() {
        super(new ResourceLocation(Reference.MOD_ID, "mod_item_obtained"));
        MinecraftForge.EVENT_BUS.register(this);
    }

    @Override
    public Instance deserializeInstance(JsonObject json, JsonDeserializationContext context) {
        return new Instance(getId(), json.get("mod_id").getAsString());
    }

    @SubscribeEvent
    public void onPickup(PlayerEvent.ItemPickupEvent event) {
        if (event.player != null && event.player instanceof EntityPlayerMP) {
            this.trigger((EntityPlayerMP) event.player, event.pickedUp.getItem());
        }
    }

    @SubscribeEvent
    public void onCrafted(PlayerEvent.ItemCraftedEvent event) {
        if (event.player != null && event.player instanceof EntityPlayerMP) {
            this.trigger((EntityPlayerMP) event.player, event.crafting);
        }
    }

    @SubscribeEvent
    public void onSmelted(PlayerEvent.ItemSmeltedEvent event) {
        if (event.player != null && event.player instanceof EntityPlayerMP) {
            this.trigger((EntityPlayerMP) event.player, event.smelting);
        }
    }

    public static class Instance extends AbstractCriterionInstance implements ICriterionInstanceTestable<ItemStack> {
        private final String modId;

        public Instance(ResourceLocation criterionIn, String modId) {
            super(criterionIn);
            this.modId = modId;
        }

        @Override
        public boolean test(EntityPlayerMP player, ItemStack itemStack) {
            return !itemStack.isEmpty()
                    && Item.REGISTRY.getNameForObject(itemStack.getItem()).getResourceDomain().equals(this.modId);
        }
    }

}

@@ -419,16 +419,85 @@
 tForge;%0A
+import net.minecraftforge.event.entity.player.EntityItemPickupEvent;%0A
 import n
@@ -1258,28 +1258,22 @@
 nPickup(
-PlayerEvent.
+Entity
 ItemPick
@@ -1299,38 +1299,49 @@
       if (event.
-p
+getEntityP
 layer
+()
  != null && even
@@ -1334,38 +1334,49 @@
 = null && event.
-p
+getEntityP
 layer
+()
  instanceof Enti
@@ -1437,30 +1437,41 @@
 ) event.
-p
+getEntityP
 layer
+()
 , event.
 pickedUp
@@ -1466,16 +1466,17 @@
 ent.
-pickedUp
+getItem()
 .get

Fix mod item obtain advancement criterion always triggering with an empty ItemStack