public void RemoveProtection(TSPlayer player, DPoint tileLocation, bool checkIfBlockTypeDeprotectableByConfig = true)
        {
            Contract.Requires <ArgumentNullException>(player != null);

            bool canDeprotectEverything = player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission);

            if (TerrariaUtils.Tiles.IsValidCoord(tileLocation))
            {
                Tile tile = TerrariaUtils.Tiles[tileLocation];
                if (tile.active())
                {
                    if (!canDeprotectEverything && checkIfBlockTypeDeprotectableByConfig && this.Config.NotDeprotectableTiles[tile.type])
                    {
                        throw new InvalidBlockTypeException((BlockType)tile.type);
                    }

                    tileLocation = TerrariaUtils.Tiles.MeasureObject(tileLocation).OriginTileLocation;
                }
            }

            ProtectionEntry protection;

            lock (this.WorldMetadata.Protections)
                if (!this.WorldMetadata.Protections.TryGetValue(tileLocation, out protection))
                {
                    throw new NoProtectionException(tileLocation);
                }

            if (!canDeprotectEverything && protection.Owner != player.User.ID)
            {
                throw new TileProtectedException(tileLocation);
            }

            if (protection.BankChestKey != BankChestDataKey.Invalid)
            {
                IChest chest = this.ChestManager.ChestFromLocation(tileLocation);
                if (chest != null)
                {
                    for (int i = 0; i < Chest.maxItems; i++)
                    {
                        chest.SetItem(i, ItemData.None);
                    }
                }
            }

            lock (this.WorldMetadata.Protections)
                this.WorldMetadata.Protections.Remove(tileLocation);
        }
Example #2
0
        public bool TryRefillChest(IChest chest, RefillChestMetadata refillChestData)
        {
            for (int i = 0; i < Chest.maxItems; i++)
            {
                chest.SetItem(i, refillChestData.RefillItems[i]);
            }

            if (
                refillChestData.AutoLock && refillChestData.RefillTime != TimeSpan.Zero &&
                !TerrariaUtils.Tiles.IsChestLocked(TerrariaUtils.Tiles[chest.Location])
                )
            {
                TerrariaUtils.Tiles.LockChest(chest.Location);
            }

            return(true);
        }
Example #3
0
        /// <returns>
        ///   A <c>bool</c> which is <c>false</c> if a refill chest already existed at the given location and thus just its refill
        ///   time was set or <c>true</c> if a new refill chest was actually defined.
        /// </returns>
        public bool SetUpRefillChest(
            TSPlayer player, DPoint tileLocation, TimeSpan?refillTime, bool?oneLootPerPlayer = null, int?lootLimit = null,
            bool?autoLock = null, bool?autoEmpty = null, bool fairLoot = false, bool checkPermissions = false
            )
        {
            Contract.Requires <ArgumentNullException>(player != null);
            Contract.Requires <ArgumentException>(TerrariaUtils.Tiles[tileLocation] != null, "tileLocation");
            Contract.Requires <ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
            Contract.Requires <ArgumentOutOfRangeException>(lootLimit == null || lootLimit >= -1);

            Tile      tile      = TerrariaUtils.Tiles[tileLocation];
            BlockType blockType = (BlockType)tile.type;

            if (blockType != BlockType.Chest && blockType != BlockType.Dresser)
            {
                throw new InvalidBlockTypeException(blockType);
            }

            if (checkPermissions && !player.Group.HasPermission(ProtectorPlugin.SetRefillChests_Permission))
            {
                throw new MissingPermissionException(ProtectorPlugin.SetRefillChests_Permission);
            }

            DPoint          chestLocation = TerrariaUtils.Tiles.MeasureObject(tileLocation).OriginTileLocation;
            ProtectionEntry protection;

            lock (this.WorldMetadata.Protections)
                if (!this.WorldMetadata.Protections.TryGetValue(chestLocation, out protection))
                {
                    throw new NoProtectionException(chestLocation);
                }

            if (protection.BankChestKey != BankChestDataKey.Invalid)
            {
                throw new ChestIncompatibilityException();
            }

            RefillChestMetadata refillChestData;

            if (protection.RefillChestData != null)
            {
                refillChestData = protection.RefillChestData;

                if (refillTime != null)
                {
                    refillChestData.RefillTimer.TimeSpan = refillTime.Value;
                }
                if (oneLootPerPlayer != null)
                {
                    refillChestData.OneLootPerPlayer = oneLootPerPlayer.Value;
                }
                if (lootLimit != null)
                {
                    refillChestData.RemainingLoots = lootLimit.Value;
                }
                if (autoLock != null)
                {
                    refillChestData.AutoLock = autoLock.Value;
                }
                if (autoEmpty != null)
                {
                    refillChestData.AutoEmpty = autoEmpty.Value;
                }

                if (refillChestData.OneLootPerPlayer || refillChestData.RemainingLoots > 0)
                {
                    if (refillChestData.Looters == null)
                    {
                        refillChestData.Looters = new Collection <int>();
                    }
                    else
                    {
                        refillChestData.Looters = null;
                    }
                }

                this.RefillTimers.RemoveTimer(refillChestData.RefillTimer);

                return(false);
            }

            IChest chest = this.ChestFromLocation(chestLocation);

            if (chest == null)
            {
                throw new NoChestDataException(chestLocation);
            }

            TimeSpan actualRefillTime = TimeSpan.Zero;

            if (refillTime != null)
            {
                actualRefillTime = refillTime.Value;
            }

            refillChestData             = new RefillChestMetadata(player.User.ID);
            refillChestData.RefillTimer = new Timer(actualRefillTime, refillChestData, this.RefillTimerCallbackHandler);
            if (oneLootPerPlayer != null)
            {
                refillChestData.OneLootPerPlayer = oneLootPerPlayer.Value;
            }
            if (lootLimit != null)
            {
                refillChestData.RemainingLoots = lootLimit.Value;
            }

            if (refillChestData.OneLootPerPlayer || refillChestData.RemainingLoots > 0)
            {
                refillChestData.Looters = new Collection <int>();
            }
            else
            {
                refillChestData.Looters = null;
            }

            if (autoLock != null)
            {
                refillChestData.AutoLock = autoLock.Value;
            }

            if (autoEmpty != null)
            {
                refillChestData.AutoEmpty = autoEmpty.Value;
            }

            bool fairLootPutItem = fairLoot;

            for (int i = 0; i < Chest.maxItems; i++)
            {
                ItemData item = chest[i];
                if (item.StackSize == 0 && fairLootPutItem)
                {
                    try {
                        bool isLocked;
                        item.Type = TerrariaUtils.Tiles.GetItemTypeFromChestType(TerrariaUtils.Tiles.GetChestStyle(tile, out isLocked));

                        item.StackSize = 1;
                        chest.SetItem(i, item);
                    } catch (ArgumentException) {}

                    fairLootPutItem = false;
                }

                refillChestData.RefillItems[i] = item;
            }

            protection.RefillChestData = refillChestData;

            return(true);
        }
Example #4
0
        public bool EnsureBankChest(ProtectionEntry protection, bool resetContent)
        {
            if (protection.BankChestKey == BankChestDataKey.Invalid)
            {
                return(false);
            }

            BankChestMetadata bankChest = this.ServerMetadataHandler.EnqueueGetBankChestMetadata(protection.BankChestKey).Result;

            if (bankChest == null)
            {
                protection.BankChestKey = BankChestDataKey.Invalid;
                return(false);
            }

            IChest chest = this.ChestFromLocation(protection.TileLocation);

            if (chest == null)
            {
                protection.BankChestKey = BankChestDataKey.Invalid;
                return(false);
            }

            User owner = TShock.Users.GetUserByID(protection.Owner);

            if (owner == null)
            {
                this.DestroyChest(chest);

                protection.BankChestKey = BankChestDataKey.Invalid;
                return(false);
            }

            Group ownerGroup = TShock.Groups.GetGroupByName(owner.Group);

            if (ownerGroup != null)
            {
                if (protection.SharedUsers != null && !ownerGroup.HasPermission(ProtectorPlugin.BankChestShare_Permission))
                {
                    protection.SharedUsers.Clear();
                }
                if (protection.SharedGroups != null && !ownerGroup.HasPermission(ProtectorPlugin.BankChestShare_Permission))
                {
                    protection.SharedGroups.Clear();
                }
                if (protection.IsSharedWithEveryone && !ownerGroup.HasPermission(ProtectorPlugin.BankChestShare_Permission))
                {
                    protection.IsSharedWithEveryone = false;
                }
            }

            if (resetContent)
            {
                for (int i = 0; i < Chest.maxItems; i++)
                {
                    chest.SetItem(i, bankChest.Items[i]);
                }
            }

            return(true);
        }
Example #5
0
        public void SetUpBankChest(TSPlayer player, DPoint tileLocation, int bankChestIndex, bool checkPermissions = false)
        {
            Contract.Requires <ArgumentNullException>(player != null);
            Contract.Requires <ArgumentException>(TerrariaUtils.Tiles[tileLocation] != null, "tileLocation");
            Contract.Requires <ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
            Contract.Requires <ArgumentOutOfRangeException>(bankChestIndex >= 1, "bankChestIndex");

            Tile      tile      = TerrariaUtils.Tiles[tileLocation];
            BlockType blockType = (BlockType)tile.type;

            if (blockType != BlockType.Chest && blockType != BlockType.Dresser)
            {
                throw new InvalidBlockTypeException(blockType);
            }

            if (checkPermissions && !player.Group.HasPermission(ProtectorPlugin.SetBankChests_Permission))
            {
                throw new MissingPermissionException(ProtectorPlugin.SetBankChests_Permission);
            }

            if (
                checkPermissions && !player.Group.HasPermission(ProtectorPlugin.NoBankChestLimits_Permision)
                )
            {
                if (bankChestIndex > this.Config.MaxBankChestsPerPlayer)
                {
                    throw new ArgumentOutOfRangeException("bankChestIndex", this.Config.MaxBankChestsPerPlayer, "Global bank chest limit reached.");
                }

                int byGroupLimit;
                if (
                    this.Config.MaxBankChests.TryGetValue(player.Group.Name, out byGroupLimit) &&
                    bankChestIndex > byGroupLimit
                    )
                {
                    throw new ArgumentOutOfRangeException("bankChestIndex", byGroupLimit, "Group bank chest limit reached.");
                }
            }

            DPoint          chestLocation = TerrariaUtils.Tiles.MeasureObject(tileLocation).OriginTileLocation;
            ProtectionEntry protection;

            lock (this.WorldMetadata.Protections)
                if (!this.WorldMetadata.Protections.TryGetValue(chestLocation, out protection))
                {
                    throw new NoProtectionException(chestLocation);
                }

            if (protection.RefillChestData != null)
            {
                throw new ChestIncompatibilityException();
            }

            IChest chest = this.ChestFromLocation(chestLocation);

            if (chest == null)
            {
                throw new NoChestDataException(chestLocation);
            }

            if (protection.BankChestKey != BankChestDataKey.Invalid)
            {
                throw new ChestTypeAlreadyDefinedException();
            }

            BankChestDataKey bankChestKey = new BankChestDataKey(player.User.ID, bankChestIndex);

            lock (this.WorldMetadata.Protections) {
                if (this.WorldMetadata.Protections.Values.Count(p => p.BankChestKey == bankChestKey) > 0)
                {
                    throw new BankChestAlreadyInstancedException();
                }
            }

            if (checkPermissions && !player.Group.HasPermission(ProtectorPlugin.BankChestShare_Permission))
            {
                protection.Unshare();
            }

            BankChestMetadata bankChest = this.ServerMetadataHandler.EnqueueGetBankChestMetadata(bankChestKey).Result;

            if (bankChest == null)
            {
                bankChest = new BankChestMetadata();
                for (int i = 0; i < Chest.maxItems; i++)
                {
                    bankChest.Items[i] = chest[i];
                }

                this.ServerMetadataHandler.EnqueueAddOrUpdateBankChest(bankChestKey, bankChest);
            }
            else
            {
                for (int i = 0; i < Chest.maxItems; i++)
                {
                    if (chest[i].StackSize > 0)
                    {
                        throw new ChestNotEmptyException(chestLocation);
                    }
                }

                for (int i = 0; i < Chest.maxItems; i++)
                {
                    chest.SetItem(i, bankChest.Items[i]);
                }
            }

            protection.BankChestKey = bankChestKey;
        }