#2527: Implements charging of tools via RF (Forge Energy) and Tesla. Tested with Tesla Essentials and EnderIO.

This commit is contained in:
Sebastian Hartte 2016-10-30 02:38:20 +02:00
parent 89299cdb3c
commit 53c32cc296
7 changed files with 177 additions and 273 deletions

View File

@ -19,6 +19,8 @@
package appeng.capabilities;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.darkhax.tesla.api.ITeslaHolder;
import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
@ -41,6 +43,12 @@ public final class Capabilities
@CapabilityInject( IStorageMonitorableAccessor.class )
public static Capability<IStorageMonitorableAccessor> STORAGE_MONITORABLE_ACCESSOR;
@CapabilityInject(ITeslaConsumer.class)
public static Capability<ITeslaConsumer> TESLA_CONSUMER;
@CapabilityInject(ITeslaHolder.class)
public static Capability<ITeslaHolder> TESLA_HOLDER;
/**
* Register AE2 provided capabilities.
*/

View File

@ -27,6 +27,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import appeng.api.config.AccessRestriction;
import appeng.api.config.PowerUnits;
@ -145,27 +146,6 @@ public abstract class AERootPoweredItem extends AEBaseItem implements IAEItemPow
return currentStorage;
}
/**
* inject external
*/
double injectExternalPower( final PowerUnits input, final ItemStack is, final double amount, final boolean simulate )
{
if( simulate )
{
final int requiredEU = (int) PowerUnits.AE.convertTo( PowerUnits.EU, this.getAEMaxPower( is ) - this.getAECurrentPower( is ) );
if( amount < requiredEU )
{
return 0;
}
return amount - requiredEU;
}
else
{
final double powerRemainder = this.injectAEPower( is, PowerUnits.EU.convertTo( PowerUnits.AE, amount ) );
return PowerUnits.AE.convertTo( PowerUnits.EU, powerRemainder );
}
}
@Override
public double injectAEPower( final ItemStack is, final double amt )
{
@ -196,6 +176,12 @@ public abstract class AERootPoweredItem extends AEBaseItem implements IAEItemPow
return AccessRestriction.WRITE;
}
@Override
public ICapabilityProvider initCapabilities( ItemStack stack, NBTTagCompound nbt )
{
return new PoweredItemCapabilities( stack, this );
}
private enum batteryOperation
{
STORAGE, INJECT, EXTRACT

View File

@ -0,0 +1,158 @@
/*
* 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();
}
}
}

View File

@ -1,64 +0,0 @@
/*
* 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 java.util.Optional;
//
//import net.minecraft.item.ItemStack;
//
//import cofh.api.energy.IEnergyContainerItem;
//
//import appeng.api.config.PowerUnits;
//import appeng.integration.IntegrationType;
//import appeng.transformer.annotations.Integration.Interface;
//
//
//@Interface( iface = "cofh.api.energy.IEnergyContainerItem", iname = IntegrationType.RFItem )
//public abstract class RedstoneFlux extends IC2 implements IEnergyContainerItem
//{
// public RedstoneFlux( double powerCapacity, Optional<String> subName )
// {
// super( powerCapacity, subName );
// }
//
// @Override
// public int receiveEnergy( ItemStack is, int maxReceive, boolean simulate )
// {
// return maxReceive - (int) this.injectExternalPower( PowerUnits.RF, is, maxReceive, simulate );
// }
//
// @Override
// public int extractEnergy( ItemStack container, int maxExtract, boolean simulate )
// {
// return 0;
// }
//
// @Override
// public int getEnergyStored( ItemStack is )
// {
// return (int) PowerUnits.AE.convertTo( PowerUnits.RF, this.getAECurrentPower( is ) );
// }
//
// @Override
// public int getMaxEnergyStored( ItemStack is )
// {
// return (int) PowerUnits.AE.convertTo( PowerUnits.RF, this.getAEMaxPower( is ) );
// }
// }

View File

@ -1,56 +0,0 @@
/*
* 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;
/*
@Interface(iface = "universalelectricity.core.item.IItemElectric", modid = "IC2")
public class UniversalElectricity extends ThermalExpansion implements IItemElectric
{
*
* public UniversalElectricity(Class c, String subName) { super( c, subName ); }
*
* @Override public float recharge(ItemStack is, float energy, boolean
* doRecharge) { return (float) (energy - injectExternalPower( PowerUnits.KJ,
* is, energy, !doRecharge )); }
*
* @Override public float discharge(ItemStack is, float energy, boolean
* doDischarge) { return 0; }
*
* @Override public float getElectricityStored(ItemStack is) { return (int)
* PowerUnits.AE.convertTo( PowerUnits.KJ, getAECurrentPower( is ) ); }
*
* @Override public float getMaxElectricityStored(ItemStack is) { return (int)
* PowerUnits.AE.convertTo( PowerUnits.KJ, getAEMaxPower( is ) ); }
*
* @Override public void setElectricity(ItemStack is, float joules) { double
* currentPower = getAECurrentPower( is ); double targetPower =
* PowerUnits.KJ.convertTo( PowerUnits.AE, joules ); if ( targetPower >
* currentPower ) injectAEPower( is, targetPower - currentPower ); else
* extractAEPower( is, currentPower - targetPower ); }
*
* @Override public float getTransfer(ItemStack is) { return (float)
* PowerUnits.AE.convertTo( PowerUnits.KJ, getAEMaxPower( is ) -
* getAECurrentPower( is ) ); }
*
* @Override public float getVoltage(ItemStack itemStack) { return 120; }
}
*/

View File

@ -22,11 +22,9 @@ package appeng.tile.powersink;
import java.util.EnumSet;
import javax.annotation.Nullable;
import net.darkhax.tesla.api.ITeslaConsumer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
@ -36,6 +34,7 @@ import appeng.api.config.PowerMultiplier;
import appeng.api.config.PowerUnits;
import appeng.api.networking.energy.IAEPowerStorage;
import appeng.api.networking.events.MENetworkPowerStorage.PowerEventType;
import appeng.capabilities.Capabilities;
import appeng.integration.modules.IC2;
import appeng.integration.modules.ic2.IC2PowerSink;
import appeng.tile.AEBaseInvTile;
@ -45,8 +44,6 @@ import appeng.tile.events.TileEventType;
public abstract class AERootPoweredTile extends AEBaseInvTile implements IAEPowerStorage, IExternalPowerSink
{
@CapabilityInject(ITeslaConsumer.class)
private static Capability<ITeslaConsumer> teslaConsumerCapability;
// values that determine general function, are set by inheriting classes if
// needed. These should generally remain static.
@ -64,7 +61,7 @@ public abstract class AERootPoweredTile extends AEBaseInvTile implements IAEPowe
public AERootPoweredTile()
{
forgeEnergyAdapter = new ForgeEnergyAdapter( this );
if( teslaConsumerCapability != null )
if( Capabilities.TESLA_CONSUMER != null )
{
teslaEnergyAdapter = new TeslaEnergyAdapter( this );
}
@ -293,7 +290,7 @@ public abstract class AERootPoweredTile extends AEBaseInvTile implements IAEPowe
return true;
}
}
else if( capability == teslaConsumerCapability )
else if( capability == Capabilities.TESLA_CONSUMER )
{
if( this.getPowerSides().contains( facing ) )
{
@ -315,7 +312,7 @@ public abstract class AERootPoweredTile extends AEBaseInvTile implements IAEPowe
return (T) forgeEnergyAdapter;
}
}
else if( capability == teslaConsumerCapability )
else if( capability == Capabilities.TESLA_CONSUMER )
{
if( this.getPowerSides().contains( facing ) )
{

View File

@ -1,125 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, 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.tile.powersink;
//import java.util.EnumSet;
//
//import net.minecraft.tileentity.TileEntity;
//import net.minecraftforge.common.util.ForgeDirection;
//
//import ic2.api.energy.tile.IEnergySink;
//
//import appeng.api.config.PowerUnits;
//import appeng.integration.IntegrationRegistry;
//import appeng.integration.IntegrationType;
//import appeng.integration.abstraction.IIC2;
//import appeng.transformer.annotations.Integration.Interface;
//import appeng.util.Platform;
//
//
//@Interface( iname = IntegrationType.IC2, iface = "ic2.api.energy.tile.IEnergySink" )
//public abstract class IC2 extends AERootPoweredTile implements IEnergySink
//{
//
// boolean isInIC2 = false;
//
// @Override
// public final boolean acceptsEnergyFrom( TileEntity emitter, ForgeDirection direction )
// {
// return this.getPowerSides().contains( direction );
// }
//
// @Override
// public final double getDemandedEnergy()
// {
// return this.getExternalPowerDemand( PowerUnits.EU, Double.MAX_VALUE );
// }
//
// @Override
// public final int getSinkTier()
// {
// return Integer.MAX_VALUE;
// }
//
// @Override
// public final double injectEnergy( ForgeDirection directionFrom, double amount, double voltage )
// {
// // just store the excess in the current block, if I return the waste,
// // IC2 will just disintegrate it - Oct 20th 2013
// double overflow = PowerUnits.EU.convertTo( PowerUnits.AE, this.injectExternalPower( PowerUnits.EU, amount ) );
// this.internalCurrentPower += overflow;
// return 0; // see above comment.
// }
//
// @Override
// public void invalidate()
// {
// super.invalidate();
// this.removeFromENet();
// }
//
// private void removeFromENet()
// {
// if( IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.IC2 ) )
// {
// IIC2 ic2Integration = (IIC2) IntegrationRegistry.INSTANCE.getInstance( IntegrationType.IC2 );
// if( this.isInIC2 && Platform.isServer() && ic2Integration != null )
// {
// ic2Integration.removeFromEnergyNet( this );
// this.isInIC2 = false;
// }
// }
// }
//
// @Override
// public void onChunkUnload()
// {
// super.onChunkUnload();
// this.removeFromENet();
// }
//
// @Override
// public void onReady()
// {
// super.onReady();
// this.addToENet();
// }
//
// private void addToENet()
// {
// if( IntegrationRegistry.INSTANCE.isEnabled( IntegrationType.IC2 ) )
// {
// IIC2 ic2Integration = (IIC2) IntegrationRegistry.INSTANCE.getInstance( IntegrationType.IC2 );
// if( !this.isInIC2 && Platform.isServer() && ic2Integration != null )
// {
// ic2Integration.addToEnergyNet( this );
// this.isInIC2 = true;
// }
// }
// }
//
// @Override
// protected void setPowerSides( EnumSet<ForgeDirection> sides )
// {
// super.setPowerSides( sides );
// this.removeFromENet();
// this.addToENet();
// }
// }