2014-04-29 10:54:20 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
2023-09-25 00:35:24 +02:00
|
|
|
import static codechicken.lib.gui.GuiDraw.getMousePosition;
|
|
|
|
|
|
|
|
|
|
import java.awt.Point;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
|
|
|
|
import net.minecraft.client.gui.inventory.GuiContainerCreative;
|
|
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
|
import net.minecraft.inventory.Slot;
|
|
|
|
|
import net.minecraft.inventory.SlotCrafting;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
2025-07-26 03:42:34 +03:00
|
|
|
import net.minecraftforge.client.ClientCommandHandler;
|
2023-09-25 00:35:24 +02:00
|
|
|
|
2025-07-26 03:42:34 +03:00
|
|
|
import codechicken.core.CommonUtils;
|
2019-12-30 20:19:16 -08:00
|
|
|
import codechicken.nei.api.API;
|
|
|
|
|
import codechicken.nei.api.GuiInfo;
|
|
|
|
|
import codechicken.nei.api.IInfiniteItemHandler;
|
|
|
|
|
import codechicken.nei.api.INEIGuiHandler;
|
|
|
|
|
import codechicken.nei.api.ItemInfo;
|
2025-07-26 03:42:34 +03:00
|
|
|
import codechicken.nei.commands.CommandBookmarkAdd;
|
2025-08-16 14:56:59 +03:00
|
|
|
import codechicken.nei.commands.CommandRecipeId;
|
2025-09-17 15:42:35 +03:00
|
|
|
import codechicken.nei.commands.CommandUntranslator;
|
2014-04-29 10:54:20 +10:00
|
|
|
import codechicken.nei.guihook.GuiContainerManager;
|
|
|
|
|
import codechicken.nei.guihook.IContainerInputHandler;
|
|
|
|
|
import codechicken.nei.guihook.IContainerSlotClickHandler;
|
2022-03-05 19:05:26 +09:00
|
|
|
import codechicken.nei.recipe.GuiRecipe;
|
2025-07-26 03:42:34 +03:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class NEIController implements IContainerSlotClickHandler, IContainerInputHandler {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2019-12-30 20:19:16 -08:00
|
|
|
private static final NEIController instance = new NEIController();
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
public static GuiContainerManager manager;
|
|
|
|
|
public static FastTransferManager fastTransferManager;
|
|
|
|
|
|
|
|
|
|
private static boolean deleteMode;
|
|
|
|
|
private static int pickedUpFromSlot;
|
|
|
|
|
private static IInfiniteItemHandler heldStackInfinite;
|
|
|
|
|
|
|
|
|
|
private static int selectedItem;
|
2014-06-29 20:31:37 +10:00
|
|
|
private ItemStack firstheld;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
public static void load() {
|
|
|
|
|
GuiContainerManager.addSlotClickHandler(instance);
|
|
|
|
|
GuiContainerManager.addInputHandler(instance);
|
2025-07-26 03:42:34 +03:00
|
|
|
|
|
|
|
|
if (CommonUtils.isClient()) registerClientCommands();
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void load(GuiContainer gui) {
|
|
|
|
|
manager = GuiContainerManager.getManager(gui);
|
|
|
|
|
deleteMode = false;
|
|
|
|
|
GuiInfo.clearGuiHandlers();
|
|
|
|
|
fastTransferManager = null;
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!NEIClientConfig.isEnabled()) return;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
fastTransferManager = new FastTransferManager();
|
2022-06-24 12:32:13 +01:00
|
|
|
if (gui instanceof INEIGuiHandler) API.registerNEIGuiHandler((INEIGuiHandler) gui);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
public static boolean isSpreading(GuiContainer gui) {
|
|
|
|
|
return gui.field_147007_t && gui.field_147008_s.size() > 1;
|
|
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2025-07-26 03:42:34 +03:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
|
public static void registerClientCommands() {
|
|
|
|
|
ClientCommandHandler.instance.registerCommand(new CommandBookmarkAdd());
|
2025-08-16 14:56:59 +03:00
|
|
|
ClientCommandHandler.instance.registerCommand(new CommandRecipeId());
|
2025-09-17 15:42:35 +03:00
|
|
|
ClientCommandHandler.instance.registerCommand(new CommandUntranslator());
|
2025-07-26 03:42:34 +03:00
|
|
|
}
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
public static void updateUnlimitedItems(InventoryPlayer inventory) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!NEIClientConfig.canPerformAction("item") || !NEIClientConfig.hasSMPCounterPart()) return;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2019-12-30 20:19:16 -08:00
|
|
|
LinkedList<ItemStack> beforeStacks = new LinkedList<>();
|
2014-04-29 10:54:20 +10:00
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++)
|
|
|
|
|
beforeStacks.add(NEIServerUtils.copyStack(inventory.getStackInSlot(i)));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++) {
|
|
|
|
|
ItemStack stack = inventory.getStackInSlot(i);
|
2022-06-24 12:32:13 +01:00
|
|
|
if (stack == null) continue;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
for (IInfiniteItemHandler handler : ItemInfo.infiniteHandlers)
|
|
|
|
|
if (handler.canHandleItem(stack) && handler.isItemInfinite(stack))
|
|
|
|
|
handler.replenishInfiniteStack(inventory, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++) {
|
|
|
|
|
ItemStack newstack = inventory.getStackInSlot(i);
|
|
|
|
|
|
|
|
|
|
if (!NEIServerUtils.areStacksIdentical(beforeStacks.get(i), newstack)) {
|
2022-06-24 12:32:13 +01:00
|
|
|
inventory.setInventorySlotContents(i, beforeStacks.get(i)); // restore in case of SMP fail
|
|
|
|
|
NEIClientUtils.setSlotContents(i, newstack, false); // sends via SMP handler ;)
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void processCreativeCycling(InventoryPlayer inventory) {
|
|
|
|
|
if (NEIClientConfig.invCreativeMode() && NEIClientUtils.controlKey()) {
|
|
|
|
|
if (selectedItem != inventory.currentItem) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (inventory.currentItem == selectedItem + 1 || (inventory.currentItem == 0 && selectedItem == 8)) // foward
|
2014-04-29 10:54:20 +10:00
|
|
|
{
|
|
|
|
|
NEICPH.sendCreativeScroll(1);
|
|
|
|
|
inventory.currentItem = selectedItem;
|
2022-06-24 12:32:13 +01:00
|
|
|
} else if (inventory.currentItem == selectedItem - 1
|
|
|
|
|
|| (inventory.currentItem == 8 && selectedItem == 0)) {
|
2014-04-29 10:54:20 +10:00
|
|
|
NEICPH.sendCreativeScroll(-1);
|
|
|
|
|
inventory.currentItem = selectedItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectedItem = inventory.currentItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void beforeSlotClick(GuiContainer gui, int slotIndex, int button, Slot slot, int modifier) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!NEIClientConfig.isEnabled()) return;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
firstheld = NEIClientUtils.getHeldItem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-06-24 12:32:13 +01:00
|
|
|
public boolean handleSlotClick(GuiContainer gui, int slotIndex, int button, Slot slot, int modifier,
|
|
|
|
|
boolean eventconsumed) {
|
|
|
|
|
if (eventconsumed || !NEIClientConfig.isEnabled() || isSpreading(gui)) return eventconsumed;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
if (deleteMode && slotIndex >= 0 && slot != null) {
|
|
|
|
|
if (NEIClientUtils.shiftKey() && button == 0) {
|
|
|
|
|
ItemStack itemstack1 = slot.getStack();
|
2022-06-24 12:32:13 +01:00
|
|
|
if (itemstack1 != null) NEIClientUtils.deleteItemsOfType(itemstack1);
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
} else if (button == 1) NEIClientUtils.decreaseSlotStack(slot.slotNumber);
|
|
|
|
|
else NEIClientUtils.deleteSlotStack(slot.slotNumber);
|
2014-04-29 10:54:20 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-27 03:17:40 -07:00
|
|
|
if (button == 1 && modifier != 2 && slot instanceof SlotCrafting) // right click
|
2014-04-29 10:54:20 +10:00
|
|
|
{
|
2022-06-24 12:32:13 +01:00
|
|
|
for (int i1 = 0; i1 < 64; i1++) // click this slot 64 times
|
|
|
|
|
manager.handleSlotClick(slot.slotNumber, button, 0);
|
2014-04-29 10:54:20 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (NEIClientUtils.controlKey() && modifier != 4 // Drop key pressed:
|
|
|
|
|
// net.minecraft.client.gui.inventory.GuiContainer.keyTyped
|
|
|
|
|
&& slot != null
|
|
|
|
|
&& slot.getStack() != null
|
2014-06-29 20:31:37 +10:00
|
|
|
&& slot.isItemValid(slot.getStack())) {
|
|
|
|
|
NEIClientUtils.cheatItem(slot.getStack(), button, 1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (GuiInfo.hasCustomSlots(gui)) return false;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (slotIndex >= 0 && NEIClientUtils.shiftKey()
|
|
|
|
|
&& NEIClientUtils.getHeldItem() != null
|
|
|
|
|
&& !slot.getHasStack()) {
|
2014-04-29 10:54:20 +10:00
|
|
|
ItemStack held = NEIClientUtils.getHeldItem();
|
|
|
|
|
manager.handleSlotClick(slot.slotNumber, button, 0);
|
|
|
|
|
if (slot.isItemValid(held) && !ItemInfo.fastTransferExemptions.contains(slot.getClass()))
|
|
|
|
|
fastTransferManager.performMassTransfer(gui, pickedUpFromSlot, slotIndex, held);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slotIndex == -999 && NEIClientUtils.shiftKey() && button == 0) {
|
|
|
|
|
fastTransferManager.throwAll(gui, pickedUpFromSlot);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void afterSlotClick(GuiContainer gui, int slotIndex, int button, Slot slot, int modifier) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!NEIClientConfig.isEnabled()) return;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
ItemStack nowHeld = NEIClientUtils.getHeldItem();
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (firstheld != nowHeld) pickedUpFromSlot = slotIndex;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
if (NEIClientConfig.canPerformAction("item") && NEIClientConfig.hasSMPCounterPart()) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (heldStackInfinite != null && slot != null
|
|
|
|
|
&& slot.inventory == NEIClientUtils.mc().thePlayer.inventory) {
|
2014-04-29 10:54:20 +10:00
|
|
|
ItemStack stack = slot.getStack();
|
|
|
|
|
if (stack != null) {
|
|
|
|
|
heldStackInfinite.onPlaceInfinite(stack);
|
|
|
|
|
}
|
|
|
|
|
NEIClientUtils.setSlotContents(slotIndex, stack, true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (firstheld != nowHeld) heldStackInfinite = null;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
if (firstheld != nowHeld && nowHeld != null) {
|
|
|
|
|
for (IInfiniteItemHandler handler : ItemInfo.infiniteHandlers) {
|
|
|
|
|
if (handler.canHandleItem(nowHeld) && handler.isItemInfinite(nowHeld)) {
|
|
|
|
|
handler.onPickup(nowHeld);
|
|
|
|
|
NEIClientUtils.setSlotContents(-999, nowHeld, true);
|
|
|
|
|
heldStackInfinite = handler;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean lastKeyTyped(GuiContainer gui, char keyChar, int keyCode) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!NEIClientConfig.isEnabled() || GuiInfo.hasCustomSlots(gui) || isSpreading(gui)) return false;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
Slot slot = GuiContainerManager.getSlotMouseOver(gui);
|
2022-06-24 12:32:13 +01:00
|
|
|
if (slot == null) return false;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
int slotIndex = slot.slotNumber;
|
|
|
|
|
|
|
|
|
|
if (keyCode == NEIClientUtils.mc().gameSettings.keyBindDrop.getKeyCode() && NEIClientUtils.shiftKey()) {
|
|
|
|
|
FastTransferManager.clickSlot(gui, slotIndex);
|
|
|
|
|
fastTransferManager.throwAll(gui, slotIndex);
|
|
|
|
|
FastTransferManager.clickSlot(gui, slotIndex);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean mouseScrolled(GuiContainer gui, int mousex, int mousey, int scrolled) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!NEIClientConfig.isEnabled() || GuiInfo.hasCustomSlots(gui)
|
|
|
|
|
|| !NEIClientConfig.isMouseScrollTransferEnabled())
|
|
|
|
|
return false;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2022-04-28 22:46:01 +09:00
|
|
|
// slot in GuiRecipe does not contain actual stack
|
|
|
|
|
// and scroll should be handled by GuiRecipe, not here
|
2022-06-24 12:32:13 +01:00
|
|
|
if (gui instanceof GuiRecipe) return false;
|
2022-04-28 22:46:01 +09:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
Point mousePos = getMousePosition();
|
|
|
|
|
Slot mouseover = manager.window.getSlotAtPosition(mousePos.x, mousePos.y);
|
|
|
|
|
if (mouseover != null && mouseover.getHasStack()) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (NEIClientConfig.shouldInvertMouseScrollTransfer()) {
|
2021-04-10 22:42:09 -07:00
|
|
|
scrolled = -scrolled;
|
|
|
|
|
}
|
2022-06-24 12:32:13 +01:00
|
|
|
if (scrolled > 0) fastTransferManager.transferItem(manager.window, mouseover.slotNumber);
|
|
|
|
|
else fastTransferManager.retrieveItem(manager.window, mouseover.slotNumber);
|
2022-04-28 22:46:01 +09:00
|
|
|
return true;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean keyTyped(GuiContainer gui, char keyChar, int keyCode) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean mouseClicked(GuiContainer gui, int mousex, int mousey, int button) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-06-24 12:32:13 +01:00
|
|
|
public void onKeyTyped(GuiContainer gui, char keyChar, int keyID) {}
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
@Override
|
2022-06-24 12:32:13 +01:00
|
|
|
public void onMouseClicked(GuiContainer gui, int mousex, int mousey, int button) {}
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
@Override
|
2022-06-24 12:32:13 +01:00
|
|
|
public void onMouseDragged(GuiContainer gui, int mousex, int mousey, int button, long heldTime) {}
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
@Override
|
2022-06-24 12:32:13 +01:00
|
|
|
public void onMouseScrolled(GuiContainer gui, int mousex, int mousey, int scrolled) {}
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
@Override
|
2022-06-24 12:32:13 +01:00
|
|
|
public void onMouseUp(GuiContainer gui, int mousex, int mousey, int button) {}
|
2014-04-29 10:54:20 +10:00
|
|
|
|
|
|
|
|
public static boolean canUseDeleteMode() {
|
|
|
|
|
return !(NEIClientUtils.getGuiContainer() instanceof GuiContainerCreative);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void toggleDeleteMode() {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (canUseDeleteMode()) deleteMode = !deleteMode;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean getDeleteMode() {
|
|
|
|
|
return deleteMode;
|
|
|
|
|
}
|
|
|
|
|
}
|