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

487 lines
13 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 java.io.IOException;
import java.util.ArrayList;
2014-09-24 02:26:27 +02:00
import java.util.EnumSet;
2014-12-29 21:59:05 +01:00
import io.netty.buffer.ByteBuf;
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.world.World;
2014-09-24 02:26:27 +02:00
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.Upgrades;
import appeng.api.definitions.IComparableDefinition;
import appeng.api.definitions.ITileDefinition;
import appeng.api.implementations.IUpgradeableHost;
2014-09-24 02:26:27 +02:00
import appeng.api.networking.IGridNode;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.energy.IEnergySource;
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.IConfigManager;
2014-09-24 02:26:27 +02:00
import appeng.core.settings.TickRates;
import appeng.helpers.Reflected;
2014-09-24 02:26:27 +02:00
import appeng.me.GridAccessException;
import appeng.parts.automation.DefinitionUpgradeInventory;
import appeng.parts.automation.UpgradeInventory;
2014-09-24 02:26:27 +02:00
import appeng.recipes.handlers.Inscribe;
import appeng.recipes.handlers.Inscribe.InscriberRecipe;
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.ConfigManager;
import appeng.util.IConfigManagerHost;
2014-09-24 02:26:27 +02:00
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.inv.WrapperInventoryRange;
import appeng.util.item.AEItemStack;
public class TileInscriber extends AENetworkPowerTile implements IGridTickable, IUpgradeableHost, IConfigManagerHost
2014-09-24 02:26:27 +02:00
{
public final int maxProcessingTime = 100;
final int[] top = new int[] { 0 };
final int[] bottom = new int[] { 1 };
final int[] sides = new int[] { 2, 3 };
final AppEngInternalInventory inv = new AppEngInternalInventory( this, 4 );
private final IConfigManager settings;
private final UpgradeInventory upgrades;
2014-09-24 02:26:27 +02:00
public int processingTime = 0;
// cycles from 0 - 16, at 8 it preforms the action, at 16 it re-enables the normal routine.
public boolean smash;
public int finalStep;
public long clientStart;
@Reflected
public TileInscriber()
{
this.gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
this.internalMaxPower = 1500;
this.gridProxy.setIdlePowerUsage( 0 );
this.settings = new ConfigManager( this );
final ITileDefinition inscriberDefinition = AEApi.instance().definitions().blocks().inscriber();
this.upgrades = new DefinitionUpgradeInventory( inscriberDefinition, this, this.getUpgradeSlots() );
}
protected int getUpgradeSlots()
{
return 3;
}
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.WORLD_NBT_WRITE )
public void writeToNBT_TileInscriber( NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.inv.writeToNBT( data, "inscriberInv" );
this.upgrades.writeToNBT( data, "upgrades" );
this.settings.writeToNBT( data );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.WORLD_NBT_READ )
public void readFromNBT_TileInscriber( NBTTagCompound data )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.inv.readFromNBT( data, "inscriberInv" );
this.upgrades.readFromNBT( data, "upgrades" );
this.settings.readFromNBT( data );
2014-09-24 02:26:27 +02:00
}
@TileEvent( TileEventType.NETWORK_READ )
public boolean readFromStream_TileInscriber( ByteBuf data ) throws IOException
2014-09-24 02:26:27 +02:00
{
int slot = data.readByte();
2014-12-29 15:13:47 +01:00
boolean oldSmash = this.smash;
boolean newSmash = ( slot & 64 ) == 64;
2014-09-24 02:26:27 +02:00
if ( oldSmash != newSmash && newSmash )
{
2014-12-29 15:13:47 +01:00
this.smash = true;
this.clientStart = System.currentTimeMillis();
2014-09-24 02:26:27 +02:00
}
for ( int num = 0; num < this.inv.getSizeInventory(); num++ )
2014-09-24 02:26:27 +02:00
{
if ( ( slot & ( 1 << num ) ) > 0 )
2014-12-29 15:13:47 +01:00
this.inv.setInventorySlotContents( num, AEItemStack.loadItemStackFromPacket( data ).getItemStack() );
2014-09-24 02:26:27 +02:00
else
2014-12-29 15:13:47 +01:00
this.inv.setInventorySlotContents( num, null );
2014-09-24 02:26:27 +02:00
}
return false;
}
@TileEvent( TileEventType.NETWORK_WRITE )
public void writeToStream_TileInscriber( ByteBuf data ) throws IOException
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
int slot = this.smash ? 64 : 0;
2014-09-24 02:26:27 +02:00
for ( int num = 0; num < this.inv.getSizeInventory(); num++ )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
if ( this.inv.getStackInSlot( num ) != null )
slot |= ( 1 << num );
2014-09-24 02:26:27 +02:00
}
data.writeByte( slot );
for ( int num = 0; num < this.inv.getSizeInventory(); num++ )
2014-09-24 02:26:27 +02:00
{
if ( ( slot & ( 1 << num ) ) > 0 )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
AEItemStack st = AEItemStack.create( this.inv.getStackInSlot( num ) );
2014-09-24 02:26:27 +02:00
st.writeToPacket( data );
}
}
}
@Override
public void setOrientation( ForgeDirection inForward, ForgeDirection inUp )
2014-09-24 02:26:27 +02:00
{
super.setOrientation( inForward, inUp );
2014-12-29 15:13:47 +01:00
this.gridProxy.setValidSides( EnumSet.complementOf( EnumSet.of( this.getForward() ) ) );
this.setPowerSides( EnumSet.complementOf( EnumSet.of( this.getForward() ) ) );
2014-09-24 02:26:27 +02:00
}
@Override
public void getDrops( World w, int x, int y, int z, ArrayList<ItemStack> drops )
2014-09-24 02:26:27 +02:00
{
super.getDrops( w, x, y, z, drops );
for ( int h = 0; h < this.upgrades.getSizeInventory(); h++ )
{
ItemStack is = this.upgrades.getStackInSlot( h );
if ( is != null )
drops.add( is );
}
2014-09-24 02:26:27 +02:00
}
@Override
public boolean requiresTESR()
2014-09-24 02:26:27 +02:00
{
return true;
2014-09-24 02:26:27 +02:00
}
@Override
public int getInventoryStackLimit()
{
return 1;
}
@Override
public boolean isItemValidForSlot( int i, ItemStack itemstack )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
if ( this.smash )
2014-09-24 02:26:27 +02:00
return false;
if ( i == 0 || i == 1 )
{
if ( AEApi.instance().definitions().materials().namePress().isSameAs( itemstack ) )
{
2014-09-24 02:26:27 +02:00
return true;
}
2014-09-24 02:26:27 +02:00
for ( ItemStack s : Inscribe.PLATES )
2014-09-24 02:26:27 +02:00
if ( Platform.isSameItemPrecise( s, itemstack ) )
return true;
}
return i == 2;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean canExtractItem(int slotIndex, ItemStack extractedItem, int side )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
if ( this.smash )
2014-09-24 02:26:27 +02:00
return false;
return slotIndex == 0 || slotIndex == 1 || slotIndex == 3;
2014-09-24 02:26:27 +02:00
}
@Override
public IInventory getInternalInventory()
{
return this.inv;
}
@Override
public void onChangeInventory( IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added )
2014-09-24 02:26:27 +02:00
{
try
{
if ( mc != InvOperation.markDirty )
{
if ( slot != 3 )
2014-12-29 15:13:47 +01:00
this.processingTime = 0;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( !this.smash )
this.markForUpdate();
2014-09-24 02:26:27 +02:00
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
{
// :P
}
}
@Override
public int[] getAccessibleSlotsBySide( ForgeDirection d )
{
if ( d == ForgeDirection.UP )
return this.top;
if ( d == ForgeDirection.DOWN )
return this.bottom;
return this.sides;
}
@Override
public TickingRequest getTickingRequest( IGridNode node )
{
return new TickingRequest( TickRates.Inscriber.min, TickRates.Inscriber.max, !this.hasWork(), false );
}
private boolean hasWork()
{
if ( this.getTask() != null )
return true;
this.processingTime = 0;
return this.smash;
}
2014-09-24 02:26:27 +02:00
public InscriberRecipe getTask()
{
ItemStack plateA = this.getStackInSlot( 0 );
ItemStack plateB = this.getStackInSlot( 1 );
2014-12-29 15:13:47 +01:00
ItemStack renamedItem = this.getStackInSlot( 2 );
2014-09-24 02:26:27 +02:00
if ( plateA != null && plateA.stackSize > 1 )
2014-09-24 02:26:27 +02:00
return null;
if ( plateB != null && plateB.stackSize > 1 )
2014-09-24 02:26:27 +02:00
return null;
if ( renamedItem != null && renamedItem.stackSize > 1 )
return null;
final IComparableDefinition namePress = AEApi.instance().definitions().materials().namePress();
boolean isNameA = namePress.isSameAs( plateA );
boolean isNameB = namePress.isSameAs( plateB );
2014-09-24 02:26:27 +02:00
if ( ( isNameA || isNameB ) && ( isNameA || plateA == null ) && ( isNameB || plateB == null ) )
2014-09-24 02:26:27 +02:00
{
if ( renamedItem != null )
{
String name = "";
if ( plateA != null )
2014-09-24 02:26:27 +02:00
{
NBTTagCompound tag = Platform.openNbtData( plateA );
2014-09-24 02:26:27 +02:00
name += tag.getString( "InscribeName" );
}
if ( plateB != null )
2014-09-24 02:26:27 +02:00
{
NBTTagCompound tag = Platform.openNbtData( plateB );
2014-09-24 02:26:27 +02:00
if ( name.length() > 0 )
name += " ";
name += tag.getString( "InscribeName" );
}
ItemStack startingItem = renamedItem.copy();
renamedItem = renamedItem.copy();
NBTTagCompound tag = Platform.openNbtData( renamedItem );
NBTTagCompound display = tag.getCompoundTag( "display" );
tag.setTag( "display", display );
if ( name.length() > 0 )
display.setString( "Name", name );
else
display.removeTag( "Name" );
return new InscriberRecipe( new ItemStack[] { startingItem }, plateA, plateB, renamedItem, false );
2014-09-24 02:26:27 +02:00
}
}
for ( InscriberRecipe i : Inscribe.RECIPES )
2014-09-24 02:26:27 +02:00
{
boolean matchA = ( plateA == null && i.plateA == null ) || ( Platform.isSameItemPrecise( plateA, i.plateA ) ) && // and...
( plateB == null && i.plateB == null ) | ( Platform.isSameItemPrecise( plateB, i.plateB ) );
2014-09-24 02:26:27 +02:00
boolean matchB = ( plateB == null && i.plateA == null ) || ( Platform.isSameItemPrecise( plateB, i.plateA ) ) && // and...
( plateA == null && i.plateB == null ) | ( Platform.isSameItemPrecise( plateA, i.plateB ) );
2014-09-24 02:26:27 +02:00
if ( matchA || matchB )
{
for ( ItemStack option : i.imprintable )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
if ( Platform.isSameItemPrecise( option, this.getStackInSlot( 2 ) ) )
2014-09-24 02:26:27 +02:00
return i;
}
}
}
return null;
}
@Override
public TickRateModulation tickingRequest( IGridNode node, int TicksSinceLastCall )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
if ( this.smash )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.finalStep++;
if ( this.finalStep == 8 )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
InscriberRecipe out = this.getTask();
2014-09-24 02:26:27 +02:00
if ( out != null )
{
ItemStack is = out.output.copy();
2014-12-29 15:13:47 +01:00
InventoryAdaptor ad = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( this.inv, 3, 1, true ), ForgeDirection.UNKNOWN );
2014-09-24 02:26:27 +02:00
if ( ad.addItems( is ) == null )
{
2014-12-29 15:13:47 +01:00
this.processingTime = 0;
2014-09-24 02:26:27 +02:00
if ( out.usePlates )
{
2014-12-29 15:13:47 +01:00
this.setInventorySlotContents( 0, null );
this.setInventorySlotContents( 1, null );
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
this.setInventorySlotContents( 2, null );
2014-09-24 02:26:27 +02:00
}
}
2014-12-29 15:13:47 +01:00
this.markDirty();
2014-09-24 02:26:27 +02:00
}
2014-12-29 15:13:47 +01:00
else if ( this.finalStep == 16 )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.finalStep = 0;
this.smash = false;
this.markForUpdate();
2014-09-24 02:26:27 +02:00
}
}
else
{
IEnergyGrid eg;
try
{
2014-12-29 15:13:47 +01:00
eg = this.gridProxy.getEnergy();
2014-09-24 02:26:27 +02:00
IEnergySource src = this;
// Base 1, increase by 1 for each card
2014-12-29 15:13:47 +01:00
int speedFactor = 1 + this.upgrades.getInstalledUpgrades( Upgrades.SPEED );
int powerConsumption = 10 * speedFactor;
double powerThreshold = powerConsumption - 0.01;
2014-12-29 15:13:47 +01:00
double powerReq = this.extractAEPower( powerConsumption, Actionable.SIMULATE, PowerMultiplier.CONFIG );
2014-09-24 02:26:27 +02:00
if ( powerReq <= powerThreshold )
2014-09-24 02:26:27 +02:00
{
src = eg;
powerReq = eg.extractAEPower( powerConsumption, Actionable.SIMULATE, PowerMultiplier.CONFIG );
2014-09-24 02:26:27 +02:00
}
if ( powerReq > powerThreshold )
2014-09-24 02:26:27 +02:00
{
src.extractAEPower( powerConsumption, Actionable.MODULATE, PowerMultiplier.CONFIG );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( this.processingTime == 0 )
this.processingTime += speedFactor;
2014-09-24 02:26:27 +02:00
else
2014-12-29 15:13:47 +01:00
this.processingTime += TicksSinceLastCall * speedFactor;
2014-09-24 02:26:27 +02:00
}
}
catch ( GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// :P
}
2014-12-29 15:13:47 +01:00
if ( this.processingTime > this.maxProcessingTime )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.processingTime = this.maxProcessingTime;
InscriberRecipe out = this.getTask();
2014-09-24 02:26:27 +02:00
if ( out != null )
{
ItemStack is = out.output.copy();
2014-12-29 15:13:47 +01:00
InventoryAdaptor ad = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( this.inv, 3, 1, true ), ForgeDirection.UNKNOWN );
2014-09-24 02:26:27 +02:00
if ( ad.simulateAdd( is ) == null )
{
2014-12-29 15:13:47 +01:00
this.smash = true;
this.finalStep = 0;
this.markForUpdate();
2014-09-24 02:26:27 +02:00
}
}
}
}
2014-12-29 15:13:47 +01:00
return this.hasWork() ? TickRateModulation.URGENT : TickRateModulation.SLEEP;
2014-09-24 02:26:27 +02:00
}
@Override
public IConfigManager getConfigManager()
{
2014-12-29 15:13:47 +01:00
return this.settings;
}
@Override
public IInventory getInventoryByName( String name )
{
if ( name.equals( "inv" ) )
2014-12-29 15:13:47 +01:00
return this.inv;
if ( name.equals( "upgrades" ) )
2014-12-29 15:13:47 +01:00
return this.upgrades;
return null;
}
@Override
public int getInstalledUpgrades( Upgrades u )
{
2014-12-29 15:13:47 +01:00
return this.upgrades.getInstalledUpgrades( u );
}
@Override
public void updateSetting( IConfigManager manager, Enum settingName, Enum newValue )
{
}
2014-09-24 02:26:27 +02:00
}