Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit b22f5a3

Browse files
committed
fix(chests): fix refill chest auto locking and key of night/light mimic spawning
Closes #53
1 parent 04c86b8 commit b22f5a3

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

Implementation/UserInteractionHandler.cs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,26 +2335,48 @@ public virtual bool HandleChestOpen(TSPlayer player, int chestIndex, DPoint ches
23352335
return false;
23362336

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

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

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

2352-
if (
2353-
protection.RefillChestData.AutoLock &&
2354-
TerrariaUtils.Tiles.IsChestStyleLockable(chestStyle) &&
2355-
protection.RefillChestData.RefillTime == TimeSpan.Zero
2356-
)
2357-
TerrariaUtils.Tiles.LockChest(chestLocation);
2364+
if (protection == null)
2365+
return false;
2366+
2367+
if (protection.RefillChestData != null) {
2368+
if (protection.RefillChestData.AutoEmpty && !player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission)) {
2369+
for (int i = 0; i < Chest.maxItems; i++)
2370+
chest.Items[i] = ItemData.None;
2371+
}
2372+
2373+
if (
2374+
protection.RefillChestData.AutoLock &&
2375+
TerrariaUtils.Tiles.IsChestStyleLockable(chestStyle) &&
2376+
protection.RefillChestData.RefillTime == TimeSpan.Zero
2377+
)
2378+
TerrariaUtils.Tiles.LockChest(chest.Location);
2379+
}
23582380

23592381
return false;
23602382
}
@@ -3344,7 +3366,7 @@ public bool TrySwapChestData(TSPlayer player, DPoint anyChestTileLocation, out I
33443366

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

0 commit comments

Comments
 (0)