Skip to content

Commit

Permalink
fix(compatibility): prefer ITile over Tile to support different tile …
Browse files Browse the repository at this point in the history
…implementations
  • Loading branch information
CoderCow committed May 17, 2017
1 parent c74b100 commit 9db8fb4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 36 deletions.
9 changes: 5 additions & 4 deletions Implementation/ChestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OTAPI.Tile;
using Terraria.ID;
using Terraria.Plugins.Common;
using TShockAPI;
Expand Down Expand Up @@ -66,7 +67,7 @@ public bool SetUpRefillChest(
Contract.Requires<ArgumentException>(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation");
Contract.Requires<ArgumentOutOfRangeException>(lootLimit == null || lootLimit >= -1);

Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
if (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)
throw new InvalidBlockTypeException(tile.type);

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

Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
if (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)
throw new InvalidBlockTypeException(tile.type);

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

Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
if (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)
throw new InvalidBlockTypeException(tile.type);

Expand Down Expand Up @@ -377,7 +378,7 @@ public IChest CreateChestData(DPoint chestLocation) {
}

public IChest ChestFromLocation(DPoint chestLocation, TSPlayer reportToPlayer = null) {
Tile tile = TerrariaUtils.Tiles[chestLocation];
ITile tile = TerrariaUtils.Tiles[chestLocation];
if (!tile.active() || (tile.type != TileID.Containers && tile.type != TileID.Containers2 && tile.type != TileID.Dressers)) {
reportToPlayer?.SendErrorMessage("There is no chest at this position.");
return null;
Expand Down
5 changes: 3 additions & 2 deletions Implementation/PluginCooperationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using OTAPI.Tile;
using Terraria.ID;
using Terraria.Plugins.Common;

Expand Down Expand Up @@ -93,7 +94,7 @@ public void InfiniteChests_ChestDataImport(
continue;

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

if (signIndex == -1) {
Tile signTile = TerrariaUtils.Tiles[signLocation];
ITile signTile = TerrariaUtils.Tiles[signLocation];
if (!signTile.active() || (signTile.type != TileID.Signs && signTile.type != TileID.Tombstones)) {
this.PluginTrace.WriteLineWarning(
$"The sign data on the location {signLocation} could not be imported because no corresponding sign does exist in the world.");
Expand Down
23 changes: 12 additions & 11 deletions Implementation/ProtectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics.Contracts;
using System.Collections.ObjectModel;
using System.Linq;
using OTAPI.Tile;
using Terraria.Enums;
using Terraria.ID;
using Terraria.ObjectData;
Expand Down Expand Up @@ -57,7 +58,7 @@ public ProtectionManager(
}

public IEnumerable<ProtectionEntry> EnumerateProtectionEntries(DPoint tileLocation) {
Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
if (!tile.active())
yield break;

Expand All @@ -72,10 +73,10 @@ public IEnumerable<ProtectionEntry> EnumerateProtectionEntries(DPoint tileLocati
DPoint leftTileLocation = new DPoint(tileLocation.X - 1, tileLocation.Y);
DPoint rightTileLocation = new DPoint(tileLocation.X + 1, tileLocation.Y);
DPoint bottomTileLocation = new DPoint(tileLocation.X, tileLocation.Y + 1);
Tile topTile = TerrariaUtils.Tiles[topTileLocation];
Tile leftTile = TerrariaUtils.Tiles[leftTileLocation];
Tile rightTile = TerrariaUtils.Tiles[rightTileLocation];
Tile bottomTile = TerrariaUtils.Tiles[bottomTileLocation];
ITile topTile = TerrariaUtils.Tiles[topTileLocation];
ITile leftTile = TerrariaUtils.Tiles[leftTileLocation];
ITile rightTile = TerrariaUtils.Tiles[rightTileLocation];
ITile bottomTile = TerrariaUtils.Tiles[bottomTileLocation];
TileObjectData topTileData = TileObjectData.GetTileData(topTile);
TileObjectData leftTileData = TileObjectData.GetTileData(leftTile);
TileObjectData rightTileData = TileObjectData.GetTileData(rightTile);
Expand Down Expand Up @@ -170,7 +171,7 @@ public IEnumerable<ProtectionEntry> EnumerateProtectionEntries(DPoint tileLocati

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

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

Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
int blockType = tile.type;
tileLocation = TerrariaUtils.Tiles.MeasureObject(tileLocation).OriginTileLocation;

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

bool canDeprotectEverything = player.Group.HasPermission(ProtectorPlugin.ProtectionMaster_Permission);
if (TerrariaUtils.Tiles.IsValidCoord(tileLocation)) {
Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
if (tile.active()) {
if (!canDeprotectEverything && checkIfBlockTypeDeprotectableByConfig && this.Config.NotDeprotectableTiles[tile.type])
throw new InvalidBlockTypeException(tile.type);
Expand Down Expand Up @@ -412,7 +413,7 @@ public void ProtectionShareGroup(
private void ProtectionSharePreValidation(
TSPlayer player, DPoint tileLocation, bool shareOrUnshare, bool checkPermissions, out ProtectionEntry protection
) {
Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
int blockType = tile.type;
if (!ProtectionManager.IsShareableBlockType(blockType))
throw new InvalidBlockTypeException(blockType);
Expand Down Expand Up @@ -485,7 +486,7 @@ public void EnsureProtectionData(
foreach (KeyValuePair<DPoint,ProtectionEntry> protectionPair in this.WorldMetadata.Protections) {
DPoint location = protectionPair.Key;
ProtectionEntry protection = protectionPair.Value;
Tile tile = TerrariaUtils.Tiles[location];
ITile tile = TerrariaUtils.Tiles[location];

if (!tile.active() || tile.type != protection.BlockType) {
invalidProtectionLocations.Add(location);
Expand Down
2 changes: 1 addition & 1 deletion Implementation/ProtectorPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private void Net_ObjectPlacement(object sender, ObjectPlacementEventArgs e) {
if (this.isDisposed || !this.hooksEnabled || e.Handled)
return;

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

private void Net_SignEdit(object sender, SignEditEventArgs e) {
Expand Down
4 changes: 2 additions & 2 deletions Implementation/ServerMetadataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ protected string ItemMetadataToString(IEnumerable<ItemData> items) {
if (builder.Length > 0)
builder.Append(';');

builder.Append((int)item.Prefix);
builder.Append(item.Prefix);
builder.Append(',');
builder.Append((int)item.Type);
builder.Append(item.Type);
builder.Append(',');
builder.Append(item.StackSize);
}
Expand Down
27 changes: 12 additions & 15 deletions Implementation/UserInteractionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using OTAPI.Tile;
using Terraria.ID;
using Terraria.Localization;
using DPoint = System.Drawing.Point;
Expand Down Expand Up @@ -434,7 +435,7 @@ select pt.TileLocation

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

DPoint chestLocation = new DPoint(chest.x, chest.y);
Tile chestTile = TerrariaUtils.Tiles[chestLocation];
ITile chestTile = TerrariaUtils.Tiles[chestLocation];
if (!chestTile.active() || (chestTile.type != TileID.Containers && chestTile.type != TileID.Containers2))
continue;

Expand Down Expand Up @@ -2090,15 +2091,11 @@ public override bool HandleTileEdit(

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

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

NetMessage.SendData((int)PacketTypes.Tile, -1, player.Index, NetworkText.Empty, 1, location.X, location.Y, (int)blockType, objectStyle);

if (this.Config.AutoProtectedTiles[(int)blockType])
if (this.Config.AutoProtectedTiles[blockType])
this.TryCreateAutoProtection(player, location);

return true;
Expand All @@ -2109,7 +2106,7 @@ public override bool HandleTileEdit(
//if (blockType != 0)
// break;

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

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

Tile chestTile = TerrariaUtils.Tiles[chest.Location];
ITile chestTile = TerrariaUtils.Tiles[chest.Location];
bool isLocked;
ChestStyle chestStyle = TerrariaUtils.Tiles.GetChestStyle(chestTile, out isLocked);
if (isLocked)
Expand Down Expand Up @@ -2821,7 +2818,7 @@ public virtual bool HandlePlayerSpawn(TSPlayer player, DPoint spawnTileLocation)
return false;

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

bool allowNewSpawnSet = true;
Expand Down Expand Up @@ -3177,7 +3174,7 @@ private bool TryRemoveProtection(TSPlayer player, DPoint tileLocation, bool send
}

private bool TryGetProtectionInfo(TSPlayer player, DPoint tileLocation, bool sendFailureMessages = true) {
Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
if (!tile.active())
return false;

Expand Down Expand Up @@ -3751,7 +3748,7 @@ public void EnsureProtectionData(TSPlayer player) {
}

private void DestroyBlockOrObject(DPoint tileLocation) {
Tile tile = TerrariaUtils.Tiles[tileLocation];
ITile tile = TerrariaUtils.Tiles[tileLocation];
if (!tile.active())
return;

Expand Down
3 changes: 2 additions & 1 deletion Implementation/WorldMetadataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using OTAPI.Tile;
using DPoint = System.Drawing.Point;

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

if (protection.BlockType == -1) {
Tile tile = TerrariaUtils.Tiles[location];
ITile tile = TerrariaUtils.Tiles[location];
if (tile.active())
protection.BlockType = tile.type;
}
Expand Down

0 comments on commit 9db8fb4

Please sign in to comment.