Skip to content

Commit

Permalink
fix: switch to loot tables for nbt dropping blocks
Browse files Browse the repository at this point in the history
Closes #1121
  • Loading branch information
klikli-dev committed Jun 3, 2024
1 parent 7b079d7 commit 710feee
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,50 @@
"pools": [
{
"bonus_rolls": 0.0,
"entries": [],
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
],
"entries": [
{
"type": "minecraft:item",
"functions": [
{
"function": "minecraft:copy_nbt",
"ops": [
{
"op": "replace",
"source": "linkedStorageControllerPosition",
"target": "BlockEntityTag.linkedStorageControllerPosition"
},
{
"op": "replace",
"source": "sortDirection",
"target": "BlockEntityTag.sortDirection"
},
{
"op": "replace",
"source": "sortType",
"target": "BlockEntityTag.sortType"
},
{
"op": "replace",
"source": "matrix",
"target": "BlockEntityTag.matrix"
},
{
"op": "replace",
"source": "orderStack",
"target": "BlockEntityTag.orderStack"
}
],
"source": "block_entity"
}
],
"name": "occultism:stable_wormhole"
}
],
"rolls": 1.0
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,55 @@
"pools": [
{
"bonus_rolls": 0.0,
"entries": [],
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
],
"entries": [
{
"type": "minecraft:item",
"functions": [
{
"function": "minecraft:copy_nbt",
"ops": [
{
"op": "replace",
"source": "items",
"target": "BlockEntityTag.items"
},
{
"op": "replace",
"source": "sortDirection",
"target": "BlockEntityTag.sortDirection"
},
{
"op": "replace",
"source": "sortType",
"target": "BlockEntityTag.sortType"
},
{
"op": "replace",
"source": "maxSlots",
"target": "BlockEntityTag.maxSlots"
},
{
"op": "replace",
"source": "matrix",
"target": "BlockEntityTag.matrix"
},
{
"op": "replace",
"source": "orderStack",
"target": "BlockEntityTag.orderStack"
}
],
"source": "block_entity"
}
],
"name": "occultism:storage_controller"
}
],
"rolls": 1.0
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState n
}
}
}
BlockEntityUtil.onBlockChangeDropWithNbt(this, state, worldIn, pos, newState);
super.onRemove(state, worldIn, pos, newState, isMoving);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ public boolean isPathfindable(BlockState pState, BlockGetter pLevel, BlockPos pP
return super.isPathfindable(pState, pLevel, pPos, pType);
}

@Override
@SuppressWarnings("deprecation")
public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
BlockEntityUtil.onBlockChangeDropWithNbt(this, state, worldIn, pos, newState);
super.onRemove(state, worldIn, pos, newState, isMoving);
}

@Override
@SuppressWarnings("deprecation")
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.klikli_dev.occultism.common.block.otherworld.IOtherworldBlock;
import com.klikli_dev.occultism.registry.OccultismBlocks;
import com.klikli_dev.occultism.registry.OccultismItems;
import com.klikli_dev.theurgy.registry.BlockRegistry;
import com.mojang.datafixers.util.Pair;
import net.minecraft.advancements.critereon.StatePropertiesPredicate;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.loot.BlockLootSubProvider;
Expand All @@ -19,14 +21,17 @@
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.functions.CopyNbtFunction;
import net.minecraft.world.level.storage.loot.functions.SetItemCountFunction;
import net.minecraft.world.level.storage.loot.predicates.BonusLevelTableCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.providers.nbt.ContextNbtProvider;
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue;
import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator;
import net.neoforged.neoforge.registries.DeferredHolder;

import java.util.Arrays;
import java.util.Set;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -81,6 +86,24 @@ else if (settings.lootTableType == OccultismBlocks.LootTableType.OTHERWORLD_BLOC
this.add(OccultismBlocks.SILVER_ORE.get(), this.createOreDrop(OccultismBlocks.SILVER_ORE.get(), OccultismItems.RAW_SILVER.get()));
this.add(OccultismBlocks.SILVER_ORE_DEEPSLATE.get(), this.createOreDrop(OccultismBlocks.SILVER_ORE_DEEPSLATE.get(), OccultismItems.RAW_SILVER.get()));
this.add(OccultismBlocks.IESNIUM_ORE.get(), this.createOreDrop(OccultismBlocks.IESNIUM_ORE.get(), OccultismItems.RAW_IESNIUM.get()));

this.dropSelfWithNbt(OccultismBlocks.STORAGE_CONTROLLER.get(),
"items",
"sortDirection",
"sortType",
"maxSlots",
"matrix",
"orderStack"
);


this.dropSelfWithNbt(OccultismBlocks.STABLE_WORMHOLE.get(),
"linkedStorageControllerPosition",
"sortDirection",
"sortType",
"matrix",
"orderStack"
);
}

protected void registerOtherworldBlockTable(Block block) {
Expand Down Expand Up @@ -135,4 +158,36 @@ protected LootTable.Builder createOtherworldLeavesDrops(Block leavesBlock, Block
public void registerDropNothingLootTable(Block block) {
this.add(block, LootTable.lootTable().withPool(LootPool.lootPool()));
}

@SuppressWarnings("unchecked")
protected final void dropSelfWithNbt(Block pBlock, String... sourcePaths) {
this.dropSelfWithNbt(pBlock, Arrays.stream(sourcePaths).map(s -> Pair.of(s, "BlockEntityTag." + s)).toArray(Pair[]::new));
}

@SafeVarargs
protected final void dropSelfWithNbt(Block pBlock, Pair<String, String>... sourceTargetPathPairs) {
this.add(pBlock, this.createSelfWithNbtDrop(pBlock, this.copyData(sourceTargetPathPairs)));
}

protected LootTable.Builder createSelfWithNbtDrop(Block pBlock, CopyNbtFunction.Builder data) {
return LootTable.lootTable()
.withPool(
this.applyExplosionCondition(pBlock,
LootPool.lootPool().setRolls(ConstantValue.exactly(1.0F)).add(
LootItem.lootTableItem(pBlock)
.apply(
data
)
)
)
);
}

protected CopyNbtFunction.Builder copyData(Pair<String, String>... sourceTargetPathPairs) {
var builder = CopyNbtFunction.copyData(ContextNbtProvider.BLOCK_ENTITY);
for (var pair : sourceTargetPathPairs) {
builder.copy(pair.getFirst(), pair.getSecond());
}
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public void tick(BlockState pState, ServerLevel pLevel, BlockPos pPos, RandomSou
Block.Properties.of()
.mapColor(MapColor.STONE)
.sound(SoundType.STONE)
.strength(5f, 100).noOcclusion()), false, LootTableType.EMPTY);
.strength(5f, 100).noOcclusion()), false, LootTableType.CUSTOM);

public static final DeferredBlock<StorageStabilizerBlock> STORAGE_STABILIZER_TIER1 = register(
"storage_stabilizer_tier1", () -> new StorageStabilizerBlock(
Block.Properties.of()
Expand Down Expand Up @@ -216,7 +217,7 @@ public void tick(BlockState pState, ServerLevel pLevel, BlockPos pPos, RandomSou
Block.Properties.of()
.mapColor(MapColor.STONE)
.sound(SoundType.STONE).noCollission()
.strength(2f, 2).noOcclusion()), false, LootTableType.EMPTY);
.strength(2f, 2).noOcclusion()), false, LootTableType.CUSTOM);

public static final DeferredBlock<DimensionalMineshaftBlock> DIMENSIONAL_MINESHAFT =
register("dimensional_mineshaft", () -> new DimensionalMineshaftBlock(
Expand Down

0 comments on commit 710feee

Please sign in to comment.