2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-05-27 19:15:49 +02:00
|
|
|
package appeng.parts.layers;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
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;
|
2015-06-16 02:44:59 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.IChatComponent;
|
2014-05-27 19:06:55 +02:00
|
|
|
import appeng.api.parts.IPart;
|
|
|
|
import appeng.api.parts.IPartHost;
|
|
|
|
import appeng.api.parts.LayerBase;
|
2015-06-16 02:44:59 +02:00
|
|
|
import appeng.api.util.AEPartLocation;
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-05-27 19:06:55 +02:00
|
|
|
/**
|
|
|
|
* Inventory wrapper for parts,
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2014-05-27 19:06:55 +02:00
|
|
|
* this is considerably more complicated then the other wrappers as it requires creating a "unified inventory".
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2014-05-27 19:06:55 +02:00
|
|
|
* You must use {@link ISidedInventory} instead of {@link IInventory}.
|
2015-02-03 12:04:13 +01:00
|
|
|
*
|
2014-05-27 19:06:55 +02:00
|
|
|
* 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..
|
2015-04-06 00:35:42 +02:00
|
|
|
private static final int[] NULL_SIDES = new int[] {};
|
2014-05-27 19:06:55 +02:00
|
|
|
|
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.
|
2015-01-01 21:15:03 +01:00
|
|
|
int[][] sideData = null;
|
2014-09-28 11:47:17 +02:00
|
|
|
List<ISidedInventory> inventories = null;
|
2014-08-28 10:57:06 +02:00
|
|
|
List<InvSot> slots = null;
|
|
|
|
|
2014-09-28 22:20:14 +02:00
|
|
|
inventories = new ArrayList<ISidedInventory>();
|
2014-05-27 19:06:55 +02:00
|
|
|
int slotCount = 0;
|
|
|
|
|
2015-06-16 02:44:59 +02:00
|
|
|
for( AEPartLocation side : AEPartLocation.SIDE_LOCATIONS )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
IPart bp = this.getPart( side );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( bp instanceof ISidedInventory )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
ISidedInventory part = (ISidedInventory) bp;
|
|
|
|
slotCount += part.getSizeInventory();
|
2014-09-28 11:47:17 +02:00
|
|
|
inventories.add( part );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( inventories.isEmpty() || slotCount == 0 )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
inventories = null;
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-01 22:13:10 +01:00
|
|
|
sideData = new int[][] { NULL_SIDES, NULL_SIDES, NULL_SIDES, NULL_SIDES, NULL_SIDES, NULL_SIDES };
|
2014-05-27 19:06:55 +02:00
|
|
|
slots = new ArrayList<InvSot>( Collections.nCopies( slotCount, (InvSot) null ) );
|
|
|
|
|
|
|
|
int offsetForLayer = 0;
|
|
|
|
int offsetForPart = 0;
|
2015-04-03 08:54:31 +02:00
|
|
|
for( ISidedInventory sides : inventories )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
offsetForPart = 0;
|
|
|
|
slotCount = sides.getSizeInventory();
|
|
|
|
|
2015-06-16 02:44:59 +02:00
|
|
|
AEPartLocation currentSide = AEPartLocation.INTERNAL;
|
|
|
|
for( AEPartLocation side : AEPartLocation.SIDE_LOCATIONS )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.getPart( side ) == sides )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
currentSide = side;
|
|
|
|
break;
|
|
|
|
}
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2015-01-01 21:15:03 +01:00
|
|
|
int[] cSidesList = sideData[currentSide.ordinal()] = new int[slotCount];
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int cSlot = 0; cSlot < slotCount; cSlot++ )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
cSidesList[cSlot] = offsetForLayer;
|
2015-03-26 11:07:26 +01:00
|
|
|
slots.set( offsetForLayer, new InvSot( sides, offsetForPart ) );
|
|
|
|
offsetForLayer++;
|
|
|
|
offsetForPart++;
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( sideData == null || slots == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.invLayer = null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-08-28 10:57:06 +02:00
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.invLayer = new InvLayerData( sideData, inventories, slots );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-08-28 10:57:06 +02:00
|
|
|
|
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 int getSizeInventory()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-05-27 19:06:55 +02:00
|
|
|
return 0;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.invLayer.getSizeInventory();
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public ItemStack getStackInSlot( int slot )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.invLayer.getStackInSlot( slot );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public ItemStack decrStackSize( int slot, int amount )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-05-27 19:06:55 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.invLayer.decreaseStackSize( slot, amount );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public ItemStack getStackInSlotOnClosing( int slot )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents( int slot, ItemStack itemstack )
|
|
|
|
{
|
|
|
|
if( this.invLayer == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-08-28 10:57:06 +02:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-08-28 10:57:06 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.invLayer.setInventorySlotContents( slot, itemstack );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public String getName()
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return "AEMultiPart";
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public boolean hasCustomName()
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return false;
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public int getInventoryStackLimit()
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return 64; // no options here.
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public boolean isUseableByPlayer( EntityPlayer entityplayer )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return false;
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public void openInventory(EntityPlayer player)
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public void closeInventory(EntityPlayer player)
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public boolean isItemValidForSlot( int slot, ItemStack itemstack )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
return this.invLayer.isItemValidForSlot( slot, itemstack );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void markDirty()
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.invLayer.markDirty();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
super.markForSave();
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public int[] getSlotsForFace( EnumFacing side )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-06-16 02:44:59 +02:00
|
|
|
return this.invLayer.getSlotsForFace( side );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
return NULL_SIDES;
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public boolean canInsertItem( int slot, ItemStack itemstack, EnumFacing side )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
return this.invLayer.canInsertItem( slot, itemstack, side );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-06-16 02:44:59 +02:00
|
|
|
public boolean canExtractItem( int slot, ItemStack itemstack, EnumFacing side )
|
2014-05-27 19:06:55 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.invLayer == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
return this.invLayer.canExtractItem( slot, itemstack, side );
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|
2015-06-16 02:44:59 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getField(
|
|
|
|
int id )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setField(
|
|
|
|
int id,
|
|
|
|
int value )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getFieldCount()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IChatComponent getDisplayName()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-05-27 19:06:55 +02:00
|
|
|
}
|