Applied-Energistics-2-tiler.../src/main/java/appeng/parts/automation/UpgradeInventory.java

202 lines
5.3 KiB
Java
Raw Normal View History

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>.
*/
package appeng.parts.automation;
2014-01-20 17:41:37 +01:00
import net.minecraft.block.Block;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
2014-01-20 17:41:37 +01:00
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2014-12-29 21:59:05 +01:00
import appeng.api.config.Upgrades;
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.items.IUpgradeModule;
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
{
private final Object itemOrBlock;
2014-01-20 17:41:37 +01:00
private final IAEAppEngInventory parent;
2014-01-01 10:04:54 +01:00
private boolean cached = false;
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;
2014-07-02 03:44:12 +02:00
private int CraftingUpgrades = 0;
2014-01-20 17:41:37 +01:00
public UpgradeInventory(Object itemOrBlock, IAEAppEngInventory _te, int s) {
super( null, s );
2014-12-29 15:13:47 +01:00
this.te = this;
this.parent = _te;
this.itemOrBlock = itemOrBlock;
}
@Override
protected boolean eventsEnabled()
{
return true;
}
@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-12-29 15:13:47 +01:00
return this.getInstalledUpgrades( u ) < this.getMaxInstalled( u );
}
}
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())
{
2014-12-29 15:13:47 +01:00
if ( is.getItem() == this.itemOrBlock )
2014-01-20 17:41:37 +01:00
{
max = u.getSupported().get( is );
break;
}
2014-12-29 15:13:47 +01:00
else if ( is.getItem() instanceof ItemBlock && Block.getBlockFromItem( is.getItem() ) == this.itemOrBlock )
2014-01-20 17:41:37 +01:00
{
max = u.getSupported().get( is );
break;
}
2014-12-29 15:13:47 +01:00
else if ( this.itemOrBlock instanceof ItemStack && Platform.isSameItem( (ItemStack) this.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;
}
private void updateUpgradeInfo()
{
2014-12-29 15:13:47 +01:00
this.cached = true;
this.InverterUpgrades = this.CapacityUpgrades = this.RedstoneUpgrades = this.SpeedUpgrades = this.FuzzyUpgrades = this.CraftingUpgrades = 0;
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:
2014-12-29 15:13:47 +01:00
this.CapacityUpgrades++;
break;
case FUZZY:
2014-12-29 15:13:47 +01:00
this.FuzzyUpgrades++;
break;
case REDSTONE:
2014-12-29 15:13:47 +01:00
this.RedstoneUpgrades++;
break;
case SPEED:
2014-12-29 15:13:47 +01:00
this.SpeedUpgrades++;
break;
2014-01-20 17:41:37 +01:00
case INVERTER:
2014-12-29 15:13:47 +01:00
this.InverterUpgrades++;
2014-01-20 17:41:37 +01:00
break;
2014-07-02 03:44:12 +02:00
case CRAFTING:
2014-12-29 15:13:47 +01:00
this.CraftingUpgrades++;
2014-07-02 03:44:12 +02:00
break;
default:
break;
}
}
2014-12-29 15:13:47 +01:00
this.CapacityUpgrades = Math.min( this.CapacityUpgrades, this.getMaxInstalled( Upgrades.CAPACITY ) );
this.FuzzyUpgrades = Math.min( this.FuzzyUpgrades, this.getMaxInstalled( Upgrades.FUZZY ) );
this.RedstoneUpgrades = Math.min( this.RedstoneUpgrades, this.getMaxInstalled( Upgrades.REDSTONE ) );
this.SpeedUpgrades = Math.min( this.SpeedUpgrades, this.getMaxInstalled( Upgrades.SPEED ) );
this.InverterUpgrades = Math.min( this.InverterUpgrades, this.getMaxInstalled( Upgrades.INVERTER ) );
this.CraftingUpgrades = Math.min( this.CraftingUpgrades, this.getMaxInstalled( Upgrades.CRAFTING ) );
}
public int getInstalledUpgrades(Upgrades u)
{
2014-12-29 15:13:47 +01:00
if ( !this.cached )
this.updateUpgradeInfo();
2014-01-01 10:04:54 +01:00
switch (u)
{
case CAPACITY:
2014-12-29 15:13:47 +01:00
return this.CapacityUpgrades;
case FUZZY:
2014-12-29 15:13:47 +01:00
return this.FuzzyUpgrades;
case REDSTONE:
2014-12-29 15:13:47 +01:00
return this.RedstoneUpgrades;
case SPEED:
2014-12-29 15:13:47 +01:00
return this.SpeedUpgrades;
2014-01-20 17:41:37 +01:00
case INVERTER:
2014-12-29 15:13:47 +01:00
return this.InverterUpgrades;
2014-07-02 03:44:12 +02:00
case CRAFTING:
2014-12-29 15:13:47 +01:00
return this.CraftingUpgrades;
default:
return 0;
}
}
@Override
public void readFromNBT(NBTTagCompound target)
{
super.readFromNBT( target );
2014-12-29 15:13:47 +01:00
this.updateUpgradeInfo();
}
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack)
{
2014-12-29 15:13:47 +01:00
this.cached = false;
if ( this.parent != null && Platform.isServer() )
this.parent.onChangeInventory( inv, slot, mc, removedStack, newStack );
}
2014-01-31 01:50:11 +01:00
@Override
public void saveChanges()
{
2014-12-29 15:13:47 +01:00
this.parent.saveChanges();
2014-01-31 01:50:11 +01:00
}
}