2013-12-30 06:55:02 +01:00
|
|
|
package appeng.parts.automation;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
import net.minecraft.block.Block;
|
2013-12-30 06:55:02 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.Item;
|
2014-01-20 17:41:37 +01:00
|
|
|
import net.minecraft.item.ItemBlock;
|
2013-12-30 06:55:02 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2014-01-05 09:46:00 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2013-12-30 06:55:02 +01:00
|
|
|
import appeng.api.config.Upgrades;
|
2014-01-23 20:02:48 +01:00
|
|
|
import appeng.api.implementations.items.IUpgradeModule;
|
2013-12-30 06:55:02 +01:00
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.inventory.IAEAppEngInventory;
|
|
|
|
import appeng.tile.inventory.InvOperation;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
|
|
|
public class UpgradeInventory extends AppEngInternalInventory implements IAEAppEngInventory
|
|
|
|
{
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
private final Object itemorblock;
|
|
|
|
private final IAEAppEngInventory parent;
|
|
|
|
|
2014-01-01 10:04:54 +01:00
|
|
|
private boolean cached = false;
|
2013-12-30 06:55:02 +01:00
|
|
|
private int FuzzyUpgrades = 0;
|
|
|
|
private int SpeedUpgrades = 0;
|
|
|
|
private int RedstoneUpgrades = 0;
|
|
|
|
private int CapacityUpgrades = 0;
|
2014-01-20 17:41:37 +01:00
|
|
|
private int InverterUpgrades = 0;
|
2013-12-30 06:55:02 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public UpgradeInventory(Object itemOrBlock, IAEAppEngInventory _te, int s) {
|
2013-12-30 06:55:02 +01:00
|
|
|
super( null, s );
|
|
|
|
te = this;
|
|
|
|
parent = _te;
|
2014-01-20 17:41:37 +01:00
|
|
|
itemorblock = itemOrBlock;
|
2013-12-30 06:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean eventsEnabled()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-01 10:04:54 +01:00
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int slot, ItemStack newItemStack)
|
|
|
|
{
|
|
|
|
super.setInventorySlotContents( slot, newItemStack );
|
|
|
|
}
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
|
|
|
{
|
|
|
|
if ( itemstack == null )
|
|
|
|
return false;
|
|
|
|
Item it = itemstack.getItem();
|
|
|
|
if ( it instanceof IUpgradeModule )
|
|
|
|
{
|
|
|
|
Upgrades u = ((IUpgradeModule) it).getType( itemstack );
|
|
|
|
if ( u != null )
|
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
return getInstalledUpgrades( u ) < getMaxInstalled( u );
|
2013-12-30 06:55:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-26 04:19:20 +02:00
|
|
|
public int getMaxInstalled(Upgrades u)
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
|
|
|
Integer max = null;
|
|
|
|
for (ItemStack is : u.getSupported().keySet())
|
|
|
|
{
|
|
|
|
if ( is.getItem() == itemorblock )
|
|
|
|
{
|
|
|
|
max = u.getSupported().get( is );
|
|
|
|
break;
|
|
|
|
}
|
2014-02-09 02:34:52 +01:00
|
|
|
else if ( is.getItem() instanceof ItemBlock && Block.getBlockFromItem( is.getItem() ) == itemorblock )
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
|
|
|
max = u.getSupported().get( is );
|
|
|
|
break;
|
|
|
|
}
|
2014-01-24 17:35:35 +01:00
|
|
|
else if ( itemorblock instanceof ItemStack && Platform.isSameItem( (ItemStack) itemorblock, is ) )
|
|
|
|
{
|
|
|
|
max = u.getSupported().get( is );
|
|
|
|
break;
|
|
|
|
}
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( max == null )
|
|
|
|
return 0;
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
private void updateUpgradeInfo()
|
|
|
|
{
|
2014-01-01 10:04:54 +01:00
|
|
|
cached = true;
|
2014-01-20 17:41:37 +01:00
|
|
|
InverterUpgrades = CapacityUpgrades = RedstoneUpgrades = SpeedUpgrades = FuzzyUpgrades = 0;
|
2013-12-30 06:55:02 +01:00
|
|
|
|
|
|
|
for (ItemStack is : this)
|
|
|
|
{
|
|
|
|
if ( is == null || is.getItem() == null || !(is.getItem() instanceof IUpgradeModule) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Upgrades myUpgrade = ((IUpgradeModule) is.getItem()).getType( is );
|
|
|
|
switch (myUpgrade)
|
|
|
|
{
|
|
|
|
case CAPACITY:
|
|
|
|
CapacityUpgrades++;
|
|
|
|
break;
|
|
|
|
case FUZZY:
|
|
|
|
FuzzyUpgrades++;
|
|
|
|
break;
|
|
|
|
case REDSTONE:
|
|
|
|
RedstoneUpgrades++;
|
|
|
|
break;
|
|
|
|
case SPEED:
|
|
|
|
SpeedUpgrades++;
|
|
|
|
break;
|
2014-01-20 17:41:37 +01:00
|
|
|
case INVERTER:
|
|
|
|
InverterUpgrades++;
|
|
|
|
break;
|
2013-12-30 06:55:02 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
CapacityUpgrades = Math.min( CapacityUpgrades, getMaxInstalled( Upgrades.CAPACITY ) );
|
|
|
|
FuzzyUpgrades = Math.min( FuzzyUpgrades, getMaxInstalled( Upgrades.FUZZY ) );
|
|
|
|
RedstoneUpgrades = Math.min( RedstoneUpgrades, getMaxInstalled( Upgrades.REDSTONE ) );
|
|
|
|
SpeedUpgrades = Math.min( SpeedUpgrades, getMaxInstalled( Upgrades.SPEED ) );
|
|
|
|
InverterUpgrades = Math.min( InverterUpgrades, getMaxInstalled( Upgrades.INVERTER ) );
|
2013-12-30 06:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getInstalledUpgrades(Upgrades u)
|
|
|
|
{
|
2014-01-01 10:04:54 +01:00
|
|
|
if ( !cached )
|
|
|
|
updateUpgradeInfo();
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
switch (u)
|
|
|
|
{
|
|
|
|
case CAPACITY:
|
|
|
|
return CapacityUpgrades;
|
|
|
|
case FUZZY:
|
|
|
|
return FuzzyUpgrades;
|
|
|
|
case REDSTONE:
|
|
|
|
return RedstoneUpgrades;
|
|
|
|
case SPEED:
|
|
|
|
return SpeedUpgrades;
|
2014-01-20 17:41:37 +01:00
|
|
|
case INVERTER:
|
|
|
|
return InverterUpgrades;
|
2013-12-30 06:55:02 +01:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 09:46:00 +01:00
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound target)
|
|
|
|
{
|
|
|
|
super.readFromNBT( target );
|
|
|
|
updateUpgradeInfo();
|
|
|
|
}
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
@Override
|
|
|
|
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack)
|
|
|
|
{
|
2014-01-01 10:04:54 +01:00
|
|
|
cached = false;
|
2013-12-30 06:55:02 +01:00
|
|
|
if ( parent != null && Platform.isServer() )
|
|
|
|
parent.onChangeInventory( inv, slot, mc, removedStack, newStack );
|
|
|
|
}
|
|
|
|
|
2014-01-31 01:50:11 +01:00
|
|
|
@Override
|
|
|
|
public void saveChanges()
|
|
|
|
{
|
|
|
|
parent.saveChanges();
|
|
|
|
}
|
|
|
|
|
2013-12-30 06:55:02 +01:00
|
|
|
}
|