Skip to content

Commit 10b6012

Browse files
Add egg collection interaction to Chicken Coop block
Implement interaction for collecting eggs from the Chicken Coop block.
1 parent 8998e6d commit 10b6012

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

src/main/java/com/tcm/MineTale/block/ChickenCoopBlock.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,39 @@ private BlockPos calculateOffset(BlockPos origin, Direction facing, int x, int z
228228
.above(y);
229229
}
230230

231-
@Override
231+
@Override
232232
protected MapCodec<? extends HorizontalDirectionalBlock> codec() {
233233
return CODEC;
234234
}
235-
}
235+
236+
@Override
237+
protected ItemInteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
238+
if (level.isClientSide()) return ItemInteractionResult.SUCCESS;
239+
240+
Direction facing = state.getValue(FACING);
241+
CoopPart part = state.getValue(PART);
242+
243+
// 1. Find the Brain (Bottom Front Center)
244+
// We reverse the offset from the clicked part to find the origin (0,0,0)
245+
// Then we add the specific offset for the Bottom Front Center (1,0,0)
246+
BlockPos origin = pos.subtract(calculateOffset(BlockPos.ZERO, facing, part.getXOffset(), part.getZOffset(), part.getYOffset()));
247+
BlockPos brainPos = calculateOffset(origin, facing, 1, 0, 0);
248+
249+
if (level.getBlockEntity(brainPos) instanceof ChickenCoopBlockEntity be) {
250+
if (be.takeEgg() > 0) {
251+
// Give player the egg
252+
ItemStack eggStack = new ItemStack(Items.EGG);
253+
if (!player.getInventory().add(eggStack)) {
254+
player.drop(eggStack, false);
255+
}
256+
257+
// Visual/Sound feedback
258+
level.playSound(null, pos, SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2f, (level.random.nextFloat() - level.random.nextFloat()) * 0.7f + 1.2f);
259+
return ItemInteractionResult.CONSUME;
260+
}
261+
}
262+
263+
return ItemInteractionResult.PASS;
264+
}
265+
266+
}

0 commit comments

Comments
 (0)