Applied-Energistics-2-tiler.../src/main/java/appeng/block/AEBaseItemBlockChargeable.java

148 lines
4.2 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>.
*/
2014-09-24 02:26:27 +02:00
package appeng.block;
2014-09-24 02:26:27 +02:00
import java.text.MessageFormat;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.config.AccessRestriction;
import appeng.api.config.PowerUnits;
import appeng.api.definitions.IBlockDefinition;
2014-09-24 02:26:27 +02:00
import appeng.api.implementations.items.IAEItemPowerStorage;
import appeng.core.Api;
2014-09-24 02:26:27 +02:00
import appeng.core.localization.GuiText;
import appeng.util.Platform;
2014-09-24 02:26:27 +02:00
public class AEBaseItemBlockChargeable extends AEBaseItemBlock implements IAEItemPowerStorage
{
2015-09-30 14:24:40 +02:00
public AEBaseItemBlockChargeable( final Block id )
{
2014-09-24 02:26:27 +02:00
super( id );
}
@Override
@SideOnly( Side.CLIENT )
2015-09-30 14:24:40 +02:00
public void addCheckedInformation( final ItemStack itemStack, final EntityPlayer player, final List<String> toolTip, final boolean advancedTooltips )
2014-09-24 02:26:27 +02:00
{
final NBTTagCompound tag = itemStack.getTagCompound();
2014-09-24 02:26:27 +02:00
double internalCurrentPower = 0;
2015-09-30 14:24:40 +02:00
final double internalMaxPower = this.getMaxEnergyCapacity();
2014-09-24 02:26:27 +02:00
if( tag != null )
2014-09-24 02:26:27 +02:00
{
internalCurrentPower = tag.getDouble( "internalCurrentPower" );
}
final double percent = internalCurrentPower / internalMaxPower;
2014-09-24 02:26:27 +02:00
toolTip.add( GuiText.StoredEnergy.getLocal() + ':' + MessageFormat.format( " {0,number,#} ", internalCurrentPower ) + Platform.gui_localize( PowerUnits.AE.unlocalizedName ) + " - " + MessageFormat.format( " {0,number,#.##%} ", percent ) );
2014-09-24 02:26:27 +02:00
}
private double getMaxEnergyCapacity()
2014-09-24 02:26:27 +02:00
{
final Block blockID = Block.getBlockFromItem( this );
final IBlockDefinition energyCell = Api.INSTANCE.definitions().blocks().energyCell();
return energyCell.maybeBlock().map( block -> {
if( blockID == block )
{
return 200000;
}
else
{
return 8 * 200000;
}
}
).orElse( 0 );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public double injectAEPower( final ItemStack is, double amt )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
double internalCurrentPower = this.getInternal( is );
2015-09-30 14:24:40 +02:00
final double internalMaxPower = this.getMaxEnergyCapacity();
2014-09-24 02:26:27 +02:00
internalCurrentPower += amt;
if( internalCurrentPower > internalMaxPower )
2014-09-24 02:26:27 +02:00
{
amt = internalCurrentPower - internalMaxPower;
internalCurrentPower = internalMaxPower;
2014-12-29 15:13:47 +01:00
this.setInternal( is, internalCurrentPower );
2014-09-24 02:26:27 +02:00
return amt;
}
2014-12-29 15:13:47 +01:00
this.setInternal( is, internalCurrentPower );
2014-09-24 02:26:27 +02:00
return 0;
}
2015-09-30 14:24:40 +02:00
private double getInternal( final ItemStack is )
{
final NBTTagCompound nbt = Platform.openNbtData( is );
return nbt.getDouble( "internalCurrentPower" );
}
2015-09-30 14:24:40 +02:00
private void setInternal( final ItemStack is, final double amt )
{
final NBTTagCompound nbt = Platform.openNbtData( is );
nbt.setDouble( "internalCurrentPower", amt );
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public double extractAEPower( final ItemStack is, double amt )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
double internalCurrentPower = this.getInternal( is );
if( internalCurrentPower > amt )
2014-09-24 02:26:27 +02:00
{
internalCurrentPower -= amt;
2014-12-29 15:13:47 +01:00
this.setInternal( is, internalCurrentPower );
2014-09-24 02:26:27 +02:00
return amt;
}
amt = internalCurrentPower;
2014-12-29 15:13:47 +01:00
this.setInternal( is, 0 );
2014-09-24 02:26:27 +02:00
return amt;
}
@Override
2015-09-30 14:24:40 +02:00
public double getAEMaxPower( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.getMaxEnergyCapacity();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public double getAECurrentPower( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.getInternal( is );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public AccessRestriction getPowerFlow( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
return AccessRestriction.WRITE;
}
}