Applied-Energistics-2-tiler.../container/implementations/ContainerCraftingCPU.java

182 lines
4.3 KiB
Java
Raw Normal View History

2014-06-26 03:49:37 +02:00
package appeng.container.implementations;
import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ICrafting;
2014-06-26 03:49:37 +02:00
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.AEApi;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.crafting.CraftingItemList;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.storage.IBaseMonitor;
import appeng.api.storage.IMEMonitorHandlerReceiver;
2014-06-26 03:49:37 +02:00
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.container.AEBaseContainer;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketMEInventoryUpdate;
2014-07-16 05:26:12 +02:00
import appeng.helpers.ICustomNameObject;
import appeng.me.cluster.IAECluster;
import appeng.me.cluster.implementations.CraftingCPUCluster;
2014-06-26 03:49:37 +02:00
import appeng.tile.crafting.TileCraftingTile;
import appeng.util.Platform;
2014-07-16 05:26:12 +02:00
public class ContainerCraftingCPU extends AEBaseContainer implements IMEMonitorHandlerReceiver<IAEItemStack>, ICustomNameObject
2014-06-26 03:49:37 +02:00
{
CraftingCPUCluster monitor = null;
2014-07-16 05:26:12 +02:00
String cpuName = null;
2014-06-26 03:49:37 +02:00
IGrid network;
IItemList<IAEItemStack> list = AEApi.instance().storage().createItemList();
2014-06-26 03:49:37 +02:00
public ContainerCraftingCPU(InventoryPlayer ip, TileCraftingTile te) {
super( ip, null, null );
IGridHost host = te;// .getGridHost();
if ( host != null )
{
findNode( host, ForgeDirection.UNKNOWN );
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS)
findNode( host, d );
}
if ( te instanceof TileCraftingTile )
{
IAECluster c = ((TileCraftingTile) te).getCluster();
if ( c instanceof CraftingCPUCluster )
{
2014-07-16 05:26:12 +02:00
cpuName = ((CraftingCPUCluster) c).getName();
monitor = (CraftingCPUCluster) c;
if ( monitor != null )
{
monitor.getListOfItem( list, CraftingItemList.ALL );
monitor.addListener( this, null );
}
}
}
2014-06-26 03:49:37 +02:00
if ( network == null && Platform.isServer() )
isContainerValid = false;
}
2014-06-28 07:20:12 +02:00
public void cancelCrafting()
{
if ( monitor != null )
{
monitor.cancel();
}
}
2014-06-26 03:49:37 +02:00
private void findNode(IGridHost host, ForgeDirection d)
{
if ( network == null )
{
IGridNode node = host.getGridNode( d );
if ( node != null )
network = node.getGrid();
}
}
int delay = 40;
@Override
public void onContainerClosed(EntityPlayer player)
{
super.onContainerClosed( player );
if ( monitor != null )
monitor.removeListener( this );
}
@Override
public void removeCraftingFromCrafters(ICrafting c)
{
super.removeCraftingFromCrafters( c );
if ( this.crafters.isEmpty() && monitor != null )
monitor.removeListener( this );
}
2014-06-26 03:49:37 +02:00
@Override
public void detectAndSendChanges()
{
if ( Platform.isServer() && !list.isEmpty() )
2014-06-26 03:49:37 +02:00
{
try
{
PacketMEInventoryUpdate a = new PacketMEInventoryUpdate( (byte) 0 );
PacketMEInventoryUpdate b = new PacketMEInventoryUpdate( (byte) 1 );
PacketMEInventoryUpdate c = new PacketMEInventoryUpdate( (byte) 2 );
2014-06-26 03:49:37 +02:00
for (IAEItemStack out : list)
2014-06-26 03:49:37 +02:00
{
a.appendItem( monitor.getItemStack( out, CraftingItemList.STORAGE ) );
b.appendItem( monitor.getItemStack( out, CraftingItemList.ACTIVE ) );
c.appendItem( monitor.getItemStack( out, CraftingItemList.PENDING ) );
2014-06-26 03:49:37 +02:00
}
list.resetStatus();
for (Object g : this.crafters)
2014-06-26 03:49:37 +02:00
{
if ( g instanceof EntityPlayer )
{
if ( !a.isEmpty() )
NetworkHandler.instance.sendTo( a, (EntityPlayerMP) g );
if ( !b.isEmpty() )
NetworkHandler.instance.sendTo( b, (EntityPlayerMP) g );
if ( !c.isEmpty() )
NetworkHandler.instance.sendTo( c, (EntityPlayerMP) g );
}
2014-06-26 03:49:37 +02:00
}
}
catch (IOException e)
{
// :P
}
}
super.detectAndSendChanges();
}
@Override
public boolean isValid(Object verificationToken)
{
return true;
}
@Override
public void postChange(IBaseMonitor<IAEItemStack> monitor, IAEItemStack change, BaseActionSource actionSource)
{
change = change.copy();
change.setStackSize( 1 );
list.add( change );
}
@Override
public void onListUpdate()
{
}
2014-07-16 05:26:12 +02:00
@Override
public String getCustomName()
{
return cpuName;
}
@Override
public boolean hasCustomName()
{
return cpuName != null && cpuName.length() > 0;
}
2014-06-26 03:49:37 +02:00
}