Inventory Layer should now be smaller without inventory parts.

This commit is contained in:
AlgorithmX2 2014-08-28 03:57:06 -05:00
parent eabc21e53c
commit 16ab9142f0
2 changed files with 149 additions and 47 deletions

View file

@ -0,0 +1,107 @@
package appeng.parts.layers;
import java.util.List;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
public class InvLayerData
{
// a simple empty array for empty stuff..
private final static int[] nullSides = new int[] {};
// cache of inventory state.
final private int sides[][];
final private List<ISidedInventory> invs;
final private List<InvSot> slots;
public InvLayerData(int a[][], List<ISidedInventory> b, List<InvSot> c) {
sides = a;
invs = b;
slots = c;
}
/**
* check if a slot index is valid, prevent crashes from bad code :)
*
* @param slot
* @return true, if the slot exists.
*/
boolean isSlotValid(int slot)
{
return slots != null && slot >= 0 && slot < slots.size();
}
public ItemStack decrStackSize(int slot, int amount)
{
if ( isSlotValid( slot ) )
return slots.get( slot ).decrStackSize( amount );
return null;
}
public int getSizeInventory()
{
if ( slots == null )
return 0;
return slots.size();
}
public ItemStack getStackInSlot(int slot)
{
if ( isSlotValid( slot ) )
return slots.get( slot ).getStackInSlot();
return null;
}
public boolean isItemValidForSlot(int slot, ItemStack itemstack)
{
if ( isSlotValid( slot ) )
return slots.get( slot ).isItemValidForSlot( itemstack );
return false;
}
public void setInventorySlotContents(int slot, ItemStack itemstack)
{
if ( isSlotValid( slot ) )
slots.get( slot ).setInventorySlotContents( itemstack );
}
public boolean canExtractItem(int slot, ItemStack itemstack, int side)
{
if ( isSlotValid( slot ) )
return slots.get( slot ).canExtractItem( itemstack, side );
return false;
}
public boolean canInsertItem(int slot, ItemStack itemstack, int side)
{
if ( isSlotValid( slot ) )
return slots.get( slot ).canInsertItem( itemstack, side );
return false;
}
public void markDirty()
{
if ( invs != null )
{
for (IInventory inv : invs)
inv.markDirty();
}
}
public int[] getAccessibleSlotsFromSide(int side)
{
if ( sides == null || side < 0 || side > 5 )
return nullSides;
return sides[side];
}
}

View file

@ -29,10 +29,7 @@ public class LayerISidedInventory extends LayerBase implements ISidedInventory
// a simple empty array for empty stuff.. // a simple empty array for empty stuff..
private final static int[] nullSides = new int[] {}; private final static int[] nullSides = new int[] {};
// cache of inventory state. InvLayerData invLayer = null;
private int sides[][] = null;
private List<ISidedInventory> invs = null;
private List<InvSot> slots = null;
/** /**
* Recalculate inventory wrapper cache. * Recalculate inventory wrapper cache.
@ -40,6 +37,11 @@ public class LayerISidedInventory extends LayerBase implements ISidedInventory
@Override @Override
public void notifyNeighbors() public void notifyNeighbors()
{ {
// cache of inventory state.
int sideData[][] = null;
List<ISidedInventory> invs = null;
List<InvSot> slots = null;
invs = new ArrayList(); invs = new ArrayList();
int slotCount = 0; int slotCount = 0;
@ -57,12 +59,12 @@ public class LayerISidedInventory extends LayerBase implements ISidedInventory
if ( invs.isEmpty() || slotCount == 0 ) if ( invs.isEmpty() || slotCount == 0 )
{ {
invs = null; invs = null;
sides = null; sideData = null;
slots = null; slots = null;
} }
else else
{ {
sides = new int[][] { nullSides, nullSides, nullSides, nullSides, nullSides, nullSides }; sideData = new int[][] { nullSides, nullSides, nullSides, nullSides, nullSides, nullSides };
slots = new ArrayList<InvSot>( Collections.nCopies( slotCount, (InvSot) null ) ); slots = new ArrayList<InvSot>( Collections.nCopies( slotCount, (InvSot) null ) );
int offsetForLayer = 0; int offsetForLayer = 0;
@ -80,7 +82,7 @@ public class LayerISidedInventory extends LayerBase implements ISidedInventory
break; break;
} }
int cSidesList[] = this.sides[currentSide.ordinal()] = new int[slotCount]; int cSidesList[] = sideData[currentSide.ordinal()] = new int[slotCount];
for (int cSlot = 0; cSlot < slotCount; cSlot++) for (int cSlot = 0; cSlot < slotCount; cSlot++)
{ {
cSidesList[cSlot] = offsetForLayer; cSidesList[cSlot] = offsetForLayer;
@ -89,100 +91,94 @@ public class LayerISidedInventory extends LayerBase implements ISidedInventory
} }
} }
if ( sideData == null || slots == null )
invLayer = null;
else
invLayer = new InvLayerData( sideData, invs, slots );
// make sure inventory is updated before we call FMP. // make sure inventory is updated before we call FMP.
super.notifyNeighbors(); super.notifyNeighbors();
} }
/**
* check if a slot index is valid, prevent crashes from bad code :)
*
* @param slot
* @return true, if the slot exists.
*/
boolean isSlotValid(int slot)
{
return slots != null && slot >= 0 && slot < slots.size();
}
@Override @Override
public ItemStack decrStackSize(int slot, int amount) public ItemStack decrStackSize(int slot, int amount)
{ {
if ( isSlotValid( slot ) ) if ( invLayer == null )
return slots.get( slot ).decrStackSize( amount ); return null;
return null; return invLayer.decrStackSize( slot, amount );
} }
@Override @Override
public int getSizeInventory() public int getSizeInventory()
{ {
if ( slots == null ) if ( invLayer == null )
return 0; return 0;
return slots.size(); return invLayer.getSizeInventory();
} }
@Override @Override
public ItemStack getStackInSlot(int slot) public ItemStack getStackInSlot(int slot)
{ {
if ( isSlotValid( slot ) ) if ( invLayer == null )
return slots.get( slot ).getStackInSlot(); return null;
return null; return invLayer.getStackInSlot( slot );
} }
@Override @Override
public boolean isItemValidForSlot(int slot, ItemStack itemstack) public boolean isItemValidForSlot(int slot, ItemStack itemstack)
{ {
if ( isSlotValid( slot ) ) if ( invLayer == null )
return slots.get( slot ).isItemValidForSlot( itemstack ); return false;
return false; return invLayer.isItemValidForSlot( slot, itemstack );
} }
@Override @Override
public void setInventorySlotContents(int slot, ItemStack itemstack) public void setInventorySlotContents(int slot, ItemStack itemstack)
{ {
if ( isSlotValid( slot ) ) if ( invLayer == null )
slots.get( slot ).setInventorySlotContents( itemstack ); return;
invLayer.setInventorySlotContents( slot, itemstack );
} }
@Override @Override
public boolean canExtractItem(int slot, ItemStack itemstack, int side) public boolean canExtractItem(int slot, ItemStack itemstack, int side)
{ {
if ( isSlotValid( slot ) ) if ( invLayer == null )
return slots.get( slot ).canExtractItem( itemstack, side ); return false;
return false; return invLayer.canExtractItem( slot, itemstack, side );
} }
@Override @Override
public boolean canInsertItem(int slot, ItemStack itemstack, int side) public boolean canInsertItem(int slot, ItemStack itemstack, int side)
{ {
if ( isSlotValid( slot ) ) if ( invLayer == null )
return slots.get( slot ).canInsertItem( itemstack, side ); return false;
return false; return invLayer.canInsertItem( slot, itemstack, side );
} }
@Override @Override
public void markDirty() public void markDirty()
{ {
super.markForSave(); if ( invLayer != null )
invLayer.markDirty();
if ( invs != null ) super.markForSave();
{
for (IInventory inv : invs)
inv.markDirty();
}
} }
@Override @Override
public int[] getAccessibleSlotsFromSide(int side) public int[] getAccessibleSlotsFromSide(int side)
{ {
if ( sides == null || side < 0 || side > 5 ) if ( invLayer != null )
return nullSides; return invLayer.getAccessibleSlotsFromSide( side );
return sides[side];
return nullSides;
} }
@Override @Override
@ -224,5 +220,4 @@ public class LayerISidedInventory extends LayerBase implements ISidedInventory
public void openInventory() public void openInventory()
{ {
} }
} }