diff --git a/src/main/java/net/shadowmage/ancientwarfare/automation/container/ContainerWarehouseCraftingStation.java b/src/main/java/net/shadowmage/ancientwarfare/automation/container/ContainerWarehouseCraftingStation.java index 243da9b32..0f1b56434 100644 --- a/src/main/java/net/shadowmage/ancientwarfare/automation/container/ContainerWarehouseCraftingStation.java +++ b/src/main/java/net/shadowmage/ancientwarfare/automation/container/ContainerWarehouseCraftingStation.java @@ -5,17 +5,27 @@ import net.minecraft.inventory.Slot; import net.minecraft.inventory.SlotCrafting; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.util.Constants; +import net.shadowmage.ancientwarfare.automation.tile.warehouse2.TileWarehouseBase; import net.shadowmage.ancientwarfare.automation.tile.warehouse2.TileWarehouseCraftingStation; import net.shadowmage.ancientwarfare.core.api.AWItems; +import net.shadowmage.ancientwarfare.core.config.AWLog; import net.shadowmage.ancientwarfare.core.container.ContainerBase; +import net.shadowmage.ancientwarfare.core.inventory.ItemQuantityMap; +import net.shadowmage.ancientwarfare.core.inventory.ItemQuantityMap.ItemHashEntry; import net.shadowmage.ancientwarfare.core.item.ItemResearchBook; public class ContainerWarehouseCraftingStation extends ContainerBase { public TileWarehouseCraftingStation station; +public ItemQuantityMap itemMap = new ItemQuantityMap(); +public ItemQuantityMap cache = new ItemQuantityMap(); +boolean shouldUpdate = true; -public ContainerWarehouseCraftingStation(EntityPlayer player, int x, int y, int z) +public ContainerWarehouseCraftingStation(final EntityPlayer player, int x, int y, int z) { super(player, x, y, z); station = (TileWarehouseCraftingStation) player.worldObj.getTileEntity(x, y, z); @@ -60,6 +70,16 @@ public boolean isItemValid(ItemStack par1ItemStack) int y1 = 8+3*18+8; y1 = this.addPlayerSlots(player, 8, y1, 4); + TileWarehouseBase warehouse = station.getWarehouse(); + if(warehouse!=null){warehouse.addCraftingViewer(this);} + } + +@Override +public void onContainerClosed(EntityPlayer par1EntityPlayer) + { + TileWarehouseBase warehouse = station.getWarehouse(); + if(warehouse!=null){warehouse.removeCraftingViewer(this);} + super.onContainerClosed(par1EntityPlayer); } @Override @@ -115,5 +135,109 @@ else if(slotClickedIndex >=playerSlotStart &&slotClickedIndex < 36+playerSlotSta return slotStackCopy; } +@Override +public void handlePacketData(NBTTagCompound tag) + { + if(tag.hasKey("changeList")) + { + AWLog.logDebug("rec. warehouse item map.."); + handleChangeList(tag.getTagList("changeList", Constants.NBT.TAG_COMPOUND)); + } + refreshGui(); + } + +@Override +public void detectAndSendChanges() + { + super.detectAndSendChanges(); + if(shouldUpdate) + { + synchItemMaps(); + shouldUpdate = false; + } + } + +private void handleChangeList(NBTTagList changeList) + { + NBTTagCompound tag; + int qty; + ItemHashEntry wrap = null; + for(int i = 0; i < changeList.tagCount(); i++) + { + tag = changeList.getCompoundTagAt(i); + wrap = ItemHashEntry.readFromNBT(tag); + qty = tag.getInteger("qty"); + if(qty==0) + { + itemMap.remove(wrap); + } + else + { + itemMap.put(wrap, qty); + } + } + TileWarehouseBase warehouse = station.getWarehouse(); + if(warehouse!=null) + { + warehouse.clearItemCache(); + warehouse.addItemsToCache(itemMap); + } + } + +private void synchItemMaps() + { + /** + * need to loop through this.itemMap and compare quantities to warehouse.itemMap + * add any changes to change-list + * need to loop through warehouse.itemMap and find new entries + * add any new entries to change-list + */ + + cache.clear(); + TileWarehouseBase warehouse = station.getWarehouse(); + if(warehouse!=null) + { + warehouse.getItems(cache); + } + ItemQuantityMap warehouseItemMap = cache; + int qty; + NBTTagList changeList = new NBTTagList(); + NBTTagCompound tag; + for(ItemHashEntry wrap : this.itemMap.keySet()) + { + qty = this.itemMap.getCount(wrap); + if(qty!=warehouseItemMap.getCount(wrap)) + { + qty = warehouseItemMap.getCount(wrap); + tag = wrap.writeToNBT(new NBTTagCompound()); + tag.setInteger("qty", qty); + changeList.appendTag(tag); + this.itemMap.put(wrap, qty); + } + } + for(ItemHashEntry entry : warehouseItemMap.keySet()) + { + if(!itemMap.contains(entry)) + { + qty = warehouseItemMap.getCount(entry); + tag = ItemHashEntry.writeToNBT(entry, new NBTTagCompound()); + tag.setInteger("qty", qty); + changeList.appendTag(tag); + this.itemMap.put(entry, qty); + } + } + if(changeList.tagCount()>0) + { + tag = new NBTTagCompound(); + tag.setTag("changeList", changeList); + sendDataToClient(tag); + } + } + +public void onWarehouseInventoryUpdated() + { + AWLog.logDebug("update callback from warehouse..."); + shouldUpdate = true; + } } diff --git a/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseBase.java b/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseBase.java index 389c83fb6..59a6c3594 100644 --- a/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseBase.java +++ b/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseBase.java @@ -11,6 +11,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.shadowmage.ancientwarfare.automation.container.ContainerWarehouseControl; +import net.shadowmage.ancientwarfare.automation.container.ContainerWarehouseCraftingStation; import net.shadowmage.ancientwarfare.automation.tile.warehouse2.TileWarehouseInterface.InterfaceEmptyRequest; import net.shadowmage.ancientwarfare.automation.tile.warehouse2.TileWarehouseInterface.InterfaceFillRequest; import net.shadowmage.ancientwarfare.automation.tile.worksite.TileWorksiteBounded; @@ -46,7 +47,7 @@ public abstract class TileWarehouseBase extends TileWorksiteBounded implements I private ItemQuantityMap cachedItemMap = new ItemQuantityMap(); private Set viewers = new HashSet(); - +private Set craftingViewers = new HashSet(); public TileWarehouseBase() { @@ -265,6 +266,10 @@ public final void getItems(ItemQuantityMap map) map.addAll(cachedItemMap); } +public final void clearItemCache(){cachedItemMap.clear();} + +public final void addItemsToCache(ItemQuantityMap map){cachedItemMap.addAll(map);} + @Override public final void updateEntity() { @@ -345,18 +350,21 @@ public final void addViewer(ContainerWarehouseControl viewer) viewers.add(viewer); } +public final void addCraftingViewer(ContainerWarehouseCraftingStation viewer) + { + if(worldObj.isRemote){return;} + craftingViewers.add(viewer); + } + public final void removeViewer(ContainerWarehouseControl viewer){viewers.remove(viewer);} +public final void removeCraftingViewer(ContainerWarehouseCraftingStation viewer){craftingViewers.remove(viewer);} + public final void updateViewers() { - for(ContainerWarehouseControl viewer : viewers) - { - viewer.onWarehouseInventoryUpdated(); - } - for(TileWarehouseStockViewer viewer : stockViewers) - { - viewer.onWarehouseInventoryUpdated(); - } + for(ContainerWarehouseControl viewer : viewers){viewer.onWarehouseInventoryUpdated();} + for(TileWarehouseStockViewer viewer : stockViewers){viewer.onWarehouseInventoryUpdated();} + for(ContainerWarehouseCraftingStation viewer : craftingViewers){viewer.onWarehouseInventoryUpdated();} } public final void addStorageTile(IWarehouseStorageTile tile) @@ -499,7 +507,7 @@ public final boolean onBlockClicked(EntityPlayer player) @Override public final boolean shouldRenderInPass(int pass) { - return pass==1;//TODO can render in pass1? + return pass==1;//it is for the TESR (for bounds rendering), block renders as per normal } public int getCountOf(ItemStack layoutStack) @@ -509,6 +517,11 @@ public int getCountOf(ItemStack layoutStack) public void decreaseCountOf(ItemStack layoutStack, int i) { + if(worldObj.isRemote) + { + cachedItemMap.decreaseCount(layoutStack, i); + return; + } List dest = new ArrayList(); storageMap.getDestinations(layoutStack, dest); int found = 0; diff --git a/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseCraftingStation.java b/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseCraftingStation.java index 310b44ca8..467ff252c 100644 --- a/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseCraftingStation.java +++ b/src/main/java/net/shadowmage/ancientwarfare/automation/tile/warehouse2/TileWarehouseCraftingStation.java @@ -8,6 +8,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.shadowmage.ancientwarfare.core.config.AWLog; import net.shadowmage.ancientwarfare.core.crafting.AWCraftingManager; import net.shadowmage.ancientwarfare.core.interfaces.IInteractableTile; import net.shadowmage.ancientwarfare.core.inventory.InventoryBasic; @@ -72,6 +73,7 @@ public void onItemCrafted() { return; } + AWLog.logDebug("crafting item..."); int q; ItemStack layoutStack; for(int i = 0; i < 9; i++) @@ -80,17 +82,19 @@ public void onItemCrafted() if(layoutStack==null){continue;} if(layoutMatrix.getStackInSlot(i)!=null){continue;} q = warehouse.getCountOf(layoutStack); + AWLog.logDebug("warehouse count of: "+layoutStack+" :: "+q); if(q>0) { warehouse.decreaseCountOf(layoutStack, 1); + layoutStack = layoutStack.copy(); + layoutStack.stackSize=1; + layoutMatrix.setInventorySlotContents(i, layoutStack); } - layoutStack = layoutStack.copy(); - layoutStack.stackSize=1; - layoutMatrix.setInventorySlotContents(i, layoutStack); } + if(!worldObj.isRemote){warehouse.updateViewers();} } -private TileWarehouseBase getWarehouse() +public final TileWarehouseBase getWarehouse() { if(yCoord<=1)//could not possibly be a warehouse below... {