2014-06-16 07:49:37 +02:00
|
|
|
package appeng.core.sync.packets;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-06-18 08:23:37 +02:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ThreadFactory;
|
2014-06-16 07:49:37 +02:00
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2014-06-18 08:23:37 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-06-16 07:49:37 +02:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.container.ContainerOpenContext;
|
2014-06-16 07:49:37 +02:00
|
|
|
import appeng.container.implementations.ContainerCraftAmount;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.container.implementations.ContainerCraftConfirm;
|
2014-06-16 07:49:37 +02:00
|
|
|
import appeng.core.AELog;
|
|
|
|
import appeng.core.sync.AppEngPacket;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.core.sync.GuiBridge;
|
2014-06-16 07:49:37 +02:00
|
|
|
import appeng.core.sync.network.INetworkInfo;
|
|
|
|
import appeng.crafting.CraftingJob;
|
|
|
|
import appeng.me.cache.CraftingCache;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.util.Platform;
|
2014-06-16 07:49:37 +02:00
|
|
|
import appeng.util.item.AEItemStack;
|
|
|
|
|
|
|
|
public class PacketCraftRequest extends AppEngPacket
|
|
|
|
{
|
|
|
|
|
|
|
|
final public IAEItemStack slotItem;
|
2014-06-18 08:23:37 +02:00
|
|
|
final public static ExecutorService craftingPool;
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
|
|
|
ThreadFactory factory = new ThreadFactory() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Thread newThread(Runnable ar)
|
|
|
|
{
|
|
|
|
return new Thread( ar, "AE Crafting Calculator" );
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
craftingPool = Executors.newCachedThreadPool( factory );
|
|
|
|
}
|
2014-06-16 07:49:37 +02:00
|
|
|
|
|
|
|
// automatic.
|
|
|
|
public PacketCraftRequest(ByteBuf stream) throws IOException {
|
|
|
|
slotItem = AEItemStack.loadItemStackFromPacket( stream );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPlayer player)
|
|
|
|
{
|
|
|
|
if ( player.openContainer instanceof ContainerCraftAmount )
|
|
|
|
{
|
|
|
|
ContainerCraftAmount cca = (ContainerCraftAmount) player.openContainer;
|
|
|
|
Object targ = cca.getTarget();
|
|
|
|
if ( targ instanceof IGridHost )
|
|
|
|
{
|
|
|
|
IGridHost gh = (IGridHost) targ;
|
|
|
|
IGridNode gn = gh.getGridNode( ForgeDirection.UNKNOWN );
|
|
|
|
if ( gn == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
IGrid g = gn.getGrid();
|
|
|
|
if ( g == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
CraftingCache cc = g.getCache( CraftingCache.class );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
CraftingJob cj = new CraftingJob( cca.getWorld(), cca, slotItem, Actionable.SIMULATE );
|
2014-06-16 07:49:37 +02:00
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
ContainerOpenContext context = cca.openContext;
|
|
|
|
if ( context != null )
|
|
|
|
{
|
|
|
|
TileEntity te = context.w.getTileEntity( context.x, context.y, context.z );
|
|
|
|
Platform.openGUI( player, te, cca.openContext.side, GuiBridge.GUI_CRAFTING_CONFIRM );
|
|
|
|
|
|
|
|
if ( player.openContainer instanceof ContainerCraftConfirm )
|
|
|
|
{
|
|
|
|
ContainerCraftConfirm ccc = (ContainerCraftConfirm) player.openContainer;
|
|
|
|
ccc.job = craftingPool.submit( cj, cj );
|
|
|
|
cca.detectAndSendChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:49:37 +02:00
|
|
|
}
|
|
|
|
catch (Throwable e)
|
|
|
|
{
|
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PacketCraftRequest(ItemStack stack, int parseInt) throws IOException {
|
|
|
|
this.slotItem = AEApi.instance().storage().createItemStack( stack );
|
|
|
|
this.slotItem.setStackSize( parseInt );
|
|
|
|
|
|
|
|
ByteBuf data = Unpooled.buffer();
|
|
|
|
|
|
|
|
data.writeInt( getPacketID() );
|
|
|
|
slotItem.writeToPacket( data );
|
|
|
|
|
|
|
|
configureWrite( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|