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

329 lines
8.4 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.init.Items;
2014-09-24 02:26:27 +02:00
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.helpers.Reflected;
2014-09-24 02:26:27 +02:00
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;
import appeng.util.Platform;
2014-09-24 02:26:27 +02:00
2014-09-24 02:26:27 +02:00
public class TileVibrationChamber extends AENetworkInvTile implements IGridTickable
{
private static final int FUEL_SLOT_INDEX = 0;
private static final double POWER_PER_TICK = 5;
private static final int[] ACCESSIBLE_SLOTS = { FUEL_SLOT_INDEX };
private static final int MAX_BURN_SPEED = 200;
private static final double DILATION_SCALING = 100.0;
private static final int MIN_BURN_SPEED = 20;
private final IInventory inv = new AppEngInternalInventory( this, 1 );
2014-09-24 02:26:27 +02:00
private int burnSpeed = 100;
private double burnTime = 0;
private double maxBurnTime = 0;
2014-09-24 02:26:27 +02:00
// client side..
public boolean isOn;
public TileVibrationChamber()
{
2015-12-27 12:33:50 +01:00
this.getProxy().setIdlePowerUsage( 0 );
this.getProxy().setFlags();
}
2014-09-24 02:26:27 +02:00
@Override
public AECableType getCableConnectionType( final ForgeDirection dir )
2014-09-24 02:26:27 +02:00
{
return AECableType.COVERED;
}
@Reflected
@TileEvent( TileEventType.NETWORK_READ )
public boolean hasUpdate( final ByteBuf data )
2014-09-24 02:26:27 +02:00
{
final boolean wasOn = this.isOn;
2014-12-29 15:13:47 +01:00
this.isOn = data.readBoolean();
2014-12-29 15:13:47 +01:00
return wasOn != this.isOn; // TESR doesn't need updates!
2014-09-24 02:26:27 +02:00
}
@Reflected
@TileEvent( TileEventType.NETWORK_WRITE )
public void writeToNetwork( final ByteBuf data )
2014-09-24 02:26:27 +02:00
{
data.writeBoolean( this.getBurnTime() > 0 );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_WRITE )
public void writeToNBT_TileVibrationChamber( final NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
data.setDouble( "burnTime", this.getBurnTime() );
data.setDouble( "maxBurnTime", this.getMaxBurnTime() );
data.setInteger( "burnSpeed", this.getBurnSpeed() );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_READ )
public void readFromNBT_TileVibrationChamber( final NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
this.setBurnTime( data.getDouble( "burnTime" ) );
this.setMaxBurnTime( data.getDouble( "maxBurnTime" ) );
this.setBurnSpeed( 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 boolean isItemValidForSlot( final int i, final ItemStack itemstack )
{
return TileEntityFurnace.getItemBurnTime( itemstack ) > 0;
}
@Override
public void onChangeInventory( final IInventory inv, final int slot, final InvOperation mc, final ItemStack removed, final ItemStack added )
{
if( this.getBurnTime() <= 0 )
2014-09-24 02:26:27 +02:00
{
if( this.canEatFuel() )
2014-09-24 02:26:27 +02:00
{
try
{
2015-12-27 12:33:50 +01:00
this.getProxy().getTick().wakeDevice( this.getProxy().getNode() );
2014-09-24 02:26:27 +02:00
}
catch( final GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// wake up!
}
}
}
}
@Override
public boolean canExtractItem( final int slotIndex, final ItemStack extractedItem, final int side )
2014-09-24 02:26:27 +02:00
{
return extractedItem.getItem() == Items.bucket;
2014-09-24 02:26:27 +02:00
}
@Override
public int[] getAccessibleSlotsBySide( final ForgeDirection side )
2014-09-24 02:26:27 +02:00
{
return ACCESSIBLE_SLOTS;
2014-09-24 02:26:27 +02:00
}
private boolean canEatFuel()
2014-09-24 02:26:27 +02:00
{
final ItemStack is = this.getStackInSlot( FUEL_SLOT_INDEX );
if( is != null )
{
final 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( final IGridNode node )
2014-09-24 02:26:27 +02:00
{
if( this.getBurnTime() <= 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
return new TickingRequest( TickRates.VibrationChamber.getMin(), TickRates.VibrationChamber.getMax(), this.getBurnTime() <= 0, false );
2014-09-24 02:26:27 +02:00
}
@Override
public TickRateModulation tickingRequest( final IGridNode node, final int ticksSinceLastCall )
2014-09-24 02:26:27 +02:00
{
if( this.getBurnTime() <= 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.getBurnTime() > 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
this.setBurnSpeed( 100 );
2014-09-24 02:26:27 +02:00
return TickRateModulation.SLEEP;
}
this.setBurnSpeed( Math.max( MIN_BURN_SPEED, Math.min( this.getBurnSpeed(), MAX_BURN_SPEED ) ) );
final double dilation = this.getBurnSpeed() / DILATION_SCALING;
2014-09-24 02:26:27 +02:00
double timePassed = ticksSinceLastCall * dilation;
this.setBurnTime( this.getBurnTime() - timePassed );
if( this.getBurnTime() < 0 )
2014-09-24 02:26:27 +02:00
{
timePassed += this.getBurnTime();
this.setBurnTime( 0 );
2014-09-24 02:26:27 +02:00
}
try
{
2015-12-27 12:33:50 +01:00
final IEnergyGrid grid = this.getProxy().getEnergy();
final double newPower = timePassed * POWER_PER_TICK;
final double overFlow = grid.injectPower( newPower, Actionable.SIMULATE );
2014-09-24 02:26:27 +02:00
// 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
{
this.setBurnSpeed( this.getBurnSpeed() - 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
{
this.setBurnSpeed( this.getBurnSpeed() + ticksSinceLastCall );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
this.setBurnSpeed( Math.max( MIN_BURN_SPEED, Math.min( this.getBurnSpeed(), MAX_BURN_SPEED ) ) );
2014-09-24 02:26:27 +02:00
return overFlow > 0 ? TickRateModulation.SLOWER : TickRateModulation.FASTER;
}
catch( final GridAccessException e )
2014-09-24 02:26:27 +02:00
{
this.setBurnSpeed( this.getBurnSpeed() - ticksSinceLastCall );
this.setBurnSpeed( Math.max( MIN_BURN_SPEED, Math.min( this.getBurnSpeed(), MAX_BURN_SPEED ) ) );
2014-09-24 02:26:27 +02:00
return TickRateModulation.SLOWER;
}
}
private void eatFuel()
{
final ItemStack is = this.getStackInSlot( FUEL_SLOT_INDEX );
if( is != null )
2014-09-24 02:26:27 +02:00
{
final int newBurnTime = TileEntityFurnace.getItemBurnTime( is );
if( newBurnTime > 0 && is.stackSize > 0 )
2014-09-24 02:26:27 +02:00
{
this.setBurnTime( this.getBurnTime() + newBurnTime );
this.setMaxBurnTime( this.getBurnTime() );
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() != null && 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
}
this.markDirty();
2014-09-24 02:26:27 +02:00
}
}
if( this.getBurnTime() > 0 )
2014-09-24 02:26:27 +02:00
{
try
{
2015-12-27 12:33:50 +01:00
this.getProxy().getTick().wakeDevice( this.getProxy().getNode() );
2014-09-24 02:26:27 +02:00
}
catch( final GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// gah!
}
}
// state change
if( ( !this.isOn && this.getBurnTime() > 0 ) || ( this.isOn && this.getBurnTime() <= 0 ) )
2014-09-24 02:26:27 +02:00
{
this.isOn = this.getBurnTime() > 0;
2014-12-29 15:13:47 +01:00
this.markForUpdate();
2015-08-06 18:49:57 +02:00
if( this.hasWorldObj() )
{
Platform.notifyBlocksOfNeighbors( this.worldObj, this.xCoord, this.yCoord, this.zCoord );
}
2014-09-24 02:26:27 +02:00
}
}
public int getBurnSpeed()
{
return this.burnSpeed;
}
private void setBurnSpeed( final int burnSpeed )
{
this.burnSpeed = burnSpeed;
}
public double getMaxBurnTime()
{
return this.maxBurnTime;
}
private void setMaxBurnTime( final double maxBurnTime )
{
this.maxBurnTime = maxBurnTime;
}
public double getBurnTime()
{
return this.burnTime;
}
private void setBurnTime( final double burnTime )
{
this.burnTime = burnTime;
}
2014-09-24 02:26:27 +02:00
}