Ejemplo n.º 1
0
        private void TryPullFrom(BlockFacing inputFace)
        {
            BlockPos InputPosition = Pos.AddCopy(inputFace);

            if (Api.World.BlockAccessor.GetBlockEntity(InputPosition) is BlockEntityContainer beContainer)
            {
                //do not both push and pull across the same chute-chute connection
                if (beContainer.Block is BlockChute chute)
                {
                    string[] pushFaces = chute.Attributes["pushFaces"].AsArray <string>(null);
                    if (pushFaces?.Contains(inputFace.Opposite.Code) == true)
                    {
                        return;
                    }
                }

                ItemSlot            sourceSlot = beContainer.Inventory.GetAutoPullFromSlot(inputFace.Opposite);
                ItemSlot            targetSlot = sourceSlot == null ? null : inventory.GetBestSuitedSlot(sourceSlot).slot;
                BlockEntityItemFlow beFlow     = beContainer as BlockEntityItemFlow;

                if (sourceSlot != null && targetSlot != null && (beFlow == null || targetSlot.Empty))
                {
                    ItemStackMoveOperation op = new ItemStackMoveOperation(Api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, (int)itemFlowAccum);

                    int horTravelled = sourceSlot.Itemstack.Attributes.GetInt("chuteQHTravelled");
                    if (horTravelled < 2)
                    {
                        int qmoved = sourceSlot.TryPutInto(targetSlot, ref op);
                        if (qmoved > 0)
                        {
                            if (beFlow != null)
                            {
                                targetSlot.Itemstack.Attributes.SetInt("chuteQHTravelled", inputFace.IsHorizontal ? (horTravelled + 1): 0);
                                targetSlot.Itemstack.Attributes.SetInt("chuteDir", inputFace.Opposite.Index);
                            }
                            else
                            {
                                targetSlot.Itemstack.Attributes.RemoveAttribute("chuteQHTravelled");
                                targetSlot.Itemstack.Attributes.RemoveAttribute("chuteDir");
                            }

                            sourceSlot.MarkDirty();
                            targetSlot.MarkDirty();
                            MarkDirty(false);
                            beFlow?.MarkDirty();
                        }

                        if (qmoved > 0 && Api.World.Rand.NextDouble() < 0.2)
                        {
                            Api.World.PlaySoundAt(hopperTumble, Pos.X + 0.5, Pos.Y + 0.5, Pos.Z + 0.5, null, true, 8, 0.5f);

                            itemFlowAccum -= qmoved;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to move the item from slot to slot.
        /// </summary>
        /// <param name="dt"></param>
        public void MoveItem(float dt)
        {
            //check above.  Then check below.
            BlockPos InputPosition  = pos.AddCopy(InputFace);
            BlockPos OutputPosition = pos.AddCopy(OutputFace);

            //if inventory below, attempt to move item in me to below
            if (api.World.BlockAccessor.GetBlockEntity(OutputPosition) is BlockEntityContainer && !inventory.IsEmpty)
            {
                BlockEntityContainer outputBox    = (BlockEntityContainer)api.World.BlockAccessor.GetBlockEntity(OutputPosition);
                ItemSlot             transferSlot = null;
                foreach (ItemSlot slot in inventory)
                {
                    if (!slot.Empty)
                    {
                        transferSlot = slot;
                        break;
                    }
                }

                if (transferSlot != null)
                {
                    WeightedSlot ws = outputBox.Inventory.GetBestSuitedSlot(transferSlot);
                    if (ws.slot != null)
                    {
                        ItemStackMoveOperation op = new ItemStackMoveOperation(api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, FlowAmount);
                        if (transferSlot.TryPutInto(ws.slot, ref op) > 0)
                        {
                            if (api.World.Rand.NextDouble() < 0.2)
                            {
                                api.World.PlaySoundAt(new AssetLocation("sounds/block/hoppertumble"), pos.X + 0.5, pos.Y + 0.5, pos.Z + 0.5, null, true, 8, 0.5f);
                            }
                        }
                    }
                } //transfer slot
            }     //Output move.

            //if inventory above, attempt to move item from above into me.  (LATER ON: CHECK FILTER)
            if (api.World.BlockAccessor.GetBlockEntity(InputPosition) is BlockEntityContainer)
            {
                BlockEntityContainer inputBox = (BlockEntityContainer)api.World.BlockAccessor.GetBlockEntity(InputPosition);
                if (inputBox.Inventory is InventoryGeneric)
                {
                    InventoryGeneric inputInventory = (InventoryGeneric)inputBox.Inventory;
                    if (!inputInventory.IsEmpty)
                    {
                        ItemSlot transferSlot = null;
                        foreach (ItemSlot slot in inputInventory)
                        {
                            if (!slot.Empty)
                            {
                                transferSlot = slot;
                            }
                        }

                        if (transferSlot != null)
                        {
                            WeightedSlot ws = inventory.GetBestSuitedSlot(transferSlot);
                            if (ws.slot != null)
                            {
                                ItemStackMoveOperation op = new ItemStackMoveOperation(api.World, EnumMouseButton.Left, 0, EnumMergePriority.DirectMerge, FlowAmount);

                                if (transferSlot.TryPutInto(ws.slot, ref op) > 0)
                                {
                                    if (api.World.Rand.NextDouble() < 0.2)
                                    {
                                        api.World.PlaySoundAt(new AssetLocation("sounds/block/hoppertumble"), pos.X + 0.5, pos.Y + 0.5, pos.Z + 0.5, null, true, 8, 0.5f);
                                    }
                                }
                            }
                        } //transfer slot
                    }     //Inventory empty check
                }         //Inventory Generic check.
            }             //Check for Block entity container.
        }
Ejemplo n.º 3
0
        public bool OnBlockInteractStart(IPlayer byPlayer, BlockSelection blockSel)
        {
            bool put  = byPlayer.Entity.Controls.Sneak;
            bool take = !put;
            bool bulk = byPlayer.Entity.Controls.Sprint;

            ItemSlot ownSlot    = inventory.FirstNonEmptySlot;
            var      hotbarslot = byPlayer.InventoryManager.ActiveHotbarSlot;

            bool drawIconLabel = put && hotbarslot?.Itemstack?.ItemAttributes?["pigment"]?["color"].Exists == true && blockSel.SelectionBoxIndex == 1;

            if (drawIconLabel)
            {
                if (!inventory.Empty)
                {
                    JsonObject jobj = hotbarslot.Itemstack.ItemAttributes["pigment"]["color"];
                    int        r    = jobj["red"].AsInt();
                    int        g    = jobj["green"].AsInt();
                    int        b    = jobj["blue"].AsInt();

                    labelColor = ColorUtil.ToRgba(255, (int)GameMath.Clamp(r * 1.2f, 0, 255), (int)GameMath.Clamp(g * 1.2f, 0, 255), (int)GameMath.Clamp(b * 1.2f, 0, 255));
                    labelStack = inventory.FirstNonEmptySlot.Itemstack.Clone();
                    labelMesh  = null;

                    byPlayer.Entity.World.PlaySoundAt(new AssetLocation("sounds/player/chalkdraw"), blockSel.Position.X + blockSel.HitPosition.X, blockSel.Position.Y + blockSel.HitPosition.Y, blockSel.Position.Z + blockSel.HitPosition.Z, byPlayer, true, 8);

                    MarkDirty(true);
                }
                else
                {
                    (Api as ICoreClientAPI)?.TriggerIngameError(this, "empty", Lang.Get("Can't draw item symbol on an empty crate. Put something inside the crate first"));
                }

                return(true);
            }

            if (take && ownSlot != null)
            {
                ItemStack stack = bulk ? ownSlot.TakeOutWhole() : ownSlot.TakeOut(1);
                if (!byPlayer.InventoryManager.TryGiveItemstack(stack, true))
                {
                    Api.World.SpawnItemEntity(stack, Pos.ToVec3d().Add(0.5f + blockSel.Face.Normalf.X, 0.5f + blockSel.Face.Normalf.Y, 0.5f + blockSel.Face.Normalf.Z));
                }
                else
                {
                    didMoveItems(stack, byPlayer);
                }

                if (inventory.Empty)
                {
                    labelMesh = null;
                }

                ownSlot.MarkDirty();
                MarkDirty();
            }

            if (put && !hotbarslot.Empty)
            {
                if (ownSlot == null)
                {
                    if (hotbarslot.TryPutInto(Api.World, inventory[0], bulk ? hotbarslot.StackSize : 1) > 0)
                    {
                        didMoveItems(inventory[0].Itemstack, byPlayer);
                    }
                }
                else
                {
                    if (hotbarslot.Itemstack.Equals(Api.World, ownSlot.Itemstack, GlobalConstants.IgnoredStackAttributes))
                    {
                        List <ItemSlot> skipSlots = new List <ItemSlot>();
                        while (hotbarslot.StackSize > 0 && skipSlots.Count < inventory.Count)
                        {
                            var wslot = inventory.GetBestSuitedSlot(hotbarslot, skipSlots);
                            if (wslot.slot == null)
                            {
                                break;
                            }

                            if (hotbarslot.TryPutInto(Api.World, wslot.slot, bulk ? hotbarslot.StackSize : 1) > 0)
                            {
                                didMoveItems(wslot.slot.Itemstack, byPlayer);
                                if (!bulk)
                                {
                                    break;
                                }
                            }

                            skipSlots.Add(wslot.slot);
                        }
                    }
                }

                hotbarslot.MarkDirty();
                MarkDirty();
            }


            return(true);
        }