Skip to content

Commit

Permalink
Update warehouse crafting station to eliminate shift-click duplication
Browse files Browse the repository at this point in the history
bug (appeared to be client-side dupes only).  Closes #181
  • Loading branch information
shadowmage45 committed Dec 3, 2014
1 parent 7aa0818 commit c0f4d9f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +47,7 @@ public abstract class TileWarehouseBase extends TileWorksiteBounded implements I
private ItemQuantityMap cachedItemMap = new ItemQuantityMap();

private Set<ContainerWarehouseControl> viewers = new HashSet<ContainerWarehouseControl>();

private Set<ContainerWarehouseCraftingStation> craftingViewers = new HashSet<ContainerWarehouseCraftingStation>();

public TileWarehouseBase()
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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<IWarehouseStorageTile> dest = new ArrayList<IWarehouseStorageTile>();
storageMap.getDestinations(layoutStack, dest);
int found = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,6 +73,7 @@ public void onItemCrafted()
{
return;
}
AWLog.logDebug("crafting item...");
int q;
ItemStack layoutStack;
for(int i = 0; i < 9; i++)
Expand All @@ -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...
{
Expand Down

0 comments on commit c0f4d9f

Please sign in to comment.