64 lines
2.4 KiB
Java
64 lines
2.4 KiB
Java
package com.thebrokenrail.sorcerycraft.spell.util;
|
|
|
|
import com.google.gson.JsonDeserializationContext;
|
|
import com.google.gson.JsonObject;
|
|
import com.thebrokenrail.sorcerycraft.SorceryCraft;
|
|
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.loot.function.LootFunctionType;
|
|
import net.minecraft.util.Identifier;
|
|
import net.minecraft.util.registry.Registry;
|
|
|
|
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;
|
|
}
|
|
|
|
@Override
|
|
public LootFunctionType getType() {
|
|
return Registry.LOOT_FUNCTION_TYPE.get(new Identifier(SorceryCraft.NAMESPACE, "random_spell"));
|
|
}
|
|
|
|
public static class Factory extends ConditionalLootFunction.Serializer<RandomSpellLootTableFunction> {
|
|
public Factory() {
|
|
super();
|
|
}
|
|
|
|
@Override
|
|
public RandomSpellLootTableFunction fromJson(JsonObject json, JsonDeserializationContext context, LootCondition[] conditions) {
|
|
return (RandomSpellLootTableFunction) new com.thebrokenrail.sorcerycraft.spell.util.RandomSpellLootTableFunction.Builder().build();
|
|
}
|
|
}
|
|
|
|
public static class Builder extends ConditionalLootFunction.Builder<RandomSpellLootTableFunction.Builder> {
|
|
@Override
|
|
protected RandomSpellLootTableFunction.Builder getThisBuilder() {
|
|
return this;
|
|
}
|
|
|
|
public LootFunction build() {
|
|
return new RandomSpellLootTableFunction(getConditions());
|
|
}
|
|
}
|
|
}
|