mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-19 03:49:31 +00:00
extracting items from network via inventory panel GUI
This commit is contained in:
1 parent
b2684cd757
commit
c28dc83c79
10 files changed
+242
-22
No files matched your search
@@ -220,6 +220,12 @@ public class NetworkedInventory {
|
||||
if(numInserted <= 0) {
|
||||
return false;
|
||||
}
|
||||
itemExtracted(slot, numInserted);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public void itemExtracted(int slot, int numInserted) {
|
||||
ItemStack curStack = getInventory().getStackInSlot(slot);
|
||||
if(curStack != null) {
|
||||
if(ticHack) {
|
||||
@@ -239,8 +245,6 @@ public class NetworkedInventory {
|
||||
}
|
||||
con.itemsExtracted(numInserted, slot);
|
||||
tickDeficit = Math.round(numInserted * con.getTickTimePerItem(conDir));
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int insertIntoTargets(ItemStack toExtract) {
|
||||
|
||||
@@ -19,6 +19,7 @@ public class BlockInventoryPanel extends AbstractMachineBlock<TileInventoryPanel
|
||||
PacketHandler.INSTANCE.registerMessage(PacketItemInfo.class, PacketItemInfo.class, PacketHandler.nextID(), Side.CLIENT);
|
||||
PacketHandler.INSTANCE.registerMessage(PacketItemList.class, PacketItemList.class, PacketHandler.nextID(), Side.CLIENT);
|
||||
PacketHandler.INSTANCE.registerMessage(PacketRequestMissingItems.class, PacketRequestMissingItems.class, PacketHandler.nextID(), Side.SERVER);
|
||||
PacketHandler.INSTANCE.registerMessage(PacketFetchItem.class, PacketFetchItem.class, PacketHandler.nextID(), Side.SERVER);
|
||||
|
||||
BlockInventoryPanel panel = new BlockInventoryPanel();
|
||||
panel.init();
|
||||
|
||||
@@ -6,6 +6,7 @@ import crazypants.enderio.gui.IconButtonEIO;
|
||||
import crazypants.enderio.gui.IconEIO;
|
||||
import crazypants.enderio.gui.TextFieldEIO;
|
||||
import crazypants.enderio.machine.gui.GuiMachineBase;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import crazypants.gui.GhostSlot;
|
||||
import crazypants.render.RenderUtil;
|
||||
import java.awt.Rectangle;
|
||||
@@ -13,6 +14,7 @@ import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
@@ -46,10 +48,7 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
|
||||
for(int y = 0; y < GHOST_ROWS; y++) {
|
||||
for(int x = 0; x < GHOST_COLUMNS; x++) {
|
||||
GhostSlot slot = new GhostSlot();
|
||||
slot.x = 109 + x*18;
|
||||
slot.y = 28 + y*18;
|
||||
ghostSlots.add(slot);
|
||||
ghostSlots.add(new InvSlot(109 + x*18, 28 + y*18));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,10 +178,12 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
int index = scrollPos * GHOST_COLUMNS;
|
||||
int count = database.getNumEntries();
|
||||
for(int i = 0; i < GHOST_ROWS*GHOST_COLUMNS; i++,index++) {
|
||||
GhostSlot slot = ghostSlots.get(i);
|
||||
InvSlot slot = (InvSlot) ghostSlots.get(i);
|
||||
if(index < count) {
|
||||
slot.stack = database.getItemStack(index);
|
||||
slot.entry = database.getItemEntry(index);
|
||||
slot.stack = slot.entry.makeItemStack();
|
||||
} else {
|
||||
slot.entry = null;
|
||||
slot.stack = null;
|
||||
}
|
||||
}
|
||||
@@ -231,4 +232,49 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
super.mouseClickMove(x, y, button, time);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void ghostSlotClicked(GhostSlot slot, int x, int y, int button) {
|
||||
if(slot instanceof InvSlot) {
|
||||
InvSlot invSlot = (InvSlot) slot;
|
||||
if(invSlot.entry != null && invSlot.stack != null) {
|
||||
int targetSlot;
|
||||
int count = Math.min(invSlot.stack.stackSize, invSlot.stack.getMaxStackSize());
|
||||
|
||||
if(button == 0) {
|
||||
if(isShiftKeyDown()) {
|
||||
InventoryPlayer playerInv = mc.thePlayer.inventory;
|
||||
targetSlot = playerInv.getFirstEmptyStack();
|
||||
if(targetSlot >= 0) {
|
||||
targetSlot = ((InventoryPanelContainer) inventorySlots).getSlotIndex(playerInv, targetSlot);
|
||||
}
|
||||
if(targetSlot < 0) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
targetSlot = -1;
|
||||
}
|
||||
} else if(button == 1) {
|
||||
targetSlot = -1;
|
||||
if(isCtrlKeyDown()) {
|
||||
count = 1;
|
||||
} else {
|
||||
count = (count + 1) / 2;
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
PacketHandler.INSTANCE.sendToServer(new PacketFetchItem(invSlot.entry, targetSlot, count));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class InvSlot extends GhostSlot {
|
||||
InventoryDatabaseClient.ItemEntry entry;
|
||||
|
||||
InvSlot(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public abstract class InventoryDatabase<ItemEntry extends ItemEntryBase> {
|
||||
complexItems = new ArrayList<ItemEntry>();
|
||||
}
|
||||
|
||||
public ItemEntry lookupItem(ItemStack stack, ItemEntry hint) {
|
||||
public ItemEntry lookupItem(ItemStack stack, ItemEntry hint, boolean create) {
|
||||
if(stack == null || stack.getItem() == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -47,7 +47,9 @@ public abstract class InventoryDatabase<ItemEntry extends ItemEntryBase> {
|
||||
return hint;
|
||||
}
|
||||
|
||||
if(nbt == null && itemID < SIMPLE_MAX_ITEMID && meta < SIMPLE_MAX_META) {
|
||||
if(!create) {
|
||||
return null;
|
||||
} else if(nbt == null && itemID < SIMPLE_MAX_ITEMID && meta < SIMPLE_MAX_META) {
|
||||
return getSimpleItem(itemID, meta);
|
||||
} else {
|
||||
return getComplexItem(itemID, meta, nbt);
|
||||
@@ -115,6 +117,13 @@ public abstract class InventoryDatabase<ItemEntry extends ItemEntryBase> {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ItemEntry getExistingItem(int dbID) {
|
||||
if(dbID < COMPLEX_DBINDEX_START) {
|
||||
return simpleRegsitry.get(dbID);
|
||||
}
|
||||
return getItem(dbID);
|
||||
}
|
||||
|
||||
static final class ItemEntryKey {
|
||||
public final int hash;
|
||||
public final int itemID;
|
||||
|
||||
@@ -275,10 +275,6 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
return filteredItems.get(index);
|
||||
}
|
||||
|
||||
public ItemStack getItemStack(int index) {
|
||||
return getItemEntry(index).makeItemStack();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemEntry createItemEntry(int dbId, int hash, int itemID, int meta, NBTTagCompound nbt) {
|
||||
return new ItemEntry(dbId, hash, itemID, meta, nbt);
|
||||
@@ -298,10 +294,6 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
return count;
|
||||
}
|
||||
|
||||
public Item getItem() {
|
||||
return Item.getItemById(itemID);
|
||||
}
|
||||
|
||||
public ItemStack makeItemStack() {
|
||||
ItemStack stack = new ItemStack(getItem(), count, meta);
|
||||
stack.stackTagCompound = nbt;
|
||||
|
||||
@@ -7,8 +7,10 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@@ -206,6 +208,20 @@ public class InventoryDatabaseServer extends InventoryDatabase<InventoryDatabase
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int extractItems(InventoryDatabaseServer db, int count) {
|
||||
int extracted = 0;
|
||||
Integer[] copy = slots.toArray(new Integer[slots.size()]);
|
||||
for(Integer idx : copy) {
|
||||
int niIndex = idx >> 12;
|
||||
int slotIndex = idx & 4095;
|
||||
InventoryKey key = db.inventories[niIndex];
|
||||
int amount = key.extractItem(db, this, slotIndex, niIndex, count);
|
||||
count -= amount;
|
||||
extracted += amount;
|
||||
}
|
||||
return extracted;
|
||||
}
|
||||
}
|
||||
|
||||
static class InventoryKey {
|
||||
@@ -244,7 +260,7 @@ public class InventoryDatabaseServer extends InventoryDatabase<InventoryDatabase
|
||||
db.entryChanged(current);
|
||||
}
|
||||
} else {
|
||||
ItemEntry key = db.lookupItem(stack, current);
|
||||
ItemEntry key = db.lookupItem(stack, current, true);
|
||||
if(key != current) {
|
||||
slotItems[slot] = key;
|
||||
itemCounts[slot] = stack.stackSize;
|
||||
@@ -274,6 +290,38 @@ public class InventoryDatabaseServer extends InventoryDatabase<InventoryDatabase
|
||||
slotItems = new ItemEntry[count];
|
||||
itemCounts = new int[count];
|
||||
}
|
||||
|
||||
int extractItem(InventoryDatabaseServer db, ItemEntry entry, int slot, int niIndex, int count) {
|
||||
ItemStack[] items = ni.getExtractableItemStacks();
|
||||
if(items == null || slot >= items.length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ItemStack stack = items[slot];
|
||||
if(stack == null || db.lookupItem(stack, entry, false) != entry) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int remaining = stack.stackSize;
|
||||
if(count > remaining) {
|
||||
count = remaining;
|
||||
}
|
||||
|
||||
ni.itemExtracted(slot, count);
|
||||
remaining -= count;
|
||||
|
||||
if(itemCounts[slot] != remaining) {
|
||||
itemCounts[slot] = remaining;
|
||||
if(remaining == 0) {
|
||||
slotItems[slot] = null;
|
||||
entry.removeSlot(niIndex, slot);
|
||||
}
|
||||
db.entryChanged(entry);
|
||||
}
|
||||
|
||||
System.out.println("result " + count);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ChangeLog {
|
||||
|
||||
@@ -175,7 +175,7 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
|
||||
public void sendChangeLog() {
|
||||
if(!changedItems.isEmpty() && !crafters.isEmpty()) {
|
||||
try {
|
||||
InventoryDatabaseServer db = getInventoryPanel().getDatabaseServer();
|
||||
InventoryDatabaseServer db = getInventoryPanel().getDatabaseServer();
|
||||
byte[] compressed = db.compressChangedItems(changedItems);
|
||||
PacketItemList pil = new PacketItemList(getInventoryPanel(), compressed);
|
||||
for(Object crafting : crafters) {
|
||||
@@ -191,4 +191,68 @@ public class InventoryPanelContainer extends AbstractMachineContainer implements
|
||||
changedItems.clear();
|
||||
}
|
||||
|
||||
public int getSlotIndex(IInventory inv, int index) {
|
||||
for (int i = 0; i < inventorySlots.size(); i++) {
|
||||
Slot slot = (Slot)inventorySlots.get(i);
|
||||
if (slot.isSlotInInventory(inv, index)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void executeFetchItems(EntityPlayerMP player, int dbID, int targetSlot, int count) {
|
||||
InventoryDatabaseServer db = getInventoryPanel().getDatabaseServer();
|
||||
InventoryDatabaseServer.ItemEntry entry = db.getExistingItem(dbID);
|
||||
if(entry != null) {
|
||||
ItemStack targetStack;
|
||||
Slot slot;
|
||||
int maxStackSize;
|
||||
|
||||
if(targetSlot < 0) {
|
||||
slot = null;
|
||||
targetStack = player.inventory.getItemStack();
|
||||
maxStackSize = player.inventory.getInventoryStackLimit();
|
||||
|
||||
if(targetStack != null && targetStack.stackSize > 0) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
slot = getSlot(targetSlot);
|
||||
targetStack = slot.getStack();
|
||||
maxStackSize = slot.getSlotStackLimit();
|
||||
}
|
||||
|
||||
//System.out.println("executeFetchItems targetSlot="+targetSlot+" slot="+slot+" maxStackSize="+maxStackSize+" count="+count);
|
||||
|
||||
ItemStack tmpStack = new ItemStack(entry.getItem(), 0, entry.meta);
|
||||
tmpStack.stackTagCompound = entry.nbt;
|
||||
maxStackSize = Math.min(maxStackSize, tmpStack.getMaxStackSize());
|
||||
|
||||
if(targetStack != null && targetStack.stackSize > 0) {
|
||||
if(!ItemUtil.areStackMergable(tmpStack, targetStack)) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
targetStack = tmpStack.copy();
|
||||
}
|
||||
|
||||
count = Math.min(count, maxStackSize - targetStack.stackSize);
|
||||
if(count > 0) {
|
||||
int extracted = entry.extractItems(db, count);
|
||||
if(extracted > 0) {
|
||||
targetStack.stackSize += extracted;
|
||||
|
||||
sendChangeLog();
|
||||
|
||||
if(slot != null) {
|
||||
slot.putStack(targetStack);
|
||||
} else {
|
||||
player.inventory.setItemStack(targetStack);
|
||||
player.updateHeldItem();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package crazypants.enderio.machine.invpanel;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public class ItemEntryBase {
|
||||
@@ -37,6 +38,10 @@ public class ItemEntryBase {
|
||||
(this.nbt == nbt) || (this.nbt != null && this.nbt.equals(nbt));
|
||||
}
|
||||
|
||||
public Item getItem() {
|
||||
return Item.getItemById(itemID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ItemEntryBase{" + "dbID=" + dbID + ", hash=" + hash + ", itemID=" + itemID + ", meta=" + meta + ", nbt=" + nbt + '}';
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
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 PacketFetchItem implements IMessage, IMessageHandler<PacketFetchItem, IMessage> {
|
||||
|
||||
private int dbID;
|
||||
private int targetSlot;
|
||||
private int count;
|
||||
|
||||
public PacketFetchItem() {
|
||||
}
|
||||
|
||||
public PacketFetchItem(InventoryDatabaseClient.ItemEntry entry, int targetSlot, int count) {
|
||||
this.dbID = entry.dbID;
|
||||
this.targetSlot = targetSlot;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf bb) {
|
||||
dbID = bb.readInt();
|
||||
targetSlot = bb.readShort();
|
||||
count = bb.readShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf bb) {
|
||||
bb.writeInt(dbID);
|
||||
bb.writeShort(targetSlot);
|
||||
bb.writeShort(count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketFetchItem message, MessageContext ctx) {
|
||||
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
|
||||
if(player.openContainer instanceof InventoryPanelContainer) {
|
||||
InventoryPanelContainer ipc = (InventoryPanelContainer) player.openContainer;
|
||||
ipc.executeFetchItems(player, message.dbID, message.targetSlot, message.count);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -158,6 +158,11 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe
|
||||
return ghostSlots;
|
||||
}
|
||||
|
||||
protected void ghostSlotClicked(GhostSlot slot, int x, int y, int button) {
|
||||
ItemStack st = Minecraft.getMinecraft().thePlayer.inventory.getItemStack();
|
||||
slot.putStack(st);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void mouseClicked(int x, int y, int button) {
|
||||
for (GuiTextField f : textFields) {
|
||||
@@ -178,8 +183,7 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe
|
||||
if(!ghostSlots.isEmpty()) {
|
||||
GhostSlot slot = getGhostSlot(x, y);
|
||||
if(slot != null) {
|
||||
ItemStack st = Minecraft.getMinecraft().thePlayer.inventory.getItemStack();
|
||||
slot.putStack(st);
|
||||
ghostSlotClicked(slot, x, y, button);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user