Ejemplo n.º 1
0
        private CommandResult SkillCommand(ICommandSource src, ICommandArgs args)
        {
            // skill [skill] [value]
            if (args.Length == 2)
            {
                if (src.IsConsole)
                {
                    return(CommandResult.ShowUsage());
                }

                if (!USkill.FromName(args[0].ToString(), out var skill))
                {
                    return(CommandResult.LangError("INVALID_SKILL", args[0]));
                }

                var  player = src.ToPlayer();
                byte value;

                if (args[1].ToLowerString == "max")
                {
                    value = player.GetSkill(skill).max;
                }
                else if (!args[1].TryConvertToByte(out value, out var error))
        private void GenericPlayerDeath(UnturnedPlayer rocketPlayer, EDeathCause cause, ELimb limb, CSteamID murderer)
        {
            const string METADATA_KEY    = "KEEP_SKILL";
            const string KEEP_SKILL_PERM = "essentials.keepskill.";

            var player        = UPlayer.From(rocketPlayer);
            var allPercentage = -1; // essentials.keepskill.all.<percentage>

            // Save skills in metadata, that will be restored when player respawn.
            // Format: Skill -> NewValue
            // Get or instantiate new Dictionary
            Dictionary <USkill, byte> skillsToRestore;

            if (player.Metadata.Has(METADATA_KEY))
            {
                skillsToRestore = player.Metadata.Get <Dictionary <USkill, byte> >(METADATA_KEY);
            }
            else
            {
                skillsToRestore = new Dictionary <USkill, byte>();
                player.Metadata[METADATA_KEY] = skillsToRestore;
            }

            // Parse keepskill permissions
            foreach (var perm in player.Permissions.Where(perm => perm.StartsWith(KEEP_SKILL_PERM)))
            {
                var kind           = perm.Substring(KEEP_SKILL_PERM.Length);
                var keepPercentage = 100;

                if (string.IsNullOrEmpty(kind))
                {
                    continue;
                }

                // Parse percentage, if present.
                // e.g 'essentials.keepskill.cardio.25' -> keepPercentage = 25
                if (kind.IndexOf('.') >= 0)
                {
                    // Split 'skill.percentage'
                    var parts = kind.Split('.');
                    if (!int.TryParse(parts[1], out keepPercentage))
                    {
                        continue;
                    }
                    // Percentage must be between 0-100
                    if (keepPercentage < 0)
                    {
                        keepPercentage = 0;
                    }
                    if (keepPercentage > 100)
                    {
                        keepPercentage = 100;
                    }
                    kind = parts[0]; // let only skill name
                }

                if (kind.EqualsIgnoreCase("all"))
                {
                    allPercentage = keepPercentage;
                    continue;
                }

                // Parse skill from name
                var fromName = USkill.FromName(kind);
                if (fromName.IsAbsent)
                {
                    continue;
                }
                skillsToRestore[fromName.Value] = (byte)Math.Ceiling(player.GetSkillLevel(fromName.Value) * (keepPercentage / 100.0));
            }

            // All Skills
            if (allPercentage != -1)
            {
                foreach (var skill in USkill.Skills)
                {
                    // We wanna not change previously added skills.
                    // This will allow to set a separated percentage while using modifier 'all' (essentials.keepskill.all)
                    // e.g
                    // essentials.keepskill.all.50
                    // essentials.keepskill.cardio.100
                    // this will keep 50% of all skills and 100% of cardio skill
                    if (skillsToRestore.ContainsKey(skill))
                    {
                        continue;
                    }
                    var newValue = Math.Ceiling(player.GetSkillLevel(skill) * (allPercentage / 100.0));
                    skillsToRestore[skill] = (byte)newValue;
                }
            }
        }
Ejemplo n.º 3
0
        private CommandResult SkillCommand(ICommandSource src, ICommandArgs args)
        {
            switch (args.Length)
            {
            // /skill [skill] [value]
            case 2:
                if (src.IsConsole)
                {
                    return(CommandResult.ShowUsage());
                }

                var optSkill = USkill.FromName(args[0].ToString());

                if (optSkill.IsAbsent)
                {
                    return(CommandResult.Lang("INVALID_SKILL", args[0]));
                }

                var  player = src.ToPlayer();
                byte value;

                if (args[1].Equals("max"))
                {
                    value = player.GetSkill(optSkill.Value).max;
                }
                else if (args[1].IsInt)
                {
                    if (args[1].ToInt <0 || args[1].ToInt> byte.MaxValue)
                    {
                        return(CommandResult.Lang("NEGATIVE_OR_LARGE"));
                    }

                    value = (byte)args[1].ToInt;
                }
                else
                {
                    return(CommandResult.Lang("INVALID_NUMBER", args[1]));
                }

                player.SetSkillLevel(optSkill.Value, value);
                EssLang.Send(src, "SKILL_SET", optSkill.Value.Name.Capitalize(), args[1]);
                break;

            // /skill [player|*] [skill] [value]
            case 3:
                if (args[0].Equals("*"))
                {
                    if (!UServer.Players.Any())
                    {
                        return(CommandResult.Lang("ANYONE_ONLINE"));
                    }

                    player = UServer.Players.First();
                }
                else
                {
                    if (!args[0].IsValidPlayerIdentifier)
                    {
                        return(CommandResult.Lang("PLAYER_NOT_FOUND", args[0]));
                    }

                    player = args[0].ToPlayer;
                }

                optSkill = USkill.FromName(args[1].ToString());

                if (optSkill.IsAbsent)
                {
                    return(CommandResult.Lang("INVALID_SKILL", args[0]));
                }

                if (args[2].Equals("max"))
                {
                    value = player.GetSkill(optSkill.Value).max;
                }
                else if (args[2].IsInt)
                {
                    if (args[2].ToInt <0 || args[2].ToInt> byte.MaxValue)
                    {
                        return(CommandResult.Lang("NEGATIVE_OR_LARGE"));
                    }

                    value = (byte)args[2].ToInt;
                }
                else
                {
                    return(CommandResult.Lang("INVALID_NUMBER", args[2]));
                }

                if (args[0].Equals("*"))
                {
                    UServer.Players.ForEach(p => p.SetSkillLevel(optSkill.Value, value));
                    EssLang.Send(src, "SKILL_SET_ALL", optSkill.Value.Name.Capitalize(), args[2]);
                }
                else
                {
                    player.SetSkillLevel(optSkill.Value, value);
                    EssLang.Send(src, "SKILL_SET_PLAYER", optSkill.Value.Name.Capitalize(),
                                 player.CharacterName, args[2]);
                }
                break;

            default:
                return(CommandResult.ShowUsage());
            }
            return(CommandResult.Success());
        }
Ejemplo n.º 4
0
        private void GenericPlayerDeath(UnturnedPlayer rocketPlayer, EDeathCause c, ELimb l, CSteamID k)
        {
            const string METADATA_KEY    = "KEEP_SKILL";
            const string KEEP_SKILL_PERM = "essentials.keepskill.";

            var player        = UPlayer.From(rocketPlayer);
            var allPercentage = -1; // essentials.keepskill.all.<percentage>

            // Save skills in metadata, it will be restored when player respawn.
            // Format: Skill -> NewValue
            // Get or instantiate new Dictionary
            Dictionary <USkill, byte> skillsToRestore;

            if (player.Metadata.Has(METADATA_KEY))
            {
                skillsToRestore = player.Metadata.Get <Dictionary <USkill, byte> >(METADATA_KEY);
            }
            else
            {
                skillsToRestore = new Dictionary <USkill, byte>();
                player.Metadata[METADATA_KEY] = skillsToRestore;
            }

            // Parse keepskill permissions
            // TODO: We should cache this. We need to find a way to detect when permissions change
            // and then re-compute this.
            foreach (var perm in player.Permissions.Where(perm => perm.StartsWith(KEEP_SKILL_PERM)))
            {
                var kind             = perm.Substring(KEEP_SKILL_PERM.Length);
                var percentageToKeep = 100;

                if (string.IsNullOrEmpty(kind))
                {
                    continue;
                }

                // Parse percentage, if present.
                // e.g 'essentials.keepskill.cardio.25' -> keepPercentage = 25
                if (kind.IndexOf('.') >= 0)
                {
                    // Split 'skill.percentage'
                    var parts = kind.Split('.');
                    if (!int.TryParse(parts[1], out percentageToKeep))
                    {
                        continue;
                    }
                    // Percentage must be between 0-100
                    if (percentageToKeep < 0)
                    {
                        percentageToKeep = 0;
                    }
                    if (percentageToKeep > 100)
                    {
                        percentageToKeep = 100;
                    }
                    kind = parts[0]; // let only skill name
                }

                if (kind.EqualsIgnoreCase("all"))
                {
                    allPercentage = percentageToKeep;
                    continue;
                }

                // Parse skill from name
                if (!USkill.FromName(kind, out var skill))
                {
                    continue;
                }
                skillsToRestore[skill] = (byte)System.Math.Ceiling(player.GetSkillLevel(skill) * (percentageToKeep / 100.0));
            }

            // All Skills
            if (allPercentage != -1)
            {
                foreach (var skill in USkill.Skills)
                {
                    // We don't want change previously added (skillsToRestore) skills.
                    // This will allow to set a separated percentage while using modifier 'all' (essentials.keepskill.all)
                    // e.g
                    // essentials.keepskill.all.50
                    // essentials.keepskill.cardio.100
                    // this will keep 50% of all skills and 100% of cardio skill
                    if (skillsToRestore.ContainsKey(skill))
                    {
                        continue;
                    }
                    skillsToRestore[skill] = (byte)System.Math.Ceiling(player.GetSkillLevel(skill) * (allPercentage / 100.0));
                }
            }
        }