Files
NotEnoughItems/src/main/java/codechicken/nei/NEIController.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

290 lines
11 KiB
Java
Raw Normal View History

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;
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;
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
public class NEIController implements IContainerSlotClickHandler, IContainerInputHandler {
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;
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;
if (!NEIClientConfig.isEnabled()) return;
2014-04-29 10:54:20 +10:00
fastTransferManager = new FastTransferManager();
if (gui instanceof INEIGuiHandler) API.registerNEIGuiHandler((INEIGuiHandler) gui);
2014-04-29 10:54:20 +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());
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) {
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);
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)) {
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) {
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;
} 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) {
if (!NEIClientConfig.isEnabled()) return;
2014-04-29 10:54:20 +10:00
firstheld = NEIClientUtils.getHeldItem();
}
@Override
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();
if (itemstack1 != null) NEIClientUtils.deleteItemsOfType(itemstack1);
2014-04-29 10:54:20 +10:00
} else if (button == 1) NEIClientUtils.decreaseSlotStack(slot.slotNumber);
else NEIClientUtils.deleteSlotStack(slot.slotNumber);
2014-04-29 10:54:20 +10:00
return true;
}
if (button == 1 && slot instanceof SlotCrafting) // right click
2014-04-29 10:54:20 +10: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;
}
if (NEIClientUtils.controlKey() && modifier != 4 // Drop key pressed:
// net.minecraft.client.gui.inventory.GuiContainer.keyTyped
&& slot != null
&& slot.getStack() != null
&& slot.isItemValid(slot.getStack())) {
NEIClientUtils.cheatItem(slot.getStack(), button, 1);
return true;
}
if (GuiInfo.hasCustomSlots(gui)) return false;
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) {
if (!NEIClientConfig.isEnabled()) return;
2014-04-29 10:54:20 +10:00
ItemStack nowHeld = NEIClientUtils.getHeldItem();
if (firstheld != nowHeld) pickedUpFromSlot = slotIndex;
2014-04-29 10:54:20 +10:00
if (NEIClientConfig.canPerformAction("item") && NEIClientConfig.hasSMPCounterPart()) {
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);
}
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) {
if (!NEIClientConfig.isEnabled() || GuiInfo.hasCustomSlots(gui) || isSpreading(gui)) return false;
2014-04-29 10:54:20 +10:00
Slot slot = GuiContainerManager.getSlotMouseOver(gui);
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) {
if (!NEIClientConfig.isEnabled() || GuiInfo.hasCustomSlots(gui)
|| !NEIClientConfig.isMouseScrollTransferEnabled())
return false;
2014-04-29 10:54:20 +10:00
// slot in GuiRecipe does not contain actual stack
// and scroll should be handled by GuiRecipe, not here
if (gui instanceof GuiRecipe) return false;
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()) {
if (NEIClientConfig.shouldInvertMouseScrollTransfer()) {
scrolled = -scrolled;
}
if (scrolled > 0) fastTransferManager.transferItem(manager.window, mouseover.slotNumber);
else fastTransferManager.retrieveItem(manager.window, mouseover.slotNumber);
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
public void onKeyTyped(GuiContainer gui, char keyChar, int keyID) {}
2014-04-29 10:54:20 +10:00
@Override
public void onMouseClicked(GuiContainer gui, int mousex, int mousey, int button) {}
2014-04-29 10:54:20 +10:00
@Override
public void onMouseDragged(GuiContainer gui, int mousex, int mousey, int button, long heldTime) {}
2014-04-29 10:54:20 +10:00
@Override
public void onMouseScrolled(GuiContainer gui, int mousex, int mousey, int scrolled) {}
2014-04-29 10:54:20 +10:00
@Override
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() {
if (canUseDeleteMode()) deleteMode = !deleteMode;
2014-04-29 10:54:20 +10:00
}
public static boolean getDeleteMode() {
return deleteMode;
}
}