i do not understand loot modifiers

This commit is contained in:
gamma-delta 2022-01-19 18:31:33 -06:00
parent 2d00a0aa16
commit 1391daf8bc
7 changed files with 55 additions and 124 deletions

View file

@ -26,7 +26,6 @@ a354a2dc83d220c43d4c9f156059cbd8255e9e19 assets/hex/models/item/spellbook.json
eaddd1e5ae74293d784748e56e4a238a7607e34c assets/hex/models/item/trinket.json
f0cce957861ee854cbd68984015da2c530c9ef6d assets/hex/models/item/trinket_filled.json
837e7ed749afa44bd3be4114c7d81f3b8fe47852 assets/hex/models/item/wand.json
2ac42506f03235f4335dd62f0d6a0c4f23ca61b9 data/forge/loot_modifiers/global_loot_modifiers.json
40eee88282c6d02518aab36a1da1d2f7d5c0a051 data/hex/advancements/aaa_wasteful_cast.json
f6d43f5b05b88aeebb1529aa700cc3dcc24bbc32 data/hex/advancements/aab_big_cast.json
9c6cc95ec58d62db530e5f717f130249c9a2e476 data/hex/advancements/enlightenment.json
@ -38,7 +37,6 @@ f6d43f5b05b88aeebb1529aa700cc3dcc24bbc32 data/hex/advancements/aab_big_cast.json
e27f5d32775d3965f03db2c6e58f6f5da78b62df data/hex/advancements/recipes/hex/trinket.json
2bcb08b33e9cbc9d17285c0eff9ce4f7f0f35159 data/hex/advancements/recipes/hex/wand.json
31e2e34bcb1ff2294af8a44ff9942d0bd8d9a765 data/hex/advancements/root.json
a46281d65532086be7fbb63beb8c515edf2baaee data/hex/loot_modifiers/amethyst_cluster.json
247829c255cc76b9f82d60ce057e8cf5bea0dc84 data/hex/recipes/artifact.json
b438e051fc58c58c93dddf88ea858cea6ae9fc13 data/hex/recipes/cypher.json
d31bc9597a6320a886a059579edc610b24ad225b data/hex/recipes/focus.json

View file

@ -1,6 +0,0 @@
{
"replace": false,
"entries": [
"hex:amethyst_cluster"
]
}

View file

@ -1,23 +0,0 @@
{
"conditions": [
{
"condition": "forge:loot_table_id",
"loot_table_id": "minecraft:blocks/amethyst_cluster"
},
{
"condition": "minecraft:inverted",
"term": {
"condition": "minecraft:match_tool",
"predicate": {
"enchantments": [
{
"enchantment": "minecraft:silk_touch"
}
]
}
}
}
],
"chargedChance": 0.95,
"type": "hex:amethyst_cluster"
}

View file

@ -8,7 +8,6 @@ import at.petrak.hex.common.lib.HexStatistics;
import at.petrak.hex.common.lib.LibCapabilities;
import at.petrak.hex.common.network.HexMessages;
import at.petrak.hex.datagen.Advancements;
import at.petrak.hex.datagen.LootModifiers;
import at.petrak.hex.server.TickScheduler;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
@ -41,7 +40,6 @@ public class HexMod {
evbus.register(HexMod.class);
HexItems.ITEMS.register(evbus);
LootModifiers.LOOT_MODS.register(evbus);
HexSounds.SOUNDS.register(evbus);
MinecraftForge.EVENT_BUS.register(TickScheduler.INSTANCE);

View file

@ -19,7 +19,6 @@ public class DataGenerators {
gen.addProvider(new Recipes(gen));
}
// On both sides
gen.addProvider(new LootModifiers(gen));
gen.addProvider(new Advancements(gen, efh));
}
}

View file

@ -1,90 +0,0 @@
package at.petrak.hex.datagen;
import at.petrak.hex.HexMod;
import at.petrak.hex.common.items.HexItems;
import com.google.gson.JsonObject;
import net.minecraft.advancements.critereon.EnchantmentPredicate;
import net.minecraft.advancements.critereon.ItemPredicate;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.data.DataGenerator;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.MatchTool;
import net.minecraftforge.common.data.GlobalLootModifierProvider;
import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
import net.minecraftforge.common.loot.LootModifier;
import net.minecraftforge.common.loot.LootTableIdCondition;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class LootModifiers extends GlobalLootModifierProvider {
public static final DeferredRegister<GlobalLootModifierSerializer<?>> LOOT_MODS = DeferredRegister.create(
ForgeRegistries.LOOT_MODIFIER_SERIALIZERS, HexMod.MOD_ID);
private static final RegistryObject<AmethystClusterModifier.Serializer> AMETHYST_CLUSTER = LOOT_MODS.register(
"amethyst_cluster", AmethystClusterModifier.Serializer::new);
public LootModifiers(DataGenerator gen) {
super(gen, HexMod.MOD_ID);
}
@Override
protected void start() {
add("amethyst_cluster", AMETHYST_CLUSTER.get(), new AmethystClusterModifier(new LootItemCondition[]{
LootTableIdCondition.builder(new ResourceLocation("minecraft:blocks/amethyst_cluster")).build(),
MatchTool.toolMatches(
ItemPredicate.Builder.item().hasEnchantment(
new EnchantmentPredicate(Enchantments.SILK_TOUCH, MinMaxBounds.Ints.ANY)))
.invert().build(),
}, 0.95f));
}
public static final class AmethystClusterModifier extends LootModifier {
private final float chargedChance;
public AmethystClusterModifier(LootItemCondition[] conditions, float chargedChance) {
super(conditions);
this.chargedChance = chargedChance;
}
@NotNull
@Override
protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
var rand = context.getRandom();
var tool = context.getParamOrNull(LootContextParams.TOOL);
var fortuneLevel = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, tool);
var hasCharged = (rand.nextFloat() * (fortuneLevel / 2f + 1)) > this.chargedChance;
var dustCount = hasCharged ? 0 : 1 + Math.round(rand.nextFloat() * fortuneLevel);
generatedLoot.add(new ItemStack(HexItems.AMETHYST_DUST.get(), dustCount));
generatedLoot.add(new ItemStack(HexItems.CHARGED_AMETHYST.get(), hasCharged ? 1 : 0));
return generatedLoot;
}
private static class Serializer extends GlobalLootModifierSerializer<AmethystClusterModifier> {
@Override
public AmethystClusterModifier read(ResourceLocation location, JsonObject object,
LootItemCondition[] conditions) {
var chargedChance = GsonHelper.getAsFloat(object, "chargedChance");
return new AmethystClusterModifier(conditions, chargedChance);
}
@Override
public JsonObject write(AmethystClusterModifier instance) {
var obj = makeConditions(instance.conditions);
obj.addProperty("chargedChance", instance.chargedChance);
return obj;
}
}
}
}

View file

@ -0,0 +1,55 @@
{
"pools": [
{
"name": "pool1",
"rolls": 1,
"entries": [
{
"type": "minecraft:alternatives",
"children": [
{
"type": "minecraft:item",
"name": "hex:charged_amethyst",
"conditions": [
{
"condition": "minecraft:alternative",
"terms": [
{
"type": "minecraft:random_chance",
"chance": 0.1
},
{
"type": "minecraft:table_bonus",
"enchantment": "minecraft:fortune",
"chances": [
0.25,
0.5,
0.75
]
}
]
}
]
},
{
"type": "minecraft:item",
"name": "hex:amethyst_dust",
"functions": [
{
"function": "minecraft:set_count",
"count": 2,
"add": true
},
{
"function": "minecraft:apply_bonus",
"enchantment": "minecraft:fortune",
"formula": "minecraft:ore_drops"
}
]
}
]
}
]
}
]
}