Refactor CarryOn functionality to improve slot selection logic. Ensure that inventory slot changes are only allowed when not carrying an item.

This commit is contained in:
ElysianCat 2025-09-23 23:43:03 +03:30
parent ece3ed7ff2
commit c4346a4c00
2 changed files with 34 additions and 13 deletions

View File

@ -52,14 +52,16 @@ public class CarryOnCommonClient
return (CarryOnKeybinds.carryKey.matchesMouse(0) && mc.mouseHandler.isLeftPressed()) || (CarryOnKeybinds.carryKey.matchesMouse(1) && mc.mouseHandler.isRightPressed()) || (CarryOnKeybinds.carryKey.matchesMouse(3) && mc.mouseHandler.isMiddlePressed()); return (CarryOnKeybinds.carryKey.matchesMouse(0) && mc.mouseHandler.isLeftPressed()) || (CarryOnKeybinds.carryKey.matchesMouse(1) && mc.mouseHandler.isRightPressed()) || (CarryOnKeybinds.carryKey.matchesMouse(3) && mc.mouseHandler.isMiddlePressed());
} }
public static void onCarryClientTick() public static void onCarryClientTick() {
{
Player player = Minecraft.getInstance().player; Player player = Minecraft.getInstance().player;
if(player != null) { if (player != null) {
CarryOnData carry = CarryOnDataManager.getCarryData(player); CarryOnData carry = CarryOnDataManager.getCarryData(player);
if(carry.isCarrying()) if (carry.isCarrying()) {
{ int wantedSlot = carry.getSelected();
player.getInventory().setSelectedSlot(carry.getSelected()); if (player.getInventory().getSelectedSlot() != wantedSlot) {
// System.out.println("[CarryOn] Forcing slot " + wantedSlot);
player.getInventory().setSelectedSlot(wantedSlot);
}
} }
} }
} }

View File

@ -29,11 +29,30 @@ import org.spongepowered.asm.mixin.injection.Redirect;
import tschipp.carryon.common.carry.CarryOnDataManager; import tschipp.carryon.common.carry.CarryOnDataManager;
@Mixin(Minecraft.class) @Mixin(Minecraft.class)
public class MinecraftMixin public class MinecraftMixin {
{
@WrapWithCondition(method = "handleKeybinds()V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Inventory;setSelectedSlot(I)V", ordinal = 0)) @WrapWithCondition(
private boolean allowSlotSelection(Inventory inv,int slot) method = "handleKeybinds()V",
{ at = @At(
return !CarryOnDataManager.getCarryData(inv.player).isCarrying(); value = "INVOKE",
} target = "Lnet/minecraft/world/entity/player/Inventory;setSelectedSlot(I)V",
ordinal = 0
)
)
private boolean allowSlotSelection(Inventory inv, int slot) {
boolean carrying = CarryOnDataManager.getCarryData(inv.player).isCarrying();
// Allow if not carrying
if (!carrying) return true;
// Block only if trying to switch away
boolean allow = inv.getSelectedSlot() == slot;
if (!allow) {
// System.out.println("[CarryOn] Blocked hotbar change while carrying. Tried " + slot + ", staying on " + inv.getSelectedSlot());
}
return allow;
}
} }