quick and dirty port to 1.16.4

This commit is contained in:
2022-02-03 10:41:28 +01:00
parent 85a4934066
commit 97a89ab403
33 changed files with 611 additions and 889 deletions
@@ -1,9 +1,8 @@
package de.skate702.craftingkeys.util;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import org.lwjgl.input.Mouse;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.inventory.container.Slot;
/**
* This class provides methods to interact with the Mouse and key input.
@@ -24,70 +23,18 @@ public class InputUtil {
/**
* Returns the Slot at the current Mouse Position. [FROM GUICONTAINER]
*
* @param guiContainer The (Inventory) Container to work with.
* @param screen The (Inventory) Container to work with.
* @return Returns the slot (or null)
*/
public static Slot getSlotAtMousePosition(GuiContainer guiContainer) {
if (guiContainer != null) {
Container container = guiContainer.inventorySlots;
int x = getMouseX(guiContainer);
int y = getMouseY(guiContainer);
for (int k = 0; k < container.inventorySlots.size(); k++) {
Slot slot = (Slot) container.inventorySlots.get(k);
if (getIsMouseOverSlot(guiContainer, slot, x, y)) {
return slot;
}
}
return null;
public static Slot getSlotAtMousePosition(Screen screen) {
if (screen instanceof ContainerScreen) {
return ((ContainerScreen) screen).getSlotUnderMouse();
} else {
Logger.debug("getSlotAtMousePosition(gui)", "guiContainer == null");
return null;
}
}
/**
* Returns, if the mouse cursor is over a specified slot [FROM GUICONTAINER]
*
* @param guiContainer the GuiContainer to work with
* @param slot The spcified slot
* @param x The Mouse x-Position
* @param y The Mouse y-Position
* @return True, if the mouse is positioned over this slot. Otherwise false
*/
private static boolean getIsMouseOverSlot(GuiContainer guiContainer, Slot slot, int x, int y) {
if (guiContainer != null) {
// Constants from Minecraft Source Code
x -= (guiContainer.width - 176) / 2;
y -= (guiContainer.height - 166) / 2;
return x >= slot.xPos - 1 && x < slot.xPos + 16 + 1
&& y >= slot.yPos - 1 && y < slot.yPos + 16 + 1;
} else {
return false;
}
}
/**
* Returns a calculated Mouse Position for getSlotAtMousePosition().
*
* @param guiContainer the GuiContainer to work with
* @return The relative Mouse-Position
*/
private static int getMouseX(GuiContainer guiContainer) {
return (Mouse.getEventX() * guiContainer.width) / Util.client.displayWidth;
}
/**
* Returns a calculated Mouse Position for getSlotAtMousePosition().
*
* @param guiContainer the GuiContainer to work with
* @return The relative Mouse-Position
*/
private static int getMouseY(GuiContainer guiContainer) {
return guiContainer.height - (Mouse.getEventY() * guiContainer.height) / Util.client.displayHeight - 1;
}
/**
* Returns, if the current KeyValue is the same
*