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-07-03 06:48:18 +02:00
|
|
|
import java.util.concurrent.Future;
|
2014-06-16 07:49:37 +02:00
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
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.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
2014-07-03 06:48:18 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingGrid;
|
|
|
|
import appeng.api.networking.crafting.ICraftingJob;
|
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;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.util.Platform;
|
2014-06-16 07:49:37 +02:00
|
|
|
|
|
|
|
public class PacketCraftRequest extends AppEngPacket
|
|
|
|
{
|
|
|
|
|
2014-08-21 06:02:31 +02:00
|
|
|
final public long amount;
|
2014-06-29 11:06:07 +02:00
|
|
|
final public boolean heldShift;
|
2014-06-16 07:49:37 +02:00
|
|
|
|
|
|
|
// automatic.
|
|
|
|
public PacketCraftRequest(ByteBuf stream) throws IOException {
|
2014-06-29 11:06:07 +02:00
|
|
|
heldShift = stream.readBoolean();
|
2014-08-21 06:02:31 +02:00
|
|
|
amount = stream.readLong();
|
2014-06-16 07:49:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
2014-08-21 06:02:31 +02:00
|
|
|
if ( g == null || cca.whatToMake == null )
|
2014-06-16 07:49:37 +02:00
|
|
|
return;
|
|
|
|
|
2014-07-03 06:48:18 +02:00
|
|
|
Future<ICraftingJob> futureJob = null;
|
|
|
|
|
2014-08-21 06:02:31 +02:00
|
|
|
cca.whatToMake.setStackSize( amount );
|
|
|
|
|
2014-06-16 07:49:37 +02:00
|
|
|
try
|
|
|
|
{
|
2014-07-03 06:48:18 +02:00
|
|
|
ICraftingGrid cg = g.getCache( ICraftingGrid.class );
|
2014-08-21 06:02:31 +02:00
|
|
|
futureJob = cg.beginCraftingJob( cca.getWorld(), cca.getGrid(), cca.getActionSrc(), cca.whatToMake, null );
|
2014-06-16 07:49:37 +02:00
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
ContainerOpenContext context = cca.openContext;
|
|
|
|
if ( context != null )
|
|
|
|
{
|
2014-07-29 04:51:52 +02:00
|
|
|
TileEntity te = context.getTile();
|
2014-06-18 08:23:37 +02:00
|
|
|
Platform.openGUI( player, te, cca.openContext.side, GuiBridge.GUI_CRAFTING_CONFIRM );
|
|
|
|
|
|
|
|
if ( player.openContainer instanceof ContainerCraftConfirm )
|
|
|
|
{
|
|
|
|
ContainerCraftConfirm ccc = (ContainerCraftConfirm) player.openContainer;
|
2014-06-29 11:06:07 +02:00
|
|
|
ccc.autoStart = heldShift;
|
2014-07-03 06:48:18 +02:00
|
|
|
ccc.job = futureJob;
|
2014-06-18 08:23:37 +02:00
|
|
|
cca.detectAndSendChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:49:37 +02:00
|
|
|
}
|
|
|
|
catch (Throwable e)
|
|
|
|
{
|
2014-07-03 06:48:18 +02:00
|
|
|
if ( futureJob != null )
|
|
|
|
futureJob.cancel( true );
|
2014-06-16 07:49:37 +02:00
|
|
|
AELog.error( e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:02:31 +02:00
|
|
|
public PacketCraftRequest(int craftAmt, boolean shift) throws IOException {
|
|
|
|
|
|
|
|
this.amount = craftAmt;
|
2014-06-29 11:06:07 +02:00
|
|
|
this.heldShift = shift;
|
2014-06-16 07:49:37 +02:00
|
|
|
|
|
|
|
ByteBuf data = Unpooled.buffer();
|
|
|
|
|
|
|
|
data.writeInt( getPacketID() );
|
2014-06-29 11:06:07 +02:00
|
|
|
data.writeBoolean( shift );
|
2014-08-21 06:02:31 +02:00
|
|
|
data.writeLong( amount );
|
2014-06-16 07:49:37 +02:00
|
|
|
|
|
|
|
configureWrite( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|