|
| 1 | +package de.hysky.skyblocker.skyblock; |
| 2 | + |
| 3 | +import de.hysky.skyblocker.annotations.Init; |
| 4 | +import de.hysky.skyblocker.config.SkyblockerConfigManager; |
| 5 | +import de.hysky.skyblocker.utils.container.RegexContainerMatcher; |
| 6 | +import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; |
| 7 | +import net.fabricmc.fabric.api.client.screen.v1.ScreenKeyboardEvents; |
| 8 | +import net.fabricmc.fabric.api.client.screen.v1.ScreenMouseEvents; |
| 9 | +import net.minecraft.client.MinecraftClient; |
| 10 | +import net.minecraft.client.gui.screen.ingame.HandledScreen; |
| 11 | +import net.minecraft.client.option.KeyBinding; |
| 12 | +import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.item.Items; |
| 14 | +import net.minecraft.screen.slot.SlotActionType; |
| 15 | +import org.lwjgl.glfw.GLFW; |
| 16 | + |
| 17 | +import java.util.function.Predicate; |
| 18 | + |
| 19 | +@SuppressWarnings("unused") |
| 20 | +public class WardrobeKeybinds extends RegexContainerMatcher { |
| 21 | + private static final WardrobeKeybinds INSTANCE = new WardrobeKeybinds(); |
| 22 | + |
| 23 | + public WardrobeKeybinds() { |
| 24 | + super("Wardrobe \\([12]/2\\)"); |
| 25 | + } |
| 26 | + |
| 27 | + @Init |
| 28 | + public static void init() { |
| 29 | + ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { |
| 30 | + if (!(screen instanceof HandledScreen<?> handledScreen) || !INSTANCE.test(handledScreen) || !INSTANCE.isEnabled() || client.interactionManager == null) return; |
| 31 | + ScreenKeyboardEvents.allowKeyPress(handledScreen).register((ignored, keyCode, scanCode, modifiers) -> |
| 32 | + allowInput(client, handledScreen, keybinding -> keybinding.matchesKey(keyCode, scanCode)) |
| 33 | + ); |
| 34 | + ScreenMouseEvents.allowMouseClick(handledScreen).register((ignored, mouseX, mouseY, button) -> |
| 35 | + allowInput(client, handledScreen, keybinding -> keybinding.matchesMouse(button)) |
| 36 | + ); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + private static boolean allowInput(MinecraftClient client, HandledScreen<?> handledScreen, Predicate<KeyBinding> predicate) { |
| 41 | + boolean found = false; |
| 42 | + int i; |
| 43 | + for (i = 0; i < client.options.hotbarKeys.length; i++) { |
| 44 | + if (predicate.test(client.options.hotbarKeys[i])) { |
| 45 | + found = true; |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + if (!found) return true; |
| 50 | + // The items start from the 5th row in the inventory. The i number we have is the column in the first row, so we have to offset it by 4 rows to get the 5th row, which is where the items start. |
| 51 | + i += 9 * 4; |
| 52 | + ItemStack itemStack = handledScreen.getScreenHandler().getSlot(i).getStack(); |
| 53 | + // Check if the item in the slot is a swap/unequip item before going further. |
| 54 | + // This prevents usage when the inventory hasn't loaded fully or when the slot pressed is locked (which would be meaningless to click) |
| 55 | + if (!itemStack.isOf(Items.PINK_DYE) && !itemStack.isOf(Items.LIME_DYE)) return true; |
| 56 | + assert client.interactionManager != null; |
| 57 | + client.interactionManager.clickSlot(handledScreen.getScreenHandler().syncId, i, GLFW.GLFW_MOUSE_BUTTON_1, SlotActionType.PICKUP, client.player); |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isEnabled() { |
| 63 | + return SkyblockerConfigManager.get().helpers.enableWardrobeHelper; |
| 64 | + } |
| 65 | +} |
0 commit comments