コード例 #1
0
        // Left click to withdraw
        public InteractResult OnActLeft(InteractionContext context)
        {
            // Some tools can't pull from containers.
            if (context.SelectedItem != null)
            {
                var canPull = ItemAttribute.Get <PullFromChest>(context.SelectedItem.Type)?.CanPull ?? true;
                if (!canPull)
                {
                    return(InteractResult.NoOp);
                }
            }

            ToolItem        selectedTool = context.SelectedItem as ToolItem;
            ValResult <int> result       = this.Inventory.MoveAsManyItemsAsPossible(context.Player.User.Inventory.Carried, itemStack =>
            {
                var requiresTool = ItemAttribute.Get <RequiresToolAttribute>(itemStack.Item.Type);
                return(requiresTool?.IsValidTool(context.SelectedItem?.Type) ?? true);
            }, context.Player.User);

            if (result.Success)
            {
                if (result.Val > 0)
                {
                    return(InteractResult.Success);
                }
                else
                {
                    return(InteractResult.NoOp);
                }
            }
            else
            {
                return(InteractResult.Failure(result.Message));
            }
        }
コード例 #2
0
ファイル: TreeObject.cs プロジェクト: Talindor2/eco_mod
    private bool TryDamageBranch(INetObject damager, float amount, InteractionContext context)
    {
        int        branchID = context.Parameters["branch"];
        TreeBranch branch   = this.branches[branchID];

        if (context.Parameters.ContainsKey("leaf"))
        {
            int leafID = context.Parameters["leaf"];

            // damage leaf
            LeafBunch leaf = branch.Leaves[leafID];

            if (leaf.Health > 0)
            {
                leaf.Health = Mathf.Max(0, leaf.Health - amount);

                if (leaf.Health <= 0)
                {
                    leaf.Health = 0;

                    if (RandomUtil.Value < this.Species.SeedDropChance)
                    {
                        var    numSeeds      = (int)this.Species.SeedRange.Max;
                        int    numBonusSeeds = 0;
                        Item[] newSeeds      = new Item[] { };
                        if (numSeeds > 0 && this.Species.SeedItem != null)
                        {
                            var yield = ItemAttribute.Get <YieldAttribute>(this.Species.SeedItem.Type);
                            numBonusSeeds = yield != null?yield.Yield.GetCurrentValueInt(context.Player.User) : 0;

                            context.Player.User.Inventory.TryAddItems(this.Species.SeedItem.Type, numSeeds + numBonusSeeds);
                        }
                    }
                    this.RPC("DestroyLeaves", branchID, leafID);
                }
            }

            this.Save();
            return(true);
        }
        else
        {
            return(this.TryDamageBranch(branch, branchID, amount));
        }
    }
コード例 #3
0
        public static void PostInitialize()
        {
            var categoryToTags = TagAttribute.CategoryToTags ?? new Dictionary <string, string[]>();
            var tiers          = new HashSet <float> {
                0
            };

            foreach (var item in Item.AllItems)
            {
                if (item.Hidden)
                {
                    continue;
                }
                var itemTier = ItemAttribute.Get <ItemTier>(item.Type);
                if (itemTier != null)
                {
                    tiers.Add(itemTier.Tier);
                }
            }

            categoryToTags["Tiers"]     = tiers.OrderBy(x => x).Select(x => string.Format("Tier {0}", x)).ToArray();
            TagAttribute.CategoryToTags = categoryToTags;
        }
コード例 #4
0
    private bool TryDamageBranch(INetObject damager, float amount, InteractionContext context)
    {
        int        branchID = context.Parameters["branch"];
        TreeBranch branch   = this.branches[branchID];

        if (context.Parameters.ContainsKey("leaf"))
        {
            int leafID = context.Parameters["leaf"];

            // damage leaf
            LeafBunch leaf = branch.Leaves[leafID];

            if (leaf.Health > 0)
            {
                List <IAtomicAction> actions = new List <IAtomicAction>();
                if (damager is Player)
                {
                    var action = PlayerActions.HarvestLeaves.CreateAtomicAction((Player)damager, this);
                    actions.Add(action);

                    if (!PlayerActions.HarvestLeaves.CreateAtomicAction((Player)damager, this).CanApply().Notify((Player)damager))
                    {
                        // We only want to dispose the action if it is invalid.  Othewise we want to keep it around to possibly apply later.
                        action.Dispose();
                        return(false);
                    }
                }

                leaf.Health = Mathf.Max(0, leaf.Health - amount);

                if (leaf.Health <= 0)
                {
                    if (!new MultiAtomicAction(actions).TryApply().Success)
                    {
                        throw new Exception("Removing this stump was verified to be legal a moment ago, but is not anymore.");
                    }

                    leaf.Health = 0;

                    if (RandomUtil.Value < this.Species.SeedDropChance)
                    {
                        var    numSeeds      = (int)this.Species.SeedRange.Max;
                        int    numBonusSeeds = 0;
                        Item[] newSeeds      = new Item[] { };
                        if (numSeeds > 0 && this.Species.SeedItem.Type != null)
                        {
                            var yield = ItemAttribute.Get <YieldAttribute>(this.Species.SeedItem.Type);
                            numBonusSeeds = yield != null?yield.Yield.GetCurrentValueInt(context.Player.User) : 0;

                            context.Player.User.Inventory.TryAddItems(this.Species.SeedItem.Type, numSeeds + numBonusSeeds);
                        }
                    }
                    this.RPC("DestroyLeaves", branchID, leafID);
                }
                else
                {
                    new MultiAtomicAction(actions).Dispose();
                }
            }

            this.Save();
            return(true);
        }
        else
        {
            return(this.TryDamageBranch(branch, branchID, amount));
        }
    }