2014-05-27 19:15:49 +02:00
|
|
|
package appeng.parts.layers;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
import appeng.api.parts.IPart;
|
|
|
|
import appeng.api.parts.IPartHost;
|
|
|
|
import appeng.api.parts.LayerBase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inventory wrapper for parts,
|
|
|
|
*
|
|
|
|
* this is considerably more complicated then the other wrappers as it requires creating a "unified inventory".
|
|
|
|
*
|
|
|
|
* You must use {@link ISidedInventory} instead of {@link IInventory}.
|
|
|
|
*
|
|
|
|
* If your inventory changes in between placement and removal, you must trigger a PartChange on the {@link IPartHost} so
|
|
|
|
* it can recalculate the inventory wrapper.
|
|
|
|
*/
|
|
|
|
public class LayerISidedInventory extends LayerBase implements ISidedInventory
|
|
|
|
{
|
|
|
|
|
|
|
|
// a simple empty array for empty stuff..
|
|
|
|
private final static int[] nullSides = new int[] {};
|
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
InvLayerData invLayer = null;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Recalculate inventory wrapper cache.
|
|
|
|
*/
|
|
|
|
@Override
|
2014-06-09 01:54:38 +02:00
|
|
|
public void notifyNeighbors()
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
// cache of inventory state.
|
|
|
|
int sideData[][] = null;
|
|
|
|
List<ISidedInventory> invs = null;
|
|
|
|
List<InvSot> slots = null;
|
|
|
|
|
2014-05-27 19:06:55 +02:00
|
|
|
invs = new ArrayList();
|
|
|
|
int slotCount = 0;
|
|
|
|
|
|
|
|
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
{
|
|
|
|
IPart bp = getPart( side );
|
|
|
|
if ( bp instanceof ISidedInventory )
|
|
|
|
{
|
|
|
|
ISidedInventory part = (ISidedInventory) bp;
|
|
|
|
slotCount += part.getSizeInventory();
|
|
|
|
invs.add( part );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( invs.isEmpty() || slotCount == 0 )
|
|
|
|
{
|
|
|
|
invs = null;
|
2014-08-28 10:57:06 +02:00
|
|
|
sideData = null;
|
2014-05-27 19:06:55 +02:00
|
|
|
slots = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
sideData = new int[][] { nullSides, nullSides, nullSides, nullSides, nullSides, nullSides };
|
2014-05-27 19:06:55 +02:00
|
|
|
slots = new ArrayList<InvSot>( Collections.nCopies( slotCount, (InvSot) null ) );
|
|
|
|
|
|
|
|
int offsetForLayer = 0;
|
|
|
|
int offsetForPart = 0;
|
|
|
|
for (ISidedInventory sides : invs)
|
|
|
|
{
|
|
|
|
offsetForPart = 0;
|
|
|
|
slotCount = sides.getSizeInventory();
|
|
|
|
|
|
|
|
ForgeDirection currentSide = ForgeDirection.UNKNOWN;
|
|
|
|
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
if ( getPart( side ) == sides )
|
|
|
|
{
|
|
|
|
currentSide = side;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
int cSidesList[] = sideData[currentSide.ordinal()] = new int[slotCount];
|
2014-05-27 19:06:55 +02:00
|
|
|
for (int cSlot = 0; cSlot < slotCount; cSlot++)
|
|
|
|
{
|
|
|
|
cSidesList[cSlot] = offsetForLayer;
|
|
|
|
slots.set( offsetForLayer++, new InvSot( sides, offsetForPart++ ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( sideData == null || slots == null )
|
|
|
|
invLayer = null;
|
|
|
|
else
|
|
|
|
invLayer = new InvLayerData( sideData, invs, slots );
|
|
|
|
|
2014-05-27 19:06:55 +02:00
|
|
|
// make sure inventory is updated before we call FMP.
|
2014-06-09 01:54:38 +02:00
|
|
|
super.notifyNeighbors();
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int slot, int amount)
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer == null )
|
|
|
|
return null;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
return invLayer.decrStackSize( slot, amount );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory()
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer == null )
|
2014-05-27 19:06:55 +02:00
|
|
|
return 0;
|
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
return invLayer.getSizeInventory();
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int slot)
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer == null )
|
|
|
|
return null;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
return invLayer.getStackInSlot( slot );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int slot, ItemStack itemstack)
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer == null )
|
|
|
|
return false;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
return invLayer.isItemValidForSlot( slot, itemstack );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int slot, ItemStack itemstack)
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
invLayer.setInventorySlotContents( slot, itemstack );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack itemstack, int side)
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer == null )
|
|
|
|
return false;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
return invLayer.canExtractItem( slot, itemstack, side );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slot, ItemStack itemstack, int side)
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer == null )
|
|
|
|
return false;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
return invLayer.canInsertItem( slot, itemstack, side );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void markDirty()
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer != null )
|
|
|
|
invLayer.markDirty();
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-08-28 10:57:06 +02:00
|
|
|
super.markForSave();
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getAccessibleSlotsFromSide(int side)
|
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
if ( invLayer != null )
|
|
|
|
return invLayer.getAccessibleSlotsFromSide( side );
|
|
|
|
|
|
|
|
return nullSides;
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
return 64; // no options here.
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getInventoryName()
|
|
|
|
{
|
|
|
|
return "AEMultiPart";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCustomInventoryName()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlotOnClosing(int slot)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUseableByPlayer(EntityPlayer entityplayer)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeInventory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void openInventory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|