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

489 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;
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.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.me.GridAccessException;
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
{
final int[] top = new int[] { 0 };
final int[] bottom = new int[] { 1 };
final int[] sides = new int[] { 2, 3 };
2014-09-24 02:26:27 +02:00
final AppEngInternalInventory inv = new AppEngInternalInventory( this, 4 );
2014-09-24 02:26:27 +02:00
public final int maxProcessingTime = 100;
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;
2015-01-01 22:13:10 +01:00
static final ItemStack STACK_INSCRIBER = AEApi.instance().blocks().blockInscriber.stack( 1 );
private final IConfigManager settings = new ConfigManager( this );
2015-01-01 22:13:10 +01:00
private final UpgradeInventory upgrades = new UpgradeInventory( STACK_INSCRIBER, this, this.getUpgradeSlots() );
2014-09-24 02:26:27 +02:00
@Override
public AECableType getCableConnectionType(ForgeDirection dir)
{
return AECableType.COVERED;
}
@TileEvent(TileEventType.WORLD_NBT_WRITE)
public void writeToNBT_TileInscriber(NBTTagCompound data)
{
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-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
{
int slot = data.readByte();
2014-12-29 15:13:47 +01:00
boolean oldSmash = this.smash;
2014-09-24 02:26:27 +02:00
boolean newSmash = (slot & 64) == 64;
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
}
2014-12-29 15:13:47 +01: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-12-29 15:13:47 +01:00
int slot = this.smash ? 64 : 0;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01: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 );
2014-12-29 15:13:47 +01: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
AEItemStack st = AEItemStack.create( this.inv.getStackInSlot( num ) );
2014-09-24 02:26:27 +02:00
st.writeToPacket( data );
}
}
}
@Override
public boolean requiresTESR()
{
return true;
}
public TileInscriber()
{
2014-12-29 15:13:47 +01:00
this.gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
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.complementOf( EnumSet.of( this.getForward() ) ) );
this.setPowerSides( EnumSet.complementOf( EnumSet.of( this.getForward() ) ) );
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[] getAccessibleSlotsBySide(ForgeDirection d)
{
if ( d == ForgeDirection.UP )
2014-12-29 15:13:47 +01:00
return this.top;
2014-09-24 02:26:27 +02:00
if ( d == ForgeDirection.DOWN )
2014-12-29 15:13:47 +01:00
return this.bottom;
2014-09-24 02:26:27 +02:00
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)
{
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().materials().materialNamePress.sameAsStack( itemstack ) )
return true;
2015-01-01 22:13:10 +01:00
for (ItemStack s : Inscribe.PLATES )
2014-09-24 02:26:27 +02:00
if ( Platform.isSameItemPrecise( s, itemstack ) )
return true;
}
if ( i == 2 )
{
return true;
// for (ItemStack s : Inscribe.inputs)
// if ( Platform.isSameItemPrecise( s, itemstack ) )
// return true;
}
return false;
}
@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 void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
{
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)
{
// :P
}
}
public InscriberRecipe getTask()
{
2014-12-29 15:13:47 +01:00
ItemStack PlateA = this.getStackInSlot( 0 );
ItemStack PlateB = this.getStackInSlot( 1 );
ItemStack renamedItem = this.getStackInSlot( 2 );
2014-09-24 02:26:27 +02:00
if ( PlateA != null && PlateA.stackSize > 1 )
return null;
if ( PlateB != null && PlateB.stackSize > 1 )
return null;
if ( renamedItem != null && renamedItem.stackSize > 1 )
return null;
boolean isNameA = AEApi.instance().materials().materialNamePress.sameAsStack( PlateA );
boolean isNameB = AEApi.instance().materials().materialNamePress.sameAsStack( PlateB );
if ( (isNameA || isNameB) && (isNameA || PlateA == null) && (isNameB || PlateB == null) )
{
if ( renamedItem != null )
{
String name = "";
if ( PlateA != null )
{
NBTTagCompound tag = Platform.openNbtData( PlateA );
name += tag.getString( "InscribeName" );
}
if ( PlateB != null )
{
NBTTagCompound tag = Platform.openNbtData( PlateB );
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 );
}
}
2015-01-01 22:13:10 +01: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 ));
boolean matchB = (PlateB == null && i.plateA == null) || (Platform.isSameItemPrecise( PlateB, i.plateA )) && // and...
(PlateA == null && i.plateB == null) | (Platform.isSameItemPrecise( PlateA, i.plateB ));
if ( matchA || matchB )
{
for (ItemStack option : i.imprintable)
{
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;
}
private boolean hasWork()
{
2014-12-29 15:13:47 +01:00
if ( this.getTask() != null )
2014-09-24 02:26:27 +02:00
return true;
2014-12-29 15:13:47 +01:00
this.processingTime = 0;
return this.smash;
2014-09-24 02:26:27 +02:00
}
@Override
public TickingRequest getTickingRequest(IGridNode node)
{
2014-12-29 15:13:47 +01:00
return new TickingRequest( TickRates.Inscriber.min, TickRates.Inscriber.max, !this.hasWork(), false );
2014-09-24 02:26:27 +02:00
}
@Override
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
{
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)
{
// :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 );
}
protected int getUpgradeSlots()
{
return 3;
}
@Override
public void getDrops(World w, int x, int y, int z, ArrayList<ItemStack> drops)
{
super.getDrops( w, x, y, z, drops );
2014-12-29 15:13:47 +01:00
for (int h = 0; h < this.upgrades.getSizeInventory(); h++)
{
2014-12-29 15:13:47 +01:00
ItemStack is = this.upgrades.getStackInSlot( h );
if ( is != null )
drops.add( is );
}
}
@Override
public void updateSetting(IConfigManager manager, Enum settingName, Enum newValue)
{
}
2014-09-24 02:26:27 +02:00
}