private void AddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            bool insertRequired;

            using (QueryResult reader = this.DbConnection.QueryReader(
                       "SELECT COUNT(UserId) AS Count FROM Protector_BankChests WHERE UserId = @0 AND ChestIndex = @1;",
                       key.UserId, key.BankChestIndex
                       )) {
                if (!reader.Read())
                {
                    throw new InvalidOperationException("Unexpected data were returned.");
                }

                insertRequired = (reader.Get <int>("Count") == 0);
            }

            if (insertRequired)
            {
                this.DbConnection.Query(
                    "INSERT INTO Protector_BankChests (UserId, ChestIndex, Content) VALUES (@0, @1, @2);",
                    key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
                    );
            }
            else
            {
                this.DbConnection.Query(
                    "UPDATE Protector_BankChests SET Content = @2 WHERE UserId = @0 AND ChestIndex = @1;",
                    key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
                    );
            }
        }
        public Task EnqueueAddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            Contract.Requires <ObjectDisposedException>(!this.IsDisposed);

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask(() => {
                    this.AddOrUpdateBankChest(key, bankChest);
                }));
            }
        }
        public Task EnqueueAddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            Contract.Requires<ObjectDisposedException>(!this.IsDisposed);

              lock (this.workQueueLock) {
            return this.WorkQueue.EnqueueTask(() => {
              this.AddOrUpdateBankChest(key, bankChest);
            });
              }
        }
        private void UpdateBankChestItem(BankChestDataKey key, int slotIndex, ItemData newItem)
        {
            BankChestMetadata bankChest = this.GetBankChestMetadata(key);

            if (bankChest == null)
            {
                throw new ArgumentException("No bank chest with the given key found.", "key");
            }

            bankChest.Items[slotIndex] = newItem;
            this.AddOrUpdateBankChest(key, bankChest);
        }
        public Task EnqueueAddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            if (base.IsDisposed)
            {
                throw new ObjectDisposedException(this.ToString());
            }

            lock (this.workQueueLock) {
                return(this.WorkQueue.EnqueueTask(() => {
                    this.AddOrUpdateBankChest(key, bankChest);
                }));
            }
        }
        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 (!this.CheckBlockAccess(player, chestLocation, true))
            throw new TileProtectedException(chestLocation);

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

              int tChestIndex = Chest.FindChest(chestLocation.X, chestLocation.Y);
              if (tChestIndex == -1 || Main.chest[tChestIndex] == 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();

              Chest tChest = Main.chest[tChestIndex];
              BankChestMetadata bankChest = this.ServerMetadataHandler.EnqueueGetBankChestMetadata(bankChestKey).Result;
              if (bankChest == null) {
            bankChest = new BankChestMetadata();
            for (int i = 0; i < Chest.maxItems; i++)
              bankChest.Items[i] = ItemData.FromItem(tChest.item[i]);

            this.ServerMetadataHandler.EnqueueAddOrUpdateBankChest(bankChestKey, bankChest);
              } else {
            for (int i = 0; i < tChest.item.Length; i++) {
              if (tChest.item[i].stack > 0)
            throw new ChestNotEmptyException(chestLocation);
            }

            for (int i = 0; i < Chest.maxItems; i++)
              tChest.item[i] = bankChest.Items[i].ToItem();
              }

              protection.BankChestKey = bankChestKey;
        }
Esempio n. 7
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);
        }
Esempio n. 8
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;
        }
Esempio n. 9
0
        public void EnsureProtectionData(
            out int invalidProtectionsCount, out int invalidRefillChestCount, out int invalidBankChestCount
            )
        {
            invalidRefillChestCount = 0;
            invalidBankChestCount   = 0;

            lock (this.WorldMetadata.Protections) {
                List <DPoint> invalidProtectionLocations = new List <DPoint>();

                foreach (KeyValuePair <DPoint, ProtectionEntry> protectionPair in this.WorldMetadata.Protections)
                {
                    DPoint          location   = protectionPair.Key;
                    ProtectionEntry protection = protectionPair.Value;
                    Tile            tile       = TerrariaUtils.Tiles[location];

                    if (!tile.active() || (BlockType)tile.type != protection.BlockType)
                    {
                        invalidProtectionLocations.Add(location);
                        continue;
                    }

                    if (protection.RefillChestData != null)
                    {
                        int tChestIndex = Chest.FindChest(location.X, location.Y);
                        if (!tile.active() || tile.type != (int)BlockType.Chest || tChestIndex == -1)
                        {
                            protection.RefillChestData = null;
                            invalidRefillChestCount++;
                            continue;
                        }

                        protection.RefillChestData.RefillTimer.Data     = protection.RefillChestData;
                        protection.RefillChestData.RefillTimer.Callback = this.RefillTimerCallbackHandler;
                        this.RefillTimers.ContinueTimer(protection.RefillChestData.RefillTimer);
                    }
                    if (protection.BankChestKey != BankChestDataKey.Invalid)
                    {
                        BankChestMetadata bankChest = this.ServerMetadataHandler.EnqueueGetBankChestMetadata(protection.BankChestKey).Result;
                        if (bankChest == null)
                        {
                            protection.BankChestKey = BankChestDataKey.Invalid;
                            invalidBankChestCount++;
                            continue;
                        }

                        int tChestIndex = Chest.FindChest(location.X, location.Y);
                        if (!tile.active() || tile.type != (int)BlockType.Chest || tChestIndex == -1)
                        {
                            protection.BankChestKey = BankChestDataKey.Invalid;
                            invalidBankChestCount++;
                            continue;
                        }

                        Chest tChest = Main.chest[tChestIndex];
                        for (int i = 0; i < Chest.maxItems; i++)
                        {
                            tChest.item[i] = bankChest.Items[i].ToItem();
                        }
                    }
                }

                foreach (DPoint invalidProtectionLocation in invalidProtectionLocations)
                {
                    this.WorldMetadata.Protections.Remove(invalidProtectionLocation);
                }


                invalidProtectionsCount = invalidProtectionLocations.Count;
            }
        }
        private void AddOrUpdateBankChest(BankChestDataKey key, BankChestMetadata bankChest)
        {
            bool insertRequired;
              using (QueryResult reader = this.DbConnection.QueryReader(
            "SELECT COUNT(UserId) AS Count FROM Protector_BankChests WHERE UserId = @0 AND ChestIndex = @1;",
            key.UserId, key.BankChestIndex
              )) {
            if (!reader.Read())
              throw new InvalidOperationException("Unexpected data were returned.");

            insertRequired = (reader.Get<int>("Count") == 0);
              }

              if (insertRequired) {
            this.DbConnection.Query(
              "INSERT INTO Protector_BankChests (UserId, ChestIndex, Content) VALUES (@0, @1, @2);",
              key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
            );
              } else {
            this.DbConnection.Query(
              "UPDATE Protector_BankChests SET Content = @2 WHERE UserId = @0 AND ChestIndex = @1;",
              key.UserId, key.BankChestIndex, this.ItemMetadataToString(bankChest.Items)
            );
              }
        }