Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 105
src/main/java/com/example/piglinpearls/PiglinPearls.java
package com.example.piglinpearls;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.functions.SetItemCountFunction;
import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProviders;
public class PiglinPearls implements ModInitializer {
public static final String MOD_ID = "piglinpearls";
// Vanilla barter gives an ender pearl with weight 10 out of 469 (p = 10/469).
// An extra independent roll with chance q adds q * (1 - p), so q = 0.6p / (1 - p) = 2/153
// makes the total pearl chance exactly 1.6p.
private static final float BONUS_PEARL_CHANCE = 2.0F / 153.0F;
@Override
public void onInitialize() {
LootTableEvents.MODIFY.register((key, tableBuilder, source, holder) -> {
if (key.equals(BuiltInLootTables.PIGLIN_BARTERING)) {
tableBuilder.withPool(LootPool.lootPool()
.setRolls(ContextIntProviders.exactly(1))
.when(LootItemRandomChanceCondition.randomChance(BONUS_PEARL_CHANCE))
.add(LootItem.lootTableItem(Items.ENDER_PEARL)
.apply(SetItemCountFunction.setCount(ContextIntProviders.between(2, 4)))));
}
});
}
}