2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
package appeng.core.sync.packets;
|
|
|
|
|
2014-12-29 21:59:05 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
2014-01-05 09:39:48 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2014-05-24 06:20:56 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2014-02-10 01:56:35 +01:00
|
|
|
import appeng.client.ClientHelper;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.container.AEBaseContainer;
|
2014-05-24 06:20:56 +02:00
|
|
|
import appeng.container.ContainerOpenContext;
|
|
|
|
import appeng.container.implementations.ContainerCraftAmount;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.core.sync.AppEngPacket;
|
2014-05-24 06:20:56 +02:00
|
|
|
import appeng.core.sync.GuiBridge;
|
2014-02-09 02:34:52 +01:00
|
|
|
import appeng.core.sync.network.INetworkInfo;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.helpers.InventoryAction;
|
2014-05-13 06:11:19 +02:00
|
|
|
import appeng.util.Platform;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.util.item.AEItemStack;
|
|
|
|
|
|
|
|
public class PacketInventoryAction extends AppEngPacket
|
|
|
|
{
|
|
|
|
|
|
|
|
final public InventoryAction action;
|
|
|
|
final public int slot;
|
2014-07-09 10:17:11 +02:00
|
|
|
final public long id;
|
2013-12-27 23:59:59 +01:00
|
|
|
final public IAEItemStack slotItem;
|
|
|
|
|
|
|
|
// automatic.
|
2014-02-09 02:34:52 +01:00
|
|
|
public PacketInventoryAction(ByteBuf stream) throws IOException {
|
2014-12-29 15:13:47 +01:00
|
|
|
this.action = InventoryAction.values()[stream.readInt()];
|
|
|
|
this.slot = stream.readInt();
|
|
|
|
this.id = stream.readLong();
|
2013-12-27 23:59:59 +01:00
|
|
|
boolean hasItem = stream.readBoolean();
|
|
|
|
if ( hasItem )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.slotItem = AEItemStack.loadItemStackFromPacket( stream );
|
2013-12-27 23:59:59 +01:00
|
|
|
else
|
2014-12-29 15:13:47 +01:00
|
|
|
this.slotItem = null;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void serverPacketData(INetworkInfo manager, AppEngPacket packet, EntityPlayer player)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
EntityPlayerMP sender = (EntityPlayerMP) player;
|
2014-01-23 17:28:12 +01:00
|
|
|
if ( sender.openContainer instanceof AEBaseContainer )
|
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
AEBaseContainer baseContainer = (AEBaseContainer) sender.openContainer;
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.action == InventoryAction.AUTO_CRAFT )
|
2014-05-24 06:20:56 +02:00
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
ContainerOpenContext context = baseContainer.openContext;
|
2014-05-24 06:20:56 +02:00
|
|
|
if ( context != null )
|
|
|
|
{
|
2014-07-29 04:51:52 +02:00
|
|
|
TileEntity te = context.getTile();
|
2014-09-28 11:47:17 +02:00
|
|
|
Platform.openGUI( sender, te, baseContainer.openContext.side, GuiBridge.GUI_CRAFTING_AMOUNT );
|
2014-05-24 06:20:56 +02:00
|
|
|
|
|
|
|
if ( sender.openContainer instanceof ContainerCraftAmount )
|
|
|
|
{
|
|
|
|
ContainerCraftAmount cca = (ContainerCraftAmount) sender.openContainer;
|
2014-08-21 06:02:31 +02:00
|
|
|
|
2014-09-28 11:47:17 +02:00
|
|
|
if ( baseContainer.getTargetStack() != null )
|
2014-08-21 06:02:31 +02:00
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
cca.craftingItem.putStack( baseContainer.getTargetStack().getItemStack() );
|
|
|
|
cca.whatToMake = baseContainer.getTargetStack();
|
2014-08-21 06:02:31 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 06:20:56 +02:00
|
|
|
cca.detectAndSendChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
baseContainer.doAction( sender, this.action, this.slot, this.id );
|
2014-05-24 06:20:56 +02:00
|
|
|
}
|
2014-01-23 17:28:12 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:56:35 +01:00
|
|
|
@Override
|
|
|
|
public void clientPacketData(INetworkInfo network, AppEngPacket packet, EntityPlayer player)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.action == InventoryAction.UPDATE_HAND )
|
2014-02-10 01:56:35 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.slotItem == null )
|
2014-02-10 01:56:35 +01:00
|
|
|
ClientHelper.proxy.getPlayers().get( 0 ).inventory.setItemStack( null );
|
|
|
|
else
|
2014-12-29 15:13:47 +01:00
|
|
|
ClientHelper.proxy.getPlayers().get( 0 ).inventory.setItemStack( this.slotItem.getItemStack() );
|
2014-02-10 01:56:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
// api
|
|
|
|
public PacketInventoryAction(InventoryAction action, int slot, IAEItemStack slotItem) throws IOException {
|
2014-05-13 06:11:19 +02:00
|
|
|
|
2014-07-09 10:17:11 +02:00
|
|
|
if ( Platform.isClient() )
|
2014-05-13 06:11:19 +02:00
|
|
|
throw new RuntimeException( "invalid packet, client cannot post inv actions with stacks." );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
this.action = action;
|
|
|
|
this.slot = slot;
|
2014-07-09 10:17:11 +02:00
|
|
|
this.id = 0;
|
2013-12-27 23:59:59 +01:00
|
|
|
this.slotItem = slotItem;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
ByteBuf data = Unpooled.buffer();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
data.writeInt( this.getPacketID() );
|
2013-12-27 23:59:59 +01:00
|
|
|
data.writeInt( action.ordinal() );
|
|
|
|
data.writeInt( slot );
|
2014-12-29 15:13:47 +01:00
|
|
|
data.writeLong( this.id );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
if ( slotItem == null )
|
|
|
|
data.writeBoolean( false );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data.writeBoolean( true );
|
|
|
|
slotItem.writeToPacket( data );
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.configureWrite( data );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-07-09 10:17:11 +02:00
|
|
|
// api
|
2014-10-04 08:08:28 +02:00
|
|
|
public PacketInventoryAction(InventoryAction action, int slot, long id)
|
|
|
|
{
|
2014-07-09 10:17:11 +02:00
|
|
|
this.action = action;
|
|
|
|
this.slot = slot;
|
|
|
|
this.id = id;
|
|
|
|
this.slotItem = null;
|
|
|
|
|
|
|
|
ByteBuf data = Unpooled.buffer();
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
data.writeInt( this.getPacketID() );
|
2014-07-09 10:17:11 +02:00
|
|
|
data.writeInt( action.ordinal() );
|
|
|
|
data.writeInt( slot );
|
|
|
|
data.writeLong( id );
|
|
|
|
data.writeBoolean( false );
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.configureWrite( data );
|
2014-07-09 10:17:11 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|