From 631ed4b8fae08ba908418013a7c13d3cefae1cef Mon Sep 17 00:00:00 2001 From: MCZME <13183052+mczme@user.noreply.gitee.com> Date: Mon, 12 Aug 2024 11:32:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=EF=BC=9A=E5=88=80=E4=B8=8E?= =?UTF-8?q?=E7=A0=A7=E6=9D=BF=E4=BA=A4=E4=BA=92=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/block/ChoppingBoardBlock.java | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java b/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java index 7f3f6b9..f266c84 100644 --- a/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java +++ b/src/main/java/mczme/lingshi/common/block/ChoppingBoardBlock.java @@ -9,6 +9,7 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.ItemInteractionResult; +import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.BlockPlaceContext; @@ -62,27 +63,30 @@ public ItemInteractionResult useItemOn( ItemStack pStack, BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHitResult ) { if (pLevel.getBlockEntity(pPos) instanceof ChoppingBoardBlockEntity blockEntity) { - ItemStack left_stack = pPlayer.getItemInHand(InteractionHand.OFF_HAND); - if (blockEntity.getTheItem().isEmpty() && !pStack.isEmpty() && left_stack.isEmpty()) { - blockEntity.setTheItem(pStack.consumeAndReturn(1,pPlayer)); - blockEntity.setChanged(); - return ItemInteractionResult.SUCCESS; - } else if(blockEntity.getTheItem().isEmpty() && pStack.is(NeoforgeTags.KNIFE) && !left_stack.isEmpty()){ - blockEntity.setTheItem(left_stack.consumeAndReturn(1,pPlayer)); - blockEntity.setChanged(); - return ItemInteractionResult.SUCCESS; - }else if(!blockEntity.getTheItem().isEmpty()&& !pStack.isEmpty()) { + if (!pStack.isEmpty()) { + if (pStack.is(NeoforgeTags.KNIFE) && !blockEntity.getTheItem().isEmpty()) { List stacks = blockEntity.getRecipeAndResult(pPlayer.getMainHandItem()); - if(stacks == null||stacks.isEmpty()) { + if (stacks == null || stacks.isEmpty()) { return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } stacks.forEach(itemStack -> Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), itemStack)); blockEntity.setTheItem(ItemStack.EMPTY); + if(!pLevel.isClientSide()){ + pStack.hurtAndBreak(1, pPlayer, EquipmentSlot.MAINHAND); + } + blockEntity.setChanged(); + return ItemInteractionResult.SUCCESS; + } else if (pStack.is(NeoforgeTags.KNIFE) && blockEntity.getTheItem().isEmpty() && !pPlayer.getItemInHand(InteractionHand.OFF_HAND).isEmpty()) { + blockEntity.setTheItem(pPlayer.getItemInHand(InteractionHand.OFF_HAND).consumeAndReturn(1, pPlayer)); + blockEntity.setChanged(); + return ItemInteractionResult.SUCCESS; + } else if (blockEntity.getTheItem().isEmpty()) { + blockEntity.setTheItem(pStack.consumeAndReturn(1, pPlayer)); blockEntity.setChanged(); return ItemInteractionResult.SUCCESS; - }else { - return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } + } + return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } return ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION; @@ -91,13 +95,13 @@ public ItemInteractionResult useItemOn( @Override protected InteractionResult useWithoutItem(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, BlockHitResult pHitResult) { if (pLevel.getBlockEntity(pPos) instanceof ChoppingBoardBlockEntity blockentity) { - ItemStack stack = blockentity.getTheItem(); - if (!stack.isEmpty()) { - Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), stack); - blockentity.setTheItem(ItemStack.EMPTY); - blockentity.setChanged(); - return InteractionResult.SUCCESS; - } + ItemStack stack = blockentity.getTheItem(); + if (!stack.isEmpty()) { + Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY(), pPos.getZ(), stack); + blockentity.setTheItem(ItemStack.EMPTY); + blockentity.setChanged(); + return InteractionResult.SUCCESS; + } } return InteractionResult.PASS; }