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

288 lines
6.9 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;
2014-09-24 02:26:27 +02:00
import io.netty.buffer.ByteBuf;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityFurnace;
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.config.Actionable;
import appeng.api.networking.IGridNode;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
import appeng.core.settings.TickRates;
import appeng.me.GridAccessException;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkInvTile;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.InvOperation;
2014-09-24 02:26:27 +02:00
public class TileVibrationChamber extends AENetworkInvTile implements IGridTickable
{
final double powerPerTick = 5;
final int[] sides = new int[] { 0 };
final AppEngInternalInventory inv = new AppEngInternalInventory( this, 1 );
2014-09-24 02:26:27 +02:00
public int burnSpeed = 100;
public double burnTime = 0;
public double maxBurnTime = 0;
// client side..
public boolean isOn;
public TileVibrationChamber()
{
this.gridProxy.setIdlePowerUsage( 0 );
this.gridProxy.setFlags();
}
2014-09-24 02:26:27 +02:00
@Override
public AECableType getCableConnectionType( ForgeDirection dir )
2014-09-24 02:26:27 +02:00
{
return AECableType.COVERED;
}
@TileEvent( TileEventType.NETWORK_READ )
public boolean readFromStream_TileVibrationChamber( ByteBuf data )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
boolean wasOn = this.isOn;
this.isOn = data.readBoolean();
return wasOn != this.isOn; // TESR doesn't need updates!
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.NETWORK_WRITE )
public void writeToStream_TileVibrationChamber( ByteBuf data )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
data.writeBoolean( this.burnTime > 0 );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_WRITE )
public void writeToNBT_TileVibrationChamber( NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
data.setDouble( "burnTime", this.burnTime );
data.setDouble( "maxBurnTime", this.maxBurnTime );
data.setInteger( "burnSpeed", this.burnSpeed );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_READ )
public void readFromNBT_TileVibrationChamber( NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.burnTime = data.getDouble( "burnTime" );
this.maxBurnTime = data.getDouble( "maxBurnTime" );
this.burnSpeed = data.getInteger( "burnSpeed" );
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 int getInventoryStackLimit()
2014-09-24 02:26:27 +02:00
{
return 64;
}
@Override
public boolean isItemValidForSlot( int i, ItemStack itemstack )
{
return TileEntityFurnace.getItemBurnTime( itemstack ) > 0;
}
@Override
public void onChangeInventory( IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added )
{
if( this.burnTime <= 0 )
2014-09-24 02:26:27 +02:00
{
if( this.canEatFuel() )
2014-09-24 02:26:27 +02:00
{
try
{
2014-12-29 15:13:47 +01:00
this.gridProxy.getTick().wakeDevice( this.gridProxy.getNode() );
2014-09-24 02:26:27 +02:00
}
catch( GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// wake up!
}
}
}
}
@Override
public boolean canExtractItem( int slotIndex, ItemStack extractedItem, int side )
2014-09-24 02:26:27 +02:00
{
return false;
2014-09-24 02:26:27 +02:00
}
@Override
public int[] getAccessibleSlotsBySide( ForgeDirection side )
2014-09-24 02:26:27 +02:00
{
return this.sides;
2014-09-24 02:26:27 +02:00
}
private boolean canEatFuel()
2014-09-24 02:26:27 +02:00
{
ItemStack is = this.getStackInSlot( 0 );
if( is != null )
{
int newBurnTime = TileEntityFurnace.getItemBurnTime( is );
if( newBurnTime > 0 && is.stackSize > 0 )
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
return false;
}
@Override
public DimensionalCoord getLocation()
{
return new DimensionalCoord( this );
}
@Override
public TickingRequest getTickingRequest( IGridNode node )
2014-09-24 02:26:27 +02:00
{
if( this.burnTime <= 0 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.eatFuel();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
return new TickingRequest( TickRates.VibrationChamber.min, TickRates.VibrationChamber.max, this.burnTime <= 0, false );
2014-09-24 02:26:27 +02:00
}
@Override
public TickRateModulation tickingRequest( IGridNode node, int TicksSinceLastCall )
2014-09-24 02:26:27 +02:00
{
if( this.burnTime <= 0 )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.eatFuel();
2014-09-24 02:26:27 +02:00
if( this.burnTime > 0 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return TickRateModulation.URGENT;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.burnSpeed = 100;
2014-09-24 02:26:27 +02:00
return TickRateModulation.SLEEP;
}
2014-12-29 15:13:47 +01:00
this.burnSpeed = Math.max( 20, Math.min( this.burnSpeed, 200 ) );
double dilation = this.burnSpeed / 100.0;
2014-09-24 02:26:27 +02:00
2014-09-28 20:56:16 +02:00
double timePassed = TicksSinceLastCall * dilation;
2014-12-29 15:13:47 +01:00
this.burnTime -= timePassed;
if( this.burnTime < 0 )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
timePassed += this.burnTime;
this.burnTime = 0;
2014-09-24 02:26:27 +02:00
}
try
{
2014-12-29 15:13:47 +01:00
IEnergyGrid grid = this.gridProxy.getEnergy();
double newPower = timePassed * this.powerPerTick;
2014-09-24 02:26:27 +02:00
double overFlow = grid.injectPower( newPower, Actionable.SIMULATE );
// burn the over flow.
grid.injectPower( Math.max( 0.0, newPower - overFlow ), Actionable.MODULATE );
if( overFlow > 0 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.burnSpeed -= TicksSinceLastCall;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.burnSpeed += TicksSinceLastCall;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.burnSpeed = Math.max( 20, Math.min( this.burnSpeed, 200 ) );
2014-09-24 02:26:27 +02:00
return overFlow > 0 ? TickRateModulation.SLOWER : TickRateModulation.FASTER;
}
catch( GridAccessException e )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.burnSpeed -= TicksSinceLastCall;
this.burnSpeed = Math.max( 20, Math.min( this.burnSpeed, 200 ) );
2014-09-24 02:26:27 +02:00
return TickRateModulation.SLOWER;
}
}
private void eatFuel()
{
2014-12-29 15:13:47 +01:00
ItemStack is = this.getStackInSlot( 0 );
if( is != null )
2014-09-24 02:26:27 +02:00
{
int newBurnTime = TileEntityFurnace.getItemBurnTime( is );
if( newBurnTime > 0 && is.stackSize > 0 )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.burnTime += newBurnTime;
this.maxBurnTime = this.burnTime;
2014-09-24 02:26:27 +02:00
is.stackSize--;
if( is.stackSize <= 0 )
2014-09-24 02:26:27 +02:00
{
ItemStack container = null;
if( is.getItem().hasContainerItem( is ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
container = is.getItem().getContainerItem( is );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.setInventorySlotContents( 0, container );
2014-09-24 02:26:27 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.setInventorySlotContents( 0, is );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
if( this.burnTime > 0 )
2014-09-24 02:26:27 +02:00
{
try
{
2014-12-29 15:13:47 +01:00
this.gridProxy.getTick().wakeDevice( this.gridProxy.getNode() );
2014-09-24 02:26:27 +02:00
}
catch( GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// gah!
}
}
if( ( !this.isOn && this.burnTime > 0 ) || ( this.isOn && this.burnTime <= 0 ) )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.isOn = this.burnTime > 0;
this.markForUpdate();
2014-09-24 02:26:27 +02:00
}
}
}