Support replacing blocks when using your off-hand
Some checks failed
Java CI with Gradle / build (push) Failing after 16m7s

Co-authored-by: termilu <47648082+termilu@users.noreply.github.com>
This commit is contained in:
Adam 2025-04-23 23:06:42 +01:00
parent 4ea309bbe9
commit 91dd7f9811

View File

@ -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,33 +28,26 @@ 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) {
if (client == null)
return;
// 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.
@ -63,12 +58,13 @@ public class HotbarReplace implements ClientModInitializer {
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,
inventory.getSelectedSlot() + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1,
slot + PlayerInventory.MAIN_SIZE, GLFW.GLFW_MOUSE_BUTTON_1,
SlotActionType.PICKUP, player);
}, click_delay, TimeUnit.MILLISECONDS);
}
return;
}