2014-04-08 02:43:57 +02:00
|
|
|
package appeng.tile.crafting;
|
|
|
|
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.inventory.ISidedInventory;
|
2014-05-08 07:58:43 +02:00
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
2014-04-08 02:43:57 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-05-08 07:58:43 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.world.World;
|
2014-04-08 02:43:57 +02:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-04-24 08:14:26 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-05-09 06:09:42 +02:00
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.config.PowerMultiplier;
|
2014-04-24 08:14:26 +02:00
|
|
|
import appeng.api.config.RedstoneMode;
|
|
|
|
import appeng.api.config.Settings;
|
|
|
|
import appeng.api.config.Upgrades;
|
2014-05-09 06:09:42 +02:00
|
|
|
import appeng.api.crafting.ICraftingMedium;
|
2014-05-08 07:58:43 +02:00
|
|
|
import appeng.api.crafting.ICraftingPatternDetails;
|
2014-04-24 08:14:26 +02:00
|
|
|
import appeng.api.implementations.IUpgradeableHost;
|
2014-05-08 07:58:43 +02:00
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.ticking.IGridTickable;
|
|
|
|
import appeng.api.networking.ticking.TickRateModulation;
|
|
|
|
import appeng.api.networking.ticking.TickingRequest;
|
2014-04-08 02:43:57 +02:00
|
|
|
import appeng.api.util.AECableType;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
2014-04-24 08:14:26 +02:00
|
|
|
import appeng.api.util.IConfigManager;
|
2014-05-08 07:58:43 +02:00
|
|
|
import appeng.container.ContainerNull;
|
|
|
|
import appeng.items.misc.ItemEncodedPattern;
|
|
|
|
import appeng.me.GridAccessException;
|
2014-04-24 08:14:26 +02:00
|
|
|
import appeng.parts.automation.UpgradeInventory;
|
2014-04-08 02:43:57 +02:00
|
|
|
import appeng.tile.events.AETileEventHandler;
|
|
|
|
import appeng.tile.events.TileEventType;
|
|
|
|
import appeng.tile.grid.AENetworkInvTile;
|
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.inventory.IAEAppEngInventory;
|
|
|
|
import appeng.tile.inventory.InvOperation;
|
2014-04-24 08:14:26 +02:00
|
|
|
import appeng.util.ConfigManager;
|
|
|
|
import appeng.util.IConfigManagerHost;
|
2014-05-08 07:58:43 +02:00
|
|
|
import appeng.util.InventoryAdaptor;
|
|
|
|
import appeng.util.Platform;
|
2014-04-08 02:43:57 +02:00
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEngInventory, ISidedInventory, IUpgradeableHost, IConfigManagerHost,
|
2014-05-09 06:09:42 +02:00
|
|
|
IGridTickable, ICraftingMedium
|
2014-04-08 02:43:57 +02:00
|
|
|
{
|
|
|
|
|
2014-04-24 08:14:26 +02:00
|
|
|
static final int[] sides = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
static final ItemStack is = AEApi.instance().blocks().blockMolecularAssembler.stack( 1 );
|
2014-04-08 02:43:57 +02:00
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
private InventoryCrafting craftingInv = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
2014-04-24 08:14:26 +02:00
|
|
|
private AppEngInternalInventory inv = new AppEngInternalInventory( this, 9 + 2 );
|
|
|
|
private IConfigManager settings = new ConfigManager( this );
|
|
|
|
private UpgradeInventory upgrades = new UpgradeInventory( is, this, getUpgradeSlots() );
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
private ForgeDirection pushDirection = ForgeDirection.UNKNOWN;
|
|
|
|
private ItemStack myPattern = null;
|
|
|
|
private ICraftingPatternDetails myPlan = null;
|
2014-05-09 06:09:42 +02:00
|
|
|
private double progress = 0;
|
2014-05-08 07:58:43 +02:00
|
|
|
private boolean isAwake = false;
|
|
|
|
|
2014-05-09 06:09:42 +02:00
|
|
|
@Override
|
|
|
|
public boolean pushPattern(ICraftingPatternDetails patternDetails, InventoryCrafting table, ForgeDirection where)
|
2014-05-08 07:58:43 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recalculatePlan()
|
|
|
|
{
|
|
|
|
ItemStack is = inv.getStackInSlot( 10 );
|
|
|
|
|
|
|
|
boolean wasEnabled = isAwake;
|
|
|
|
|
|
|
|
if ( is != null && is.getItem() instanceof ItemEncodedPattern )
|
|
|
|
{
|
|
|
|
if ( !Platform.isSameItem( is, myPattern ) )
|
|
|
|
{
|
|
|
|
World w = getWorldObj();
|
|
|
|
ItemEncodedPattern iep = (ItemEncodedPattern) is.getItem();
|
|
|
|
ICraftingPatternDetails ph = iep.getPatternForItem( is, w );
|
|
|
|
|
|
|
|
progress = 0;
|
|
|
|
myPattern = is;
|
|
|
|
myPlan = ph;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
progress = 0;
|
|
|
|
myPlan = null;
|
|
|
|
myPattern = null;
|
|
|
|
}
|
|
|
|
|
2014-05-09 06:09:42 +02:00
|
|
|
isAwake = myPlan != null && hasMats() || canPush();
|
2014-05-08 07:58:43 +02:00
|
|
|
if ( wasEnabled != isAwake )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( isAwake )
|
|
|
|
gridProxy.getTick().wakeDevice( gridProxy.getNode() );
|
|
|
|
else
|
|
|
|
gridProxy.getTick().sleepDevice( gridProxy.getNode() );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// :P
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 06:09:42 +02:00
|
|
|
private boolean canPush()
|
|
|
|
{
|
|
|
|
return inv.getStackInSlot( 9 ) != null;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:14:26 +02:00
|
|
|
@Override
|
|
|
|
public int getInstalledUpgrades(Upgrades u)
|
|
|
|
{
|
|
|
|
return upgrades.getInstalledUpgrades( u );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getUpgradeSlots()
|
|
|
|
{
|
|
|
|
return 5;
|
|
|
|
}
|
2014-04-08 02:43:57 +02:00
|
|
|
|
|
|
|
private class TileMolecularAssemblerHandler extends AETileEventHandler
|
|
|
|
{
|
|
|
|
|
|
|
|
public TileMolecularAssemblerHandler() {
|
|
|
|
super( TileEventType.WORLD_NBT );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound data)
|
|
|
|
{
|
2014-04-24 08:14:26 +02:00
|
|
|
upgrades.writeToNBT( data, "upgrades" );
|
|
|
|
inv.writeToNBT( data, "inv" );
|
|
|
|
settings.writeToNBT( data );
|
2014-04-08 02:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound data)
|
|
|
|
{
|
2014-04-24 08:14:26 +02:00
|
|
|
upgrades.readFromNBT( data, "upgrades" );
|
|
|
|
inv.readFromNBT( data, "inv" );
|
|
|
|
settings.readFromNBT( data );
|
2014-05-08 07:58:43 +02:00
|
|
|
recalculatePlan();
|
2014-04-08 02:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public TileMolecularAssembler() {
|
2014-04-24 08:14:26 +02:00
|
|
|
settings.registerSetting( Settings.REDSTONE_CONTROLLED, RedstoneMode.IGNORE );
|
2014-04-25 03:55:29 +02:00
|
|
|
inv.setMaxStackSize( 1 );
|
2014-04-08 02:43:57 +02:00
|
|
|
addNewHandler( new TileMolecularAssemblerHandler() );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack itemstack)
|
|
|
|
{
|
|
|
|
if ( i >= 9 )
|
|
|
|
return false;
|
|
|
|
|
2014-04-24 08:14:26 +02:00
|
|
|
if ( hasPattern() )
|
2014-05-08 07:58:43 +02:00
|
|
|
return myPlan.isValidItemForSlot( i, itemstack, getWorldObj() );
|
2014-04-08 02:43:57 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:14:26 +02:00
|
|
|
private boolean hasPattern()
|
|
|
|
{
|
2014-05-08 07:58:43 +02:00
|
|
|
return myPlan != null && inv.getStackInSlot( 10 ) != null;
|
2014-04-24 08:14:26 +02:00
|
|
|
}
|
|
|
|
|
2014-04-08 02:43:57 +02:00
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int i, ItemStack itemstack, int j)
|
|
|
|
{
|
|
|
|
return i == 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IInventory getInternalInventory()
|
|
|
|
{
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added)
|
|
|
|
{
|
2014-05-08 07:58:43 +02:00
|
|
|
if ( inv == this.inv )
|
|
|
|
recalculatePlan();
|
2014-04-08 02:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getAccessibleSlotsBySide(ForgeDirection whichSide)
|
|
|
|
{
|
|
|
|
return sides;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AECableType getCableConnectionType(ForgeDirection dir)
|
|
|
|
{
|
|
|
|
return AECableType.COVERED;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DimensionalCoord getLocation()
|
|
|
|
{
|
|
|
|
return new DimensionalCoord( this );
|
|
|
|
}
|
|
|
|
|
2014-04-24 08:14:26 +02:00
|
|
|
@Override
|
|
|
|
public IConfigManager getConfigManager()
|
|
|
|
{
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IInventory getInventoryByName(String name)
|
|
|
|
{
|
|
|
|
if ( name.equals( "upgrades" ) )
|
|
|
|
return upgrades;
|
|
|
|
|
|
|
|
if ( name.equals( "mac" ) )
|
|
|
|
return inv;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateSetting(IConfigManager manager, Enum settingName, Enum newValue)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-05-08 02:59:05 +02:00
|
|
|
public int getCraftingProgress()
|
|
|
|
{
|
2014-05-09 06:09:42 +02:00
|
|
|
return (int) progress;
|
2014-05-08 07:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TickingRequest getTickingRequest(IGridNode node)
|
|
|
|
{
|
2014-05-09 06:09:42 +02:00
|
|
|
return new TickingRequest( 1, 5, isAwake = hasPattern() && hasMats() || canPush(), false );
|
2014-05-08 02:59:05 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
private boolean hasMats()
|
|
|
|
{
|
|
|
|
if ( myPlan == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int x = 0; x < craftingInv.getSizeInventory(); x++)
|
|
|
|
craftingInv.setInventorySlotContents( x, inv.getStackInSlot( x ) );
|
|
|
|
|
|
|
|
return myPlan.getOutput( craftingInv, getWorldObj() ) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall)
|
|
|
|
{
|
|
|
|
if ( inv.getStackInSlot( 9 ) != null )
|
|
|
|
{
|
|
|
|
pushOut( inv.getStackInSlot( 9 ) );
|
|
|
|
return inv.getStackInSlot( 9 ) == null ? TickRateModulation.SLEEP : TickRateModulation.IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( myPlan == null )
|
|
|
|
{
|
|
|
|
isAwake = false;
|
|
|
|
return TickRateModulation.SLEEP;
|
|
|
|
}
|
|
|
|
|
2014-05-09 06:09:42 +02:00
|
|
|
switch (upgrades.getInstalledUpgrades( Upgrades.SPEED ))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
progress += userPower( TicksSinceLastCall );
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
progress += userPower( TicksSinceLastCall * 2 );
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
progress += userPower( TicksSinceLastCall * 6 );
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
progress += userPower( TicksSinceLastCall * 12 );
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
progress += userPower( TicksSinceLastCall * 30 );
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
progress += userPower( TicksSinceLastCall * 120 );
|
|
|
|
break;
|
|
|
|
}
|
2014-05-08 07:58:43 +02:00
|
|
|
|
|
|
|
if ( progress >= 100 )
|
|
|
|
{
|
|
|
|
for (int x = 0; x < craftingInv.getSizeInventory(); x++)
|
|
|
|
craftingInv.setInventorySlotContents( x, inv.getStackInSlot( x ) );
|
|
|
|
|
2014-05-09 06:09:42 +02:00
|
|
|
progress = 0;
|
2014-05-08 07:58:43 +02:00
|
|
|
ItemStack output = myPlan.getOutput( craftingInv, getWorldObj() );
|
|
|
|
if ( output != null )
|
|
|
|
{
|
|
|
|
for (int x = 0; x < craftingInv.getSizeInventory(); x++)
|
|
|
|
inv.setInventorySlotContents( x, null );
|
|
|
|
|
|
|
|
pushOut( output.copy() );
|
|
|
|
isAwake = false;
|
|
|
|
return inv.getStackInSlot( 9 ) == null ? TickRateModulation.SLEEP : TickRateModulation.IDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TickRateModulation.FASTER;
|
|
|
|
}
|
|
|
|
|
2014-05-09 06:09:42 +02:00
|
|
|
private int userPower(int i)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return (int) gridProxy.getEnergy().extractAEPower( i, Actionable.MODULATE, PowerMultiplier.CONFIG );
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 07:58:43 +02:00
|
|
|
private void pushOut(ItemStack output)
|
|
|
|
{
|
|
|
|
if ( pushDirection == ForgeDirection.UNKNOWN )
|
|
|
|
{
|
|
|
|
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS)
|
|
|
|
output = pushTo( output, d );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
output = pushTo( output, pushDirection );
|
|
|
|
|
|
|
|
inv.setInventorySlotContents( 9, output );
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemStack pushTo(ItemStack output, ForgeDirection d)
|
|
|
|
{
|
|
|
|
if ( output == null )
|
|
|
|
return output;
|
|
|
|
|
|
|
|
TileEntity te = getWorldObj().getTileEntity( xCoord + d.offsetX, yCoord + d.offsetY, zCoord + d.offsetZ );
|
|
|
|
|
|
|
|
if ( te == null )
|
|
|
|
return output;
|
|
|
|
|
|
|
|
InventoryAdaptor adaptor = InventoryAdaptor.getAdaptor( te, d.getOpposite() );
|
|
|
|
|
|
|
|
if ( adaptor == null )
|
|
|
|
return output;
|
|
|
|
|
|
|
|
return adaptor.addItems( output );
|
|
|
|
}
|
2014-04-08 02:43:57 +02:00
|
|
|
}
|