Exemplo n.º 1
0
        private void RefillSlotIfEmpty(ItemSlot slot, EntityAgent byEntity)
        {
            if (!slot.Empty)
            {
                return;
            }

            byEntity.WalkInventory((invslot) =>
            {
                if (invslot is ItemSlotCreative)
                {
                    return(true);
                }

                if (invslot.Itemstack != null && invslot.Itemstack.Collectible is ItemSpear)
                {
                    invslot.TryPutInto(byEntity.World, slot);
                    invslot.Inventory.PerformNotifySlot(invslot.Inventory.GetSlotId(invslot));
                    slot.Inventory.PerformNotifySlot(slot.Inventory.GetSlotId(slot));

                    return(false);
                }

                return(true);
            });
        }
Exemplo n.º 2
0
        public static int GetPlayerAssets(EntityAgent eagent)
        {
            int totalAssets = 0;

            eagent.WalkInventory((invslot) =>
            {
                if (invslot is ItemSlotCreative || !(invslot.Inventory is InventoryBasePlayer))
                {
                    return(true);
                }

                totalAssets += CurrencyValuePerItem(invslot) * invslot.StackSize;

                return(true);
            });

            return(totalAssets);
        }
Exemplo n.º 3
0
        ItemSlot GetNextAmmo(EntityAgent byEntity)
        {
            List <AssetLocation> locs = new List <AssetLocation>();

            if (Attributes.KeyExists("ammos"))
            {
                //foreach (string s in Attributes["ammos"].AsArray<string>())
                foreach (string s in Attributes["ammos"].AsStringArray()) // Yes, this is deprecated, but the other one compiles wrong.
                {
                    locs.Add(AssetLocation.Create(s, Code.Domain));
                }
            }
            else
            {
                locs.Add(AssetLocation.Create(Attributes["ammo"].AsString("arrow-*"), Code.Domain));
            }
            ItemSlot slot = null;

            byEntity.WalkInventory((invslot) =>
            {
                if (invslot is ItemSlotCreative)
                {
                    return(true);
                }

                foreach (AssetLocation loc in locs)
                {
                    if (invslot.Itemstack != null && WildcardUtil.Match(loc, invslot.Itemstack.Collectible.Code))
                    {
                        // api.Logger.Error("ammoSlot " + invslot.Itemstack.Collectible.Code);
                        slot = invslot;
                        return(false);
                    }
                }

                return(true);
            });

            return(slot);
        }
Exemplo n.º 4
0
        ItemSlot GetNextMunition(EntityAgent byEntity)
        {
            ItemSlot slot = null;

            byEntity.WalkInventory((invslot) =>
            {
                if (invslot is ItemSlotCreative)
                {
                    return(true);
                }

                if (invslot.Itemstack != null && invslot.Itemstack.Collectible is ItemStone)
                {
                    slot = invslot;
                    return(false);
                }

                return(true);
            });

            return(slot);
        }
Exemplo n.º 5
0
        ItemSlot GetNextArrow(EntityAgent byEntity)
        {
            ItemSlot slot = null;

            byEntity.WalkInventory((invslot) =>
            {
                if (invslot is ItemSlotCreative)
                {
                    return(true);
                }

                if (invslot.Itemstack != null && invslot.Itemstack.Collectible.Code.Path.StartsWith("arrow-"))
                {
                    slot = invslot;
                    return(false);
                }

                return(true);
            });

            return(slot);
        }
Exemplo n.º 6
0
        public static void DeductFromEntity(ICoreAPI api, EntityAgent eagent, int totalUnitsToDeduct)
        {
            SortedDictionary <int, List <ItemSlot> > moneys = new SortedDictionary <int, List <ItemSlot> >();

            eagent.WalkInventory((invslot) =>
            {
                if (invslot is ItemSlotCreative)
                {
                    return(true);
                }
                if (invslot.Itemstack == null || invslot.Itemstack.Collectible.Attributes == null)
                {
                    return(true);
                }

                int pieceValue = CurrencyValuePerItem(invslot);
                if (pieceValue != 0)
                {
                    List <ItemSlot> slots = null;
                    if (!moneys.TryGetValue(pieceValue, out slots))
                    {
                        slots = new List <ItemSlot>();
                    }

                    slots.Add(invslot);

                    moneys[pieceValue] = slots;
                }

                return(true);
            });

            foreach (var val in moneys.Reverse())
            {
                int pieceValue = val.Key;

                foreach (ItemSlot slot in val.Value)
                {
                    int removeUnits = Math.Min(pieceValue * slot.StackSize, totalUnitsToDeduct);

                    removeUnits = (removeUnits / pieceValue) * pieceValue;

                    slot.Itemstack.StackSize -= removeUnits / pieceValue;
                    if (slot.StackSize <= 0)
                    {
                        slot.Itemstack = null;
                    }
                    slot.MarkDirty();

                    totalUnitsToDeduct -= removeUnits;
                }

                if (totalUnitsToDeduct <= 0)
                {
                    break;
                }
            }


            // Maybe didn't have small moneys? Take a bigger piece....
            if (totalUnitsToDeduct > 0)
            {
                foreach (var val in moneys)
                {
                    int pieceValue = val.Key;

                    foreach (ItemSlot slot in val.Value)
                    {
                        int removeUnits = Math.Max(pieceValue, Math.Min(pieceValue * slot.StackSize, totalUnitsToDeduct));

                        removeUnits = (removeUnits / pieceValue) * pieceValue;

                        slot.Itemstack.StackSize -= removeUnits / pieceValue;

                        if (slot.StackSize <= 0)
                        {
                            slot.Itemstack = null;
                        }
                        slot.MarkDirty();

                        totalUnitsToDeduct -= removeUnits;
                    }

                    if (totalUnitsToDeduct <= 0)
                    {
                        break;
                    }
                }
            }

            // ...and return single value gears
            if (totalUnitsToDeduct < 0)
            {
                GiveOrDrop(eagent, new ItemStack(api.World.GetItem(new AssetLocation("gear-rusty"))), -totalUnitsToDeduct);
            }
        }