Skip to content

Commit

Permalink
fix: fix block interacting and spawn egg
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 28, 2024
1 parent cf786da commit 037162b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public class EntityPlayerBaseComponentImpl extends EntityBaseComponentImpl<Entit
protected boolean awaitingDimensionChangeACK;
@Getter
@Setter
protected boolean interactingBlock;
protected boolean usingItem;
protected AtomicInteger formIdCounter = new AtomicInteger(0);
protected Map<Integer, Form> forms = new Int2ObjectOpenHashMap<>();
protected Map<Integer, CustomForm> serverSettingForms = new Int2ObjectOpenHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -65,6 +69,7 @@ public void handleSync(EntityPlayer player, InventoryTransactionPacket packet) {
dimension.sendBlockUpdateTo(blockStateReplaced, placeBlockPos, 0, player);
}
}

}
}
case ITEM_USE_CLICK_AIR -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 037162b

Please sign in to comment.