From 037162ba9108bb0cdfd9ee39e619deecc658c567 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Fri, 28 Jun 2024 18:54:40 +0800 Subject: [PATCH] fix: fix block interacting and spawn egg --- .../component/player/EntityPlayerBaseComponent.java | 4 ++-- .../api/item/component/common/ItemBaseComponent.java | 9 +++++++++ .../component/player/EntityPlayerBaseComponentImpl.java | 2 +- .../processor/InventoryTransactionPacketProcessor.java | 9 +++++++-- .../network/processor/PlayerActionPacketProcessor.java | 8 ++++---- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java b/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java index 48aa20600..c9b8a29a6 100644 --- a/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java +++ b/Allay-API/src/main/java/org/allaymc/api/entity/component/player/EntityPlayerBaseComponent.java @@ -42,9 +42,9 @@ public interface EntityPlayerBaseComponent extends EntityBaseComponent, ChunkLoa void setCrawling(boolean crawling); - boolean isInteractingBlock(); + boolean isUsingItem(); - void setInteractingBlock(boolean interactingBlock); + void setUsingItem(boolean usingItemOnBlock); int getHandSlot(); diff --git a/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java b/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java index 9f5cbb445..9cf738aea 100644 --- a/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java +++ b/Allay-API/src/main/java/org/allaymc/api/item/component/common/ItemBaseComponent.java @@ -91,9 +91,18 @@ default void clearStackNetworkId() { void loadExtraTag(NbtMap extraTag); + /** + * Called when player right-click a block no matter the return value of player.isUsingItem() + * @param dimension The dimension the player is in + * @param placeBlockPos The position of the block being right-clicked + * @param interactInfo Information about the interaction + */ + default void rightClickItemOn(Dimension dimension, Vector3ic placeBlockPos, PlayerInteractInfo interactInfo) {} + /** * Attempt to use this item on a block. *

+ * This method will be called only when client think "he can" use the item. In other words, when player.isUsingItem() return true. * This method should handle reducing item count, durability, etc., on successful use. * No need to send item updates separately as the caller will handle it. *

diff --git a/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java b/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java index d2d0fd3a1..7f7f4db3d 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java +++ b/Allay-Server/src/main/java/org/allaymc/server/entity/component/player/EntityPlayerBaseComponentImpl.java @@ -100,7 +100,7 @@ public class EntityPlayerBaseComponentImpl extends EntityBaseComponentImpl forms = new Int2ObjectOpenHashMap<>(); protected Map serverSettingForms = new Int2ObjectOpenHashMap<>(); diff --git a/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java b/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java index 0283a5c28..87153f458 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java +++ b/Allay-Server/src/main/java/org/allaymc/server/network/processor/InventoryTransactionPacketProcessor.java @@ -50,8 +50,12 @@ public void handleSync(EntityPlayer player, InventoryTransactionPacket packet) { player, clickBlockPos, clickPos, blockFace ); - if (player.isInteractingBlock()) { - itemStack.useItemOn(dimension, placeBlockPos, interactInfo); + itemStack.rightClickItemOn(dimension, placeBlockPos, interactInfo); + if (player.isUsingItem()) { + if (itemStack.useItemOn(dimension, placeBlockPos, interactInfo)) { + // Using item on the block successfully, no need to call BlockBehavior::onInteract() + break; + } if (!interactedBlock.getBehavior().onInteract(itemStack, dimension, interactInfo)) { // Player interaction with the block was unsuccessful, possibly the plugin rolled back the event, need to override the client block change // Override the block change that was clicked @@ -65,6 +69,7 @@ public void handleSync(EntityPlayer player, InventoryTransactionPacket packet) { dimension.sendBlockUpdateTo(blockStateReplaced, placeBlockPos, 0, player); } } + } } case ITEM_USE_CLICK_AIR -> { diff --git a/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerActionPacketProcessor.java b/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerActionPacketProcessor.java index 8e5793bba..e98dd4a03 100644 --- a/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerActionPacketProcessor.java +++ b/Allay-Server/src/main/java/org/allaymc/server/network/processor/PlayerActionPacketProcessor.java @@ -69,21 +69,21 @@ public PacketSignal handleAsync(EntityPlayer player, PlayerActionPacket packet) yield PacketSignal.HANDLED; } case START_ITEM_USE_ON -> { - if (player.isInteractingBlock()) { + if (player.isUsingItem()) { log.warn("Player {} tried to start item use on without stopping", player.getOriginName()); yield PacketSignal.HANDLED; } - player.setInteractingBlock(true); + player.setUsingItem(true); yield PacketSignal.HANDLED; } case STOP_ITEM_USE_ON -> { - if (!player.isInteractingBlock()) { + if (!player.isUsingItem()) { log.warn("Player {} tried to stop item use on without starting", player.getOriginName()); yield PacketSignal.HANDLED; } - player.setInteractingBlock(false); + player.setUsingItem(false); yield PacketSignal.HANDLED; } default -> PacketSignal.UNHANDLED;