From b11561b7c81d28ea93af92b0bf87e935e7a0e9af Mon Sep 17 00:00:00 2001 From: Adam Macdonald Date: Thu, 24 Apr 2025 00:29:56 +0100 Subject: [PATCH] Use ItemStack instead of BlockItem specifically --- gradle.properties | 2 +- .../java/xyz/twokilohertz/HotbarReplace.java | 8 +- .../twokilohertz/mixin/BlockItemMixin.java | 35 --------- .../twokilohertz/mixin/ItemStackMixin.java | 77 +++++++++++++++++++ src/client/resources/fabric.mod.json | 2 +- .../resources/hotbarreplace.mixins.json | 2 +- 6 files changed, 83 insertions(+), 43 deletions(-) delete mode 100644 src/client/java/xyz/twokilohertz/mixin/BlockItemMixin.java create mode 100644 src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java diff --git a/gradle.properties b/gradle.properties index a4b4d50..759b8ac 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ yarn_mappings=1.21.5+build.1 loader_version=0.16.13 # Mod Properties -mod_version = 0.1.3 +mod_version = 0.1.4 maven_group = xyz.twokilohertz archives_base_name = HotbarReplace diff --git a/src/client/java/xyz/twokilohertz/HotbarReplace.java b/src/client/java/xyz/twokilohertz/HotbarReplace.java index f607eaf..d3842c7 100644 --- a/src/client/java/xyz/twokilohertz/HotbarReplace.java +++ b/src/client/java/xyz/twokilohertz/HotbarReplace.java @@ -5,7 +5,6 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.entity.player.PlayerEntity; 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; @@ -24,12 +23,11 @@ public class HotbarReplace implements ClientModInitializer { @Override public void onInitializeClient() { - LOGGER.info("HotbarReplace v0.1.3 initialised"); + LOGGER.info("HotbarReplace v0.1.4 initialised"); } - public static void tryReplaceSlot(ItemPlacementContext context, Item item) { + public static void tryReplaceSlot(PlayerEntity player, Hand hand, Item item) { // Return immediately if player is a spectator or in creative - PlayerEntity player = context.getPlayer(); if (player == null || player.isSpectator() || player.getAbilities().creativeMode) return; @@ -58,7 +56,7 @@ 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()); + int slot = (hand == Hand.OFF_HAND ? 9 : inventory.getSelectedSlot()); scheduler.schedule(() -> { client.interactionManager.clickSlot(player.currentScreenHandler.syncId, diff --git a/src/client/java/xyz/twokilohertz/mixin/BlockItemMixin.java b/src/client/java/xyz/twokilohertz/mixin/BlockItemMixin.java deleted file mode 100644 index faa746e..0000000 --- a/src/client/java/xyz/twokilohertz/mixin/BlockItemMixin.java +++ /dev/null @@ -1,35 +0,0 @@ -package xyz.twokilohertz.mixin; - -import net.minecraft.item.BlockItem; -import net.minecraft.item.Item; -import net.minecraft.item.ItemPlacementContext; -import net.minecraft.util.ActionResult; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import xyz.twokilohertz.HotbarReplace; - -@Mixin(BlockItem.class) -public class BlockItemMixin { - private Item lastPlacedItem; - - @Inject(at = @At("HEAD"), method = "Lnet/minecraft/item/BlockItem;place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;") - private void BlockItem_place_head(ItemPlacementContext context, CallbackInfoReturnable info) { - lastPlacedItem = context.getStack().getItem(); - } - - @Inject(at = @At("TAIL"), method = "Lnet/minecraft/item/BlockItem;place(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/util/ActionResult;") - private void BlockItem_place_tail(ItemPlacementContext context, CallbackInfoReturnable info) { - // Early return if the block place action would fail - if (info.getReturnValue() != ActionResult.SUCCESS) - return; - - // Check if the stack is not empty, return if so - if (context.getStack().getCount() != 0) - return; - - // Try to replace the hotbar slot - HotbarReplace.tryReplaceSlot(context, lastPlacedItem); - } -} diff --git a/src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java b/src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java new file mode 100644 index 0000000..6b96d4c --- /dev/null +++ b/src/client/java/xyz/twokilohertz/mixin/ItemStackMixin.java @@ -0,0 +1,77 @@ +package xyz.twokilohertz.mixin; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemUsageContext; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.world.World; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import xyz.twokilohertz.HotbarReplace; + +@Mixin(ItemStack.class) +public class ItemStackMixin { + Item lastUsedItem = null; + + @Inject(at = @At("HEAD"), method = "use") + private void ItemStack_use_HEAD(World world, PlayerEntity player, Hand hand, + CallbackInfoReturnable info) { + lastUsedItem = player.getStackInHand(hand).getItem(); + } + + @Inject(at = @At("HEAD"), method = "useOnBlock") + private void ItemStack_useOnBlock_HEAD(ItemUsageContext context, + CallbackInfoReturnable info) { + lastUsedItem = context.getPlayer().getStackInHand(context.getHand()).getItem(); + } + + @Inject(at = @At("HEAD"), method = "useOnEntity") + private void ItemStack_useOnEntity_HEAD(PlayerEntity player, LivingEntity entity, Hand hand, + CallbackInfoReturnable info) { + lastUsedItem = player.getStackInHand(hand).getItem(); + } + + @Inject(at = @At("TAIL"), method = "use") + private void ItemStack_use_TAIL(World world, PlayerEntity player, Hand hand, + CallbackInfoReturnable info) { + if (info.getReturnValue() != ActionResult.SUCCESS) + return; + + if (player.getStackInHand(hand).getCount() != 0) + return; + + HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem); + } + + @Inject(at = @At("TAIL"), method = "useOnBlock") + private void ItemStack_useOnBlock_TAIL(ItemUsageContext context, CallbackInfoReturnable info) { + if (info.getReturnValue() != ActionResult.SUCCESS) + return; + + PlayerEntity player = context.getPlayer(); + Hand hand = context.getHand(); + + if (player.getStackInHand(hand).getCount() != 0) + return; + + HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem); + } + + @Inject(at = @At("TAIL"), method = "useOnEntity") + private void ItemStack_useOnEntity_TAIL(PlayerEntity player, LivingEntity entity, Hand hand, + CallbackInfoReturnable info) { + if (info.getReturnValue() != ActionResult.SUCCESS) + return; + + if (player.getStackInHand(hand).getCount() != 0) + return; + + HotbarReplace.tryReplaceSlot(player, hand, lastUsedItem); + } +} diff --git a/src/client/resources/fabric.mod.json b/src/client/resources/fabric.mod.json index a57582b..bf635d4 100644 --- a/src/client/resources/fabric.mod.json +++ b/src/client/resources/fabric.mod.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "id": "hotbarreplace", - "version": "0.1.3", + "version": "0.1.4", "name": "HotbarReplace", "description": "Replace blocks in your hotbar when you run out with blocks from your inventory", "authors": [ diff --git a/src/client/resources/hotbarreplace.mixins.json b/src/client/resources/hotbarreplace.mixins.json index f90dac2..ba0221e 100644 --- a/src/client/resources/hotbarreplace.mixins.json +++ b/src/client/resources/hotbarreplace.mixins.json @@ -4,7 +4,7 @@ "compatibilityLevel": "JAVA_21", "mixins": [], "client": [ - "BlockItemMixin" + "ItemStackMixin" ], "injectors": { "defaultRequire": 1