public bool TryRefillChest(DPoint chestLocation, RefillChestMetadata refillChestData) { int tChestIndex = Chest.FindChest(chestLocation.X, chestLocation.Y); if (tChestIndex == -1) { return(false); } Chest tChest = Main.chest[tChestIndex]; if (tChest == null) { return(false); } for (int i = 0; i < Chest.maxItems; i++) { tChest.item[i] = refillChestData.RefillItems[i].ToItem(); } if ( refillChestData.AutoLock && refillChestData.RefillTime != TimeSpan.Zero && !TerrariaUtils.Tiles.IsChestLocked(TerrariaUtils.Tiles[chestLocation]) ) { TerrariaUtils.Tiles.LockChest(chestLocation); } return(true); }
public bool TryRefillChest(DPoint chestLocation, RefillChestMetadata refillChestData) { IChest chest = this.ChestFromLocation(chestLocation); if (chest == null) { throw new InvalidOperationException("No chest data found at lcoation."); } return(this.TryRefillChest(chest, refillChestData)); }
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); }
private bool RefillChestTimer_Callback(TimerBase timer) { RefillChestMetadata refillChest = (RefillChestMetadata)timer.Data; lock (this.WorldMetadata.Protections) { ProtectionEntry protection = this.WorldMetadata.Protections.Values.SingleOrDefault(p => p.RefillChestData == refillChest); if (protection == null) { return(false); } DPoint chestLocation = protection.TileLocation; this.TryRefillChest(chestLocation, refillChest); // Returning true would mean the Timer would repeat. return(false); } }
private bool RefillChestTimer_Callback(TimerBase timer) { RefillChestMetadata refillChest = (RefillChestMetadata)timer.Data; lock (this.WorldMetadata.Protections) { ProtectionEntry protection = this.WorldMetadata.Protections.Values.SingleOrDefault(p => p.RefillChestData == refillChest); if (protection == null) { return(false); } DPoint chestLocation = protection.TileLocation; try { this.TryRefillChest(chestLocation, refillChest); } catch (InvalidOperationException) { this.PluginTrace.WriteLineWarning($"Chest at position {chestLocation} doesn't seem to exist anymore. Can't refill it."); return(false); } // Returning true would mean the Timer would repeat. return(false); } }
public bool CheckRefillChestLootability(RefillChestMetadata refillChest, TSPlayer player, bool sendReasonMessages = true) { if (!player.IsLoggedIn && (refillChest.OneLootPerPlayer || refillChest.RemainingLoots != -1)) { if (sendReasonMessages) player.SendErrorMessage("You have to be logged in in order to use this chest."); return false; } if ( !this.Config.AllowRefillChestContentChanges || (player.User.ID != refillChest.Owner && !player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission)) ) { if (refillChest.RemainingLoots == 0) { if (sendReasonMessages) player.SendErrorMessage("This chest has a loot limit attached to it and can not be looted anymore."); return false; } if (refillChest.OneLootPerPlayer) { //Contract.Assert(refillChest.Looters != null); if (refillChest.Looters == null) refillChest.Looters = new Collection<int>(); if (refillChest.Looters.Contains(player.User.ID)) { if (sendReasonMessages) player.SendErrorMessage("This chest can only be looted a single time per player, and you have already looted it once."); return false; } } } return true; }
public bool TryRefillChest(DPoint chestLocation, RefillChestMetadata refillChestData) { int tChestIndex = Chest.FindChest(chestLocation.X, chestLocation.Y); if (tChestIndex == -1) return false; Chest tChest = Main.chest[tChestIndex]; if (tChest == null) return false; for (int i = 0; i < Chest.maxItems; i++) tChest.item[i] = refillChestData.RefillItems[i].ToItem(); if ( refillChestData.AutoLock && refillChestData.RefillTime != TimeSpan.Zero && !TerrariaUtils.Tiles.IsChestLocked(TerrariaUtils.Tiles[chestLocation]) ) { TerrariaUtils.Tiles.LockChest(chestLocation); } return true; }
/// <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 ( !player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission) && !this.CheckBlockAccess(player, chestLocation, true) ) throw new TileProtectedException(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; lock (this.RefillTimers) if (this.RefillTimers.IsTimerRunning(refillChestData.RefillTimer)) this.RefillTimers.RemoveTimer(refillChestData.RefillTimer); return false; } int tChestIndex = Chest.FindChest(chestLocation.X, chestLocation.Y); if (tChestIndex == -1 || Main.chest[tChestIndex] == 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; Chest tChest = Main.chest[tChestIndex]; for (int i = 0; i < Chest.maxItems; i++) { ItemData item = ItemData.FromItem(tChest.item[i]); if (item.StackSize == 0 && fairLootPutItem) { try { bool isLocked; item.Type = TerrariaUtils.Tiles.GetItemTypeFromChestType(TerrariaUtils.Tiles.GetChestStyle(tile, out isLocked)); item.StackSize = 1; tChest.item[i] = item.ToItem(); } catch (ArgumentException) {} fairLootPutItem = false; } refillChestData.RefillItems[i] = item; } protection.RefillChestData = refillChestData; return true; }
/// <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); }
/// <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 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) { 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 ( !player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission) && !this.CheckBlockAccess(player, chestLocation, true) ) { throw new TileProtectedException(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; } } lock (this.RefillTimers) if (this.RefillTimers.IsTimerRunning(refillChestData.RefillTimer)) { this.RefillTimers.RemoveTimer(refillChestData.RefillTimer); } return(false); } int tChestIndex = Chest.FindChest(chestLocation.X, chestLocation.Y); if (tChestIndex == -1 || Main.chest[tChestIndex] == null) { throw new NoChestDataException(chestLocation); } TimeSpan actualRefillTime = TimeSpan.Zero; if (refillTime != null) { actualRefillTime = refillTime.Value; } refillChestData = new RefillChestMetadata(player.UserID); 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; } Chest tChest = Main.chest[tChestIndex]; for (int i = 0; i < Chest.maxItems; i++) { refillChestData.RefillItems[i] = ItemData.FromItem(tChest.item[i]); } protection.RefillChestData = refillChestData; return(true); }
public bool TryRefillChest(IChest chest, RefillChestMetadata refillChestData) { for (int i = 0; i < Chest.maxItems; i++) chest.Items[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; }
public bool TryRefillChest(DPoint chestLocation, RefillChestMetadata refillChestData) { IChest chest = this.ChestFromLocation(chestLocation); if (chest == null) throw new InvalidOperationException("No chest data found at lcoation."); return this.TryRefillChest(chest, refillChestData); }