Skip to content

Commit

Permalink
fix(chests): fix refill chest auto locking and key of night/light mim…
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderCow committed May 14, 2017
1 parent 04c86b8 commit b22f5a3
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions Implementation/UserInteractionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2335,26 +2335,48 @@ public virtual bool HandleChestOpen(TSPlayer player, int chestIndex, DPoint ches
return false;

ProtectionEntry protection = null;
// Only need the first enumerated entry as we don't need the protections of adjacent blocks.
foreach (ProtectionEntry enumProtection in this.ProtectionManager.EnumerateProtectionEntries(chestLocation)) {
foreach (ProtectionEntry enumProtection in this.ProtectionManager.EnumerateProtectionEntries(chest.Location)) {
protection = enumProtection;
break;
}

if (protection == null || protection.RefillChestData == null)
return false;
// Convert this chest to a world chest if it contains a key of night/light only, so that Terraria can do its
// thing with it.
if (!chest.IsWorldChest) {
int containedLightNightKeys = 0;
bool isOtherwiseEmpty = true;
for (int i = 0; i < Chest.maxItems; i++) {
ItemData chestItem = chest.Items[i];
if (chestItem.StackSize == 1 && (chestItem.Type == ItemID.NightKey || chestItem.Type == ItemID.LightKey)) {
containedLightNightKeys++;
} else if (chestItem.StackSize > 0) {
isOtherwiseEmpty = false;
break;
}
}

if (protection.RefillChestData.AutoEmpty && !player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission)) {
for (int i = 0; i < Chest.maxItems; i++)
chest.Items[i] = ItemData.None;
if (containedLightNightKeys == 1 && isOtherwiseEmpty) {
this.TrySwapChestData(null, chest.Location, out chest);
player.TPlayer.lastChest = chest.Index;
}
}

if (
protection.RefillChestData.AutoLock &&
TerrariaUtils.Tiles.IsChestStyleLockable(chestStyle) &&
protection.RefillChestData.RefillTime == TimeSpan.Zero
)
TerrariaUtils.Tiles.LockChest(chestLocation);
if (protection == null)
return false;

if (protection.RefillChestData != null) {
if (protection.RefillChestData.AutoEmpty && !player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission)) {
for (int i = 0; i < Chest.maxItems; i++)
chest.Items[i] = ItemData.None;
}

if (
protection.RefillChestData.AutoLock &&
TerrariaUtils.Tiles.IsChestStyleLockable(chestStyle) &&
protection.RefillChestData.RefillTime == TimeSpan.Zero
)
TerrariaUtils.Tiles.LockChest(chest.Location);
}

return false;
}
Expand Down Expand Up @@ -3344,7 +3366,7 @@ public bool TrySwapChestData(TSPlayer player, DPoint anyChestTileLocation, out I

int tileID = TerrariaUtils.Tiles[anyChestTileLocation].type;
if (tileID != TileID.Containers && tileID != TileID.Containers2 && tileID != TileID.Dressers) {
player.SendErrorMessage("The selected tile is not a chest or dresser.");
player?.SendErrorMessage("The selected tile is not a chest or dresser.");
return false;
}

Expand Down

0 comments on commit b22f5a3

Please sign in to comment.