Applied-Energistics-2-tiler.../src/main/java/appeng/items/storage/ItemBasicStorageCell.java

317 lines
8.7 KiB
Java
Raw Normal View History

/*
* 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.items.storage;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
2015-06-17 23:25:16 +02:00
import com.google.common.base.Optional;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
2014-01-20 17:41:37 +01:00
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
2015-06-17 23:25:16 +02:00
2014-01-20 17:41:37 +01:00
import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
2014-09-08 21:22:15 +02:00
import appeng.api.config.IncludeExclude;
import appeng.api.exceptions.MissingDefinition;
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.items.IItemGroup;
import appeng.api.implementations.items.IStorageCell;
import appeng.api.implementations.items.IUpgradeModule;
2014-03-28 01:48:38 +01:00
import appeng.api.storage.ICellInventory;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.IMEInventoryHandler;
2014-01-20 17:41:37 +01:00
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.core.AEConfig;
import appeng.core.features.AEFeature;
2014-01-20 17:41:37 +01:00
import appeng.core.localization.GuiText;
import appeng.items.AEBaseItem;
import appeng.items.contents.CellConfig;
import appeng.items.contents.CellUpgrades;
import appeng.items.materials.MaterialType;
import appeng.util.InventoryAdaptor;
2014-01-20 17:41:37 +01:00
import appeng.util.Platform;
public final class ItemBasicStorageCell extends AEBaseItem implements IStorageCell, IItemGroup
{
private final MaterialType component;
private final int totalBytes;
private final int perType;
private final double idleDrain;
2015-09-30 14:24:40 +02:00
public ItemBasicStorageCell( final MaterialType whichCell, final int kilobytes )
{
super( Optional.of( kilobytes + "k" ) );
this.setFeature( EnumSet.of( AEFeature.StorageCells ) );
this.setMaxStackSize( 1 );
this.totalBytes = kilobytes * 1024;
this.component = whichCell;
switch( this.component )
{
case Cell1kPart:
this.idleDrain = 0.5;
this.perType = 8;
break;
case Cell4kPart:
this.idleDrain = 1.0;
this.perType = 32;
break;
case Cell16kPart:
this.idleDrain = 1.5;
this.perType = 128;
break;
case Cell64kPart:
this.idleDrain = 2.0;
this.perType = 512;
break;
default:
this.idleDrain = 0.0;
this.perType = 8;
}
}
@Override
2015-09-30 14:24:40 +02:00
public void addCheckedInformation( final ItemStack stack, final EntityPlayer player, final List<String> lines, final boolean displayMoreInfo )
{
2015-09-30 14:24:40 +02:00
final IMEInventoryHandler<?> inventory = AEApi.instance().registries().cell().getCellInventory( stack, null, StorageChannel.ITEMS );
if( inventory instanceof ICellInventoryHandler )
{
2015-09-30 14:24:40 +02:00
final ICellInventoryHandler handler = (ICellInventoryHandler) inventory;
final ICellInventory cellInventory = handler.getCellInv();
if( cellInventory != null )
2014-01-20 17:41:37 +01:00
{
lines.add( cellInventory.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() );
lines.add( cellInventory.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalItemTypes() + ' ' + GuiText.Types.getLocal() );
if( handler.isPreformatted() )
2014-09-08 21:22:15 +02:00
{
2015-09-30 14:24:40 +02:00
final String list = ( handler.getIncludeExcludeMode() == IncludeExclude.WHITELIST ? GuiText.Included : GuiText.Excluded ).getLocal();
if( handler.isFuzzy() )
2015-04-29 02:30:53 +02:00
{
lines.add( GuiText.Partitioned.getLocal() + " - " + list + ' ' + GuiText.Fuzzy.getLocal() );
2015-04-29 02:30:53 +02:00
}
2014-09-08 21:22:15 +02:00
else
2015-04-29 02:30:53 +02:00
{
lines.add( GuiText.Partitioned.getLocal() + " - " + list + ' ' + GuiText.Precise.getLocal() );
2015-04-29 02:30:53 +02:00
}
2014-09-08 21:22:15 +02:00
}
2014-01-20 17:41:37 +01:00
}
}
}
@Override
2015-09-30 14:24:40 +02:00
public int getBytes( final ItemStack cellItem )
{
return this.totalBytes;
}
@Override
2015-09-30 14:24:40 +02:00
public int getBytesPerType( final ItemStack cellItem )
{
return this.perType;
}
@Override
2015-09-30 14:24:40 +02:00
public int getTotalTypes( final ItemStack cellItem )
{
return 63;
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isBlackListed( final ItemStack cellItem, final IAEItemStack requestedAddition )
{
return false;
}
@Override
public boolean storableInStorageCell()
{
return false;
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isStorageCell( final ItemStack i )
{
return true;
}
@Override
public double getIdleDrain()
{
return this.idleDrain;
}
2014-01-20 17:41:37 +01:00
@Override
2015-09-30 14:24:40 +02:00
public String getUnlocalizedGroupName( final Set<ItemStack> others, final ItemStack is )
{
return GuiText.StorageCells.getUnlocalized();
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isEditable( final ItemStack is )
{
return true;
}
2014-01-20 17:41:37 +01:00
@Override
2015-09-30 14:24:40 +02:00
public IInventory getUpgradesInventory( final ItemStack is )
2014-01-20 17:41:37 +01:00
{
return new CellUpgrades( is, 2 );
}
@Override
2015-09-30 14:24:40 +02:00
public IInventory getConfigInventory( final ItemStack is )
2014-01-20 17:41:37 +01:00
{
return new CellConfig( is );
}
@Override
2015-09-30 14:24:40 +02:00
public FuzzyMode getFuzzyMode( final ItemStack is )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final String fz = Platform.openNbtData( is ).getString( "FuzzyMode" );
2014-01-20 17:41:37 +01:00
try
{
return FuzzyMode.valueOf( fz );
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
2014-01-20 17:41:37 +01:00
{
return FuzzyMode.IGNORE_ALL;
}
}
@Override
2015-09-30 14:24:40 +02:00
public void setFuzzyMode( final ItemStack is, final FuzzyMode fzMode )
2014-01-20 17:41:37 +01:00
{
Platform.openNbtData( is ).setString( "FuzzyMode", fzMode.name() );
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack onItemRightClick( final ItemStack stack, final World world, final EntityPlayer player )
2014-01-20 17:41:37 +01:00
{
2014-12-29 15:13:47 +01:00
this.disassembleDrive( stack, world, player );
return stack;
2014-01-20 17:41:37 +01:00
}
2015-09-30 14:24:40 +02:00
private boolean disassembleDrive( final ItemStack stack, final World world, final EntityPlayer player )
{
if( player.isSneaking() )
{
if( Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final InventoryPlayer playerInventory = player.inventory;
final IMEInventoryHandler inv = AEApi.instance().registries().cell().getCellInventory( stack, null, StorageChannel.ITEMS );
if( inv != null && playerInventory.getCurrentItem() == stack )
{
2015-09-30 14:24:40 +02:00
final InventoryAdaptor ia = InventoryAdaptor.getAdaptor( player, EnumFacing.UP );
final IItemList<IAEItemStack> list = inv.getAvailableItems( StorageChannel.ITEMS.createList() );
if( list.isEmpty() && ia != null )
{
playerInventory.setInventorySlotContents( playerInventory.currentItem, null );
// drop core
2015-09-30 14:24:40 +02:00
final ItemStack extraB = ia.addItems( this.component.stack( 1 ) );
if( extraB != null )
2015-04-29 02:30:53 +02:00
{
player.dropPlayerItemWithRandomChoice( extraB, false );
2015-04-29 02:30:53 +02:00
}
// drop upgrades
final IInventory upgradesInventory = this.getUpgradesInventory( stack );
for( int upgradeIndex = 0; upgradeIndex < upgradesInventory.getSizeInventory(); upgradeIndex++ )
{
final ItemStack upgradeStack = upgradesInventory.getStackInSlot( upgradeIndex );
final ItemStack leftStack = ia.addItems( upgradeStack );
if( leftStack != null && upgradeStack.getItem() instanceof IUpgradeModule )
{
player.dropPlayerItemWithRandomChoice( upgradeStack, false );
}
}
// drop empty storage cell case
2015-09-30 14:24:40 +02:00
for( final ItemStack storageCellStack : AEApi.instance().definitions().materials().emptyStorageCell().maybeStack( 1 ).asSet() )
{
final ItemStack extraA = ia.addItems( storageCellStack );
if( extraA != null )
{
player.dropPlayerItemWithRandomChoice( extraA, false );
}
}
if( player.inventoryContainer != null )
2015-04-29 02:30:53 +02:00
{
player.inventoryContainer.detectAndSendChanges();
2015-04-29 02:30:53 +02:00
}
return true;
}
}
}
return false;
}
@Override
2015-06-16 02:44:59 +02:00
public boolean onItemUseFirst(
2015-09-30 14:24:40 +02:00
final ItemStack stack,
final EntityPlayer player,
final World world,
final BlockPos pos,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ )
{
2014-12-29 15:13:47 +01:00
return this.disassembleDrive( stack, world, player );
}
@Override
2015-09-30 14:24:40 +02:00
public ItemStack getContainerItem( final ItemStack itemStack )
{
2015-09-30 14:24:40 +02:00
for( final ItemStack stack : AEApi.instance().definitions().materials().emptyStorageCell().maybeStack( 1 ).asSet() )
{
return stack;
}
throw new MissingDefinition( "Tried to use empty storage cells while basic storage cells are defined." );
}
@Override
2015-09-30 14:24:40 +02:00
public boolean hasContainerItem( final ItemStack stack )
{
return AEConfig.instance.isFeatureEnabled( AEFeature.EnableDisassemblyCrafting );
}
}