From 91dd7f9811e6c627855bf7a47532e220da2b9032 Mon Sep 17 00:00:00 2001 From: Adam Macdonald Date: Wed, 23 Apr 2025 23:06:42 +0100 Subject: [PATCH] Support replacing blocks when using your off-hand Co-authored-by: termilu <47648082+termilu@users.noreply.github.com> --- .../java/xyz/twokilohertz/HotbarReplace.java | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/client/java/xyz/twokilohertz/HotbarReplace.java b/src/client/java/xyz/twokilohertz/HotbarReplace.java index 0bfaac1..f607eaf 100644 --- a/src/client/java/xyz/twokilohertz/HotbarReplace.java +++ b/src/client/java/xyz/twokilohertz/HotbarReplace.java @@ -7,6 +7,8 @@ import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemPlacementContext; import net.minecraft.screen.slot.SlotActionType; +import net.minecraft.util.Hand; + import org.lwjgl.glfw.GLFW; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,49 +28,43 @@ public class HotbarReplace implements ClientModInitializer { } public static void tryReplaceSlot(ItemPlacementContext context, Item item) { - // Return immediately if player is a spectator + // Return immediately if player is a spectator or in creative PlayerEntity player = context.getPlayer(); - if (player.isSpectator()) - return; - - // Creative inventories don't run out of blocks anyway - if (player.getAbilities().creativeMode) + if (player == null || player.isSpectator() || player.getAbilities().creativeMode) return; // Get reference to player's current inventory PlayerInventory inventory = player.getInventory(); - if (inventory == null) - return; // Return if the inventory is empty - if (inventory.isEmpty()) - return; - - // If current screen handler is null, return - if (player.currentScreenHandler == null) + if (inventory == null || inventory.isEmpty() || player.currentScreenHandler == null) return; // Attempt to find a stack of matching items in the player's inventory for (int i = 0; i < player.currentScreenHandler.slots.size(); i++) { if (player.currentScreenHandler.slots.get(i).getStack().isOf(item)) { // Simulate moving the stack from one slot to another - if (client != null) { - // TODO: This still feels like a bit of a hack - // I honestly do not know Minecraft internals enough to be sure that there won't - // be de-sync issues. - int current_fps = client.getCurrentFps(); - int click_delay = Math.round(1.0f / (float) current_fps) * 1000; + if (client == null) + return; - client.interactionManager.clickSlot(player.currentScreenHandler.syncId, i, GLFW.GLFW_MOUSE_BUTTON_1, + // TODO: This still feels like a bit of a hack + // I honestly do not know Minecraft internals enough to be sure that there won't + // be de-sync issues. + + int current_fps = client.getCurrentFps(); + int click_delay = Math.round(1.0f / (float) current_fps) * 1000; + + client.interactionManager.clickSlot(player.currentScreenHandler.syncId, i, GLFW.GLFW_MOUSE_BUTTON_1, + SlotActionType.PICKUP, player); + + int slot = (context.getHand() == Hand.OFF_HAND ? 9 : inventory.getSelectedSlot()); + + scheduler.schedule(() -> { + client.interactionManager.clickSlot(player.currentScreenHandler.syncId, + slot + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1, SlotActionType.PICKUP, player); - - scheduler.schedule(() -> { - client.interactionManager.clickSlot(player.currentScreenHandler.syncId, - inventory.getSelectedSlot() + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1, - SlotActionType.PICKUP, player); - }, click_delay, TimeUnit.MILLISECONDS); - } + }, click_delay, TimeUnit.MILLISECONDS); return; }