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

560 lines
15 KiB
Java
Raw Normal View History

2014-04-29 10:54:20 +10:00
package codechicken.nei;
2022-02-06 17:42:11 +02:00
import codechicken.lib.vec.Rectangle4i;
2014-04-29 10:54:20 +10:00
import codechicken.nei.api.GuiInfo;
import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.recipe.GuiCraftingRecipe;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.GuiUsageRecipe;
2022-01-02 23:30:54 +02:00
import codechicken.nei.recipe.StackInfo;
2014-04-29 10:54:20 +10:00
import net.minecraft.client.gui.inventory.GuiContainer;
2022-01-02 23:30:54 +02:00
import net.minecraft.client.gui.inventory.GuiContainerCreative;
2014-04-29 10:54:20 +10:00
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
2022-01-02 23:30:54 +02:00
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;
2014-04-29 10:54:20 +10:00
2022-02-06 17:42:11 +02:00
import static codechicken.lib.gui.GuiDraw.drawRect;
import static codechicken.nei.LayoutManager.searchField;
2014-04-29 10:54:20 +10:00
import java.util.ArrayList;
import java.util.regex.Pattern;
2014-04-29 10:54:20 +10:00
2022-01-02 23:30:54 +02:00
public class ItemPanel extends Widget
{
public ItemStack draggedStack;
public int mouseDownSlot = -1;
public Button pagePrev;
public Button pageNext;
public Label pageLabel;
2014-04-29 10:54:20 +10:00
2020-01-20 09:42:39 -08:00
/**
* Backwards compat :-/
*/
2022-01-02 23:30:54 +02:00
@Deprecated
private int itemsPerPage = 0;
@Deprecated
2020-01-20 09:42:39 -08:00
public static ArrayList<ItemStack> items = new ArrayList<>();
2022-01-02 23:30:54 +02:00
@Deprecated
2020-01-20 09:42:39 -08:00
public ArrayList<ItemStack> realItems = new ArrayList<>();
protected Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");
2022-01-02 23:30:54 +02:00
public static class ItemPanelSlot
2014-04-29 10:54:20 +10:00
{
public ItemStack item;
2014-04-29 10:54:20 +10:00
public int slotIndex;
2022-01-02 23:30:54 +02:00
public ItemPanelSlot(int idx, ItemStack stack)
{
slotIndex = idx;
item = stack;
}
@Deprecated
public ItemPanelSlot(int idx)
{
this(idx, ItemPanels.itemPanel.getGrid().getItem(idx));
2014-04-29 10:54:20 +10:00
}
2022-01-02 23:30:54 +02:00
2014-04-29 10:54:20 +10:00
}
2022-01-02 23:30:54 +02:00
protected static class ItemPanelGrid extends ItemsGrid
{
public ArrayList<ItemStack> newItems;
2022-01-02 23:30:54 +02:00
public void setItems(ArrayList<ItemStack> items)
{
newItems = items;
}
2019-12-24 13:37:41 -08:00
2022-01-02 23:30:54 +02:00
public void refresh(GuiContainer gui)
{
2019-12-24 13:37:41 -08:00
2022-01-02 23:30:54 +02:00
if (newItems != null) {
realItems = newItems;
newItems = null;
}
2022-01-02 23:30:54 +02:00
super.refresh(gui);
}
2022-02-06 17:42:11 +02:00
@Override
protected void drawItem(Rectangle4i rect, int idx, ItemPanelSlot focus)
{
if (PresetsWidget.inEditMode() && !PresetsWidget.isHidden(getItem(idx))) {
drawRect(rect.x, rect.y, rect.w, rect.h, 0xee555555);
}
super.drawItem(rect, idx, PresetsWidget.inEditMode()? null: focus);
}
2022-01-02 23:30:54 +02:00
}
2022-01-02 23:30:54 +02:00
protected ItemsGrid grid = new ItemPanelGrid();
2019-12-24 13:37:41 -08:00
2022-01-02 23:30:54 +02:00
public ArrayList<ItemStack> getItems()
{
return grid.getItems();
}
public static void updateItemList(ArrayList<ItemStack> newItems)
{
((ItemPanelGrid) ItemPanels.itemPanel.getGrid()).setItems(newItems);
ItemPanels.itemPanel.realItems = newItems;
}
@Deprecated
public void scroll(int i)
{
grid.shiftPage(i);
}
public ItemsGrid getGrid()
{
return grid;
}
2019-12-24 13:37:41 -08:00
public void init() {
pageLabel = new Label("(0/0)", true);
2022-01-02 23:30:54 +02:00
pagePrev = new Button("Prev")
2019-12-24 13:37:41 -08:00
{
public boolean onButtonPress(boolean rightclick) {
if (!rightclick) {
2022-01-02 23:30:54 +02:00
grid.shiftPage(-1);
2019-12-24 13:37:41 -08:00
return true;
}
return false;
}
@Override
public String getRenderLabel() {
return "<";
}
};
2022-01-02 23:30:54 +02:00
pageNext = new Button("Next")
2019-12-24 13:37:41 -08:00
{
public boolean onButtonPress(boolean rightclick) {
if (!rightclick) {
2022-01-02 23:30:54 +02:00
grid.shiftPage(1);
2019-12-24 13:37:41 -08:00
return true;
}
return false;
}
@Override
public String getRenderLabel() {
return ">";
}
};
2020-02-04 20:11:17 -08:00
}
2022-01-02 23:30:54 +02:00
public int getMarginLeft(GuiContainer gui)
{
if (gui instanceof GuiContainerCreative && NEIClientUtils.getGamemode() == 2) {
//then activate creative+ first render creative gui and oly then creative+
return (gui.width + 176) / 2 + 2;
}
2019-12-24 13:37:41 -08:00
2022-01-02 23:30:54 +02:00
return (gui.width + gui.xSize) / 2 + 2;
2019-12-24 13:37:41 -08:00
}
2022-01-02 23:30:54 +02:00
public int getMarginTop(GuiContainer gui)
{
2019-12-24 13:37:41 -08:00
return 2;
}
2022-01-02 23:30:54 +02:00
public int getWidth(GuiContainer gui)
{
return LayoutManager.getSideWidth(gui) - getMarginLeft(gui);
2019-12-24 13:37:41 -08:00
}
2022-01-02 23:30:54 +02:00
public int getHeight(GuiContainer gui)
{
// - 19 = 2 + 16 + 1 -> input in bottom
return gui.height - getMarginTop(gui) - 19;
2019-12-24 13:37:41 -08:00
}
2022-01-02 23:30:54 +02:00
public String getLabelText()
{
return "(" + getPage() + "/" + Math.max(1, getNumPages()) + ")";
2020-01-20 09:42:39 -08:00
}
2022-01-02 23:30:54 +02:00
public void resize(GuiContainer gui)
{
final int marginLeft = getMarginLeft(gui);
final int marginTop = getMarginTop(gui);
final int width = getWidth(gui);
final int height = getHeight(gui);
2019-12-24 13:37:41 -08:00
final int buttonHeight = 16;
final int buttonWidth = 16;
2022-01-02 23:30:54 +02:00
pagePrev.w = pageNext.w = buttonWidth;
pagePrev.h = pageNext.h = buttonHeight;
pagePrev.y = pageNext.y = marginTop;
2022-01-02 23:30:54 +02:00
pagePrev.x = marginLeft;
pageNext.x = marginLeft + width - buttonWidth;
2019-12-24 13:37:41 -08:00
2022-01-02 23:30:54 +02:00
pageLabel.x = marginLeft + width / 2;
pageLabel.y = marginTop + 5;
2019-12-24 13:37:41 -08:00
pageLabel.text = getLabelText();
2014-04-29 10:54:20 +10:00
2022-01-02 23:30:54 +02:00
y = marginTop + buttonHeight + 2;
x = marginLeft;
w = width;
h = height - buttonHeight - 2;
2014-04-29 10:54:20 +10:00
2022-01-02 23:30:54 +02:00
grid.setGridSize(x, y, w, h);
grid.refresh(gui);
2014-04-29 10:54:20 +10:00
}
2022-01-02 23:30:54 +02:00
public void setVisible()
{
LayoutManager.addWidget(pagePrev);
LayoutManager.addWidget(pageNext);
2019-12-24 13:37:41 -08:00
LayoutManager.addWidget(pageLabel);
2014-04-29 10:54:20 +10:00
}
@Override
2022-01-02 23:30:54 +02:00
public void draw(int mousex, int mousey)
{
grid.draw(mousex, mousey);
2014-04-29 10:54:20 +10:00
}
2014-04-29 10:54:20 +10:00
@Override
public void postDraw(int mousex, int mousey) {
if (draggedStack != null) {
2014-04-29 10:54:20 +10:00
GuiContainerManager.drawItems.zLevel += 100;
2022-01-02 23:30:54 +02:00
GuiContainerManager.drawItem(mousex - 8, mousey - 8, draggedStack, true);
2014-04-29 10:54:20 +10:00
GuiContainerManager.drawItems.zLevel -= 100;
}
}
2014-04-29 10:54:20 +10:00
@Override
public void mouseDragged(int mousex, int mousey, int button, long heldTime) {
if (mouseDownSlot >= 0 && draggedStack == null && NEIClientUtils.getHeldItem() == null &&
2019-12-24 13:37:41 -08:00
NEIClientConfig.hasSMPCounterPart() && !GuiInfo.hasCustomSlots(NEIClientUtils.getGuiContainer())) {
2014-04-29 10:54:20 +10:00
ItemPanelSlot mouseOverSlot = getSlotMouseOver(mousex, mousey);
2022-01-02 23:30:54 +02:00
if (mouseOverSlot == null || mouseOverSlot.slotIndex != mouseDownSlot || heldTime > 500) {
draggedStack = getDraggedStackWithQuantity(mouseDownSlot);
2022-02-06 17:42:11 +02:00
mouseDownSlot = -1;
2014-04-29 10:54:20 +10:00
}
2022-01-02 23:30:54 +02:00
2014-04-29 10:54:20 +10:00
}
}
2022-01-02 23:30:54 +02:00
protected ItemStack getDraggedStackWithQuantity(int mouseDownSlot)
{
ItemStack stack = grid.getItem(mouseDownSlot);
if (stack != null) {
int amount = NEIClientConfig.getItemQuantity();
if (amount == 0) {
amount = stack.getMaxStackSize();
}
return NEIServerUtils.copyStack(stack, amount);
}
return null;
}
2014-04-29 10:54:20 +10:00
@Override
2022-01-02 23:30:54 +02:00
public boolean handleClick(int mousex, int mousey, int button)
{
if (handleClickExt(mousex, mousey, button))
2014-04-29 10:54:20 +10:00
return true;
if (NEIClientUtils.getHeldItem() != null) {
2022-01-02 23:30:54 +02:00
if (!grid.contains(mousex, mousey)) {
return false;
}
if (NEIClientConfig.canPerformAction("delete") && NEIClientConfig.canPerformAction("item"))
if (button == 1)
2014-04-29 10:54:20 +10:00
NEIClientUtils.decreaseSlotStack(-999);
else
NEIClientUtils.deleteHeldItem();
else
NEIClientUtils.dropHeldItem();
2014-04-29 10:54:20 +10:00
return true;
}
ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
if (hoverSlot != null) {
2022-01-02 23:30:54 +02:00
if (button == 2) {
2022-01-02 23:30:54 +02:00
if (hoverSlot.item != null) {
draggedStack = getDraggedStackWithQuantity(hoverSlot.slotIndex);
2014-04-29 10:54:20 +10:00
}
2022-01-02 23:30:54 +02:00
} else {
2014-04-29 10:54:20 +10:00
mouseDownSlot = hoverSlot.slotIndex;
}
2022-01-02 23:30:54 +02:00
2014-04-29 10:54:20 +10:00
return true;
}
return false;
}
2022-01-02 23:30:54 +02:00
@Override
public boolean handleClickExt(int mouseX, int mouseY, int button)
{
if (ItemPanels.itemPanel.draggedStack != null) {
return ItemPanels.itemPanel.handleDraggedClick(mouseX, mouseY, button);
}
if (ItemPanels.bookmarkPanel.draggedStack != null) {
return ItemPanels.bookmarkPanel.handleDraggedClick(mouseX, mouseY, button);
}
return false;
}
protected boolean handleDraggedClick(int mouseX, int mouseY, int button)
{
if (draggedStack == null) {
return false;
}
if (handleFillFluidContainerClick(mouseX, mouseY, button)) {
return true;
}
if (handleSearchInputClick(mouseX, mouseY, button)) {
return true;
}
2022-01-02 23:30:54 +02:00
if (handleGUIContainerClick(mouseX, mouseY, button)) {
return true;
}
if (handleCheatItemClick(mouseX, mouseY, button)) {
return true;
}
final GuiContainer gui = NEIClientUtils.getGuiContainer();
if (mouseX < gui.guiLeft || mouseY < gui.guiTop || mouseX >= gui.guiLeft + gui.xSize || mouseY >= gui.guiTop + gui.ySize) {
draggedStack = null;
}
return true;
}
protected boolean handleFillFluidContainerClick(int mouseX, int mouseY, int button)
{
if (button == 2) {
2014-04-29 10:54:20 +10:00
return false;
2022-01-02 23:30:54 +02:00
}
ItemPanelSlot mouseOverSlot = ItemPanels.itemPanel.getSlotMouseOver(mouseX, mouseY);
if (mouseOverSlot == null) {
mouseOverSlot = ItemPanels.bookmarkPanel.getSlotMouseOver(mouseX, mouseY);
}
if (mouseOverSlot != null && draggedStack.getItem() instanceof IFluidContainerItem) {
FluidStack fluidStack = StackInfo.getFluid(mouseOverSlot.item);
if (fluidStack != null) {
final int stackSize = draggedStack.stackSize;
fluidStack = fluidStack.copy();
if (fluidStack.amount == 0) {
fluidStack.amount = 1000;
}
if (mouseOverSlot.item.stackSize > 1) {
fluidStack.amount *= mouseOverSlot.item.stackSize;
}
draggedStack.stackSize = 1;
((IFluidContainerItem) draggedStack.getItem()).fill(draggedStack, fluidStack, true);
draggedStack.stackSize = stackSize;
if (button == 1 && ((IFluidContainerItem) draggedStack.getItem()).getFluid(draggedStack) != null) {
ItemPanels.bookmarkPanel.addOrRemoveItem(draggedStack.copy());
}
}
return true;
}
2022-01-02 23:30:54 +02:00
return false;
}
protected boolean handleGUIContainerClick(int mouseX, int mouseY, int button)
{
final GuiContainer gui = NEIClientUtils.getGuiContainer();
2014-04-29 10:54:20 +10:00
boolean handled = false;
2022-01-02 23:30:54 +02:00
try {
GuiInfo.readLock.lock();
for (INEIGuiHandler handler : GuiInfo.guiHandlers) {
2022-01-02 23:30:54 +02:00
if (handler.handleDragNDrop(gui, mouseX, mouseY, draggedStack, button)) {
handled = true;
2022-01-02 23:30:54 +02:00
break;
2014-04-29 10:54:20 +10:00
}
}
} finally {
GuiInfo.readLock.unlock();
}
2022-01-02 23:30:54 +02:00
if (draggedStack.stackSize == 0) {
draggedStack = null;
}
return handled;
}
protected boolean handleCheatItemClick(int mouseX, int mouseY, int button)
{
final GuiContainer gui = NEIClientUtils.getGuiContainer();
final Slot overSlot = gui.getSlotAtPosition(mouseX, mouseY);
if (overSlot != null && overSlot.isItemValid(draggedStack)) {
2022-01-02 23:30:54 +02:00
if (NEIClientConfig.canCheatItem(draggedStack)) {
2014-04-29 10:54:20 +10:00
int contents = overSlot.getHasStack() ? overSlot.getStack().stackSize : 0;
2022-01-02 23:30:54 +02:00
final int add = button == 0 ? draggedStack.stackSize : 1;
if (overSlot.getHasStack() && !NEIServerUtils.areStacksSameType(draggedStack, overSlot.getStack())) {
2014-04-29 10:54:20 +10:00
contents = 0;
2022-01-02 23:30:54 +02:00
}
final int total = Math.min(contents + add, Math.min(overSlot.getSlotStackLimit(), draggedStack.getMaxStackSize()));
if (total > contents) {
2014-04-29 10:54:20 +10:00
NEIClientUtils.setSlotContents(overSlot.slotNumber, NEIServerUtils.copyStack(draggedStack, total), true);
NEICPH.sendGiveItem(NEIServerUtils.copyStack(draggedStack, total), false, false);
draggedStack.stackSize -= total - contents;
2014-04-29 10:54:20 +10:00
}
2022-01-02 23:30:54 +02:00
if (draggedStack.stackSize == 0) {
2014-04-29 10:54:20 +10:00
draggedStack = null;
2022-01-02 23:30:54 +02:00
}
} else {
2014-04-29 10:54:20 +10:00
draggedStack = null;
}
return true;
}
return false;
}
protected boolean handleSearchInputClick(int mouseX, int mouseY, int button)
{
if (searchField.contains(mouseX, mouseY)) {
final FluidStack fluidStack = StackInfo.getFluid(draggedStack);
if (fluidStack != null) {
searchField.setText(SPECIAL_REGEX_CHARS.matcher(fluidStack.getLocalizedName()).replaceAll("\\\\$0"));
} else {
searchField.setText(SPECIAL_REGEX_CHARS.matcher(draggedStack.getDisplayName()).replaceAll("\\\\$0"));
}
2022-01-02 23:30:54 +02:00
return true;
}
2022-01-02 23:30:54 +02:00
return false;
2014-04-29 10:54:20 +10:00
}
2014-04-29 10:54:20 +10:00
@Override
public void mouseUp(int mousex, int mousey, int button) {
ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
2022-01-02 23:30:54 +02:00
if (hoverSlot != null && hoverSlot.slotIndex == mouseDownSlot && draggedStack == null) {
ItemStack item = hoverSlot.item;
2022-01-02 23:30:54 +02:00
if (NEIController.manager.window instanceof GuiRecipe || !NEIClientConfig.canCheatItem(item)) {
2022-01-02 23:30:54 +02:00
if (button == 0)
2014-04-29 10:54:20 +10:00
GuiCraftingRecipe.openRecipeGui("item", item);
else if (button == 1)
2014-04-29 10:54:20 +10:00
GuiUsageRecipe.openRecipeGui("item", item);
mouseDownSlot = -1;
return;
}
2022-01-02 23:30:54 +02:00
NEIClientUtils.cheatItem(getDraggedStackWithQuantity(hoverSlot.slotIndex), button, -1);
2014-04-29 10:54:20 +10:00
}
2014-04-29 10:54:20 +10:00
mouseDownSlot = -1;
}
@Override
public boolean onMouseWheel(int i, int mousex, int mousey) {
if (!contains(mousex, mousey))
2014-04-29 10:54:20 +10:00
return false;
2022-01-02 23:30:54 +02:00
grid.shiftPage(-i);
2014-04-29 10:54:20 +10:00
return true;
}
2014-04-29 10:54:20 +10:00
@Override
public boolean handleKeyPress(int keyID, char keyChar) {
2022-01-02 23:30:54 +02:00
if (keyID == NEIClientConfig.getKeyBinding("gui.next")) {
2022-01-02 23:30:54 +02:00
grid.shiftPage(1);
2014-04-29 10:54:20 +10:00
return true;
}
2022-01-02 23:30:54 +02:00
if (keyID == NEIClientConfig.getKeyBinding("gui.prev")) {
2022-01-02 23:30:54 +02:00
grid.shiftPage(-1);
2014-04-29 10:54:20 +10:00
return true;
}
2022-01-02 23:30:54 +02:00
2014-04-29 10:54:20 +10:00
return false;
}
@Override
public ItemStack getStackMouseOver(int mousex, int mousey) {
2014-04-29 10:54:20 +10:00
ItemPanelSlot slot = getSlotMouseOver(mousex, mousey);
return slot == null ? null : slot.item;
2014-04-29 10:54:20 +10:00
}
2022-01-02 23:30:54 +02:00
public ItemPanelSlot getSlotMouseOver(int mousex, int mousey)
{
return grid.getSlotMouseOver(mousex, mousey);
2014-04-29 10:54:20 +10:00
}
public int getPage() {
2022-01-02 23:30:54 +02:00
return grid.page + 1;
2014-04-29 10:54:20 +10:00
}
public int getNumPages() {
2022-01-02 23:30:54 +02:00
return grid.numPages;
2014-04-29 10:54:20 +10:00
}
@Override
2022-01-02 23:30:54 +02:00
public boolean contains(int px, int py)
{
return grid.contains(px, py);
}
2022-01-02 23:30:54 +02:00
2014-04-29 10:54:20 +10:00
}