This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
SorceryCraft/src/main/java/com/thebrokenrail/sorcerycraft/spell/util/RandomSpellLootTableFunctio...

43 lines
1.6 KiB
Java

package com.thebrokenrail.sorcerycraft.spell.util;
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
import com.thebrokenrail.sorcerycraft.spell.api.registry.SpellRegistry;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.condition.LootCondition;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.function.ConditionalLootFunction;
import net.minecraft.loot.function.LootFunction;
import net.minecraft.util.Identifier;
import java.util.Map;
public class RandomSpellLootTableFunction extends ConditionalLootFunction {
private RandomSpellLootTableFunction(LootCondition[] conditions) {
super(conditions);
}
public ItemStack process(ItemStack stack, LootContext context) {
double chance = 1.0d;
while (!(context.getRandom().nextDouble() > chance)) {
Spell[] spells = SpellRegistry.getSpells();
int index = context.getRandom().nextInt(spells.length);
Map<Identifier, Integer> spell = SpellHelper.getSpells(stack);
spell.put(spells[index].getID(), spells[index].getLevel());
SpellHelper.setSpells(stack, spell);
chance = chance * 0.25d;
}
return stack;
}
public static class Builder extends ConditionalLootFunction.Builder<RandomSpellLootTableFunction.Builder> {
@Override
protected RandomSpellLootTableFunction.Builder getThisBuilder() {
return this;
}
public LootFunction build() {
return new RandomSpellLootTableFunction(getConditions());
}
}
}