Repository: spongepowered/sponge
Issue: Cancelling a `InteractBlockEvent.Secondary` doesn't refresh count in inventory

### Affected Product(s)

SpongeForge, SpongeVanilla

### Version

1.16.5-36.2.5-8.1.0-RC1164

### Operating System

Windows

### Java Version

OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)

### Plugins/Mods

```shell
None other than this plugin
```


### Describe the bug

```
@Listener
public void test(InteractBlockEvent.Secondary event, @First ServerPlayer p) {
    event.setCancelled(true);
}
```

When a user tries to place some a block such as dirt with this listener active, the dirt will be prevented from placing, but the quantity of the item stack will drop by 1 as-if the place went through.  The count can be updated by moving the stack from one slot or another, or simply collecting another block of dirt.

### Link to logs

_No response_

Changed files:
src/mixins/java/org/spongepowered/common/mixin/tracker/server/level/ServerPlayerGameModeMixin_Tracker.java
src/mixins/java/org/spongepowered/common/mixin/tracker/server/network/ServerGamePacketListenerImplMixin_Tracker.java

diff --git a/src/mixins/java/org/spongepowered/common/mixin/tracker/server/level/ServerPlayerGameModeMixin_Tracker.java b/src/mixins/java/org/spongepowered/common/mixin/tracker/server/level/ServerPlayerGameModeMixin_Tracker.java
index fe8b20b4f..b94a1d308 100644
--- a/src/mixins/java/org/spongepowered/common/mixin/tracker/server/level/ServerPlayerGameModeMixin_Tracker.java
+++ b/src/mixins/java/org/spongepowered/common/mixin/tracker/server/level/ServerPlayerGameModeMixin_Tracker.java
@@ -53,6 +53,7 @@
 import org.spongepowered.asm.mixin.injection.At;
 import org.spongepowered.asm.mixin.injection.Inject;
 import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+import org.spongepowered.common.bridge.server.level.ServerPlayerGameModeBridge;
 import org.spongepowered.common.bridge.world.inventory.container.ContainerBridge;
 import org.spongepowered.common.event.SpongeCommonEventFactory;
 import org.spongepowered.common.event.inventory.InventoryEventFactory;
@@ -100,11 +101,12 @@ public InteractionResult useItemOn(final ServerPlayer playerIn, final Level worl
         final Vector3d hitVec = Vector3d.from(blockRaytraceResultIn.getBlockPos().getX(), blockRaytraceResultIn.getBlockPos().getY(), blockRaytraceResultIn.getBlockPos().getZ());
         final org.spongepowered.api.util.Direction direction = DirectionFacingProvider.INSTANCE.getKey(blockRaytraceResultIn.getDirection()).get();
         final InteractBlockEvent.Secondary event = SpongeCommonEventFactory.callInteractBlockEventSecondary(playerIn, stackIn, hitVec, snapshot, direction, handIn);
+        final Tristate useItem = event.useItemResult();
+        final Tristate useBlock = event.useBlockResult();
+        ((ServerPlayerGameModeBridge) this).bridge$setInteractBlockRightClickCancelled(event.isCancelled());
         if (event.isCancelled()) {
             return InteractionResult.FAIL;
         }
-        final Tristate useItem = event.useItemResult();
-        final Tristate useBlock = event.useBlockResult();
         // Sponge end
         if (this.gameModeForPlayer == GameType.SPECTATOR) {
             final MenuProvider inamedcontainerprovider = blockstate.getMenuProvider(worldIn, blockpos);
@@ -152,6 +154,7 @@ public InteractionResult useItemOn(final ServerPlayer playerIn, final Level worl
             if (!stackIn.isEmpty() && !playerIn.getCooldowns().isOnCooldown(stackIn.getItem())) {
                 // Sponge start
                 if (useItem == Tristate.FALSE) {
+                    ((ServerPlayerGameModeBridge) this).bridge$setInteractBlockRightClickCancelled(true);
                     return InteractionResult.PASS;
                 }
                 // Sponge end
@@ -177,6 +180,12 @@ public InteractionResult useItemOn(final ServerPlayer playerIn, final Level worl
 
                 return result;
             } else {
+                // Sponge start
+                if(useBlock == Tristate.FALSE && !flag1) {
+                    ((ServerPlayerGameModeBridge) this).bridge$setInteractBlockRightClickCancelled(true);
+                }
+                // Sponge end
+
                 return InteractionResult.PASS;
             }
         }
diff --git a/src/mixins/java/org/spongepowered/common/mixin/tracker/server/network/ServerGamePacketListenerImplMixin_Tracker.java b/src/mixins/java/org/spongepowered/common/mixin/tracker/server/network/ServerGamePacketListenerImplMixin_Tracker.java
index 25bc612a1..c2863fb86 100644
--- a/src/mixins/java/org/spongepowered/common/mixin/tracker/server/network/ServerGamePacketListenerImplMixin_Tracker.java
+++ b/src/mixins/java/org/spongepowered/common/mixin/tracker/server/network/ServerGamePacketListenerImplMixin_Tracker.java
@@ -39,11 +39,9 @@
 import org.spongepowered.asm.mixin.injection.Inject;
 import org.spongepowered.asm.mixin.injection.Redirect;
 import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
-import org.spongepowered.common.SpongeCommon;
 import org.spongepowered.common.bridge.world.entity.PlatformEntityBridge;
 import org.spongepowered.common.bridge.server.level.ServerPlayerGameModeBridge;
 import org.spongepowered.common.bridge.world.level.LevelBridge;
-import org.spongepowered.common.event.SpongeCommonEventFactory;
 import org.spongepowered.common.event.tracking.PhaseTracker;
 import org.spongepowered.common.event.tracking.phase.packet.PacketContext;
 import org.spongepowered.common.event.tracking.phase.packet.PacketPhaseUtil;
@@ -80,21 +78,17 @@ public abstract class ServerGamePacketListenerImplMixin_Tracker {
         if (PhaseTracker.getInstance().getPhaseContext().isEmpty()) {
             return actionResult;
         }
-        final PacketContext<?> context = ((PacketContext<?>) PhaseTracker.getInstance().getPhaseContext());
 
-        // If a plugin or mod has changed the item, avoid restoring
-        if (!context.getInteractItemChanged()) {
-            final ItemStack itemStack = ItemStackUtil.toNative(context.getItemUsed());
+        final PacketContext<?> context = ((PacketContext<?>) PhaseTracker.getInstance().getPhaseContext());
+        final ItemStack itemStack = ItemStackUtil.toNative(context.getItemUsed());
 
-            // Only do a restore if something actually changed. The client does an identity check ('==')
-            // to determine if it should continue using an itemstack. If we always resend the itemstack, we end up
-            // cancelling item usage (e.g. eating food) that occurs while targeting a block
-            final boolean isInteractionCancelled = ((ServerPlayerGameModeBridge) this.player.gameMode).bridge$isInteractBlockRightClickCancelled();
-            if (!ItemStack.matches(itemStack, this.player.getItemInHand(hand)) && isInteractionCancelled) {
-                PacketPhaseUtil.handlePlayerSlotRestore(this.player, itemStack, hand);
-            }
+        // Only do a restore if the items should stay the same. The client does an identity check ('==')
+        // to determine if it should continue using an itemstack.
+        final boolean isInteractionCancelled = ((ServerPlayerGameModeBridge) this.player.gameMode).bridge$isInteractBlockRightClickCancelled();
+        if (ItemStack.matches(itemStack, this.player.getItemInHand(hand)) && isInteractionCancelled) {
+            PacketPhaseUtil.handlePlayerSlotRestore(this.player, itemStack, hand);
         }
-        context.interactItemChanged(false);
+
         ((ServerPlayerGameModeBridge) this.player.gameMode).bridge$setInteractBlockRightClickCancelled(false);
         return actionResult;
     }