Ejemplo n.º 1
0
 public ReleaseItemData(InventoryTransactionPacket pk)
 {
     this.ActionType   = (int)pk.ReadUVarInt();
     this.HotbarSlot   = pk.ReadSVarInt();
     this.MainHandItem = pk.ReadItem();
     this.HeadRot      = pk.ReadVector3();
 }
Ejemplo n.º 2
0
 public UseItemOnEntityData(InventoryTransactionPacket pk)
 {
     this.EntityRuntimeId = pk.ReadEntityRuntimeId();
     this.ActionType      = (int)pk.ReadUVarInt();
     this.HotbarSlot      = pk.ReadSVarInt();
     this.ItemMainHand    = pk.ReadItem();
     this.PlayerPos       = pk.ReadVector3();
     this.ClickPos        = pk.ReadVector3();
 }
Ejemplo n.º 3
0
 public UseItemData(InventoryTransactionPacket pk)
 {
     this.ActionType   = (int)pk.ReadUVarInt();
     this.BlockPos     = (Vector3i)pk.ReadBlockVector3();
     this.Face         = pk.ReadBlockFace();
     this.HotbarSlot   = pk.ReadSVarInt();
     this.ItemMainHand = pk.ReadItem();
     this.PlayerPos    = pk.ReadVector3();
     this.ClickPos     = pk.ReadVector3();
 }
Ejemplo n.º 4
0
        public NetworkInventoryAction(InventoryTransactionPacket pk)
        {
            this.SourceType = (int)pk.ReadUVarInt();

            if (this.SourceType == NetworkInventoryAction.SOURCE_CONTAINER)
            {
                this.WindowId = pk.ReadSVarInt();
            }
            else if (this.SourceType == NetworkInventoryAction.SOURCE_WORLD)
            {
                this.Unknown = pk.ReadUVarInt();
            }
            else if (this.SourceType == NetworkInventoryAction.SOURCE_CREATIVE)
            {
            }
            else if (this.SourceType == NetworkInventoryAction.SOURCE_CRAFT_SLOT || this.SourceType == NetworkInventoryAction.SOURCE_TODO)
            {
                this.WindowId = pk.ReadSVarInt();
            }

            this.InventorySlot = (int)pk.ReadUVarInt();
            this.OldItem       = pk.ReadItem();
            this.NewItem       = pk.ReadItem();
        }
        private void InventoryTransactionHandle(InventoryTransactionPacket pk)
        {
            List <InventoryAction> actions = new List <InventoryAction>();

            for (int i = 0; i < pk.Actions.Length; ++i)
            {
                try
                {
                    InventoryAction action = pk.Actions[i].GetInventoryAction(this);
                    actions.Add(action);
                }
                catch (Exception e)
                {
                    Logger.Log($"Unhandled inventory action from {this.Name}: {e.Message}");
                    this.SendAllInventories();
                    return;
                }
            }

            if (pk.TransactionType == InventoryTransactionPacket.TYPE_NORMAL)
            {
                InventoryTransaction transaction = new InventoryTransaction(this, actions);
                if (this.IsSpectator)
                {
                    this.SendAllInventories();
                    return;
                }
                if (!transaction.Execute())
                {
                    Logger.Log($"Failed to execute inventory transaction from {this.Name} with actions");
                }
            }
            else if (pk.TransactionType == InventoryTransactionPacket.TYPE_MISMATCH)
            {
                this.SendAllInventories();
                return;
            }
            else if (pk.TransactionType == InventoryTransactionPacket.TYPE_USE_ITEM)
            {
                UseItemData data     = (UseItemData)pk.TransactionData;
                Vector3     blockPos = data.BlockPos;
                BlockFace   face     = data.Face;

                if (data.ActionType == InventoryTransactionPacket.USE_ITEM_ACTION_CLICK_BLOCK)
                {
                    this.SetFlag(Entity.DATA_FLAGS, Entity.DATA_FLAG_ACTION, false, true);
                    if (this.CanInteract(blockPos + new Vector3(0.5f, 0.5f, 0.5f), this.IsCreative ? 13 : 7))
                    {
                        Item item = this.Inventory.MainHandItem;
                        this.World.UseItem(blockPos, item, face, data.ClickPos, this);
                    }

                    //Send MainHand
                }
                else if (data.ActionType == InventoryTransactionPacket.USE_ITEM_ACTION_BREAK_BLOCK)
                {
                    Item item = this.Inventory.MainHandItem;
                    if (this.CanInteract(blockPos + new Vector3(0.5f, 0.5f, 0.5f), this.IsCreative ? 13 : 7))
                    {
                        this.World.UseBreak(data.BlockPos, item, this);
                        if (this.IsSurvival)
                        {
                            //TODO : food
                            this.Inventory.SendMainHand();
                        }
                    }
                    else
                    {
                        this.World.SendBlocks(new Player[] { this }, new Vector3[] { data.BlockPos });
                    }
                }
            }
            else if (pk.TransactionType == InventoryTransactionPacket.TYPE_USE_ITEM_ON_ENTITY)
            {
            }
            else if (pk.TransactionType == InventoryTransactionPacket.TYPE_RELEASE_ITEM)
            {
                ReleaseItemData data = (ReleaseItemData)pk.TransactionData;
                if (data.ActionType == InventoryTransactionPacket.RELEASE_ITEM_ACTION_RELEASE)
                {
                }
                else if (data.ActionType == InventoryTransactionPacket.RELEASE_ITEM_ACTION_CONSUME)
                {
                    if (this.Inventory.MainHandItem != data.MainHandItem || this.Inventory.MainHandSlot != data.HotbarSlot)
                    {
                        this.Inventory.SendMainHand(this);
                        return;
                    }
                    Item item = this.Inventory.MainHandItem;
                    if (!(item is IConsumeable))
                    {
                        this.Inventory.SendMainHand(this);
                        return;
                    }
                    IConsumeable consume = (IConsumeable)item;
                    PlayerItemConsumeableEventArgs playerItemConsumeableEvent = new PlayerItemConsumeableEventArgs(this, consume);
                    PlayerEvents.OnPlayerItemConsumeable(playerItemConsumeableEvent);
                    if (playerItemConsumeableEvent.IsCancel)
                    {
                        this.Inventory.SendMainHand(this);
                        return;
                    }
                    consume.OnConsume(this);
                }
            }
        }