Skip to content

Commit 7e62858

Browse files
fix: block now drops one item
1 parent ad2ac0f commit 7e62858

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

src/client/java/com/tcm/MineTale/datagen/ModLootTableProvider.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
package com.tcm.MineTale.datagen;
22

3+
import com.tcm.MineTale.block.workbenches.AbstractWorkbench;
34
import com.tcm.MineTale.registry.ModBlocks;
45
import com.tcm.MineTale.registry.ModItems;
56
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
67
import net.fabricmc.fabric.api.datagen.v1.provider.FabricBlockLootTableProvider;
8+
import net.minecraft.advancements.criterion.StatePropertiesPredicate;
79
import net.minecraft.core.HolderLookup;
810
import net.minecraft.core.registries.Registries;
911
import net.minecraft.world.item.Item;
1012
import net.minecraft.world.item.enchantment.Enchantment;
1113
import net.minecraft.world.item.enchantment.Enchantments;
1214
import net.minecraft.world.level.block.Block;
15+
import net.minecraft.world.level.block.state.properties.ChestType;
16+
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
17+
import net.minecraft.world.level.storage.loot.LootPool;
1318
import net.minecraft.world.level.storage.loot.LootTable;
1419
import net.minecraft.world.level.storage.loot.entries.LootItem;
1520
import net.minecraft.world.level.storage.loot.entries.LootPoolSingletonContainer;
1621
import net.minecraft.world.level.storage.loot.functions.ApplyBonusCount;
1722
import net.minecraft.world.level.storage.loot.functions.SetItemCountFunction;
23+
import net.minecraft.world.level.storage.loot.predicates.ExplosionCondition;
24+
import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition;
1825
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue;
1926
import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator;
2027

@@ -30,11 +37,26 @@ public void generate() {
3037
///Block Drops Itself
3138
//Bug!! Workbenches currently drop items based on the amount of blocks
3239
//the multiblock is. For example the Furnaces currently drop 4 furnaces.
33-
dropSelf(ModBlocks.FURNACE_WORKBENCH_BLOCK_T1);
34-
dropSelf(ModBlocks.FURNACE_WORKBENCH_BLOCK_T1);
40+
// dropSelf(ModBlocks.FURNACE_WORKBENCH_BLOCK_T1);
41+
// dropSelf(ModBlocks.FURNACE_WORKBENCH_BLOCK_T1);
3542
dropSelf(ModBlocks.ARMORERS_WORKBENCH_BLOCK);
3643
dropSelf(ModBlocks.CAMPFIRE_WORKBENCH_BLOCK);
3744
dropSelf(ModBlocks.WORKBENCH_WORKBENCH_BLOCK);
45+
46+
this.add(ModBlocks.FURNACE_WORKBENCH_BLOCK_T1,
47+
LootTable.lootTable() // Use the static factory method to start the builder
48+
.withPool(LootPool.lootPool()
49+
.setRolls(ConstantValue.exactly(1.0F))
50+
.add(LootItem.lootTableItem(ModBlocks.FURNACE_WORKBENCH_BLOCK_T1))
51+
.when(LootItemBlockStatePropertyCondition.hasBlockStateProperties(ModBlocks.FURNACE_WORKBENCH_BLOCK_T1)
52+
.setProperties(StatePropertiesPredicate.Builder.properties()
53+
.hasProperty(AbstractWorkbench.HALF, DoubleBlockHalf.LOWER)
54+
.hasProperty(AbstractWorkbench.TYPE, ChestType.LEFT)
55+
)
56+
)
57+
.when(ExplosionCondition.survivesExplosion())
58+
)
59+
);
3860
}
3961

4062

src/main/java/com/tcm/MineTale/block/workbenches/AbstractWorkbench.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.minecraft.world.level.block.state.BlockState;
2424
import net.minecraft.world.level.block.state.StateDefinition;
2525
import net.minecraft.world.level.block.state.properties.*;
26+
import net.minecraft.world.level.gameevent.GameEvent;
2627
import net.minecraft.world.phys.BlockHitResult;
2728
import net.minecraft.world.phys.shapes.Shapes;
2829
import net.minecraft.world.phys.shapes.VoxelShape;
@@ -33,6 +34,8 @@
3334

3435
import java.util.function.Supplier;
3536

37+
// DoorBlock
38+
3639
public abstract class AbstractWorkbench<E extends AbstractWorkbenchEntity> extends BaseEntityBlock {
3740
public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
3841
public static final EnumProperty<DoubleBlockHalf> HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
@@ -351,4 +354,52 @@ protected float getShadeBrightness(BlockState state, BlockGetter level, BlockPos
351354
// We want 1.0F to ensure the block doesn't cast a pitch-black shadow on itself.
352355
return 1.0F;
353356
}
357+
358+
@Override
359+
public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
360+
if (!level.isClientSide()) {
361+
BlockPos masterPos = getMasterPos(state, pos);
362+
boolean isMaster = pos.equals(masterPos);
363+
364+
// Iterate through the entire 2x2 or 1x2 structure
365+
for (int y = 0; y <= (isTall ? 1 : 0); y++) {
366+
for (int x = 0; x <= (isWide ? 1 : 0); x++) {
367+
Direction facing = state.getValue(FACING);
368+
BlockPos targetPos = masterPos.above(y).relative(facing.getClockWise(), x);
369+
370+
// Skip the block the player is currently mining
371+
if (targetPos.equals(pos)) continue;
372+
373+
BlockState targetState = level.getBlockState(targetPos);
374+
if (targetState.is(this) && getMasterPos(targetState, targetPos).equals(masterPos)) {
375+
if (isMaster) {
376+
// If we are mining the Master, destroy others SILENTLY
377+
level.setBlock(targetPos, Blocks.AIR.defaultBlockState(), 35);
378+
} else {
379+
// If we are mining a Slave block, we need to handle the Master carefully.
380+
// If the Master is at this targetPos, destroy it WITH drops.
381+
if (targetPos.equals(masterPos)) {
382+
level.destroyBlock(targetPos, !player.isCreative());
383+
} else {
384+
// Otherwise, it's just another slave block, remove silently.
385+
level.setBlock(targetPos, Blocks.AIR.defaultBlockState(), 35);
386+
}
387+
}
388+
level.gameEvent(GameEvent.BLOCK_DESTROY, targetPos, GameEvent.Context.of(player, targetState));
389+
}
390+
}
391+
}
392+
393+
// Final tweak: If the player is mining a SLAVE block,
394+
// we must prevent the slave block itself from dropping.
395+
if (!isMaster && !player.isCreative()) {
396+
// This prevents the current block from dropping its loot table
397+
// because we already triggered the Master's drop above.
398+
state = state.setValue(BlockStateProperties.LIT, false); // Optional: change state to desync loot
399+
level.setBlock(pos, Blocks.AIR.defaultBlockState(), 35);
400+
}
401+
}
402+
403+
return super.playerWillDestroy(level, pos, state, player);
404+
}
354405
}

0 commit comments

Comments
 (0)