2014-04-29 10:54:20 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
|
|
|
|
import codechicken.lib.vec.Rectangle4i;
|
|
|
|
|
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;
|
|
|
|
|
import net.minecraft.client.gui.inventory.GuiContainer;
|
|
|
|
|
import net.minecraft.inventory.Slot;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
import static codechicken.lib.gui.GuiDraw.drawRect;
|
|
|
|
|
|
|
|
|
|
public class ItemPanel extends Widget
|
|
|
|
|
{
|
2014-06-29 20:31:37 +10:00
|
|
|
public static ArrayList<ItemStack> items = new ArrayList<ItemStack>();
|
|
|
|
|
/**
|
|
|
|
|
* Swapped into visible items on update
|
|
|
|
|
*/
|
|
|
|
|
private static ArrayList<ItemStack> _items = items;
|
|
|
|
|
|
|
|
|
|
public static void updateItemList(ArrayList<ItemStack> newItems) {
|
|
|
|
|
_items = newItems;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
public class ItemPanelSlot
|
|
|
|
|
{
|
2014-06-29 20:31:37 +10:00
|
|
|
public ItemStack item;
|
2014-04-29 10:54:20 +10:00
|
|
|
public int slotIndex;
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
public ItemPanelSlot(int index) {
|
|
|
|
|
item = items.get(index);
|
|
|
|
|
slotIndex = index;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
public ItemStack draggedStack;
|
|
|
|
|
public int mouseDownSlot = -1;
|
|
|
|
|
|
|
|
|
|
private int marginLeft;
|
|
|
|
|
private int marginTop;
|
|
|
|
|
private int rows;
|
|
|
|
|
private int columns;
|
|
|
|
|
|
|
|
|
|
private boolean[] validSlotMap;
|
|
|
|
|
private int firstIndex;
|
|
|
|
|
private int itemsPerPage;
|
|
|
|
|
|
|
|
|
|
private int page;
|
|
|
|
|
private int numPages;
|
|
|
|
|
|
|
|
|
|
public void resize() {
|
|
|
|
|
items = _items;
|
|
|
|
|
|
|
|
|
|
marginLeft = x + (w % 18) / 2;
|
|
|
|
|
marginTop = y + (h % 18) / 2;
|
|
|
|
|
columns = w / 18;
|
|
|
|
|
rows = h / 18;
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
calculatePage();
|
|
|
|
|
updateValidSlots();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
private void calculatePage() {
|
|
|
|
|
if (itemsPerPage == 0)
|
|
|
|
|
numPages = 0;
|
|
|
|
|
else
|
|
|
|
|
numPages = (int) Math.ceil((float) items.size() / (float) itemsPerPage);
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
if (firstIndex >= items.size())
|
2014-04-29 10:54:20 +10:00
|
|
|
firstIndex = 0;
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
if (numPages == 0)
|
|
|
|
|
page = 0;
|
|
|
|
|
else
|
|
|
|
|
page = firstIndex / itemsPerPage + 1;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
private void updateValidSlots() {
|
2014-06-29 20:31:37 +10:00
|
|
|
GuiContainer gui = NEIClientUtils.getGuiContainer();
|
|
|
|
|
validSlotMap = new boolean[rows * columns];
|
|
|
|
|
itemsPerPage = 0;
|
|
|
|
|
for (int i = 0; i < validSlotMap.length; i++)
|
|
|
|
|
if (slotValid(gui, i)) {
|
|
|
|
|
validSlotMap[i] = true;
|
|
|
|
|
itemsPerPage++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
private boolean slotValid(GuiContainer gui, int i) {
|
2014-06-29 20:31:37 +10:00
|
|
|
Rectangle4i rect = getSlotRect(i);
|
|
|
|
|
for (INEIGuiHandler handler : GuiInfo.guiHandlers)
|
|
|
|
|
if (handler.hideItemPanelSlot(gui, rect.x, rect.y, rect.w, rect.h))
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
public Rectangle4i getSlotRect(int i) {
|
2014-06-29 20:31:37 +10:00
|
|
|
return getSlotRect(i / columns, i % columns);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
public Rectangle4i getSlotRect(int row, int column) {
|
2014-06-29 20:31:37 +10:00
|
|
|
return new Rectangle4i(marginLeft + column * 18, marginTop + row * 18, 18, 18);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public void draw(int mousex, int mousey) {
|
|
|
|
|
if (itemsPerPage == 0)
|
2014-04-29 10:54:20 +10:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
GuiContainerManager.enableMatrixStackLogging();
|
|
|
|
|
int index = firstIndex;
|
2014-06-29 20:31:37 +10:00
|
|
|
for (int i = 0; i < rows * columns && index < items.size(); i++) {
|
|
|
|
|
if (validSlotMap[i]) {
|
2014-04-29 10:54:20 +10:00
|
|
|
Rectangle4i rect = getSlotRect(i);
|
2014-06-29 20:31:37 +10:00
|
|
|
if (rect.contains(mousex, mousey))
|
2014-04-29 10:54:20 +10:00
|
|
|
drawRect(rect.x, rect.y, rect.w, rect.h, 0xee555555);//highlight
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
GuiContainerManager.drawItem(rect.x + 1, rect.y + 1, items.get(index));
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
index++;
|
2014-06-29 20:31:37 +10:00
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
GuiContainerManager.disableMatrixStackLogging();
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public void postDraw(int mousex, int mousey) {
|
|
|
|
|
if (draggedStack != null) {
|
2014-04-29 10:54:20 +10:00
|
|
|
GuiContainerManager.drawItems.zLevel += 100;
|
|
|
|
|
GuiContainerManager.drawItem(mousex - 8, mousey - 8, draggedStack);
|
|
|
|
|
GuiContainerManager.drawItems.zLevel -= 100;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public void mouseDragged(int mousex, int mousey, int button, long heldTime) {
|
|
|
|
|
if (mouseDownSlot >= 0 && draggedStack == null && NEIClientUtils.getHeldItem() == null &&
|
|
|
|
|
NEIClientConfig.hasSMPCounterPart() && !GuiInfo.hasCustomSlots(NEIClientUtils.getGuiContainer())) {
|
2014-04-29 10:54:20 +10:00
|
|
|
ItemPanelSlot mouseOverSlot = getSlotMouseOver(mousex, mousey);
|
2014-06-29 20:31:37 +10:00
|
|
|
ItemStack stack = new ItemPanelSlot(mouseDownSlot).item;
|
|
|
|
|
if (stack != null && (mouseOverSlot == null || mouseOverSlot.slotIndex != mouseDownSlot || heldTime > 500)) {
|
2014-04-29 10:54:20 +10:00
|
|
|
int amount = NEIClientConfig.getItemQuantity();
|
2014-06-29 20:31:37 +10:00
|
|
|
if (amount == 0)
|
2014-04-29 10:54:20 +10:00
|
|
|
amount = stack.getMaxStackSize();
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
draggedStack = NEIServerUtils.copyStack(stack, amount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public boolean handleClick(int mousex, int mousey, int button) {
|
|
|
|
|
if (handleDraggedClick(mousex, mousey, button))
|
2014-04-29 10:54:20 +10:00
|
|
|
return true;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
if (NEIClientUtils.getHeldItem() != null) {
|
|
|
|
|
for (INEIGuiHandler handler : GuiInfo.guiHandlers)
|
|
|
|
|
if (handler.hideItemPanelSlot(NEIClientUtils.getGuiContainer(), mousex, mousey, 1, 1))
|
|
|
|
|
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-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
|
|
|
|
|
if (hoverSlot != null) {
|
|
|
|
|
if (button == 2) {
|
|
|
|
|
ItemStack stack = hoverSlot.item;
|
|
|
|
|
if (stack != null) {
|
2014-04-29 10:54:20 +10:00
|
|
|
int amount = NEIClientConfig.getItemQuantity();
|
2014-06-29 20:31:37 +10:00
|
|
|
if (amount == 0)
|
2014-04-29 10:54:20 +10:00
|
|
|
amount = stack.getMaxStackSize();
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
draggedStack = NEIServerUtils.copyStack(stack, amount);
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
} else {
|
2014-04-29 10:54:20 +10:00
|
|
|
mouseDownSlot = hoverSlot.slotIndex;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
private boolean handleDraggedClick(int mousex, int mousey, int button) {
|
|
|
|
|
if (draggedStack == null)
|
2014-04-29 10:54:20 +10:00
|
|
|
return false;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
GuiContainer gui = NEIClientUtils.getGuiContainer();
|
|
|
|
|
boolean handled = false;
|
2014-06-29 20:31:37 +10:00
|
|
|
for (INEIGuiHandler handler : GuiInfo.guiHandlers)
|
|
|
|
|
if (handler.handleDragNDrop(gui, mousex, mousey, draggedStack, button)) {
|
2014-04-29 10:54:20 +10:00
|
|
|
handled = true;
|
2014-06-29 20:31:37 +10:00
|
|
|
if (draggedStack.stackSize == 0) {
|
2014-04-29 10:54:20 +10:00
|
|
|
draggedStack = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
if (handled)
|
2014-04-29 10:54:20 +10:00
|
|
|
return true;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
Slot overSlot = gui.getSlotAtPosition(mousex, mousey);
|
2014-06-29 20:31:37 +10:00
|
|
|
if (overSlot != null && overSlot.isItemValid(draggedStack)) {
|
|
|
|
|
if (NEIClientConfig.canCheatItem(draggedStack)) {
|
2014-04-29 10:54:20 +10:00
|
|
|
int contents = overSlot.getHasStack() ? overSlot.getStack().stackSize : 0;
|
|
|
|
|
int add = button == 0 ? draggedStack.stackSize : 1;
|
2014-06-29 20:31:37 +10:00
|
|
|
if (overSlot.getHasStack() && !NEIServerUtils.areStacksSameType(draggedStack, overSlot.getStack()))
|
2014-04-29 10:54:20 +10:00
|
|
|
contents = 0;
|
2014-06-29 20:31:37 +10:00
|
|
|
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);
|
2014-06-29 20:31:37 +10:00
|
|
|
draggedStack.stackSize -= total - contents;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
if (draggedStack.stackSize == 0)
|
2014-04-29 10:54:20 +10:00
|
|
|
draggedStack = null;
|
2014-06-29 20:31:37 +10:00
|
|
|
} else {
|
2014-04-29 10:54:20 +10:00
|
|
|
draggedStack = null;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
} else if (mousex < gui.guiLeft || mousey < gui.guiTop || mousex >= gui.guiLeft + gui.xSize || mousey >= gui.guiTop + gui.ySize) {
|
2014-04-29 10:54:20 +10:00
|
|
|
draggedStack = null;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public boolean handleClickExt(int mousex, int mousey, int button) {
|
2014-04-29 10:54:20 +10:00
|
|
|
return handleDraggedClick(mousex, mousey, button);
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public void mouseUp(int mousex, int mousey, int button) {
|
|
|
|
|
ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
|
|
|
|
|
if (hoverSlot != null && hoverSlot.slotIndex == mouseDownSlot && draggedStack == null) {
|
|
|
|
|
ItemStack item = hoverSlot.item;
|
|
|
|
|
if (NEIController.manager.window instanceof GuiRecipe || !NEIClientConfig.canCheatItem(item)) {
|
|
|
|
|
if (button == 0)
|
2014-04-29 10:54:20 +10:00
|
|
|
GuiCraftingRecipe.openRecipeGui("item", item);
|
2014-06-29 20:31:37 +10:00
|
|
|
else if (button == 1)
|
2014-04-29 10:54:20 +10:00
|
|
|
GuiUsageRecipe.openRecipeGui("item", item);
|
|
|
|
|
|
|
|
|
|
draggedStack = null;
|
|
|
|
|
mouseDownSlot = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
NEIClientUtils.cheatItem(item, button, -1);
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
mouseDownSlot = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public boolean onMouseWheel(int i, int mousex, int mousey) {
|
|
|
|
|
if (!contains(mousex, mousey))
|
2014-04-29 10:54:20 +10:00
|
|
|
return false;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
scroll(-i);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public boolean handleKeyPress(int keyID, char keyChar) {
|
|
|
|
|
if (keyID == NEIClientConfig.getKeyBinding("gui.next")) {
|
2014-04-29 10:54:20 +10:00
|
|
|
scroll(1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
if (keyID == NEIClientConfig.getKeyBinding("gui.prev")) {
|
2014-04-29 10:54:20 +10:00
|
|
|
scroll(-1);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public ItemStack getStackMouseOver(int mousex, int mousey) {
|
2014-04-29 10:54:20 +10:00
|
|
|
ItemPanelSlot slot = getSlotMouseOver(mousex, mousey);
|
2014-06-29 20:31:37 +10:00
|
|
|
return slot == null ? null : slot.item;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public ItemPanelSlot getSlotMouseOver(int mousex, int mousey) {
|
|
|
|
|
int index = firstIndex;
|
|
|
|
|
for (int i = 0; i < rows * columns && index < items.size(); i++)
|
|
|
|
|
if (validSlotMap[i]) {
|
|
|
|
|
if (getSlotRect(i).contains(mousex, mousey))
|
|
|
|
|
return new ItemPanelSlot(index);
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
return null;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public void scroll(int i) {
|
|
|
|
|
if (itemsPerPage != 0) {
|
|
|
|
|
int oldIndex = firstIndex;
|
|
|
|
|
firstIndex += i * itemsPerPage;
|
|
|
|
|
if (firstIndex >= items.size())
|
|
|
|
|
firstIndex = 0;
|
|
|
|
|
if (firstIndex < 0)
|
|
|
|
|
if (oldIndex > 0)
|
|
|
|
|
firstIndex = 0;
|
|
|
|
|
else
|
|
|
|
|
firstIndex = (items.size() - 1) / itemsPerPage * itemsPerPage;
|
|
|
|
|
|
|
|
|
|
calculatePage();
|
|
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public int getPage() {
|
2014-04-29 10:54:20 +10:00
|
|
|
return page;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public int getNumPages() {
|
2014-04-29 10:54:20 +10:00
|
|
|
return numPages;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean contains(int px, int py) {
|
|
|
|
|
GuiContainer gui = NEIClientUtils.getGuiContainer();
|
|
|
|
|
Rectangle4i rect = new Rectangle4i(px, py, 1, 1);
|
|
|
|
|
for (INEIGuiHandler handler : GuiInfo.guiHandlers)
|
|
|
|
|
if (handler.hideItemPanelSlot(gui, rect.x, rect.y, rect.w, rect.h))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return super.contains(px, py);
|
|
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|