2014-05-01 06:01:22 +02:00
|
|
|
package appeng.core.sync.packets;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
import net.minecraft.inventory.Container;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-08-19 22:00:32 +02:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2014-07-06 08:52:29 +02:00
|
|
|
import appeng.api.config.Actionable;
|
2014-05-01 06:01:22 +02:00
|
|
|
import appeng.api.config.SecurityPermissions;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
|
|
|
import appeng.api.networking.security.ISecurityGrid;
|
|
|
|
import appeng.api.networking.storage.IStorageGrid;
|
|
|
|
import appeng.api.storage.IMEMonitor;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
import appeng.container.ContainerNull;
|
|
|
|
import appeng.core.sync.AppEngPacket;
|
|
|
|
import appeng.core.sync.network.INetworkInfo;
|
2014-07-06 08:52:29 +02:00
|
|
|
import appeng.helpers.IContainerCraftingPacket;
|
2014-09-17 06:26:25 +02:00
|
|
|
import appeng.items.storage.ItemViewCell;
|
2014-05-01 06:01:22 +02:00
|
|
|
import appeng.util.Platform;
|
|
|
|
import appeng.util.item.AEItemStack;
|
2014-09-17 06:26:25 +02:00
|
|
|
import appeng.util.prioitylist.IPartitionList;
|
2014-05-01 06:01:22 +02:00
|
|
|
|
|
|
|
public class PacketNEIRecipe extends AppEngPacket
|
|
|
|
{
|
|
|
|
|
2014-08-19 22:00:32 +02:00
|
|
|
ItemStack[][] recipe;
|
2014-05-01 06:01:22 +02:00
|
|
|
|
|
|
|
// automatic.
|
2014-09-17 06:26:25 +02:00
|
|
|
public PacketNEIRecipe(ByteBuf stream) throws IOException
|
|
|
|
{
|
2014-05-01 06:01:22 +02:00
|
|
|
ByteArrayInputStream bytes = new ByteArrayInputStream( stream.array() );
|
|
|
|
bytes.skip( stream.readerIndex() );
|
|
|
|
NBTTagCompound comp = CompressedStreamTools.readCompressed( bytes );
|
|
|
|
if ( comp != null )
|
|
|
|
{
|
2014-08-19 22:00:32 +02:00
|
|
|
recipe = new ItemStack[9][];
|
2014-05-01 06:01:22 +02:00
|
|
|
for (int x = 0; x < recipe.length; x++)
|
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
NBTTagList list = comp.getTagList( "#" + x, 10 );
|
2014-08-19 22:00:32 +02:00
|
|
|
if ( list.tagCount() > 0 )
|
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
recipe[x] = new ItemStack[list.tagCount()];
|
|
|
|
for (int y = 0; y < list.tagCount(); y++)
|
2014-08-19 22:00:32 +02:00
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
recipe[x][y] = ItemStack.loadItemStackFromNBT( list.getCompoundTagAt( y ) );
|
2014-08-19 22:00:32 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-01 06:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPlayer player)
|
|
|
|
{
|
|
|
|
EntityPlayerMP pmp = (EntityPlayerMP) player;
|
|
|
|
Container con = pmp.openContainer;
|
|
|
|
|
2014-07-06 08:52:29 +02:00
|
|
|
if ( con != null && con instanceof IContainerCraftingPacket )
|
2014-05-01 06:01:22 +02:00
|
|
|
{
|
2014-07-06 08:52:29 +02:00
|
|
|
IContainerCraftingPacket cct = (IContainerCraftingPacket) con;
|
2014-05-01 06:01:22 +02:00
|
|
|
IGridNode node = cct.getNetworkNode();
|
|
|
|
if ( node != null )
|
|
|
|
{
|
|
|
|
IGrid grid = node.getGrid();
|
|
|
|
if ( grid == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
IStorageGrid inv = grid.getCache( IStorageGrid.class );
|
|
|
|
IEnergyGrid energy = grid.getCache( IEnergyGrid.class );
|
|
|
|
ISecurityGrid security = grid.getCache( ISecurityGrid.class );
|
2014-07-06 08:52:29 +02:00
|
|
|
IInventory craftMatrix = cct.getInventoryByName( "crafting" );
|
|
|
|
|
|
|
|
Actionable realForFake = cct.useRealItems() ? Actionable.MODULATE : Actionable.SIMULATE;
|
2014-05-01 06:01:22 +02:00
|
|
|
|
|
|
|
if ( inv != null && recipe != null && security != null )
|
|
|
|
{
|
|
|
|
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
|
|
|
for (int x = 0; x < 9; x++)
|
2014-08-19 22:00:32 +02:00
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
if ( recipe[x] != null && recipe[x].length > 0 )
|
2014-08-19 22:00:32 +02:00
|
|
|
{
|
|
|
|
ic.setInventorySlotContents( x, recipe[x][0] );
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 06:26:25 +02:00
|
|
|
|
2014-05-01 06:01:22 +02:00
|
|
|
IRecipe r = Platform.findMatchingRecipe( ic, pmp.worldObj );
|
|
|
|
|
|
|
|
if ( r != null && security.hasPermission( player, SecurityPermissions.EXTRACT ) )
|
|
|
|
{
|
|
|
|
ItemStack is = r.getCraftingResult( ic );
|
|
|
|
|
|
|
|
if ( is != null )
|
|
|
|
{
|
|
|
|
IMEMonitor<IAEItemStack> stor = inv.getItemInventory();
|
|
|
|
IItemList all = stor.getStorageList();
|
2014-09-17 06:26:25 +02:00
|
|
|
IPartitionList<IAEItemStack> filter = ItemViewCell.createFilter( cct.getViewCells() );
|
2014-05-01 06:01:22 +02:00
|
|
|
|
|
|
|
for (int x = 0; x < craftMatrix.getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
ItemStack PatternItem = ic.getStackInSlot( x );
|
|
|
|
|
|
|
|
ItemStack currentItem = craftMatrix.getStackInSlot( x );
|
|
|
|
if ( currentItem != null )
|
|
|
|
{
|
|
|
|
ic.setInventorySlotContents( x, currentItem );
|
|
|
|
ItemStack newis = r.matches( ic, pmp.worldObj ) ? r.getCraftingResult( ic ) : null;
|
|
|
|
ic.setInventorySlotContents( x, PatternItem );
|
|
|
|
|
|
|
|
if ( newis == null || !Platform.isSameItemPrecise( newis, is ) )
|
|
|
|
{
|
|
|
|
IAEItemStack in = AEItemStack.create( currentItem );
|
|
|
|
if ( in != null )
|
|
|
|
{
|
2014-07-06 08:52:29 +02:00
|
|
|
IAEItemStack out = realForFake == Actionable.SIMULATE ? null : Platform.poweredInsert( energy, stor, in,
|
|
|
|
cct.getSource() );
|
2014-05-01 06:01:22 +02:00
|
|
|
if ( out != null )
|
|
|
|
craftMatrix.setInventorySlotContents( x, out.getItemStack() );
|
|
|
|
else
|
|
|
|
craftMatrix.setInventorySlotContents( x, null );
|
|
|
|
|
|
|
|
currentItem = craftMatrix.getStackInSlot( x );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( PatternItem != null && currentItem == null )
|
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
ItemStack whichItem = Platform.extractItemsByRecipe( energy, cct.getSource(), stor, player.worldObj, r, is, ic,
|
|
|
|
PatternItem, x, all, realForFake, filter );
|
|
|
|
|
2014-08-19 22:00:32 +02:00
|
|
|
if ( whichItem == null )
|
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
for (int y = 0; y < recipe[x].length; y++)
|
2014-08-19 22:00:32 +02:00
|
|
|
{
|
|
|
|
IAEItemStack request = AEItemStack.create( recipe[x][y] );
|
|
|
|
if ( request != null )
|
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
if ( filter == null || filter.isListed( request ) )
|
2014-08-19 22:00:32 +02:00
|
|
|
{
|
2014-09-17 06:26:25 +02:00
|
|
|
request.setStackSize( 1 );
|
|
|
|
IAEItemStack out = Platform.poweredExtraction( energy, stor, request, cct.getSource() );
|
|
|
|
if ( out != null )
|
|
|
|
{
|
|
|
|
whichItem = out.getItemStack();
|
|
|
|
break;
|
|
|
|
}
|
2014-08-19 22:00:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 06:26:25 +02:00
|
|
|
|
2014-08-19 22:00:32 +02:00
|
|
|
craftMatrix.setInventorySlotContents( x, whichItem );
|
2014-05-01 06:01:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
con.onCraftMatrixChanged( craftMatrix );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// api
|
2014-09-17 06:26:25 +02:00
|
|
|
public PacketNEIRecipe(NBTTagCompound recipe) throws IOException
|
|
|
|
{
|
2014-05-01 06:01:22 +02:00
|
|
|
ByteBuf data = Unpooled.buffer();
|
|
|
|
|
|
|
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
|
|
DataOutputStream datao = new DataOutputStream( bytes );
|
|
|
|
|
|
|
|
data.writeInt( getPacketID() );
|
|
|
|
|
|
|
|
CompressedStreamTools.writeCompressed( recipe, datao );
|
|
|
|
data.writeBytes( bytes.toByteArray() );
|
|
|
|
|
|
|
|
configureWrite( data );
|
|
|
|
}
|
|
|
|
}
|