2013-12-27 23:59:59 +01:00
|
|
|
package appeng.tile.misc;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.EnumSet;
|
|
|
|
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-03-07 07:15:54 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-03-13 04:14:55 +01:00
|
|
|
import appeng.api.AEApi;
|
2014-03-12 04:26:50 +01:00
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.config.PowerMultiplier;
|
2014-03-11 07:52:48 +01:00
|
|
|
import appeng.api.networking.IGridNode;
|
2014-03-12 04:26:50 +01:00
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
|
|
|
import appeng.api.networking.energy.IEnergySource;
|
2014-03-11 07:52:48 +01:00
|
|
|
import appeng.api.networking.ticking.IGridTickable;
|
|
|
|
import appeng.api.networking.ticking.TickRateModulation;
|
|
|
|
import appeng.api.networking.ticking.TickingRequest;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.util.AECableType;
|
2014-03-17 20:59:50 +01:00
|
|
|
import appeng.core.settings.TickRates;
|
2014-03-11 07:52:48 +01:00
|
|
|
import appeng.me.GridAccessException;
|
2014-03-09 04:35:53 +01:00
|
|
|
import appeng.recipes.handlers.Inscribe;
|
2014-03-11 07:52:48 +01:00
|
|
|
import appeng.recipes.handlers.Inscribe.InscriberRecipe;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.tile.events.AETileEventHandler;
|
|
|
|
import appeng.tile.events.TileEventType;
|
|
|
|
import appeng.tile.grid.AENetworkPowerTile;
|
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.inventory.InvOperation;
|
2014-03-12 04:26:50 +01:00
|
|
|
import appeng.util.InventoryAdaptor;
|
2014-03-07 07:15:54 +01:00
|
|
|
import appeng.util.Platform;
|
2014-03-12 04:26:50 +01:00
|
|
|
import appeng.util.inv.WrapperInventoryRange;
|
2014-03-07 07:15:54 +01:00
|
|
|
import appeng.util.item.AEItemStack;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-11 07:52:48 +01:00
|
|
|
public class TileInscriber extends AENetworkPowerTile implements IGridTickable
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
2014-03-07 07:15:54 +01:00
|
|
|
final int top[] = new int[] { 0 };
|
|
|
|
final int bottom[] = new int[] { 1 };
|
|
|
|
final int sides[] = new int[] { 2, 3 };
|
|
|
|
|
|
|
|
AppEngInternalInventory inv = new AppEngInternalInventory( this, 4 );
|
2014-03-11 07:52:48 +01:00
|
|
|
|
|
|
|
public final int maxProessingTime = 100;
|
|
|
|
public int processingTime = 0;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
// cycles from 0 - 16, at 8 it preforms the action, at 16 it reenables the normal rotuine.
|
|
|
|
public boolean smash;
|
|
|
|
public int finalStep;
|
|
|
|
|
|
|
|
public long clientStart;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public AECableType getCableConnectionType(ForgeDirection dir)
|
|
|
|
{
|
|
|
|
return AECableType.COVERED;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class TileInscriberHandler extends AETileEventHandler
|
|
|
|
{
|
|
|
|
|
|
|
|
public TileInscriberHandler() {
|
2014-03-30 00:36:37 +01:00
|
|
|
super( TileEventType.WORLD_NBT, TileEventType.NETWORK );
|
2014-03-07 07:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
inv.writeToNBT( data, "inscriberInv" );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
inv.readFromNBT( data, "inscriberInv" );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public boolean readFromStream(ByteBuf data) throws IOException
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-03-07 07:15:54 +01:00
|
|
|
int slot = data.readByte();
|
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
boolean oldSmash = smash;
|
|
|
|
boolean newSmash = (slot & 64) == 64;
|
|
|
|
|
|
|
|
if ( oldSmash != newSmash && newSmash )
|
|
|
|
{
|
|
|
|
smash = true;
|
|
|
|
clientStart = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
2014-03-07 07:15:54 +01:00
|
|
|
for (int num = 0; num < inv.getSizeInventory(); num++)
|
|
|
|
{
|
2014-03-11 07:52:48 +01:00
|
|
|
if ( (slot & (1 << num)) > 0 )
|
2014-03-07 07:15:54 +01:00
|
|
|
inv.setInventorySlotContents( num, AEItemStack.loadItemStackFromPacket( data ).getItemStack() );
|
|
|
|
else
|
|
|
|
inv.setInventorySlotContents( num, null );
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void writeToStream(ByteBuf data) throws IOException
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
int slot = smash ? 64 : 0;
|
2014-03-07 07:15:54 +01:00
|
|
|
|
|
|
|
for (int num = 0; num < inv.getSizeInventory(); num++)
|
|
|
|
{
|
|
|
|
if ( inv.getStackInSlot( num ) != null )
|
|
|
|
slot = slot | (1 << num);
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:52:48 +01:00
|
|
|
data.writeByte( slot );
|
2014-03-07 07:15:54 +01:00
|
|
|
for (int num = 0; num < inv.getSizeInventory(); num++)
|
|
|
|
{
|
2014-03-11 07:52:48 +01:00
|
|
|
if ( (slot & (1 << num)) > 0 )
|
2014-03-07 07:15:54 +01:00
|
|
|
{
|
|
|
|
AEItemStack st = AEItemStack.create( inv.getStackInSlot( num ) );
|
|
|
|
st.writeToPacket( data );
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
@Override
|
|
|
|
public boolean requiresTESR()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public TileInscriber() {
|
|
|
|
gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) );
|
|
|
|
internalMaxPower = 1500;
|
|
|
|
gridProxy.setIdlePowerUsage( 0 );
|
|
|
|
addNewHandler( new TileInscriberHandler() );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setOrientation(ForgeDirection inForward, ForgeDirection inUp)
|
|
|
|
{
|
|
|
|
super.setOrientation( inForward, inUp );
|
2014-03-13 04:14:55 +01:00
|
|
|
gridProxy.setValidSides( EnumSet.complementOf( EnumSet.of( getForward() ) ) );
|
|
|
|
setPowerSides( EnumSet.complementOf( EnumSet.of( getForward() ) ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IInventory getInternalInventory()
|
|
|
|
{
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-14 03:59:37 +01:00
|
|
|
public int[] getAccessibleSlotsBySide(ForgeDirection d)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-03-07 07:15:54 +01:00
|
|
|
if ( d == ForgeDirection.UP )
|
|
|
|
return top;
|
|
|
|
|
|
|
|
if ( d == ForgeDirection.DOWN )
|
|
|
|
return bottom;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
return sides;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
if ( smash )
|
|
|
|
return false;
|
|
|
|
|
2014-03-07 07:15:54 +01:00
|
|
|
if ( i == 0 || i == 1 )
|
|
|
|
{
|
2014-05-10 07:00:02 +02:00
|
|
|
if ( AEApi.instance().materials().materialNamePress.sameAsStack( itemstack ) )
|
2014-03-13 04:14:55 +01:00
|
|
|
return true;
|
|
|
|
|
2014-03-09 04:35:53 +01:00
|
|
|
for (ItemStack s : Inscribe.plates)
|
2014-03-07 07:15:54 +01:00
|
|
|
if ( Platform.isSameItemPrecise( s, itemstack ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( i == 2 )
|
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
return true;
|
|
|
|
// for (ItemStack s : Inscribe.inputs)
|
|
|
|
// if ( Platform.isSameItemPrecise( s, itemstack ) )
|
|
|
|
// return true;
|
2014-03-07 07:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int i, ItemStack itemstack, int j)
|
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
if ( smash )
|
|
|
|
return false;
|
|
|
|
|
2014-03-07 07:15:54 +01:00
|
|
|
return i == 0 || i == 1 || i == 3;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
|
|
|
|
{
|
2014-03-11 07:52:48 +01:00
|
|
|
try
|
|
|
|
{
|
2014-03-12 04:26:50 +01:00
|
|
|
if ( mc != InvOperation.markDirty )
|
|
|
|
{
|
|
|
|
if ( slot != 3 )
|
|
|
|
processingTime = 0;
|
2014-03-13 04:14:55 +01:00
|
|
|
|
|
|
|
if ( !smash )
|
|
|
|
markForUpdate();
|
|
|
|
|
2014-03-12 04:26:50 +01:00
|
|
|
gridProxy.getTick().wakeDevice( gridProxy.getNode() );
|
|
|
|
}
|
2014-03-11 07:52:48 +01:00
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
public InscriberRecipe getTask()
|
2014-03-11 07:52:48 +01:00
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
ItemStack PlateA = getStackInSlot( 0 );
|
|
|
|
ItemStack PlateB = getStackInSlot( 1 );
|
2014-03-30 00:36:37 +01:00
|
|
|
ItemStack renamedItem = getStackInSlot( 2 );
|
|
|
|
|
|
|
|
if ( PlateA != null && PlateA.stackSize > 1 )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if ( PlateB != null && PlateB.stackSize > 1 )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if ( renamedItem != null && renamedItem.stackSize > 1 )
|
|
|
|
return null;
|
2014-03-13 04:14:55 +01:00
|
|
|
|
2014-05-10 07:00:02 +02:00
|
|
|
boolean isNameA = AEApi.instance().materials().materialNamePress.sameAsStack( PlateA );
|
|
|
|
boolean isNameB = AEApi.instance().materials().materialNamePress.sameAsStack( PlateB );
|
2014-03-13 04:14:55 +01:00
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 07:52:48 +01:00
|
|
|
|
|
|
|
for (InscriberRecipe i : Inscribe.recipes)
|
|
|
|
{
|
|
|
|
|
|
|
|
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 opion : i.imprintable)
|
|
|
|
{
|
|
|
|
if ( Platform.isSameItemPrecise( opion, getStackInSlot( 2 ) ) )
|
2014-03-12 04:26:50 +01:00
|
|
|
return i;
|
2014-03-11 07:52:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean hasWork()
|
|
|
|
{
|
|
|
|
if ( getTask() != null )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
processingTime = 0;
|
2014-03-13 04:14:55 +01:00
|
|
|
return false || smash;
|
2014-03-11 07:52:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TickingRequest getTickingRequest(IGridNode node)
|
|
|
|
{
|
2014-03-17 20:59:50 +01:00
|
|
|
return new TickingRequest( TickRates.Inscriber.min, TickRates.Inscriber.max, !hasWork(), false );
|
2014-03-11 07:52:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
|
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
if ( smash )
|
2014-03-12 04:26:50 +01:00
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
finalStep++;
|
|
|
|
if ( finalStep == 8 )
|
|
|
|
{
|
2014-03-11 07:52:48 +01:00
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
InscriberRecipe out = getTask();
|
|
|
|
if ( out != null )
|
|
|
|
{
|
|
|
|
ItemStack is = out.output.copy();
|
|
|
|
InventoryAdaptor ad = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( inv, 3, 1, true ), ForgeDirection.UNKNOWN );
|
2014-03-12 04:26:50 +01:00
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
if ( ad.addItems( is ) == null )
|
|
|
|
{
|
|
|
|
processingTime = 0;
|
|
|
|
if ( out.usePlates )
|
|
|
|
{
|
|
|
|
setInventorySlotContents( 0, null );
|
|
|
|
setInventorySlotContents( 1, null );
|
|
|
|
}
|
|
|
|
setInventorySlotContents( 2, null );
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 04:26:50 +01:00
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
markDirty();
|
2014-03-12 04:26:50 +01:00
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
}
|
|
|
|
else if ( finalStep == 16 )
|
|
|
|
{
|
|
|
|
finalStep = 0;
|
|
|
|
smash = false;
|
|
|
|
markForUpdate();
|
2014-03-12 04:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-13 04:14:55 +01:00
|
|
|
else
|
2014-03-11 07:52:48 +01:00
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
IEnergyGrid eg;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
eg = gridProxy.getEnergy();
|
|
|
|
IEnergySource src = this;
|
2014-03-11 07:52:48 +01:00
|
|
|
|
2014-03-13 04:14:55 +01:00
|
|
|
double powerReq = extractAEPower( 10, Actionable.SIMULATE, PowerMultiplier.CONFIG );
|
|
|
|
|
|
|
|
if ( powerReq < 9.99 )
|
|
|
|
{
|
|
|
|
src = eg;
|
|
|
|
powerReq = eg.extractAEPower( 10, Actionable.SIMULATE, PowerMultiplier.CONFIG );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( powerReq > 9.99 )
|
|
|
|
{
|
|
|
|
src.extractAEPower( 10, Actionable.MODULATE, PowerMultiplier.CONFIG );
|
|
|
|
|
|
|
|
if ( processingTime == 0 )
|
|
|
|
processingTime++;
|
|
|
|
else
|
|
|
|
processingTime += TicksSinceLastCall;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
2014-03-11 07:52:48 +01:00
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
// :P
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( processingTime > maxProessingTime )
|
|
|
|
{
|
|
|
|
processingTime = maxProessingTime;
|
|
|
|
InscriberRecipe out = getTask();
|
|
|
|
if ( out != null )
|
2014-03-12 04:26:50 +01:00
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
ItemStack is = out.output.copy();
|
|
|
|
InventoryAdaptor ad = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( inv, 3, 1, true ), ForgeDirection.UNKNOWN );
|
|
|
|
if ( ad.simulateAdd( is ) == null )
|
2014-03-12 04:26:50 +01:00
|
|
|
{
|
2014-03-13 04:14:55 +01:00
|
|
|
smash = true;
|
|
|
|
finalStep = 0;
|
|
|
|
markForUpdate();
|
2014-03-12 04:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-11 07:52:48 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-03-11 07:52:48 +01:00
|
|
|
return hasWork() ? TickRateModulation.URGENT : TickRateModulation.SLEEP;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|