2020-03-06 23:35:29 +00:00
|
|
|
package com.thebrokenrail.sorcerycraft.spell.util;
|
2020-03-01 18:19:59 +00:00
|
|
|
|
2020-03-08 17:45:15 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
|
2020-03-06 23:35:29 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.spell.registry.SpellRegistry;
|
2020-03-01 18:19:59 +00:00
|
|
|
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;
|
2020-03-03 23:08:22 +00:00
|
|
|
import net.minecraft.util.Identifier;
|
2020-03-01 18:19:59 +00:00
|
|
|
|
2020-03-02 22:11:37 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
2020-03-01 18:19:59 +00:00
|
|
|
public class RandomSpellLootTableFunction extends ConditionalLootFunction {
|
|
|
|
private RandomSpellLootTableFunction(LootCondition[] conditions) {
|
|
|
|
super(conditions);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack process(ItemStack stack, LootContext context) {
|
2020-03-03 23:08:22 +00:00
|
|
|
double chance = 1.0d;
|
|
|
|
while (!(context.getRandom().nextDouble() > chance)) {
|
|
|
|
Spell[] spells = SpellRegistry.getSpells();
|
|
|
|
int index = context.getRandom().nextInt(spells.length);
|
|
|
|
Map<Identifier, Integer> spell = SpellTag.getSpells(stack);
|
|
|
|
spell.put(spells[index].getID(), spells[index].getLevel());
|
|
|
|
SpellTag.setSpells(stack, spell);
|
|
|
|
chance = chance * 0.25d;
|
|
|
|
}
|
2020-03-01 18:19:59 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|