public string Execute([CurrentCharacterIfNull] ICharacter player = null)
        {
            var allContainers = new AggregatedItemsContainers(
                player.SharedGetPlayerContainerInventory(),
                player.SharedGetPlayerContainerEquipment(),
                player.SharedGetPlayerContainerHotbar(),
                player.SharedGetPlayerContainerHand());

            foreach (var container in allContainers.ItemsContainers)
            {
                foreach (var item in container.Items.ToList())
                {
                    Server.Items.DestroyItem(item);
                }
            }

            return($"All items were removed from {player}.");
        }
        public string Execute(
            double durabilityPercent = 100,
            [CurrentCharacterIfNull] ICharacter character = null)
        {
            var durabilityFraction = MathHelper.Clamp(durabilityPercent / 100, min: 0, max: 1);
            var containers         = new AggregatedItemsContainers(character, includeEquipmentContainer: true);

            foreach (var container in containers)
            {
                foreach (var item in Api.Shared.WrapInTempList(container.Items))
                {
                    if (item.ProtoItem is IProtoItemWithDurablity protoItemWithDurablity)
                    {
                        var privateState = item.GetPrivateState <IItemWithDurabilityPrivateState>();
                        var durability   = protoItemWithDurablity.DurabilityMax * durabilityFraction;
                        privateState.DurabilityCurrent = (uint)Math.Round(durability, MidpointRounding.AwayFromZero);
                    }
                }
            }

            return($"Durability of all items modified to {durabilityFraction * 100}%");
        }
        protected override bool ServerIsCompleted(ICharacter character, PlayerTaskStateWithCount state)
        {
            var characterContainers = new AggregatedItemsContainers(character, includeEquipmentContainer: true);

            if (this.RequiredCount == 1)
            {
                // only one item is required
                foreach (var protoItem in this.List)
                {
                    if (characterContainers.ContainsItemsOfType(protoItem, requiredCount: 1))
                    {
                        // found at least one item of the required item type
                        state.SetCountCurrent(1, countMax: 1);
                        return(true);
                    }
                }

                state.SetCountCurrent(0, countMax: 1);
                return(false);
            }

            // more than a single item is required
            var availableCount = 0;

            foreach (var protoItem in this.List)
            {
                availableCount += characterContainers.CountItemsOfType(protoItem);
                if (availableCount >= this.RequiredCount)
                {
                    break;
                }
            }

            state.SetCountCurrent(this.IsReversible
                                      ? availableCount
                                      : Math.Max(state.CountCurrent, availableCount),
                                  this.RequiredCount);
            return(state.CountCurrent >= this.RequiredCount);
        }
Exemple #4
0
        public string Execute(
            double freshnessPercent = 100,
            [CurrentCharacterIfNull] ICharacter character = null)
        {
            var freshnessFraction = MathHelper.Clamp(freshnessPercent / 100, min: 0, max: 1);
            var containers        = new AggregatedItemsContainers(character, includeEquipmentContainer: true);

            foreach (var container in containers)
            {
                foreach (var item in Api.Shared.WrapInTempList(container.Items).EnumerateAndDispose())
                {
                    if (item.ProtoItem is IProtoItemWithFreshness protoItemWithFreshness &&
                        protoItemWithFreshness.FreshnessMaxValue > 0)
                    {
                        var privateState = item.GetPrivateState <IItemWithFreshnessPrivateState>();
                        var durability   = protoItemWithFreshness.FreshnessMaxValue * freshnessFraction;
                        privateState.FreshnessCurrent = (uint)Math.Round(durability, MidpointRounding.AwayFromZero);
                    }
                }
            }

            return($"Freshness of all items modified to {freshnessFraction * 100}%");
        }
        public string Execute(
            double chargePercent = 100,
            [CurrentCharacterIfNull] ICharacter character = null)
        {
            var chargeFraction = MathHelper.Clamp(chargePercent / 100, min: 0, max: 1);
            var containers     = new AggregatedItemsContainers(character, includeEquipmentContainer: true);

            foreach (var container in containers)
            {
                foreach (var item in Api.Shared.WrapInTempList(container.Items))
                {
                    switch (item.ProtoItem)
                    {
                    case IProtoItemPowerBank protoItemPowerBank:
                    {
                        var itemPrivateState = item.GetPrivateState <ItemPowerBankPrivateState>();
                        var capacity         = protoItemPowerBank.EnergyCapacity;
                        itemPrivateState.EnergyCharge = capacity * chargeFraction;
                        break;
                    }

                    case IProtoItemWithFuel protoItemWithFuel:
                    {
                        var privateState = item.GetPrivateState <ItemWithFuelPrivateState>();
                        var fuel         = protoItemWithFuel.ItemFuelConfig.FuelCapacity * chargeFraction;
                        privateState.FuelAmount = (uint)Math.Round(fuel, MidpointRounding.AwayFromZero);
                        protoItemWithFuel.ItemFuelConfig.SharedOnRefilled(item,
                                                                          privateState.FuelAmount,
                                                                          serverNotifyClients: true);
                        break;
                    }
                    }
                }
            }

            return($"Charge/fuel amount of all items modified to {chargeFraction * 100}%");
        }