diff options
| author | Moulberry <jjenour@student.unimelb.edu.au> | 2022-10-15 16:14:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-15 16:14:46 +0200 |
| commit | 9dff9de9be425a07691951f7f7e6d43ca2c967bf (patch) | |
| tree | 525c4086b4b8cb9ee2a329dee7a0915ceaaa788a /src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java | |
| parent | 7c6d37b2eb758a13b342b906f0aef88b940bc52a (diff) | |
| parent | df02addf3404f07f245d6e6da8ce3ee8d72bd235 (diff) | |
| download | notenoughupdates-9dff9de9be425a07691951f7f7e6d43ca2c967bf.tar.gz notenoughupdates-9dff9de9be425a07691951f7f7e6d43ca2c967bf.tar.bz2 notenoughupdates-9dff9de9be425a07691951f7f7e6d43ca2c967bf.zip | |
Merge pull request #268 from NotEnoughUpdates/master
2.1 Continued
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java | 118 |
1 files changed, 77 insertions, 41 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java index 743625f5..8a487739 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java @@ -1,3 +1,22 @@ +/* + * Copyright (C) 2022 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>. + */ + package io.github.moulberry.notenoughupdates.miscfeatures; import com.google.gson.Gson; @@ -5,6 +24,8 @@ import com.google.gson.GsonBuilder; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; import io.github.moulberry.notenoughupdates.core.config.KeybindHelper; import io.github.moulberry.notenoughupdates.core.util.render.RenderUtils; +import io.github.moulberry.notenoughupdates.events.SlotClickEvent; +import io.github.moulberry.notenoughupdates.mixins.AccessorGuiContainer; import io.github.moulberry.notenoughupdates.util.SBInfo; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.ISound; @@ -23,16 +44,20 @@ import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.event.GuiScreenEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import org.apache.commons.lang3.tuple.Triple; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; import org.lwjgl.util.vector.Vector2f; -import java.io.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; import java.util.HashMap; -import java.util.function.Consumer; public class SlotLocking { private static final SlotLocking INSTANCE = new SlotLocking(); @@ -200,7 +225,7 @@ public class SlotLocking { int mouseX = Mouse.getX() * scaledWidth / Minecraft.getMinecraft().displayWidth; int mouseY = scaledHeight - Mouse.getY() * scaledHeight / Minecraft.getMinecraft().displayHeight - 1; - Slot slot = container.getSlotAtPosition(mouseX, mouseY); + Slot slot = ((AccessorGuiContainer) container).doGetSlotAtPosition(mouseX, mouseY); if (slot != null && slot.getSlotIndex() != 8 && slot.inventory == Minecraft.getMinecraft().thePlayer.inventory) { int slotNum = slot.getSlotIndex(); if (slotNum >= 0 && slotNum <= 39) { @@ -280,7 +305,7 @@ public class SlotLocking { int mouseX = Mouse.getX() * scaledWidth / Minecraft.getMinecraft().displayWidth; int mouseY = scaledHeight - Mouse.getY() * scaledHeight / Minecraft.getMinecraft().displayHeight - 1; - Slot slot = container.getSlotAtPosition(mouseX, mouseY); + Slot slot = ((AccessorGuiContainer) container).doGetSlotAtPosition(mouseX, mouseY); if (slot != null && slot.getSlotIndex() != 8 && slot.inventory == Minecraft.getMinecraft().thePlayer.inventory) { int slotNum = slot.getSlotIndex(); if (slotNum >= 0 && slotNum <= 39) { @@ -378,10 +403,10 @@ public class SlotLocking { if (!(Minecraft.getMinecraft().currentScreen instanceof GuiContainer)) { return; } - GuiContainer container = (GuiContainer) Minecraft.getMinecraft().currentScreen; + AccessorGuiContainer container = (AccessorGuiContainer) Minecraft.getMinecraft().currentScreen; - int x1 = container.guiLeft + pairingSlot.xDisplayPosition + 8; - int y1 = container.guiTop + pairingSlot.yDisplayPosition + 8; + int x1 = container.getGuiLeft() + pairingSlot.xDisplayPosition + 8; + int y1 = container.getGuiTop() + pairingSlot.yDisplayPosition + 8; int x2 = event.mouseX; int y2 = event.mouseY; @@ -429,22 +454,21 @@ public class SlotLocking { } } - public void onWindowClick( - Slot slotIn, - int slotId, - int clickedButton, - int clickType, - Consumer<Triple<Integer, Integer, Integer>> consumer - ) { - LockedSlot locked = getLockedSlot(slotIn); + @SubscribeEvent + public void onWindowClick(SlotClickEvent slotClickEvent) { + LockedSlot locked = getLockedSlot(slotClickEvent.slot); if (locked == null) { return; - } else if (locked.locked || (clickType == 2 && SlotLocking.getInstance().isSlotIndexLocked(clickedButton))) { - consumer.accept(null); - } else if (NotEnoughUpdates.INSTANCE.config.slotLocking.enableSlotBinding && clickType == 1 && + } + if (locked.locked || + (slotClickEvent.clickType == 2 && SlotLocking.getInstance().isSlotIndexLocked(slotClickEvent.clickedButton))) { + slotClickEvent.setCanceled(true); + return; + } + if (NotEnoughUpdates.INSTANCE.config.slotLocking.enableSlotBinding + && slotClickEvent.clickType == 1 && locked.boundTo != -1) { - GuiContainer container = (GuiContainer) Minecraft.getMinecraft().currentScreen; - Slot boundSlot = container.inventorySlots.getSlotFromInventory( + Slot boundSlot = slotClickEvent.guiContainer.inventorySlots.getSlotFromInventory( Minecraft.getMinecraft().thePlayer.inventory, locked.boundTo ); @@ -455,29 +479,41 @@ public class SlotLocking { LockedSlot boundLocked = getLockedSlot(boundSlot); - int id = slotIn.getSlotIndex(); - if (id >= 9 && locked.boundTo >= 0 && locked.boundTo < 8) { - if (!boundLocked.locked) { - consumer.accept(Triple.of(slotId, locked.boundTo, 2)); - if (boundLocked == DEFAULT_LOCKED_SLOT) { - LockedSlot[] lockedSlots = getDataForProfile(); - lockedSlots[locked.boundTo] = new LockedSlot(); - lockedSlots[locked.boundTo].boundTo = id; - } else { - boundLocked.boundTo = id; - } + int from, to; + int id = slotClickEvent.slot.getSlotIndex(); + if (id >= 9 && 0 <= locked.boundTo && locked.boundTo < 8 && !boundLocked.locked) { + from = id; + to = locked.boundTo; + if (boundLocked == DEFAULT_LOCKED_SLOT) { + LockedSlot[] lockedSlots = getDataForProfile(); + lockedSlots[locked.boundTo] = new LockedSlot(); + lockedSlots[locked.boundTo].boundTo = id; + } else { + boundLocked.boundTo = id; } - } else if (id >= 0 && id < 8 && locked.boundTo >= 9 && locked.boundTo <= 39) { + } else if (0 <= id && id < 8 && locked.boundTo >= 9 && locked.boundTo <= 39) { if (boundLocked.locked || boundLocked.boundTo != id) { locked.boundTo = -1; + return; } else { - int boundTo = boundSlot.slotNumber; - consumer.accept(Triple.of(boundTo, id, 2)); + from = boundSlot.slotNumber; + to = id; } + } else { + return; } + if (from == 39) from = 5; + if (from == 38) from = 6; + if (from == 37) from = 7; + if (from == 36) from = 8; + Minecraft.getMinecraft().playerController.windowClick( + slotClickEvent.guiContainer.inventorySlots.windowId, + from, to, 2, Minecraft.getMinecraft().thePlayer + ); + slotClickEvent.setCanceled(true); } else if (NotEnoughUpdates.INSTANCE.config.slotLocking.enableSlotBinding && locked.boundTo != -1 && NotEnoughUpdates.INSTANCE.config.slotLocking.bindingAlsoLocks) { - consumer.accept(null); + slotClickEvent.setCanceled(true); } } @@ -514,7 +550,7 @@ public class SlotLocking { return; } - boolean hoverOverSlot = container.isMouseOverSlot(slot, mouseX, mouseY); + boolean hoverOverSlot = ((AccessorGuiContainer) container).doIsMouseOverSlot(slot, mouseX, mouseY); if (hoverOverSlot || slot.getSlotIndex() >= 9) { Minecraft.getMinecraft().getTextureManager().bindTexture(BOUND); @@ -545,8 +581,8 @@ public class SlotLocking { ); } } else if (pairingSlot != null && lockKeyHeld && slot.getSlotIndex() < 8) { - int x1 = container.guiLeft + pairingSlot.xDisplayPosition; - int y1 = container.guiTop + pairingSlot.yDisplayPosition; + int x1 = ((AccessorGuiContainer) container).getGuiLeft() + pairingSlot.xDisplayPosition; + int y1 = ((AccessorGuiContainer) container).getGuiTop() + pairingSlot.yDisplayPosition; if (mouseX <= x1 || mouseX >= x1 + 16 || mouseY <= y1 || mouseY >= y1 + 16) { @@ -634,8 +670,8 @@ public class SlotLocking { int mouseX = Mouse.getX() * scaledWidth / Minecraft.getMinecraft().displayWidth; int mouseY = scaledHeight - Mouse.getY() * scaledHeight / Minecraft.getMinecraft().displayHeight - 1; - int x1 = container.guiLeft + pairingSlot.xDisplayPosition; - int y1 = container.guiTop + pairingSlot.yDisplayPosition; + int x1 = ((AccessorGuiContainer) container).getGuiLeft() + pairingSlot.xDisplayPosition; + int y1 = ((AccessorGuiContainer) container).getGuiTop() + pairingSlot.yDisplayPosition; if (mouseX <= x1 || mouseX >= x1 + 16 || mouseY <= y1 || mouseY >= y1 + 16) { |
