From 03635584d60106b2dfd3faeb7d34236079081ac7 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 17 Jan 2022 22:14:31 +0100 Subject: [PATCH] Use Datapack Loot tables for `BaseOrBlock`s (paulevsGitch/BetterNether#486) --- .../java/ru/bclib/blocks/BaseOreBlock.java | 8 ++++- src/main/java/ru/bclib/util/LootUtil.java | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/bclib/util/LootUtil.java diff --git a/src/main/java/ru/bclib/blocks/BaseOreBlock.java b/src/main/java/ru/bclib/blocks/BaseOreBlock.java index c899efcf..6d6dae0e 100644 --- a/src/main/java/ru/bclib/blocks/BaseOreBlock.java +++ b/src/main/java/ru/bclib/blocks/BaseOreBlock.java @@ -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 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 getDroppedItems(ItemLike block, Item dropItem, int maxCount, int minCount, BlockState state, LootContext.Builder builder) { diff --git a/src/main/java/ru/bclib/util/LootUtil.java b/src/main/java/ru/bclib/util/LootUtil.java new file mode 100644 index 00000000..05cf3cb9 --- /dev/null +++ b/src/main/java/ru/bclib/util/LootUtil.java @@ -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> 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)); + } +}