From 172d1824fc5d1e5a6c9093e07036b721b60d3e55 Mon Sep 17 00:00:00 2001 From: emmachase Date: Mon, 19 Dec 2022 19:53:41 -0800 Subject: [PATCH] Fix duplicated swing animations on high-ping servers Use `InteractionResult.sidedSuccess` / `.CONSUME` where applicable instead of `.SUCCESS`. This prevents the server from sending an additional swing animation packet to the client. Normally this isn't a problem, since the client will de-duplicate swing packets if they are within the animation duration of the currently playing swing; however, when connected to a server with a high ping the packet is sent after the animation is already finished on the client, resulting in a duplicate animation. --- .../shared/computer/blocks/AbstractComputerBlockEntity.java | 4 ++-- .../computercraft/shared/media/items/PrintoutItem.java | 2 +- .../shared/peripheral/modem/wired/CableBlockEntity.java | 2 +- .../shared/peripheral/modem/wired/CableBlockItem.java | 6 +++--- .../peripheral/modem/wired/WiredModemFullBlockEntity.java | 2 +- .../shared/peripheral/monitor/MonitorBlock.java | 2 +- .../shared/pocket/items/PocketComputerItem.java | 2 +- .../shared/turtle/blocks/TurtleBlockEntity.java | 4 ++-- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java index 11589b403..28b398490 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/computer/blocks/AbstractComputerBlockEntity.java @@ -98,7 +98,7 @@ public abstract class AbstractComputerBlockEntity extends BlockEntity implements setLabel(currentItem.getHoverName().getString()); currentItem.shrink(1); } - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(getLevel().isClientSide); } else if (!player.isCrouching()) { // Regular right click to activate computer if (!getLevel().isClientSide && isUsable(player)) { @@ -110,7 +110,7 @@ public abstract class AbstractComputerBlockEntity extends BlockEntity implements : ItemStack.EMPTY; new ComputerContainerData(computer, stack).open(player, this); } - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(getLevel().isClientSide); } return InteractionResult.PASS; } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/media/items/PrintoutItem.java b/projects/common/src/main/java/dan200/computercraft/shared/media/items/PrintoutItem.java index c547cf0cf..80453e1ea 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/media/items/PrintoutItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/media/items/PrintoutItem.java @@ -56,7 +56,7 @@ public class PrintoutItem extends Item { new HeldItemContainerData(hand) .open(player, new HeldItemMenu.Factory(ModRegistry.Menus.PRINTOUT.get(), player.getItemInHand(hand), hand)); } - return new InteractionResultHolder<>(InteractionResult.SUCCESS, player.getItemInHand(hand)); + return new InteractionResultHolder<>(InteractionResult.sidedSuccess(world.isClientSide), player.getItemInHand(hand)); } private ItemStack createFromTitleAndText(@Nullable String title, @Nullable String[] text, @Nullable String[] colours) { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockEntity.java index 589ef74d5..0558c12dc 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockEntity.java @@ -193,7 +193,7 @@ public class CableBlockEntity extends BlockEntity { } } - return InteractionResult.SUCCESS; + return InteractionResult.CONSUME; } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockItem.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockItem.java index 271944b4f..42e46296d 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/CableBlockItem.java @@ -78,7 +78,7 @@ public abstract class CableBlockItem extends BlockItem { .setValue(CONNECTIONS.get(side), existingState.getValue(CABLE)); if (placeAt(world, pos, newState)) { stack.shrink(1); - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(world.isClientSide); } } @@ -105,7 +105,7 @@ public abstract class CableBlockItem extends BlockItem { if (insideState.getBlock() == ModRegistry.Blocks.CABLE.get() && !insideState.getValue(CableBlock.CABLE) && placeAtCorrected(world, insidePos, insideState.setValue(CableBlock.CABLE, true))) { stack.shrink(1); - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(world.isClientSide); } // Try to add a cable to a modem adjacent to this block @@ -113,7 +113,7 @@ public abstract class CableBlockItem extends BlockItem { if (existingState.getBlock() == ModRegistry.Blocks.CABLE.get() && !existingState.getValue(CableBlock.CABLE) && placeAtCorrected(world, pos, existingState.setValue(CableBlock.CABLE, true))) { stack.shrink(1); - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(world.isClientSide); } return super.place(context); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemFullBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemFullBlockEntity.java index f1dca00de..56cbe073b 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemFullBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/modem/wired/WiredModemFullBlockEntity.java @@ -139,7 +139,7 @@ public class WiredModemFullBlockEntity extends BlockEntity { sendPeripheralChanges(player, "chat.computercraft.wired_modem.peripheral_connected", periphNames); } - return InteractionResult.SUCCESS; + return InteractionResult.CONSUME; } private static void sendPeripheralChanges(Player player, String kind, Collection peripherals) { diff --git a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlock.java b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlock.java index 09d61f2cd..9719c7ca2 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlock.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/peripheral/monitor/MonitorBlock.java @@ -110,7 +110,7 @@ public class MonitorBlock extends HorizontalDirectionalBlock implements EntityBl ); } - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(level.isClientSide); } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java index 7aeeb3914..316d25266 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java @@ -141,7 +141,7 @@ public class PocketComputerItem extends Item implements IComputerItem, IMedia, I new ComputerContainerData(computer, stack).open(player, new PocketComputerMenuProvider(computer, stack, this, hand, isTypingOnly)); } } - return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack); + return new InteractionResultHolder<>(InteractionResult.sidedSuccess(world.isClientSide), stack); } @Override diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java index 190be1f73..13074836e 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/blocks/TurtleBlockEntity.java @@ -104,7 +104,7 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba } } } - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(getLevel().isClientSide); } else if (currentItem.getItem() == Items.WATER_BUCKET && brain.getColour() != -1) { // Water to remove turtle colour if (!getLevel().isClientSide) { @@ -116,7 +116,7 @@ public class TurtleBlockEntity extends AbstractComputerBlockEntity implements Ba } } } - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(getLevel().isClientSide); } }