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

578 lines
15 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 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.features.IInscriberRecipe;
import appeng.api.features.InscriberProcessType;
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;
import appeng.core.features.registries.entries.InscriberRecipe;
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.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;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.EnumSet;
import java.util.List;
2014-09-24 02:26:27 +02:00
/**
* @author AlgorithmX2
* @author thatsIch
* @version rv2
* @since rv0
*/
public class TileInscriber extends AENetworkPowerTile implements IGridTickable, IUpgradeableHost, IConfigManagerHost
2014-09-24 02:26:27 +02:00
{
private final int maxProcessingTime = 100;
private final int[] top = { 0 };
private final int[] bottom = { 1 };
private final int[] sides = { 2, 3 };
private final AppEngInternalInventory inv = new AppEngInternalInventory( this, 4 );
private final IConfigManager settings;
private final UpgradeInventory upgrades;
private int processingTime = 0;
2014-09-24 02:26:27 +02:00
// cycles from 0 - 16, at 8 it preforms the action, at 16 it re-enables the normal routine.
private boolean smash;
private int finalStep;
private long clientStart;
2014-09-24 02:26:27 +02:00
@Reflected
public TileInscriber()
{
this.getProxy().setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
this.setInternalMaxPower( 1500 );
this.getProxy().setIdlePowerUsage( 0 );
this.settings = new ConfigManager( this );
final ITileDefinition inscriberDefinition = AEApi.instance().definitions().blocks().inscriber();
this.upgrades = new DefinitionUpgradeInventory( inscriberDefinition, this, this.getUpgradeSlots() );
}
private int getUpgradeSlots()
{
return 3;
}
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;
}
@TileEvent( TileEventType.WORLD_NBT_WRITE )
public void writeToNBT_TileInscriber( final 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( final 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( final ByteBuf data ) throws IOException
2014-09-24 02:26:27 +02:00
{
final int slot = data.readByte();
2014-09-24 02:26:27 +02:00
final boolean oldSmash = this.isSmash();
final boolean newSmash = ( slot & 64 ) == 64;
2014-09-24 02:26:27 +02:00
if( oldSmash != newSmash && newSmash )
2014-09-24 02:26:27 +02:00
{
this.setSmash( true );
this.setClientStart( 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 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.inv.setInventorySlotContents( num, AEItemStack.loadItemStackFromPacket( data ).getItemStack() );
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.inv.setInventorySlotContents( num, null );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
return false;
}
@TileEvent( TileEventType.NETWORK_WRITE )
public void writeToStream_TileInscriber( final ByteBuf data ) throws IOException
2014-09-24 02:26:27 +02:00
{
int slot = this.isSmash() ? 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
{
if( this.inv.getStackInSlot( num ) != null )
2015-04-29 02:30:53 +02:00
{
slot |= ( 1 << num );
2015-04-29 02:30:53 +02:00
}
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
{
final AEItemStack st = AEItemStack.create( this.inv.getStackInSlot( num ) );
2014-09-24 02:26:27 +02:00
st.writeToPacket( data );
}
}
}
@Override
public void setOrientation( final ForgeDirection inForward, final ForgeDirection inUp )
2014-09-24 02:26:27 +02:00
{
super.setOrientation( inForward, inUp );
this.getProxy().setValidSides( EnumSet.complementOf( EnumSet.of( this.getForward() ) ) );
2014-12-29 15:13:47 +01:00
this.setPowerSides( EnumSet.complementOf( EnumSet.of( this.getForward() ) ) );
2014-09-24 02:26:27 +02:00
}
@Override
public void getDrops( final World w, final int x, final int y, final int z, final List<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++ )
{
final ItemStack is = this.upgrades.getStackInSlot( h );
if( is != null )
2015-04-29 02:30:53 +02:00
{
drops.add( is );
2015-04-29 02:30:53 +02:00
}
}
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 IInventory getInternalInventory()
{
return this.inv;
}
2014-09-24 02:26:27 +02:00
@Override
public int getInventoryStackLimit()
{
return 1;
}
@Override
public boolean isItemValidForSlot( final int i, final ItemStack itemstack )
2014-09-24 02:26:27 +02:00
{
if( this.isSmash() )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return false;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( i == 0 || i == 1 )
2014-09-24 02:26:27 +02:00
{
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( final ItemStack optionals : AEApi.instance().registries().inscriber().getOptionals() )
2015-04-29 02:30:53 +02:00
{
if( Platform.isSameItemPrecise( optionals, itemstack ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return true;
2015-04-29 02:30:53 +02:00
}
}
2014-09-24 02:26:27 +02:00
}
return i == 2;
2014-09-24 02:26:27 +02:00
}
@Override
public void onChangeInventory( final IInventory inv, final int slot, final InvOperation mc, final ItemStack removed, final ItemStack added )
2014-09-24 02:26:27 +02:00
{
try
{
if( mc != InvOperation.markDirty )
2014-09-24 02:26:27 +02:00
{
if( slot != 3 )
2015-04-29 02:30:53 +02:00
{
this.setProcessingTime( 0 );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( !this.isSmash() )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.markForUpdate();
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02: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
{
// :P
}
}
@Override
public boolean canExtractItem( final int slotIndex, final ItemStack extractedItem, final int side )
{
if( this.isSmash() )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
return slotIndex == 0 || slotIndex == 1 || slotIndex == 3;
}
@Override
public int[] getAccessibleSlotsBySide( final ForgeDirection d )
{
if( d == ForgeDirection.UP )
2015-04-29 02:30:53 +02:00
{
return this.top;
2015-04-29 02:30:53 +02:00
}
if( d == ForgeDirection.DOWN )
2015-04-29 02:30:53 +02:00
{
return this.bottom;
2015-04-29 02:30:53 +02:00
}
return this.sides;
}
@Override
public TickingRequest getTickingRequest( final IGridNode node )
{
return new TickingRequest( TickRates.Inscriber.getMin(), TickRates.Inscriber.getMax(), !this.hasWork(), false );
}
private boolean hasWork()
{
if( this.getTask() != null )
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
this.setProcessingTime( 0 );
return this.isSmash();
}
@Nullable
public IInscriberRecipe getTask()
2014-09-24 02:26:27 +02:00
{
final ItemStack plateA = this.getStackInSlot( 0 );
final 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 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( plateB != null && plateB.stackSize > 1 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( renamedItem != null && renamedItem.stackSize > 1 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
final IComparableDefinition namePress = AEApi.instance().definitions().materials().namePress();
final boolean isNameA = namePress.isSameAs( plateA );
final 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 )
2014-09-24 02:26:27 +02:00
{
String name = "";
if( plateA != null )
2014-09-24 02:26:27 +02:00
{
final 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
{
final NBTTagCompound tag = Platform.openNbtData( plateB );
if( name.length() > 0 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
name += " ";
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
name += tag.getString( "InscribeName" );
}
final ItemStack startingItem = renamedItem.copy();
2014-09-24 02:26:27 +02:00
renamedItem = renamedItem.copy();
final NBTTagCompound tag = Platform.openNbtData( renamedItem );
2014-09-24 02:26:27 +02:00
final NBTTagCompound display = tag.getCompoundTag( "display" );
2014-09-24 02:26:27 +02:00
tag.setTag( "display", display );
if( name.length() > 0 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
display.setString( "Name", name );
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-09-24 02:26:27 +02:00
display.removeTag( "Name" );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
final List<ItemStack> inputs = Lists.newArrayList( startingItem );
final InscriberProcessType type = InscriberProcessType.Inscribe;
return new InscriberRecipe( inputs, renamedItem, plateA, plateB, type );
2014-09-24 02:26:27 +02:00
}
}
for( final IInscriberRecipe recipe : AEApi.instance().registries().inscriber().getRecipes() )
2014-09-24 02:26:27 +02:00
{
final boolean matchA = ( plateA == null && !recipe.getTopOptional().isPresent() ) || ( Platform.isSameItemPrecise( plateA, recipe.getTopOptional().orNull() ) ) && // and...
( plateB == null && !recipe.getBottomOptional().isPresent() ) | ( Platform.isSameItemPrecise( plateB, recipe.getBottomOptional().orNull() ) );
2014-09-24 02:26:27 +02:00
final boolean matchB = ( plateB == null && !recipe.getTopOptional().isPresent() ) || ( Platform.isSameItemPrecise( plateB, recipe.getTopOptional().orNull() ) ) && // and...
( plateA == null && !recipe.getBottomOptional().isPresent() ) | ( Platform.isSameItemPrecise( plateA, recipe.getBottomOptional().orNull() ) );
2014-09-24 02:26:27 +02:00
if( matchA || matchB )
2014-09-24 02:26:27 +02:00
{
for( final ItemStack option : recipe.getInputs() )
2014-09-24 02:26:27 +02:00
{
if( Platform.isSameItemPrecise( option, this.getStackInSlot( 2 ) ) )
2015-04-29 02:30:53 +02:00
{
return recipe;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
}
return null;
}
@Override
public TickRateModulation tickingRequest( final IGridNode node, final int ticksSinceLastCall )
2014-09-24 02:26:27 +02:00
{
if( this.isSmash() )
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
{
final IInscriberRecipe out = this.getTask();
if( out != null )
2014-09-24 02:26:27 +02:00
{
final ItemStack outputCopy = out.getOutput().copy();
final InventoryAdaptor ad = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( this.inv, 3, 1, true ), ForgeDirection.UNKNOWN );
2014-09-24 02:26:27 +02:00
if( ad.addItems( outputCopy ) == null )
2014-09-24 02:26:27 +02:00
{
this.setProcessingTime( 0 );
if( out.getProcessType() == InscriberProcessType.Press )
2014-09-24 02:26:27 +02:00
{
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
}
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.setSmash( false );
2014-12-29 15:13:47 +01:00
this.markForUpdate();
2014-09-24 02:26:27 +02:00
}
}
else
{
try
{
final IEnergyGrid eg = this.getProxy().getEnergy();
2014-09-24 02:26:27 +02:00
IEnergySource src = this;
// Base 1, increase by 1 for each card
final int speedFactor = 1 + this.upgrades.getInstalledUpgrades( Upgrades.SPEED );
final int powerConsumption = 10 * speedFactor;
final 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
if( this.getProcessingTime() == 0 )
2015-04-29 02:30:53 +02:00
{
this.setProcessingTime( this.getProcessingTime() + speedFactor );
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.setProcessingTime( this.getProcessingTime() + ticksSinceLastCall * speedFactor );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
}
catch( final GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// :P
}
if( this.getProcessingTime() > this.getMaxProcessingTime() )
2014-09-24 02:26:27 +02:00
{
this.setProcessingTime( this.getMaxProcessingTime() );
final IInscriberRecipe out = this.getTask();
if( out != null )
2014-09-24 02:26:27 +02:00
{
final ItemStack outputCopy = out.getOutput().copy();
final InventoryAdaptor ad = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( this.inv, 3, 1, true ), ForgeDirection.UNKNOWN );
if( ad.simulateAdd( outputCopy ) == null )
2014-09-24 02:26:27 +02:00
{
this.setSmash( true );
2014-12-29 15:13:47 +01:00
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( final String name )
{
if( name.equals( "inv" ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
return this.inv;
2015-04-29 02:30:53 +02:00
}
if( name.equals( "upgrades" ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
return this.upgrades;
2015-04-29 02:30:53 +02:00
}
return null;
}
@Override
public int getInstalledUpgrades( final Upgrades u )
{
2014-12-29 15:13:47 +01:00
return this.upgrades.getInstalledUpgrades( u );
}
@Override
public void updateSetting( final IConfigManager manager, final Enum settingName, final Enum newValue )
{
}
public long getClientStart()
{
return this.clientStart;
}
private void setClientStart( final long clientStart )
{
this.clientStart = clientStart;
}
public boolean isSmash()
{
return this.smash;
}
public void setSmash( final boolean smash )
{
this.smash = smash;
}
public int getMaxProcessingTime()
{
return this.maxProcessingTime;
}
public int getProcessingTime()
{
return this.processingTime;
}
private void setProcessingTime( final int processingTime )
{
this.processingTime = processingTime;
}
2014-09-24 02:26:27 +02:00
}