From 5549bdd679b206ebdf14fa7bdd7548b3048cb10b Mon Sep 17 00:00:00 2001 From: Matthias Mann Date: Sun, 12 Apr 2015 16:43:14 +0200 Subject: [PATCH] add NEI support for the crafting grid --- .../conduit/gui/item/ItemSettings.java | 1 - .../machine/invpanel/BlockInventoryPanel.java | 2 +- .../machine/invpanel/GuiInventoryPanel.java | 18 +- .../machine/invpanel/ICraftingHelper.java | 7 + .../machine/invpanel/InventoryDatabase.java | 12 +- .../invpanel/InventoryPanelContainer.java | 66 ++++- .../InventoryPanelNEIOverlayHandler.java | 277 ++++++++++++++++++ .../machine/invpanel/PacketMoveItems.java | 51 ++++ .../enderio/nei/NEIEnderIOConfig.java | 4 + 9 files changed, 419 insertions(+), 19 deletions(-) create mode 100644 src/main/java/crazypants/enderio/machine/invpanel/ICraftingHelper.java create mode 100644 src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelNEIOverlayHandler.java create mode 100644 src/main/java/crazypants/enderio/machine/invpanel/PacketMoveItems.java diff --git a/src/main/java/crazypants/enderio/conduit/gui/item/ItemSettings.java b/src/main/java/crazypants/enderio/conduit/gui/item/ItemSettings.java index 7fc4db05..e56d347f 100644 --- a/src/main/java/crazypants/enderio/conduit/gui/item/ItemSettings.java +++ b/src/main/java/crazypants/enderio/conduit/gui/item/ItemSettings.java @@ -37,7 +37,6 @@ import crazypants.render.RenderUtil; import crazypants.util.DyeColor; import crazypants.util.Lang; import java.util.ArrayList; -import net.minecraft.util.EnumChatFormatting; public class ItemSettings extends BaseSettingsPanel { diff --git a/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java b/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java index 2ad84ac3..13ef4d87 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/BlockInventoryPanel.java @@ -7,7 +7,6 @@ import crazypants.enderio.GuiHandler; import crazypants.enderio.ModObject; import crazypants.enderio.machine.AbstractMachineBlock; import crazypants.enderio.network.PacketHandler; -import java.util.List; import java.util.Random; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; @@ -21,6 +20,7 @@ public class BlockInventoryPanel extends AbstractMachineBlock { private final String infoTextFilter; private final String infoTextOffline; + private ICraftingHelper craftingHelper; + public GuiInventoryPanel(TileInventoryPanel te, Container container) { super(te, container); redstoneButton.visible = false; @@ -93,11 +95,15 @@ public class GuiInventoryPanel extends GuiMachineBase { addToolTip(new GuiToolTip(new Rectangle(InventoryPanelContainer.FILTER_SLOT_X, InventoryPanelContainer.FILTER_SLOT_Y, 16, 16), list) { @Override public boolean shouldDraw() { - return !((InventoryPanelContainer) inventorySlots).getSlotFilter().getHasStack() && super.shouldDraw(); + return !getContainer().getSlotFilter().getHasStack() && super.shouldDraw(); } }); } + public void setCraftingHelper(ICraftingHelper craftingHelper) { + this.craftingHelper = craftingHelper; + } + @Override public void initGui() { super.initGui(); @@ -192,8 +198,12 @@ public class GuiInventoryPanel extends GuiMachineBase { itemRender.renderItemOverlayIntoGUI(font, mc.renderEngine, stack, x, y, str); } - private InventoryDatabaseClient getDatabase() { - return (InventoryDatabaseClient) getTileEntity().getDatabaseClient(); + public InventoryPanelContainer getContainer() { + return (InventoryPanelContainer) inventorySlots; + } + + public InventoryDatabaseClient getDatabase() { + return getTileEntity().getDatabaseClient(); } private IconEIO getSortOrderIcon() { @@ -373,7 +383,7 @@ public class GuiInventoryPanel extends GuiMachineBase { InventoryPlayer playerInv = mc.thePlayer.inventory; targetSlot = playerInv.getFirstEmptyStack(); if(targetSlot >= 0) { - targetSlot = ((InventoryPanelContainer) inventorySlots).getSlotIndex(playerInv, targetSlot); + targetSlot = getContainer().getSlotIndex(playerInv, targetSlot); } if(targetSlot < 0) { return; diff --git a/src/main/java/crazypants/enderio/machine/invpanel/ICraftingHelper.java b/src/main/java/crazypants/enderio/machine/invpanel/ICraftingHelper.java new file mode 100644 index 00000000..8db7aa43 --- /dev/null +++ b/src/main/java/crazypants/enderio/machine/invpanel/ICraftingHelper.java @@ -0,0 +1,7 @@ +package crazypants.enderio.machine.invpanel; + +public interface ICraftingHelper { + + void refill(GuiInventoryPanel gui, int amount); + +} diff --git a/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java b/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java index 9aeeebe9..e3a8381b 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/InventoryDatabase.java @@ -47,10 +47,8 @@ public abstract class InventoryDatabase { return hint; } - if(!create) { - return null; - } else if(nbt == null && itemID < SIMPLE_MAX_ITEMID && meta < SIMPLE_MAX_META) { - return getSimpleItem(itemID, meta); + if(nbt == null && itemID < SIMPLE_MAX_ITEMID && meta < SIMPLE_MAX_META) { + return getSimpleItem(itemID, meta, create); } else { return getComplexItem(itemID, meta, nbt); } @@ -88,10 +86,10 @@ public abstract class InventoryDatabase { return hash; } - private ItemEntry getSimpleItem(int itemID, int meta) { + private ItemEntry getSimpleItem(int itemID, int meta, boolean create) { Integer dbID = (itemID << SIMPLE_META_BITS) | meta; ItemEntry entry = simpleRegsitry.get(dbID); - if(entry == null) { + if(entry == null && create) { entry = createItemEntry(dbID, dbID, itemID, meta, null); simpleRegsitry.put(dbID, entry); System.out.println("New simple item: " + entry); @@ -102,7 +100,7 @@ public abstract class InventoryDatabase { protected ItemEntry getSimpleItem(int dbID) { int itemID = dbID >> SIMPLE_META_BITS; int meta = dbID & SIMPLE_META_MASK; - return getSimpleItem(itemID, meta); + return getSimpleItem(itemID, meta, true); } public ItemEntry getItem(int dbID) { diff --git a/src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelContainer.java b/src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelContainer.java index 948688d3..ed0308ec 100644 --- a/src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelContainer.java +++ b/src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelContainer.java @@ -46,6 +46,8 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements private int indexFilterSlot; private int startReturnSlot; private int endReturnSlot; + private int startCraftingGrid; + private int endCraftingGrid; @SuppressWarnings("LeakingThisInConstructor") public InventoryPanelContainer(InventoryPlayer playerInv, TileInventoryPanel te) { @@ -96,11 +98,13 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements } }); + startCraftingGrid = inventorySlots.size(); for(int y=0,i=TileInventoryPanel.SLOT_CRAFTING_START ; y<3 ; y++) { for(int x=0 ; x<3 ; x++,i++) { addSlotToContainer(new Slot(tileEntity, i, CRAFTING_GRID_X+x*18, CRAFTING_GRID_Y+y*18)); } } + endCraftingGrid = inventorySlots.size(); indexFilterSlot = inventorySlots.size(); slotFilter = addSlotToContainer(new Slot(tileEntity, TileInventoryPanel.SLOT_VIEW_FILTER, FILTER_SLOT_X, FILTER_SLOT_Y) { @@ -141,6 +145,18 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements return slotFilter; } + public List getCraftingGridSlots() { + return inventorySlots.subList(startCraftingGrid, endCraftingGrid); + } + + public List getReturnAreaSlots() { + return inventorySlots.subList(startReturnSlot, endReturnSlot); + } + + public List getPlayerInventorySlots() { + return inventorySlots.subList(startPlayerSlot, endPlayerSlot); + } + private void removeChangeLog() { if(changedItems != null) { getInventoryPanel().getDatabaseServer().removeChangeLog(this); @@ -198,14 +214,13 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements @Override protected List getTargetSlotsForTransfer(int slotIndex, Slot slot) { - SlotDefinition slotDef = tileEntity.getSlotDefinition(); - if(slotDef.isInputSlot(slotIndex) || slotDef.isUpgradeSlot(slotIndex)) { - return Collections.singletonList(getPlayerInventorySlotRange(false)); - } - if(slotDef.isOutputSlot(slotIndex) || slotIndex == indexCraftingSlot) { + if(slotIndex == indexCraftingSlot) { return Collections.singletonList(getPlayerInventorySlotRange(true)); } - if(slotIndex >= startPlayerSlot) { + if(slotIndex >= startReturnSlot && slotIndex < endReturnSlot) { + return Collections.singletonList(new SlotRange(startReturnSlot, endReturnSlot, false)); + } + if((slotIndex >= startCraftingGrid && slotIndex < endCraftingGrid) || slotIndex >= startPlayerSlot) { ArrayList res = new ArrayList(); res.add(new SlotRange(startReturnSlot, endReturnSlot, false)); addPlayerSlotRanges(res, slotIndex); @@ -304,4 +319,43 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements } } } + + public boolean moveItemsToReturnArea(int fromSlot) { + return moveItems(fromSlot, startReturnSlot, endReturnSlot, Short.MAX_VALUE); + } + + public boolean moveItems(int fromSlot, int toSlotStart, int toSlotEnd, int amount) { + if(!executeMoveItems(fromSlot, toSlotStart, toSlotEnd, amount)) { + return false; + } + if(getTileEntity().getWorldObj().isRemote) { + PacketHandler.INSTANCE.sendToServer(new PacketMoveItems(fromSlot, toSlotStart, toSlotEnd, amount)); + } + return true; + } + + public boolean executeMoveItems(int fromSlot, int toSlotStart, int toSlotEnd, int amount) { + if((fromSlot >= toSlotStart && fromSlot < toSlotEnd) || toSlotEnd <= toSlotStart || amount <= 0) { + return false; + } + + Slot srcSlot = getSlot(fromSlot); + ItemStack src = srcSlot.getStack(); + if(src != null) { + ItemStack toMove = src.copy(); + toMove.stackSize = Math.min(src.stackSize, amount); + int remaining = src.stackSize - toMove.stackSize; + if(mergeItemStack(toMove, toSlotStart, toSlotEnd, false)) { + remaining += toMove.stackSize; + if(remaining == 0) { + srcSlot.putStack(null); + } else { + src.stackSize = remaining; + srcSlot.onSlotChanged(); + } + return true; + } + } + return false; + } } diff --git a/src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelNEIOverlayHandler.java b/src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelNEIOverlayHandler.java new file mode 100644 index 00000000..9448e23d --- /dev/null +++ b/src/main/java/crazypants/enderio/machine/invpanel/InventoryPanelNEIOverlayHandler.java @@ -0,0 +1,277 @@ +package crazypants.enderio.machine.invpanel; + +import codechicken.lib.inventory.InventoryUtils; +import codechicken.nei.LayoutManager; +import codechicken.nei.OffsetPositioner; +import codechicken.nei.PositionedStack; +import codechicken.nei.api.DefaultOverlayRenderer; +import codechicken.nei.api.IOverlayHandler; +import codechicken.nei.api.IStackPositioner; +import codechicken.nei.recipe.IRecipeHandler; +import crazypants.enderio.network.PacketHandler; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +public class InventoryPanelNEIOverlayHandler implements IOverlayHandler { + + private static final int NEI_OFFSET_X = 25; + private static final int NEI_OFFSET_Y = 6; + + private static final int CRAFTING_GRID_OFFSET_X = InventoryPanelContainer.CRAFTING_GRID_X - NEI_OFFSET_X; + private static final int CRAFTING_GRID_OFFSET_Y = InventoryPanelContainer.CRAFTING_GRID_Y - NEI_OFFSET_Y; + + private final IStackPositioner positioner = new OffsetPositioner(CRAFTING_GRID_OFFSET_X, CRAFTING_GRID_OFFSET_Y); + + @Override + public void overlayRecipe(GuiContainer gui, IRecipeHandler recipe, int recipeIndex, boolean shift) { + GuiInventoryPanel guiInvPanel = (GuiInventoryPanel) gui; + System.out.println("InventoryPanelNEIOverlayHandler.overlayRecipe " + recipe + " " + recipeIndex + " " + shift); + List ingredients = recipe.getIngredientStacks(recipeIndex); + + for(PositionedStack stack : ingredients) { + System.out.println("stack={x="+stack.relx+",y="+stack.rely+",item="+stack.item+"}"); + } + + LayoutManager.overlayRenderer = new DefaultOverlayRenderer(ingredients, positioner); + + if(shift) { + shift = clearIngredients(guiInvPanel); + } + + PositionedStack[] slots = mapSlots(ingredients, guiInvPanel.getContainer()); + if(slots != null) { + CraftingHelper helper = new CraftingHelper(slots); + guiInvPanel.setCraftingHelper(helper); + if(shift) { + helper.refill(guiInvPanel, 64); + } + } else { + guiInvPanel.setCraftingHelper(null); + } + } + + private boolean clearIngredients(GuiInventoryPanel gui) { + InventoryPanelContainer c = gui.getContainer(); + for(Slot slot : c.getCraftingGridSlots()) { + if(slot.getHasStack()) { + c.moveItemsToReturnArea(slot.slotNumber); + if(slot.getHasStack()) + return false; + } + } + return true; + } + + private PositionedStack[] mapSlots(List ingredients, InventoryPanelContainer c) { + PositionedStack[] slots = new PositionedStack[9]; + List craftingGrid = c.getCraftingGridSlots(); + int found = 0; + for(PositionedStack pstack : ingredients) { + for(int idx = 0; idx < 9 ; idx++) { + Slot slot = craftingGrid.get(idx); + if(slot.xDisplayPosition == pstack.relx + CRAFTING_GRID_OFFSET_X && slot.yDisplayPosition == pstack.rely + CRAFTING_GRID_OFFSET_Y) { + slots[idx] = pstack; + found++; + break; + } + } + } + if(found != ingredients.size()) { + return null; + } + return slots; + } + + static class CraftingHelper implements ICraftingHelper { + final PositionedStack[] ingredients; + + public CraftingHelper(PositionedStack[] ingredients) { + this.ingredients = ingredients; + } + + @Override + public void refill(GuiInventoryPanel gui, int amount) { + InventoryPanelContainer container = gui.getContainer(); + List craftingGrid = container.getCraftingGridSlots(); + int slotsToProcess = (1<<9)-1; + boolean madeProgress; + int maxAmount = 64; + do { + Candidate[] candidates = new Candidate[9]; + for(int idx = 0; idx < 9; idx++) { + if((slotsToProcess & (1 << idx)) != 0) { + PositionedStack pstack = ingredients[idx]; + Slot slot = craftingGrid.get(idx); + ItemStack stack = slot.getStack(); + if(pstack == null) { + if(stack != null) { + return; + } + } else { + Candidate candidate; + if(stack != null) { + if(!isStackCompatible(pstack, stack)) { + return; + } + candidate = findCandidates(stack, gui, candidates); + } else { + candidate = findAllCandidates(pstack, gui, candidates); + } + + if(candidate == null) { + return; + } + candidate.used++; + candidates[idx] = candidate; + } + } + } + + int targetAmount = maxAmount; + int currentAmount = 0; + for(int idx = 0; idx < 9; idx++) { + Candidate candidate = candidates[idx]; + if(candidate != null) { + Slot slot = craftingGrid.get(idx); + int current = getSlotStackSize(slot); + int maxStackSize = candidate.stack.getMaxStackSize(); + currentAmount = Math.max(currentAmount, current); + if(candidate.stack.isStackable() && maxStackSize > 1) { + targetAmount = Math.min(targetAmount, current + Math.min(maxStackSize, candidate.getAvailable())); + } + } + } + + targetAmount = Math.min(targetAmount, currentAmount + amount); + madeProgress = false; + for(int idx = 0; idx < 9; idx++) { + final int mask = 1 << idx; + Candidate candidate = candidates[idx]; + if(candidate != null) { + Slot slot = craftingGrid.get(idx); + for(Slot srcSlot : candidate.sourceSlots) { + int current = getSlotStackSize(slot); + if(current >= targetAmount) { + break; + } + if(container.moveItems(srcSlot.slotNumber, slot.slotNumber, slot.slotNumber+1, targetAmount - current)) { + slotsToProcess &= ~mask; + madeProgress = true; + } + } + + int current = getSlotStackSize(slot); + if(candidate.entry != null) { + if(current < targetAmount) { + int toMove = Math.min(candidate.entry.getCount(), targetAmount - current); + System.out.println("fetching items from DB"); + PacketHandler.INSTANCE.sendToServer(new PacketFetchItem(candidate.entry, slot.slotNumber, toMove)); + slotsToProcess &= ~mask; + current += toMove; + } + } + + if(current > 0) { + maxAmount = Math.min(maxAmount, current); + } + } + } + }while(madeProgress && slotsToProcess != 0); + } + + private static int getSlotStackSize(Slot slot) { + ItemStack stack = slot.getStack(); + return (stack != null) ? stack.stackSize : 0; + } + + private static boolean isStackCompatible(PositionedStack pstack, ItemStack stack) { + for(ItemStack istack : pstack.items) { + if(InventoryUtils.canStack(stack, istack)) { + return true; + } + } + return false; + } + + private Candidate findAllCandidates(PositionedStack pstack, GuiInventoryPanel gui, Candidate[] candidates) { + Candidate bestInventory = null; + Candidate bestNetwork = null; + for(ItemStack istack : pstack.items) { + Candidate candidate = findCandidates(istack, gui, candidates); + if(candidate.available > 0) { + if(bestInventory == null || bestInventory.available < candidate.available) { + bestInventory = candidate; + } + } + if(candidate.entry != null) { + if(bestNetwork == null || bestNetwork.entry.getCount() < candidate.entry.getCount()) { + bestNetwork = candidate; + } + } + } + if(bestInventory != null) { + return bestInventory; + } else { + return bestNetwork; + } + } + + private Candidate findCandidates(ItemStack stack, GuiInventoryPanel gui, Candidate[] candidates) { + for(Candidate candidate : candidates) { + if(candidate != null && InventoryUtils.canStack(candidate.stack, stack)) { + return candidate; + } + } + Candidate candidate = new Candidate(stack); + InventoryPanelContainer container = gui.getContainer(); + findCandidates(candidate, stack, container.getReturnAreaSlots()); + findCandidates(candidate, stack, container.getPlayerInventorySlots()); + if(candidate.available == 0) { + InventoryDatabaseClient db = gui.getDatabase(); + candidate.entry = db.lookupItem(stack, null, false); + System.out.println("Looked up DB entry: stack=" + stack + " entry="+candidate.entry); + if(candidate.entry != null && candidate.entry.getCount() <= 0) { + candidate.entry = null; + } + } + return candidate; + } + + private void findCandidates(Candidate candidates, ItemStack stack, Collection slots) { + for(Slot slot : slots) { + ItemStack slotStack = slot.getStack(); + if(slotStack != null && InventoryUtils.canStack(slotStack, stack)) { + candidates.sourceSlots.add(slot); + candidates.available += slotStack.stackSize; + } + } + } + } + + static class Candidate { + final ItemStack stack; + final ArrayList sourceSlots = new ArrayList(); + InventoryDatabaseClient.ItemEntry entry; + int available; + int used; + + public Candidate(ItemStack stack) { + this.stack = stack; + } + + public int getAvailable() { + int avail = available; + if(entry != null) { + avail += entry.getCount(); + } + if(avail > 0 && used > 1) { + avail = Math.max(1, avail / used); + } + return avail; + } + } +} diff --git a/src/main/java/crazypants/enderio/machine/invpanel/PacketMoveItems.java b/src/main/java/crazypants/enderio/machine/invpanel/PacketMoveItems.java new file mode 100644 index 00000000..fd08d8f3 --- /dev/null +++ b/src/main/java/crazypants/enderio/machine/invpanel/PacketMoveItems.java @@ -0,0 +1,51 @@ +package crazypants.enderio.machine.invpanel; + +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayerMP; + +public class PacketMoveItems implements IMessage, IMessageHandler { + + private int fromSlot; + private int toSlotStart; + private int toSlotEnd; + private int amount; + + public PacketMoveItems(int fromSlot, int toSlotStart, int toSlotEnd, int amount) { + this.fromSlot = fromSlot; + this.toSlotStart = toSlotStart; + this.toSlotEnd = toSlotEnd; + this.amount = amount; + } + + public PacketMoveItems() { + } + + @Override + public void fromBytes(ByteBuf bb) { + fromSlot = bb.readShort(); + toSlotStart = bb.readShort(); + toSlotEnd = bb.readShort(); + amount = bb.readShort(); + } + + @Override + public void toBytes(ByteBuf bb) { + bb.writeShort(fromSlot); + bb.writeShort(toSlotStart); + bb.writeShort(toSlotEnd); + bb.writeShort(amount); + } + + @Override + public IMessage onMessage(PacketMoveItems message, MessageContext ctx) { + EntityPlayerMP player = ctx.getServerHandler().playerEntity; + if(player.openContainer instanceof InventoryPanelContainer) { + InventoryPanelContainer ipc = (InventoryPanelContainer) player.openContainer; + ipc.executeMoveItems(message.fromSlot, message.toSlotStart, message.toSlotEnd, message.amount); + } + return null; + } +} diff --git a/src/main/java/crazypants/enderio/nei/NEIEnderIOConfig.java b/src/main/java/crazypants/enderio/nei/NEIEnderIOConfig.java index 4e07dfcd..0054c671 100644 --- a/src/main/java/crazypants/enderio/nei/NEIEnderIOConfig.java +++ b/src/main/java/crazypants/enderio/nei/NEIEnderIOConfig.java @@ -7,6 +7,8 @@ import codechicken.nei.api.IConfigureNEI; import crazypants.enderio.EnderIO; import crazypants.enderio.conduit.gas.GasUtil; import crazypants.enderio.config.Config; +import crazypants.enderio.machine.invpanel.GuiInventoryPanel; +import crazypants.enderio.machine.invpanel.InventoryPanelNEIOverlayHandler; import crazypants.enderio.machine.painter.PainterUtil; public class NEIEnderIOConfig implements IConfigureNEI { @@ -31,6 +33,8 @@ public class NEIEnderIOConfig implements IConfigureNEI { API.registerRecipeHandler(new SoulBinderRecipeHandler()); API.registerUsageHandler(new SoulBinderRecipeHandler()); + API.registerGuiOverlayHandler(GuiInventoryPanel.class, new InventoryPanelNEIOverlayHandler(), "crafting"); + API.hideItem(new ItemStack(EnderIO.blockConduitFacade)); API.hideItem(new ItemStack(EnderIO.itemEnderface)); API.hideItem(PainterUtil.applyDefaultPaintedState(new ItemStack(EnderIO.blockPaintedCarpet)));