Applied-Energistics-2-tiler.../src/main/java/appeng/tile/misc/TileCharger.java

265 lines
7.7 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.tile.misc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
2014-12-29 21:59:05 +01:00
import io.netty.buffer.ByteBuf;
2014-09-24 02:26:27 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
2014-12-29 21:59:05 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
import appeng.api.config.PowerUnits;
import appeng.api.implementations.items.IAEItemPowerStorage;
import appeng.api.implementations.tiles.ICrankable;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
import appeng.me.GridAccessException;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkPowerTile;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
public class TileCharger extends AENetworkPowerTile implements ICrankable
{
final int[] sides = new int[] { 0 };
final AppEngInternalInventory inv = new AppEngInternalInventory( this, 1 );
2014-09-24 02:26:27 +02:00
int tickTickTimer = 0;
int lastUpdate = 0;
boolean requiresUpdate = false;
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
return AECableType.COVERED;
}
@TileEvent(TileEventType.NETWORK_READ)
2014-10-04 08:08:28 +02:00
public boolean readFromStream_TileCharger(ByteBuf data)
2014-09-24 02:26:27 +02:00
{
try
{
IAEItemStack item = AEItemStack.loadItemStackFromPacket( data );
ItemStack is = item.getItemStack();
2014-12-29 15:13:47 +01:00
this.inv.setInventorySlotContents( 0, is );
2014-09-24 02:26:27 +02:00
}
catch (Throwable t)
{
2014-12-29 15:13:47 +01:00
this.inv.setInventorySlotContents( 0, null );
2014-09-24 02:26:27 +02:00
}
return false; // TESR doesn't need updates!
}
@TileEvent(TileEventType.NETWORK_WRITE)
public void writeToStream_TileCharger(ByteBuf data) throws IOException
{
2014-12-29 15:13:47 +01:00
AEItemStack is = AEItemStack.create( this.getStackInSlot( 0 ) );
2014-09-24 02:26:27 +02:00
if ( is != null )
is.writeToPacket( data );
}
@TileEvent(TileEventType.TICK)
public void Tick_TileCharger()
{
2014-12-29 15:13:47 +01:00
if ( this.lastUpdate > 60 && this.requiresUpdate )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.requiresUpdate = false;
this.markForUpdate();
this.lastUpdate = 0;
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
this.lastUpdate++;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.tickTickTimer++;
if ( this.tickTickTimer < 20 )
2014-09-24 02:26:27 +02:00
return;
2014-12-29 15:13:47 +01:00
this.tickTickTimer = 0;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
ItemStack myItem = this.getStackInSlot( 0 );
2014-09-24 02:26:27 +02:00
// charge from the network!
2014-12-29 15:13:47 +01:00
if ( this.internalCurrentPower < 1499 )
2014-09-24 02:26:27 +02:00
{
try
{
2014-12-29 15:13:47 +01:00
this.injectExternalPower( PowerUnits.AE,
this.gridProxy.getEnergy().extractAEPower( Math.min( 150.0, 1500.0 - this.internalCurrentPower ), Actionable.MODULATE, PowerMultiplier.ONE ) );
this.tickTickTimer = 20; // keep ticking...
2014-09-24 02:26:27 +02:00
}
catch (GridAccessException e)
{
// continue!
}
}
if ( myItem == null )
return;
2014-12-29 15:13:47 +01:00
if ( this.internalCurrentPower > 149 && Platform.isChargeable( myItem ) )
2014-09-24 02:26:27 +02:00
{
IAEItemPowerStorage ps = (IAEItemPowerStorage) myItem.getItem();
if ( ps.getAEMaxPower( myItem ) > ps.getAECurrentPower( myItem ) )
{
2014-12-29 15:13:47 +01:00
double oldPower = this.internalCurrentPower;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
double adjustment = ps.injectAEPower( myItem, this.extractAEPower( 150.0, Actionable.MODULATE, PowerMultiplier.CONFIG ) );
this.internalCurrentPower += adjustment;
if ( oldPower > this.internalCurrentPower )
this.requiresUpdate = true;
this.tickTickTimer = 20; // keep ticking...
2014-09-24 02:26:27 +02:00
}
}
2014-12-29 15:13:47 +01:00
else if ( this.internalCurrentPower > 1499 && AEApi.instance().materials().materialCertusQuartzCrystal.sameAsStack( myItem ) )
2014-09-24 02:26:27 +02:00
{
if ( Platform.getRandomFloat() > 0.8f ) // simulate wait
{
2014-12-29 15:13:47 +01:00
this.extractAEPower( this.internalMaxPower, Actionable.MODULATE, PowerMultiplier.CONFIG );// 1500
this.setInventorySlotContents( 0, AEApi.instance().materials().materialCertusQuartzCrystalCharged.stack( myItem.stackSize ) );
2014-09-24 02:26:27 +02:00
}
}
}
public TileCharger() {
2014-12-29 15:13:47 +01:00
this.gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
this.gridProxy.setFlags();
this.internalMaxPower = 1500;
this.gridProxy.setIdlePowerUsage( 0 );
2014-09-24 02:26:27 +02:00
}
@Override
public void setOrientation(ForgeDirection inForward, ForgeDirection inUp)
{
super.setOrientation( inForward, inUp );
2014-12-29 15:13:47 +01:00
this.gridProxy.setValidSides( EnumSet.of( this.getUp(), this.getUp().getOpposite() ) );
this.setPowerSides( EnumSet.of( this.getUp(), this.getUp().getOpposite() ) );
2014-09-24 02:26:27 +02:00
}
@Override
public boolean canTurn()
{
2014-12-29 15:13:47 +01:00
return this.internalCurrentPower < this.internalMaxPower;
2014-09-24 02:26:27 +02:00
}
@Override
public void applyTurn()
{
2014-12-29 15:13:47 +01:00
this.injectExternalPower( PowerUnits.AE, 150 );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
ItemStack myItem = this.getStackInSlot( 0 );
if ( this.internalCurrentPower > 1499 && AEApi.instance().materials().materialCertusQuartzCrystal.sameAsStack( myItem ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.extractAEPower( this.internalMaxPower, Actionable.MODULATE, PowerMultiplier.CONFIG );// 1500
this.setInventorySlotContents( 0, AEApi.instance().materials().materialCertusQuartzCrystalCharged.stack( myItem.stackSize ) );
2014-09-24 02:26:27 +02:00
}
}
@Override
public boolean canCrankAttach(ForgeDirection directionToCrank)
{
return this.getUp() == directionToCrank || this.getUp().getOpposite() == directionToCrank;
2014-09-24 02:26:27 +02:00
}
@Override
public IInventory getInternalInventory()
{
2014-12-29 15:13:47 +01:00
return this.inv;
2014-09-24 02:26:27 +02:00
}
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
{
2014-12-29 15:13:47 +01:00
this.markForUpdate();
2014-09-24 02:26:27 +02:00
}
@Override
public int[] getAccessibleSlotsBySide(ForgeDirection whichSide)
{
2014-12-29 15:13:47 +01:00
return this.sides;
2014-09-24 02:26:27 +02:00
}
@Override
public int getInventoryStackLimit()
{
return 1;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack)
{
return Platform.isChargeable( itemstack ) || AEApi.instance().materials().materialCertusQuartzCrystal.sameAsStack( itemstack );
}
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j)
{
if ( Platform.isChargeable( itemstack ) )
{
IAEItemPowerStorage ips = (IAEItemPowerStorage) itemstack.getItem();
if ( ips.getAECurrentPower( itemstack ) >= ips.getAEMaxPower( itemstack ) )
return true;
}
return AEApi.instance().materials().materialCertusQuartzCrystalCharged.sameAsStack( itemstack );
}
public void activate(EntityPlayer player)
{
if ( !Platform.hasPermissions( new DimensionalCoord( this ), player ) )
return;
2014-12-29 15:13:47 +01:00
ItemStack myItem = this.getStackInSlot( 0 );
2014-09-24 02:26:27 +02:00
if ( myItem == null )
{
ItemStack held = player.inventory.getCurrentItem();
if ( AEApi.instance().materials().materialCertusQuartzCrystal.sameAsStack( held ) || Platform.isChargeable( held ) )
{
held = player.inventory.decrStackSize( player.inventory.currentItem, 1 );
2014-12-29 15:13:47 +01:00
this.setInventorySlotContents( 0, held );
2014-09-24 02:26:27 +02:00
}
}
else
{
2014-09-28 22:20:14 +02:00
List<ItemStack> drops = new ArrayList<ItemStack>();
2014-09-24 02:26:27 +02:00
drops.add( myItem );
2014-12-29 15:13:47 +01:00
this.setInventorySlotContents( 0, null );
Platform.spawnDrops( this.worldObj, this.xCoord + this.getForward().offsetX, this.yCoord + this.getForward().offsetY, this.zCoord + this.getForward().offsetZ, drops );
2014-09-24 02:26:27 +02:00
}
}
@Override
public boolean requiresTESR()
{
return true;
}
}