allow storing the current crafting recipe for the crafting helper

This commit is contained in:
Matthias Mann committed 2015-05-09 09:19:39 +02:00
1 parent 70e09e5164
commit 63d7aa24ae
6 files changed
+269 -207

No files matched your search

@@ -25,6 +25,7 @@ import crazypants.enderio.gui.TextFieldEIO;
import crazypants.enderio.gui.TooltipAddera;
import crazypants.enderio.gui.VScrollbarEIO;
import crazypants.enderio.machine.gui.GuiMachineBase;
import crazypants.enderio.machine.invpanel.client.CraftingHelper;
import crazypants.enderio.machine.invpanel.client.DatabaseView;
import crazypants.enderio.machine.invpanel.client.ICraftingHelper;
import crazypants.enderio.machine.invpanel.client.InventoryDatabaseClient;
@@ -55,6 +56,7 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
private final TextFieldEIO tfFilter;
private final IconButtonEIO btnSort;
private final GuiToolTip ttRefill;
private final GuiToolTip ttSetReceipe;
private final VScrollbarEIO scrollbar;
private final MultiIconButtonEIO btnClear;
@@ -130,6 +132,16 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
ttRefill.setVisible(false);
addToolTip(ttRefill);
list.clear();
TooltipAddera.addTooltipFromResources(list, "enderio.gui.inventorypanel.tooltip.setrecipe.line");
ttSetReceipe = new GuiToolTip(btnRefill, list) {
@Override
public boolean shouldDraw() {
return super.shouldDraw() && getContainer().hasCraftingRecipe();
}
};
addToolTip(ttSetReceipe);
list.clear();
TooltipAddera.addTooltipFromResources(list, "enderio.gui.inventorypanel.tooltip.clear.line");
btnClear.setToolTip(list.toArray(new String[list.size()]));
@@ -147,6 +159,7 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
public void setCraftingHelper(ICraftingHelper craftingHelper) {
this.craftingHelper = craftingHelper;
ttRefill.setVisible(craftingHelper != null);
ttSetReceipe.setVisible(craftingHelper == null);
}
@Override
@@ -170,7 +183,7 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
if(getContainer().clearCraftingGrid()) {
if(craftingHelper != null) {
craftingHelper.remove();
craftingHelper = null;
setCraftingHelper(null);
}
}
}
@@ -189,6 +202,10 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
boolean hover = btnRefill.contains(mouseX - sx, mouseY - sy);
int iconX = hover ? (isShiftKeyDown() ? 48 : 24) : 0;
drawTexturedModalRect(sx + btnRefill.x - 2, sy + btnRefill.y - 2, iconX, 232, 24, 24);
} else if(getContainer().hasCraftingRecipe()) {
boolean hover = btnRefill.contains(mouseX - sx, mouseY - sy);
int iconX = hover ? 96 : 72;
drawTexturedModalRect(sx + btnRefill.x - 2, sy + btnRefill.y - 2, iconX, 232, 24, 24);
}
int headerColor = 0x404040;
@@ -328,12 +345,21 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
x -= guiLeft;
y -= guiTop;
if(craftingHelper != null && btnRefill.contains(x, y)) {
mc.getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F));
craftingHelper.refill(this, isShiftKeyDown() ? 64 : 1);
if(btnRefill.contains(x, y)) {
if(craftingHelper != null) {
playClickSound();
craftingHelper.refill(this, isShiftKeyDown() ? 64 : 1);
} else if(getContainer().hasCraftingRecipe()) {
playClickSound();
setCraftingHelper(CraftingHelper.createFromSlots(getContainer().getCraftingGridSlots()));
}
}
}
private void playClickSound() {
mc.getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F));
}
@Override
protected void mouseWheel(int x, int y, int delta) {
super.mouseWheel(x, y, delta);
@@ -238,6 +238,10 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
return cleared;
}
public boolean hasCraftingRecipe() {
return getSlot(slotCraftResult).getHasStack();
}
@Override
protected List<SlotRange> getTargetSlotsForTransfer(int slotIndex, Slot slot) {
if((slotIndex == slotCraftResult) || (slotIndex >= firstSlotReturn && slotIndex < endSlotReturn)) {
@@ -0,0 +1,221 @@
package crazypants.enderio.machine.invpanel.client;
import crazypants.enderio.machine.invpanel.GuiInventoryPanel;
import crazypants.enderio.machine.invpanel.InventoryPanelContainer;
import crazypants.enderio.machine.invpanel.PacketFetchItem;
import crazypants.enderio.network.PacketHandler;
import crazypants.util.ItemUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
public class CraftingHelper implements ICraftingHelper {
final ItemStack[][] ingredients;
public CraftingHelper(ItemStack[][] ingredients) {
this.ingredients = ingredients;
}
public static ICraftingHelper createFromSlots(List<Slot> slots) {
if (slots.size() != 9) {
return null;
}
ItemStack[][] ingredients = new ItemStack[9][];
int count = 0;
for (int idx = 0; idx < 9; idx++) {
Slot slot = slots.get(idx);
ItemStack stack = slot.getStack();
if(stack != null) {
stack = stack.copy();
stack.stackSize = 1;
ingredients[idx] = new ItemStack[] { stack };
count++;
}
}
if (count > 0) {
return new CraftingHelper(ingredients);
}
return null;
}
@Override
public void remove() {
}
@Override
public void refill(GuiInventoryPanel gui, int amount) {
InventoryPanelContainer container = gui.getContainer();
InventoryDatabaseClient db = gui.getDatabase();
List<Slot> 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) {
ItemStack[] 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, db, candidates);
} else {
candidate = findAllCandidates(pstack, gui, db, 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);
PacketHandler.INSTANCE.sendToServer(new PacketFetchItem(db.getGeneration(), 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(ItemStack[] pstack, ItemStack stack) {
for (ItemStack istack : pstack) {
if (ItemUtil.areStackMergable(stack, istack)) {
return true;
}
}
return false;
}
private Candidate findAllCandidates(ItemStack[] pstack, GuiInventoryPanel gui, InventoryDatabaseClient db, Candidate[] candidates) {
Candidate bestInventory = null;
Candidate bestNetwork = null;
for (ItemStack istack : pstack) {
Candidate candidate = findCandidates(istack, gui, db, 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, InventoryDatabaseClient db, Candidate[] candidates) {
for (Candidate candidate : candidates) {
if (candidate != null && ItemUtil.areStackMergable(candidate.stack, stack)) {
return candidate;
}
}
Candidate candidate = new Candidate(stack);
InventoryPanelContainer container = gui.getContainer();
findCandidates(candidate, stack, container.getReturnAreaSlots());
findCandidates(candidate, stack, container.getPlayerInventorySlots());
findCandidates(candidate, stack, container.getPlayerHotbarSlots());
if (candidate.available == 0 && db != null) {
candidate.entry = db.lookupItem(stack, null, false);
if (candidate.entry != null && candidate.entry.getCount() <= 0) {
candidate.entry = null;
}
}
return candidate;
}
private void findCandidates(Candidate candidates, ItemStack stack, Collection<Slot> slots) {
for (Slot slot : slots) {
ItemStack slotStack = slot.getStack();
if (slotStack != null && ItemUtil.areStackMergable(slotStack, stack)) {
candidates.sourceSlots.add(slot);
candidates.available += slotStack.stackSize;
}
}
}
static class Candidate {
final ItemStack stack;
final ArrayList<Slot> sourceSlots = new ArrayList<Slot>();
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;
}
}
}
@@ -1,6 +1,5 @@
package crazypants.enderio.machine.invpanel.client;
import codechicken.lib.inventory.InventoryUtils;
import codechicken.nei.LayoutManager;
import codechicken.nei.OffsetPositioner;
import codechicken.nei.PositionedStack;
@@ -10,10 +9,6 @@ import codechicken.nei.api.IStackPositioner;
import codechicken.nei.recipe.IRecipeHandler;
import crazypants.enderio.machine.invpanel.GuiInventoryPanel;
import crazypants.enderio.machine.invpanel.InventoryPanelContainer;
import crazypants.enderio.machine.invpanel.PacketFetchItem;
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;
@@ -40,9 +35,15 @@ public class InventoryPanelNEIOverlayHandler implements IOverlayHandler {
shift = guiInvPanel.getContainer().clearCraftingGrid();
}
PositionedStack[] slots = mapSlots(ingredients, guiInvPanel.getContainer());
ItemStack[][] slots = mapSlots(ingredients, guiInvPanel.getContainer());
if(slots != null) {
CraftingHelper helper = new CraftingHelper(slots);
CraftingHelper helper = new CraftingHelper(slots) {
@Override
public void remove() {
super.remove();
LayoutManager.overlayRenderer = null;
}
};
guiInvPanel.setCraftingHelper(helper);
if(shift) {
helper.refill(guiInvPanel, 64);
@@ -52,15 +53,15 @@ public class InventoryPanelNEIOverlayHandler implements IOverlayHandler {
}
}
private PositionedStack[] mapSlots(List<PositionedStack> ingredients, InventoryPanelContainer c) {
PositionedStack[] slots = new PositionedStack[9];
private ItemStack[][] mapSlots(List<PositionedStack> ingredients, InventoryPanelContainer c) {
ItemStack[][] slots = new ItemStack[9][];
List<Slot> 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;
slots[idx] = pstack.items;
found++;
break;
}
@@ -71,197 +72,4 @@ public class InventoryPanelNEIOverlayHandler implements IOverlayHandler {
}
return slots;
}
static class CraftingHelper implements ICraftingHelper {
final PositionedStack[] ingredients;
public CraftingHelper(PositionedStack[] ingredients) {
this.ingredients = ingredients;
}
@Override
public void remove() {
LayoutManager.overlayRenderer = null;
}
@Override
public void refill(GuiInventoryPanel gui, int amount) {
InventoryPanelContainer container = gui.getContainer();
InventoryDatabaseClient db = gui.getDatabase();
List<Slot> 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, db, candidates);
} else {
candidate = findAllCandidates(pstack, gui, db, 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);
PacketHandler.INSTANCE.sendToServer(new PacketFetchItem(db.getGeneration(), 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, InventoryDatabaseClient db, Candidate[] candidates) {
Candidate bestInventory = null;
Candidate bestNetwork = null;
for(ItemStack istack : pstack.items) {
Candidate candidate = findCandidates(istack, gui, db, 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, InventoryDatabaseClient db, 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());
findCandidates(candidate, stack, container.getPlayerHotbarSlots());
if(candidate.available == 0 && db != null) {
candidate.entry = db.lookupItem(stack, null, false);
if(candidate.entry != null && candidate.entry.getCount() <= 0) {
candidate.entry = null;
}
}
return candidate;
}
private void findCandidates(Candidate candidates, ItemStack stack, Collection<Slot> 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<Slot> sourceSlots = new ArrayList<Slot>();
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;
}
}
}