package org.cyclops.everlastingabilities.loot.functions;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.functions.LootItemConditionalFunction;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import org.cyclops.cyclopscore.helper.LootHelpers;
import org.cyclops.everlastingabilities.Reference;
import org.cyclops.everlastingabilities.ability.AbilityHelpers;
import org.cyclops.everlastingabilities.api.Ability;
import org.cyclops.everlastingabilities.api.IAbilityType;
import org.cyclops.everlastingabilities.capability.MutableAbilityStoreConfig;

import java.util.List;

/**
 * A loot function that sets a random into an item.
 * @author rubensworks
 */
public class LootFunctionSetRandomAbility extends LootItemConditionalFunction {
    public static final LootItemFunctionType TYPE = LootHelpers.registerFunction(new ResourceLocation(Reference.MOD_ID, "set_random_ability"), new LootFunctionSetRandomAbility.Serializer());

    public LootFunctionSetRandomAbility(LootItemCondition[] conditionsIn) {
        super(conditionsIn);
    }

    @Override
    public ItemStack run(ItemStack stack, LootContext context) {
        List<IAbilityType> abilityTypes = AbilityHelpers.getAbilityTypesLoot();
        Rarity rarity = AbilityHelpers.getRandomRarity(abilityTypes, context.getRandom());
        IAbilityType abilityType = AbilityHelpers.getRandomAbility(abilityTypes, context.getRandom(), rarity).get(); // Should always be present, as the method above guarantees that

        stack.getCapability(MutableAbilityStoreConfig.CAPABILITY, null)
                .ifPresent(mutableAbilityStore -> mutableAbilityStore.addAbility(new Ability(abilityType, 1), true));
        return stack;
    }

    @Override
    public LootItemFunctionType getType() {
        return TYPE;
    }

    public static void load() {
        // Dummy call, to enforce class loading
    }

    public static class Serializer extends LootItemConditionalFunction.Serializer<LootFunctionSetRandomAbility> {
        @Override
        public void serialize(JsonObject object, LootFunctionSetRandomAbility functionClazz, JsonSerializationContext serializationContext) {

        }

        @Override
        public LootFunctionSetRandomAbility deserialize(JsonObject object, JsonDeserializationContext deserializationContext, LootItemCondition[] conditionsIn) {
            return new LootFunctionSetRandomAbility(conditionsIn);
        }
    }

}

@@ -178,32 +178,81 @@
 izationContext;%0A
+import net.minecraft.resources.ResourceLocation;%0A
 import net.minec
@@ -322,135 +322,8 @@
 ty;%0A
-import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType;%0Aimport net.minecraft.resources.ResourceLocation;%0A
 impo
@@ -458,24 +458,102 @@
 alFunction;%0A
+import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType;%0A
 import net.m
@@ -1538,24 +1538,42 @@
  context) %7B%0A
+        try %7B%0A    
         List
@@ -1640,16 +1640,20 @@
 Loot();%0A
+    
         
@@ -1743,16 +1743,20 @@
         
+  
+  
 IAbility
@@ -1930,16 +1930,20 @@
         
+    
 stack.ge
@@ -2014,16 +2014,20 @@
         
+  
+  
 .ifPrese
@@ -2124,24 +2124,28 @@
 ));%0A        
+    
 return stack
@@ -2146,16 +2146,106 @@
  stack;%0A
+        %7D catch (IllegalStateException e) %7B%0A            return ItemStack.EMPTY;%0A        %7D%0A
     %7D%0A%0A 

Fix console error when disabling all abilities as loot