public string Execute(bool isEnabled = true, [CurrentCharacterIfNull] ICharacter character = null)
        {
            if (isEnabled)
            {
                CharacterInvincibilitySystem.ServerAdd(character);
            }
            else
            {
                CharacterInvincibilitySystem.ServerRemove(character);
            }

            return($"{character} invincibility mode is {(isEnabled ? "on" : "off")}");
        }
        /// <summary>
        /// Set health - it will be clamped automatically.
        /// </summary>
        public void ServerSetHealthCurrent(float health, bool overrideDeath = false)
        {
            this.SharedTryRefreshFinalCache();

            var character            = (ICharacter)this.GameObject;
            var characterPublicState = character.GetPublicState <ICharacterPublicState>();

            if (characterPublicState.IsDead &&
                !overrideDeath)
            {
                // cannot change health this way for the dead character
                return;
            }

            health = MathHelper.Clamp(health, min: 0, max: this.HealthMax);
            if (this.HealthCurrent == health &&
                !overrideDeath)
            {
                return;
            }

            if (health < this.HealthCurrent)
            {
                if (!character.IsNpc &&
                    CharacterInvincibilitySystem.ServerIsInvincible(character))
                {
                    // don't apply damage - character is invincible
                    //Api.Logger.Important(
                    //    $"Cannot reduce character health - {character}: character is in invincible mode");
                    health = MathHelper.Clamp(this.HealthCurrent, min: 0, max: this.HealthMax);
                }
            }

            this.HealthCurrent = health;

            if (health <= 0)
            {
                characterPublicState.IsDead = true;
                Api.Logger.Important("Character dead: " + character);
                ServerCharacterDeathMechanic.OnCharacterDeath(character);
            }
            else if (characterPublicState.IsDead)
            {
                characterPublicState.IsDead = false;
                Api.Logger.Important("Character is not dead anymore: " + character);
            }
        }
Пример #3
0
        public string Execute()
        {
            var player = this.ExecutionContextCurrentCharacter;

            if (player == null)
            {
                return("This command cannot be executed directly from the server console");
            }

            var stats = player.GetPublicState <PlayerCharacterPublicState>()
                        .CurrentStatsExtended;

            if (stats.HealthCurrent <= 0)
            {
                return("You're already dead.");
            }

            // ensure character is not invincible
            CharacterInvincibilitySystem.ServerRemove(player);

            if (!ServerOperatorSystem.SharedIsOperator(player))
            {
                // check cooldown
                var time = Server.Game.FrameTime;
                if (this.characterLastKillTime.TryGetValue(player, out var lastKillTime) &&
                    time < lastKillTime + CooldownSeconds)
                {
                    return(string.Format(
                               "You've already killed yourself recently... please wait for cooldown: {0} remaining.",
                               ClientTimeFormatHelper.FormatTimeDuration(lastKillTime + CooldownSeconds - time)));
                }

                this.characterLastKillTime[player] = time;
            }

            stats.ServerSetHealthCurrent(0);
            return("You've killed yourself.");
        }