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>.
|
|
|
|
*/
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
package appeng.util.inv;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-08-28 18:32:53 +02:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
public class WrapperChainedInventory implements IInventory
|
|
|
|
{
|
|
|
|
|
2014-09-30 22:59:49 +02:00
|
|
|
static class InvOffset
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
int offset;
|
|
|
|
int size;
|
|
|
|
IInventory i;
|
2014-09-28 00:50:06 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
int fullSize = 0;
|
|
|
|
|
|
|
|
private List<IInventory> l;
|
|
|
|
private HashMap<Integer, InvOffset> offsets;
|
|
|
|
|
2014-09-28 11:47:17 +02:00
|
|
|
public WrapperChainedInventory(IInventory... inventories) {
|
2014-12-29 15:13:47 +01:00
|
|
|
this.setInventory( inventories );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:47:17 +02:00
|
|
|
public WrapperChainedInventory(List<IInventory> inventories) {
|
2014-12-29 15:13:47 +01:00
|
|
|
this.setInventory( inventories );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-06-09 01:54:38 +02:00
|
|
|
public void cycleOrder()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.l.size() > 1 )
|
2014-06-09 01:54:38 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
List<IInventory> newOrder = new ArrayList<IInventory>( this.l.size() );
|
|
|
|
newOrder.add( this.l.get( this.l.size() - 1 ) );
|
|
|
|
for (int x = 0; x < this.l.size() - 1; x++)
|
|
|
|
newOrder.add( this.l.get( x ) );
|
|
|
|
this.setInventory( newOrder );
|
2014-06-09 01:54:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public void calculateSizes()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.offsets = new HashMap<Integer, WrapperChainedInventory.InvOffset>();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
int offset = 0;
|
2014-12-29 15:13:47 +01:00
|
|
|
for (IInventory in : this.l)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
InvOffset io = new InvOffset();
|
|
|
|
io.offset = offset;
|
|
|
|
io.size = in.getSizeInventory();
|
|
|
|
io.i = in;
|
|
|
|
|
|
|
|
for (int y = 0; y < io.size; y++)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.offsets.put( y + io.offset, io );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
offset += io.size;
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.fullSize = offset;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-08-28 18:32:53 +02:00
|
|
|
public void setInventory(IInventory... a)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.l = ImmutableList.copyOf( a );
|
|
|
|
this.calculateSizes();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setInventory(List<IInventory> a)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.l = a;
|
|
|
|
this.calculateSizes();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public IInventory getInv(int idx)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
InvOffset io = this.offsets.get( idx );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( io != null )
|
|
|
|
{
|
|
|
|
return io.i;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInvSlot(int idx)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
InvOffset io = this.offsets.get( idx );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( io != null )
|
|
|
|
{
|
|
|
|
return idx - io.offset;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.fullSize;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int idx)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
InvOffset io = this.offsets.get( idx );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( io != null )
|
|
|
|
{
|
|
|
|
return io.i.getStackInSlot( idx - io.offset );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int idx, int var2)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
InvOffset io = this.offsets.get( idx );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( io != null )
|
|
|
|
{
|
|
|
|
return io.i.decrStackSize( idx - io.offset, var2 );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlotOnClosing(int idx)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
InvOffset io = this.offsets.get( idx );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( io != null )
|
|
|
|
{
|
|
|
|
return io.i.getStackInSlotOnClosing( idx - io.offset );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int idx, ItemStack var2)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
InvOffset io = this.offsets.get( idx );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( io != null )
|
|
|
|
{
|
|
|
|
io.i.setInventorySlotContents( idx - io.offset, var2 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public String getInventoryName()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
return "ChainedInv";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
int smallest = 64;
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
for (IInventory i : this.l)
|
2013-12-27 23:59:59 +01:00
|
|
|
smallest = Math.min( smallest, i.getInventoryStackLimit() );
|
|
|
|
|
|
|
|
return smallest;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void markDirty()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
for (IInventory i : this.l)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
i.markDirty();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUseableByPlayer(EntityPlayer var1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void openInventory()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void closeInventory()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public boolean hasCustomInventoryName()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int idx, ItemStack itemstack)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
InvOffset io = this.offsets.get( idx );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( io != null )
|
|
|
|
{
|
|
|
|
return io.i.isItemValidForSlot( idx - io.offset, itemstack );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-09 01:54:38 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|