From 9db8fb44a20fb4ebc793db44414c0e0fc010e5d9 Mon Sep 17 00:00:00 2001 From: CoderCow Date: Wed, 17 May 2017 23:32:04 +0200 Subject: [PATCH] fix(compatibility): prefer ITile over Tile to support different tile implementations --- Implementation/ChestManager.cs | 9 ++++---- Implementation/PluginCooperationHandler.cs | 5 ++-- Implementation/ProtectionManager.cs | 23 +++++++++--------- Implementation/ProtectorPlugin.cs | 2 +- Implementation/ServerMetadataHandler.cs | 4 ++-- Implementation/UserInteractionHandler.cs | 27 ++++++++++------------ Implementation/WorldMetadataHandler.cs | 3 ++- 7 files changed, 37 insertions(+), 36 deletions(-) diff --git a/Implementation/ChestManager.cs b/Implementation/ChestManager.cs index a303695..3db1979 100644 --- a/Implementation/ChestManager.cs +++ b/Implementation/ChestManager.cs @@ -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; @@ -66,7 +67,7 @@ public bool SetUpRefillChest( Contract.Requires(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation"); Contract.Requires(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); @@ -161,7 +162,7 @@ public void SetUpBankChest(TSPlayer player, DPoint tileLocation, int bankChestIn Contract.Requires(TerrariaUtils.Tiles[tileLocation].active(), "tileLocation"); Contract.Requires(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); @@ -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); @@ -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; diff --git a/Implementation/PluginCooperationHandler.cs b/Implementation/PluginCooperationHandler.cs index 4c62613..ec4dca1 100644 --- a/Implementation/PluginCooperationHandler.cs +++ b/Implementation/PluginCooperationHandler.cs @@ -6,6 +6,7 @@ using Mono.Data.Sqlite; using MySql.Data.MySqlClient; +using OTAPI.Tile; using Terraria.ID; using Terraria.Plugins.Common; @@ -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; @@ -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."); diff --git a/Implementation/ProtectionManager.cs b/Implementation/ProtectionManager.cs index 9b9f79b..3a45492 100644 --- a/Implementation/ProtectionManager.cs +++ b/Implementation/ProtectionManager.cs @@ -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; @@ -57,7 +58,7 @@ public ProtectionManager( } public IEnumerable EnumerateProtectionEntries(DPoint tileLocation) { - Tile tile = TerrariaUtils.Tiles[tileLocation]; + ITile tile = TerrariaUtils.Tiles[tileLocation]; if (!tile.active()) yield break; @@ -72,10 +73,10 @@ public IEnumerable 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); @@ -170,7 +171,7 @@ public IEnumerable 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) @@ -190,7 +191,7 @@ private IEnumerable 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) { @@ -244,7 +245,7 @@ public ProtectionEntry CreateProtection( Contract.Requires(TerrariaUtils.Tiles[tileLocation] != null, "tileLocation"); Contract.Requires(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; @@ -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); @@ -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); @@ -485,7 +486,7 @@ public void EnsureProtectionData( foreach (KeyValuePair 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); diff --git a/Implementation/ProtectorPlugin.cs b/Implementation/ProtectorPlugin.cs index dfa71f1..4874f15 100644 --- a/Implementation/ProtectorPlugin.cs +++ b/Implementation/ProtectorPlugin.cs @@ -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) { diff --git a/Implementation/ServerMetadataHandler.cs b/Implementation/ServerMetadataHandler.cs index 66899d5..226f4a2 100644 --- a/Implementation/ServerMetadataHandler.cs +++ b/Implementation/ServerMetadataHandler.cs @@ -173,9 +173,9 @@ protected string ItemMetadataToString(IEnumerable 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); } diff --git a/Implementation/UserInteractionHandler.cs b/Implementation/UserInteractionHandler.cs index f8ad16f..0709fc3 100644 --- a/Implementation/UserInteractionHandler.cs +++ b/Implementation/UserInteractionHandler.cs @@ -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; @@ -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) { @@ -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; @@ -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; @@ -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. @@ -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); @@ -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) @@ -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; @@ -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; @@ -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; diff --git a/Implementation/WorldMetadataHandler.cs b/Implementation/WorldMetadataHandler.cs index ed78962..c939835 100644 --- a/Implementation/WorldMetadataHandler.cs +++ b/Implementation/WorldMetadataHandler.cs @@ -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; @@ -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; }