Applied-Energistics-2-tiler.../tile/crafting/TileMolecularAssembler.java

544 lines
13 KiB
Java
Raw Normal View History

2014-04-08 02:43:57 +02:00
package appeng.tile.crafting;
2014-06-24 05:55:16 +02:00
import io.netty.buffer.ByteBuf;
import java.io.IOException;
import java.util.ArrayList;
2014-04-08 02:43:57 +02:00
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.InventoryCrafting;
2014-04-08 02:43:57 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
2014-06-22 09:00:38 +02:00
import net.minecraft.world.WorldServer;
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-06-24 05:55:16 +02:00
import appeng.api.implementations.IPowerChannelState;
2014-04-24 08:14:26 +02:00
import appeng.api.implementations.IUpgradeableHost;
import appeng.api.implementations.tiles.ICraftingMachine;
import appeng.api.networking.IGridNode;
import appeng.api.networking.crafting.ICraftingPatternDetails;
2014-06-24 05:55:16 +02:00
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.networking.ticking.IGridTickable;
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
2014-06-24 05:55:16 +02:00
import appeng.api.parts.ISimplifiedBundle;
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;
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;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
2014-06-22 09:00:38 +02:00
import cpw.mods.fml.common.FMLCommonHandler;
2014-04-08 02:43:57 +02:00
public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEngInventory, ISidedInventory, IUpgradeableHost, IConfigManagerHost,
2014-06-24 05:55:16 +02:00
IGridTickable, ICraftingMachine, IPowerChannelState
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 };
2014-06-28 07:20:12 +02:00
static final ItemStack assemblerStack = AEApi.instance().blocks().blockMolecularAssembler.stack( 1 );
2014-04-08 02:43:57 +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 );
2014-06-28 07:20:12 +02:00
private UpgradeInventory upgrades = new UpgradeInventory( assemblerStack, this, getUpgradeSlots() );
2014-04-24 08:14:26 +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;
private boolean isAwake = false;
2014-05-14 04:42:14 +02:00
private boolean forcePlan = false;
private boolean reboot = true;
2014-06-24 05:55:16 +02:00
public ISimplifiedBundle lightCache;
2014-05-09 06:09:42 +02:00
public boolean pushPattern(ICraftingPatternDetails patternDetails, InventoryCrafting table, ForgeDirection where)
{
2014-05-14 04:42:14 +02:00
if ( myPattern == null )
{
boolean isEmpty = true;
for (int x = 0; x < inv.getSizeInventory(); x++)
isEmpty = inv.getStackInSlot( x ) == null && isEmpty;
if ( isEmpty )
{
forcePlan = true;
myPlan = patternDetails;
pushDirection = where;
for (int x = 0; x < table.getSizeInventory(); x++)
inv.setInventorySlotContents( x, table.getStackInSlot( x ) );
updateSleepyness();
markDirty();
2014-05-14 04:42:14 +02:00
return true;
}
}
return false;
}
private void recalculatePlan()
{
reboot = true;
2014-05-14 04:42:14 +02:00
if ( forcePlan )
return;
2014-05-14 04:42:14 +02:00
ItemStack is = inv.getStackInSlot( 10 );
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;
2014-06-27 07:45:10 +02:00
forcePlan = false;
myPlan = null;
myPattern = null;
}
2014-05-14 04:42:14 +02:00
updateSleepyness();
}
private void updateSleepyness()
{
boolean wasEnabled = isAwake;
2014-05-09 06:09:42 +02:00
isAwake = myPlan != null && hasMats() || canPush();
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() {
2014-06-24 05:55:16 +02:00
super( TileEventType.WORLD_NBT, TileEventType.NETWORK );
}
@Override
public boolean readFromStream(ByteBuf data) throws IOException
{
boolean oldPower = isPowered;
isPowered = data.readBoolean();
return isPowered != oldPower;
}
@Override
public void writeToStream(ByteBuf data) throws IOException
{
data.writeBoolean( isPowered );
2014-04-08 02:43:57 +02:00
}
@Override
public void writeToNBT(NBTTagCompound data)
{
2014-06-27 07:45:10 +02:00
if ( forcePlan && myPlan != null )
2014-05-14 04:42:14 +02:00
{
ItemStack pattern = myPlan.getPattern();
if ( pattern != null )
{
NBTTagCompound pdata = new NBTTagCompound();
pattern.writeToNBT( pdata );
data.setTag( "myPlan", pdata );
}
}
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-05-14 04:42:14 +02:00
if ( data.hasKey( "myPlan" ) )
{
ItemStack myPat = ItemStack.loadItemStackFromNBT( data.getCompoundTag( "myPlan" ) );
if ( myPat != null && myPat.getItem() instanceof ItemEncodedPattern )
{
World w = getWorldObj();
2014-06-28 07:20:12 +02:00
ItemEncodedPattern iep = (ItemEncodedPattern) myPat.getItem();
ICraftingPatternDetails ph = iep.getPatternForItem( myPat, w );
2014-05-14 04:42:14 +02:00
if ( ph != null )
{
forcePlan = true;
myPlan = ph;
}
}
}
2014-04-24 08:14:26 +02:00
upgrades.readFromNBT( data, "upgrades" );
inv.readFromNBT( data, "inv" );
settings.readFromNBT( data );
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 );
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() )
return myPlan.isValidItemForSlot( i, itemstack, getWorldObj() );
2014-04-08 02:43:57 +02:00
return false;
}
@Override
public boolean acceptsPlans()
{
return inv.getStackInSlot( 10 ) == null;
}
2014-04-24 08:14:26 +02:00
private boolean hasPattern()
{
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)
{
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)
{
}
@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;
}
@Override
public void getDrops(World w, int x, int y, int z, ArrayList<ItemStack> drops)
{
super.getDrops( w, x, y, z, drops );
for (int h = 0; h < upgrades.getSizeInventory(); h++)
{
ItemStack is = upgrades.getStackInSlot( h );
if ( is != null )
drops.add( is );
}
}
@Override
public TickingRequest getTickingRequest(IGridNode node)
{
recalculatePlan();
updateSleepyness();
return new TickingRequest( 1, 1, !isAwake, false );
2014-05-08 02:59:05 +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 ) );
2014-06-22 09:00:38 +02:00
ejectHeldItems();
updateSleepyness();
2014-06-22 09:00:38 +02:00
return isAwake ? TickRateModulation.SLEEP : TickRateModulation.IDLE;
}
if ( myPlan == null )
{
updateSleepyness();
return TickRateModulation.SLEEP;
}
if ( reboot )
TicksSinceLastCall = 1;
reboot = false;
2014-05-09 06:09:42 +02:00
switch (upgrades.getInstalledUpgrades( Upgrades.SPEED ))
{
case 0:
2014-06-28 07:20:12 +02:00
progress += userPower( TicksSinceLastCall, 10, 1.0 );
2014-05-09 06:09:42 +02:00
break;
case 1:
2014-06-28 07:20:12 +02:00
progress += userPower( TicksSinceLastCall, 13, 1.3 );
2014-05-09 06:09:42 +02:00
break;
case 2:
2014-06-28 07:20:12 +02:00
progress += userPower( TicksSinceLastCall, 17, 1.7 );
2014-05-09 06:09:42 +02:00
break;
case 3:
2014-06-28 07:20:12 +02:00
progress += userPower( TicksSinceLastCall, 20, 2.0 );
2014-05-09 06:09:42 +02:00
break;
case 4:
2014-06-28 07:20:12 +02:00
progress += userPower( TicksSinceLastCall, 25, 2.5 );
2014-05-09 06:09:42 +02:00
break;
case 5:
2014-06-28 07:20:12 +02:00
progress += userPower( TicksSinceLastCall, 50, 5.0 );
2014-05-09 06:09:42 +02:00
break;
}
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;
ItemStack output = myPlan.getOutput( craftingInv, getWorldObj() );
if ( output != null )
{
2014-06-22 09:00:38 +02:00
FMLCommonHandler.instance().firePlayerCraftingEvent( Platform.getPlayer( (WorldServer) getWorldObj() ), output, craftingInv );
for (int x = 0; x < craftingInv.getSizeInventory(); x++)
2014-06-22 09:00:38 +02:00
inv.setInventorySlotContents( x, Platform.getContainerItem( craftingInv.getStackInSlot( x ) ) );
pushOut( output.copy() );
2014-06-22 09:00:38 +02:00
if ( inv.getStackInSlot( 10 ) == null )
2014-06-27 07:45:10 +02:00
{
forcePlan = false;
2014-06-22 09:00:38 +02:00
myPlan = null;
2014-06-27 07:45:10 +02:00
}
2014-06-22 09:00:38 +02:00
ejectHeldItems();
updateSleepyness();
2014-06-22 09:00:38 +02:00
return isAwake ? TickRateModulation.SLEEP : TickRateModulation.IDLE;
}
}
return TickRateModulation.FASTER;
}
2014-06-22 09:00:38 +02:00
private void ejectHeldItems()
{
if ( myPlan == null && inv.getStackInSlot( 9 ) == null )
{
for (int x = 0; x < 9; x++)
{
ItemStack is = inv.getStackInSlot( x );
if ( is != null )
{
inv.setInventorySlotContents( 9, is );
inv.setInventorySlotContents( x, null );
markDirty();
2014-06-22 09:00:38 +02:00
return;
}
}
}
}
2014-06-28 07:20:12 +02:00
private int userPower(int ticksPassed, int bonusValue, double acceleratorTax)
2014-05-09 06:09:42 +02:00
{
try
{
2014-06-28 07:20:12 +02:00
return (int) (gridProxy.getEnergy().extractAEPower( ticksPassed * bonusValue * acceleratorTax, Actionable.MODULATE, PowerMultiplier.CONFIG ) / acceleratorTax);
2014-05-09 06:09:42 +02:00
}
catch (GridAccessException e)
{
return 0;
}
}
private void pushOut(ItemStack output)
{
if ( pushDirection == ForgeDirection.UNKNOWN )
{
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS)
output = pushTo( output, d );
}
else
output = pushTo( output, pushDirection );
2014-05-14 04:42:14 +02:00
if ( output == null && forcePlan )
{
forcePlan = false;
recalculatePlan();
}
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;
int size = output.stackSize;
output = adaptor.addItems( output );
int newSize = output == null ? 0 : output.stackSize;
if ( size != newSize )
markDirty();
return output;
}
2014-06-24 05:55:16 +02:00
boolean isPowered = false;
@MENetworkEventSubscribe
public void onPowerEvent(MENetworkPowerStatusChange p)
{
boolean newState = gridProxy.isActive();
if ( newState != isPowered )
{
isPowered = newState;
markForUpdate();
}
}
@Override
public boolean isPowered()
{
return isPowered;
}
@Override
public boolean isActive()
{
return isPowered;
}
2014-04-08 02:43:57 +02:00
}