157 lines
3.9 KiB
Java
157 lines
3.9 KiB
Java
/*
|
|
* 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.tools.powered.powersink;
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import net.darkhax.tesla.api.ITeslaConsumer;
|
|
import net.darkhax.tesla.api.ITeslaHolder;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
|
import net.minecraftforge.energy.CapabilityEnergy;
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
|
|
|
import appeng.api.config.PowerUnits;
|
|
import appeng.api.implementations.items.IAEItemPowerStorage;
|
|
import appeng.capabilities.Capabilities;
|
|
|
|
|
|
/**
|
|
* The capability provider to expose chargable items to other mods.
|
|
*/
|
|
class PoweredItemCapabilities implements ICapabilityProvider, IEnergyStorage
|
|
{
|
|
|
|
private final ItemStack is;
|
|
|
|
private final IAEItemPowerStorage item;
|
|
|
|
private final Object teslaAdapter;
|
|
|
|
PoweredItemCapabilities( ItemStack is, IAEItemPowerStorage item )
|
|
{
|
|
this.is = is;
|
|
this.item = item;
|
|
if( Capabilities.TESLA_CONSUMER != null || Capabilities.TESLA_HOLDER != null )
|
|
{
|
|
this.teslaAdapter = new TeslaAdapter();
|
|
}
|
|
else
|
|
{
|
|
this.teslaAdapter = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean hasCapability( Capability<?> capability, @Nullable EnumFacing facing )
|
|
{
|
|
return capability == CapabilityEnergy.ENERGY || capability == Capabilities.TESLA_CONSUMER || capability == Capabilities.TESLA_HOLDER;
|
|
}
|
|
|
|
@SuppressWarnings( "unchecked" )
|
|
@Override
|
|
public <T> T getCapability( Capability<T> capability, @Nullable EnumFacing facing )
|
|
{
|
|
if( capability == CapabilityEnergy.ENERGY )
|
|
{
|
|
return (T) this;
|
|
}
|
|
else if( capability == Capabilities.TESLA_CONSUMER || capability == Capabilities.TESLA_HOLDER )
|
|
{
|
|
return (T) teslaAdapter;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public int receiveEnergy( int maxReceive, boolean simulate )
|
|
{
|
|
|
|
if( simulate )
|
|
{
|
|
final int required = (int) PowerUnits.AE.convertTo( PowerUnits.RF, item.getAEMaxPower( is ) - item.getAECurrentPower( is ) );
|
|
if( maxReceive < required )
|
|
{
|
|
return 0;
|
|
}
|
|
return maxReceive - required;
|
|
}
|
|
else
|
|
{
|
|
final double powerRemainder = item.injectAEPower( is, PowerUnits.RF.convertTo( PowerUnits.AE, maxReceive ) );
|
|
return (int) PowerUnits.AE.convertTo( PowerUnits.RF, powerRemainder );
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int extractEnergy( int maxExtract, boolean simulate )
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public int getEnergyStored()
|
|
{
|
|
return (int) PowerUnits.AE.convertTo( PowerUnits.RF, item.getAECurrentPower( is ) );
|
|
}
|
|
|
|
@Override
|
|
public int getMaxEnergyStored()
|
|
{
|
|
return (int) PowerUnits.AE.convertTo( PowerUnits.RF, item.getAEMaxPower( is ) );
|
|
}
|
|
|
|
@Override
|
|
public boolean canExtract()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean canReceive()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
private class TeslaAdapter implements ITeslaConsumer, ITeslaHolder
|
|
{
|
|
|
|
@Override
|
|
public long givePower( long power, boolean simulated )
|
|
{
|
|
return receiveEnergy( (int) power, simulated );
|
|
}
|
|
|
|
@Override
|
|
public long getStoredPower()
|
|
{
|
|
return getEnergyStored();
|
|
}
|
|
|
|
@Override
|
|
public long getCapacity()
|
|
{
|
|
return getMaxEnergyStored();
|
|
}
|
|
}
|
|
}
|