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

Commit 9db8fb4

Browse files
committed
fix(compatibility): prefer ITile over Tile to support different tile implementations
1 parent c74b100 commit 9db8fb4

File tree

7 files changed

+37
-36
lines changed

7 files changed

+37
-36
lines changed

Implementation/ChestManager.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using OTAPI.Tile;
89
using Terraria.ID;
910
using Terraria.Plugins.Common;
1011
using TShockAPI;
@@ -66,7 +67,7 @@ public bool SetUpRefillChest(
6667
Contract.Requires<ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
6768
Contract.Requires<ArgumentOutOfRangeException>(lootLimit == null || lootLimit >= -1);
6869

69-
Tile tile = TerrariaUtils.Tiles[tileLocation];
70+
ITile tile = TerrariaUtils.Tiles[tileLocation];
7071
if (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)
7172
throw new InvalidBlockTypeException(tile.type);
7273

@@ -161,7 +162,7 @@ public void SetUpBankChest(TSPlayer player, DPoint tileLocation, int bankChestIn
161162
Contract.Requires<ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
162163
Contract.Requires<ArgumentOutOfRangeException>(bankChestIndex >= 1, "bankChestIndex");
163164

164-
Tile tile = TerrariaUtils.Tiles[tileLocation];
165+
ITile tile = TerrariaUtils.Tiles[tileLocation];
165166
if (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)
166167
throw new InvalidBlockTypeException(tile.type);
167168

@@ -246,7 +247,7 @@ public void SetUpTradeChest(TSPlayer player, DPoint tileLocation, int sellAmount
246247
throw new ArgumentOutOfRangeException(nameof(payAmount));
247248
}
248249

249-
Tile tile = TerrariaUtils.Tiles[tileLocation];
250+
ITile tile = TerrariaUtils.Tiles[tileLocation];
250251
if (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)
251252
throw new InvalidBlockTypeException(tile.type);
252253

@@ -377,7 +378,7 @@ public IChest CreateChestData(DPoint chestLocation) {
377378
}
378379

379380
public IChest ChestFromLocation(DPoint chestLocation, TSPlayer reportToPlayer = null) {
380-
Tile tile = TerrariaUtils.Tiles[chestLocation];
381+
ITile tile = TerrariaUtils.Tiles[chestLocation];
381382
if (!tile.active() || (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)) {
382383
reportToPlayer?.SendErrorMessage("There is no chest at this position.");
383384
return null;

Implementation/PluginCooperationHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Mono.Data.Sqlite;
88
using MySql.Data.MySqlClient;
9+
using OTAPI.Tile;
910
using Terraria.ID;
1011
using Terraria.Plugins.Common;
1112

@@ -93,7 +94,7 @@ public void InfiniteChests_ChestDataImport(
9394
continue;
9495

9596
DPoint chestLocation = new DPoint(rawX, rawY);
96-
Tile chestTile = TerrariaUtils.Tiles[chestLocation];
97+
ITile chestTile = TerrariaUtils.Tiles[chestLocation];
9798
if (!chestTile.active() || (chestTile.type != TileID.Containers && chestTile.type != TileID.Containers2 && chestTile.type != TileID.Dressers)) {
9899
this.PluginTrace.WriteLineWarning($"Chest data at {chestLocation} could not be imported because no corresponding chest tiles exist in the world.");
99100
continue;
@@ -231,7 +232,7 @@ public void InfiniteSigns_SignDataImport(
231232
}
232233

233234
if (signIndex == -1) {
234-
Tile signTile = TerrariaUtils.Tiles[signLocation];
235+
ITile signTile = TerrariaUtils.Tiles[signLocation];
235236
if (!signTile.active() || (signTile.type != TileID.Signs && signTile.type != TileID.Tombstones)) {
236237
this.PluginTrace.WriteLineWarning(
237238
$"The sign data on the location {signLocation} could not be imported because no corresponding sign does exist in the world.");

Implementation/ProtectionManager.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics.Contracts;
44
using System.Collections.ObjectModel;
55
using System.Linq;
6+
using OTAPI.Tile;
67
using Terraria.Enums;
78
using Terraria.ID;
89
using Terraria.ObjectData;
@@ -57,7 +58,7 @@ public ProtectionManager(
5758
}
5859

5960
public IEnumerable<ProtectionEntry> EnumerateProtectionEntries(DPoint tileLocation) {
60-
Tile tile = TerrariaUtils.Tiles[tileLocation];
61+
ITile tile = TerrariaUtils.Tiles[tileLocation];
6162
if (!tile.active())
6263
yield break;
6364

@@ -72,10 +73,10 @@ public IEnumerable<ProtectionEntry> EnumerateProtectionEntries(DPoint tileLocati
7273
DPoint leftTileLocation = new DPoint(tileLocation.X - 1, tileLocation.Y);
7374
DPoint rightTileLocation = new DPoint(tileLocation.X + 1, tileLocation.Y);
7475
DPoint bottomTileLocation = new DPoint(tileLocation.X, tileLocation.Y + 1);
75-
Tile topTile = TerrariaUtils.Tiles[topTileLocation];
76-
Tile leftTile = TerrariaUtils.Tiles[leftTileLocation];
77-
Tile rightTile = TerrariaUtils.Tiles[rightTileLocation];
78-
Tile bottomTile = TerrariaUtils.Tiles[bottomTileLocation];
76+
ITile topTile = TerrariaUtils.Tiles[topTileLocation];
77+
ITile leftTile = TerrariaUtils.Tiles[leftTileLocation];
78+
ITile rightTile = TerrariaUtils.Tiles[rightTileLocation];
79+
ITile bottomTile = TerrariaUtils.Tiles[bottomTileLocation];
7980
TileObjectData topTileData = TileObjectData.GetTileData(topTile);
8081
TileObjectData leftTileData = TileObjectData.GetTileData(leftTile);
8182
TileObjectData rightTileData = TileObjectData.GetTileData(rightTile);
@@ -170,7 +171,7 @@ public IEnumerable<ProtectionEntry> EnumerateProtectionEntries(DPoint tileLocati
170171

171172
if (tile.type >= TileID.ImmatureHerbs && tile.type <= TileID.BloomingHerbs) {
172173
// Clay Pots and their plants have a special handling - the plant should not be removable if the pot is protected.
173-
Tile tileBeneath = TerrariaUtils.Tiles[tileLocation.X, tileLocation.Y + 1];
174+
ITile tileBeneath = TerrariaUtils.Tiles[tileLocation.X, tileLocation.Y + 1];
174175
if (
175176
tileBeneath.type == TileID.ClayPot &&
176177
this.WorldMetadata.Protections.TryGetValue(new DPoint(tileLocation.X, tileLocation.Y + 1), out protection)
@@ -190,7 +191,7 @@ private IEnumerable<ProtectionEntry> EnumProtectionEntriesOnTopOfObject(ObjectMe
190191
for (int rx = 0; rx < measureData.Size.X; rx++) {
191192
DPoint absoluteLocation = measureData.OriginTileLocation.OffsetEx(rx, -1);
192193

193-
Tile topTile = TerrariaUtils.Tiles[absoluteLocation];
194+
ITile topTile = TerrariaUtils.Tiles[absoluteLocation];
194195
TileObjectData topData = TileObjectData.GetTileData(topTile);
195196
if (topData != null && (topData.AnchorBottom.type & AnchorType.Table) != 0) {
196197
lock (this.WorldMetadata.Protections) {
@@ -244,7 +245,7 @@ public ProtectionEntry CreateProtection(
244245
Contract.Requires<ArgumentException>(TerrariaUtils.Tiles[tileLocation] != null, "tileLocation");
245246
Contract.Requires<ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
246247

247-
Tile tile = TerrariaUtils.Tiles[tileLocation];
248+
ITile tile = TerrariaUtils.Tiles[tileLocation];
248249
int blockType = tile.type;
249250
tileLocation = TerrariaUtils.Tiles.MeasureObject(tileLocation).OriginTileLocation;
250251

@@ -284,7 +285,7 @@ public void RemoveProtection(TSPlayer player, DPoint tileLocation, bool checkIfB
284285

285286
bool canDeprotectEverything = player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission);
286287
if (TerrariaUtils.Tiles.IsValidCoord(tileLocation)) {
287-
Tile tile = TerrariaUtils.Tiles[tileLocation];
288+
ITile tile = TerrariaUtils.Tiles[tileLocation];
288289
if (tile.active()) {
289290
if (!canDeprotectEverything && checkIfBlockTypeDeprotectableByConfig && this.Config.NotDeprotectableTiles[tile.type])
290291
throw new InvalidBlockTypeException(tile.type);
@@ -412,7 +413,7 @@ public void ProtectionShareGroup(
412413
private void ProtectionSharePreValidation(
413414
TSPlayer player, DPoint tileLocation, bool shareOrUnshare, bool checkPermissions, out ProtectionEntry protection
414415
) {
415-
Tile tile = TerrariaUtils.Tiles[tileLocation];
416+
ITile tile = TerrariaUtils.Tiles[tileLocation];
416417
int blockType = tile.type;
417418
if (!ProtectionManager.IsShareableBlockType(blockType))
418419
throw new InvalidBlockTypeException(blockType);
@@ -485,7 +486,7 @@ public void EnsureProtectionData(
485486
foreach (KeyValuePair<DPoint,ProtectionEntry> protectionPair in this.WorldMetadata.Protections) {
486487
DPoint location = protectionPair.Key;
487488
ProtectionEntry protection = protectionPair.Value;
488-
Tile tile = TerrariaUtils.Tiles[location];
489+
ITile tile = TerrariaUtils.Tiles[location];
489490

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

Implementation/ProtectorPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private void Net_ObjectPlacement(object sender, ObjectPlacementEventArgs e) {
249249
if (this.isDisposed || !this.hooksEnabled || e.Handled)
250250
return;
251251

252-
e.Handled = this.UserInteractionHandler.HandleObjectPlacement(e.Player, e.Location, (int)e.BlockType, e.ObjectStyle, e.Alternative, e.Random, e.Direction);
252+
e.Handled = this.UserInteractionHandler.HandleObjectPlacement(e.Player, e.Location, e.BlockType, e.ObjectStyle, e.Alternative, e.Random, e.Direction);
253253
}
254254

255255
private void Net_SignEdit(object sender, SignEditEventArgs e) {

Implementation/ServerMetadataHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ protected string ItemMetadataToString(IEnumerable<ItemData> items) {
173173
if (builder.Length > 0)
174174
builder.Append(';');
175175

176-
builder.Append((int)item.Prefix);
176+
builder.Append(item.Prefix);
177177
builder.Append(',');
178-
builder.Append((int)item.Type);
178+
builder.Append(item.Type);
179179
builder.Append(',');
180180
builder.Append(item.StackSize);
181181
}

Implementation/UserInteractionHandler.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Text;
99
using Microsoft.Xna.Framework;
10+
using OTAPI.Tile;
1011
using Terraria.ID;
1112
using Terraria.Localization;
1213
using DPoint = System.Drawing.Point;
@@ -434,7 +435,7 @@ select pt.TileLocation
434435

435436
bool isInvalidEntry = false;
436437
DPoint chestLocation = new DPoint(tChest.x, tChest.y);
437-
Tile chestTile = TerrariaUtils.Tiles[chestLocation];
438+
ITile chestTile = TerrariaUtils.Tiles[chestLocation];
438439
if (chestTile.active() && (chestTile.type == TileID.Containers || chestTile.type == TileID.Containers2 || chestTile.type == TileID.Dressers)) {
439440
chestLocation = TerrariaUtils.Tiles.MeasureObject(chestLocation).OriginTileLocation;
440441
lock (this.WorldMetadata.Protections) {
@@ -1684,7 +1685,7 @@ private void RefillChestManyCommand_Exec(CommandArgs args) {
16841685
continue;
16851686

16861687
DPoint chestLocation = new DPoint(chest.x, chest.y);
1687-
Tile chestTile = TerrariaUtils.Tiles[chestLocation];
1688+
ITile chestTile = TerrariaUtils.Tiles[chestLocation];
16881689
if (!chestTile.active() || (chestTile.type != TileID.Containers && chestTile.type != TileID.Containers2))
16891690
continue;
16901691

@@ -2090,15 +2091,11 @@ public override bool HandleTileEdit(
20902091

20912092
switch (editType) {
20922093
case TileEditType.PlaceTile: {
2093-
Tile tile = TerrariaUtils.Tiles[location];
2094-
if (tile == null)
2095-
Main.tile[location.X, location.Y] = tile = new Tile();
2094+
WorldGen.PlaceTile(location.X, location.Y, blockType, false, true, -1, objectStyle);
20962095

2097-
WorldGen.PlaceTile(location.X, location.Y, (int)blockType, false, true, -1, objectStyle);
2096+
NetMessage.SendData((int)PacketTypes.Tile, -1, player.Index, NetworkText.Empty, 1, location.X, location.Y, blockType, objectStyle);
20982097

2099-
NetMessage.SendData((int)PacketTypes.Tile, -1, player.Index, NetworkText.Empty, 1, location.X, location.Y, (int)blockType, objectStyle);
2100-
2101-
if (this.Config.AutoProtectedTiles[(int)blockType])
2098+
if (this.Config.AutoProtectedTiles[blockType])
21022099
this.TryCreateAutoProtection(player, location);
21032100

21042101
return true;
@@ -2109,7 +2106,7 @@ public override bool HandleTileEdit(
21092106
//if (blockType != 0)
21102107
// break;
21112108

2112-
Tile tile = TerrariaUtils.Tiles[location];
2109+
ITile tile = TerrariaUtils.Tiles[location];
21132110
bool isChest = (tile.type == TileID.Containers || tile.type == TileID.Containers2 || tile.type == TileID.Dressers);
21142111
foreach (ProtectionEntry protection in this.ProtectionManager.EnumerateProtectionEntries(location)) {
21152112
// If the protection is invalid, just remove it.
@@ -2118,7 +2115,7 @@ public override bool HandleTileEdit(
21182115
continue;
21192116
}
21202117

2121-
Tile protectedTile = TerrariaUtils.Tiles[protection.TileLocation];
2118+
ITile protectedTile = TerrariaUtils.Tiles[protection.TileLocation];
21222119
// If the protection is invalid, just remove it.
21232120
if (!protectedTile.active() || protectedTile.type != protection.BlockType) {
21242121
this.ProtectionManager.RemoveProtection(TSPlayer.Server, protection.TileLocation, false);
@@ -2328,7 +2325,7 @@ public virtual bool HandleChestOpen(TSPlayer player, int chestIndex, DPoint ches
23282325
if (chest == null)
23292326
return false;
23302327

2331-
Tile chestTile = TerrariaUtils.Tiles[chest.Location];
2328+
ITile chestTile = TerrariaUtils.Tiles[chest.Location];
23322329
bool isLocked;
23332330
ChestStyle chestStyle = TerrariaUtils.Tiles.GetChestStyle(chestTile, out isLocked);
23342331
if (isLocked)
@@ -2821,7 +2818,7 @@ public virtual bool HandlePlayerSpawn(TSPlayer player, DPoint spawnTileLocation)
28212818
return false;
28222819

28232820
DPoint bedTileLocation = new DPoint(spawnTileLocation.X, spawnTileLocation.Y - 1);
2824-
Tile spawnTile = TerrariaUtils.Tiles[bedTileLocation];
2821+
ITile spawnTile = TerrariaUtils.Tiles[bedTileLocation];
28252822
bool isInvalidBedSpawn = (!spawnTile.active() || spawnTile.type != TileID.Beds);
28262823

28272824
bool allowNewSpawnSet = true;
@@ -3177,7 +3174,7 @@ private bool TryRemoveProtection(TSPlayer player, DPoint tileLocation, bool send
31773174
}
31783175

31793176
private bool TryGetProtectionInfo(TSPlayer player, DPoint tileLocation, bool sendFailureMessages = true) {
3180-
Tile tile = TerrariaUtils.Tiles[tileLocation];
3177+
ITile tile = TerrariaUtils.Tiles[tileLocation];
31813178
if (!tile.active())
31823179
return false;
31833180

@@ -3751,7 +3748,7 @@ public void EnsureProtectionData(TSPlayer player) {
37513748
}
37523749

37533750
private void DestroyBlockOrObject(DPoint tileLocation) {
3754-
Tile tile = TerrariaUtils.Tiles[tileLocation];
3751+
ITile tile = TerrariaUtils.Tiles[tileLocation];
37553752
if (!tile.active())
37563753
return;
37573754

Implementation/WorldMetadataHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Diagnostics.Contracts;
5+
using OTAPI.Tile;
56
using DPoint = System.Drawing.Point;
67

78
using Terraria.Plugins.Common;
@@ -34,7 +35,7 @@ protected override IMetadataFile ReadMetadataFromFile(string filePath) {
3435
ProtectionEntry protection = protectionPair.Value;
3536

3637
if (protection.BlockType == -1) {
37-
Tile tile = TerrariaUtils.Tiles[location];
38+
ITile tile = TerrariaUtils.Tiles[location];
3839
if (tile.active())
3940
protection.BlockType = tile.type;
4041
}

0 commit comments

Comments
 (0)