Use Datapack Loot tables for BaseOrBlocks (paulevsGitch/BetterNether#486)

This commit is contained in:
Frank 2022-01-17 22:14:31 +01:00
parent 611fb4d50c
commit 03635584d6
2 changed files with 38 additions and 1 deletions

View file

@ -20,10 +20,12 @@ import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import ru.bclib.interfaces.BlockModelProvider;
import ru.bclib.util.LootUtil;
import ru.bclib.util.MHelper;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
public class BaseOreBlock extends OreBlock implements BlockModelProvider {
@ -66,7 +68,11 @@ public class BaseOreBlock extends OreBlock implements BlockModelProvider {
@Override
@SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
return getDroppedItems(this, dropItem.get(), maxCount, minCount, state, builder);
return LootUtil
.getDrops(this, state, builder)
.orElseGet(
()->BaseOreBlock.getDroppedItems(this, dropItem.get(), maxCount, minCount, state, builder)
);
}
public static List<ItemStack> getDroppedItems(ItemLike block, Item dropItem, int maxCount, int minCount, BlockState state, LootContext.Builder builder) {

View file

@ -0,0 +1,31 @@
package ru.bclib.util;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import java.util.List;
import java.util.Optional;
public class LootUtil {
public static Optional<List<ItemStack>> getDrops(BlockBehaviour block, BlockState state, LootContext.Builder builder){
ResourceLocation tableID = block.getLootTable();
if (tableID == BuiltInLootTables.EMPTY) {
return Optional.empty();
}
final LootContext ctx = builder.withParameter(LootContextParams.BLOCK_STATE, state).create(LootContextParamSets.BLOCK);
final ServerLevel level = ctx.getLevel();
final LootTable table = level.getServer().getLootTables().get(tableID);
if (table == LootTable.EMPTY) return Optional.empty();
return Optional.of(table.getRandomItems(ctx));
}
}