Replace RegionHandler.OnGameUpdate with RegionHandler.OnPlayerUpdate

This commit is contained in:
Ivan 2018-05-06 12:18:06 +02:00
parent 0dab238fd3
commit 4d241381be
2 changed files with 51 additions and 62 deletions

View file

@ -15,10 +15,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using TerrariaApi.Server;
using TShockAPI.DB; using TShockAPI.DB;
using TShockAPI.Hooks; using TShockAPI.Hooks;
@ -26,37 +24,37 @@ namespace TShockAPI
{ {
/// <summary> /// <summary>
/// Represents TShock's Region subsystem. This subsystem is in charge of executing region related logic, such as /// Represents TShock's Region subsystem. This subsystem is in charge of executing region related logic, such as
/// setting temp points. /// setting temp points or invoking region events.
/// </summary> /// </summary>
internal sealed class RegionHandler internal sealed class RegionHandler
{ {
private readonly RegionManager _regionManager; private readonly RegionManager _regionManager;
private DateTime _lastCheck = DateTime.Now;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RegionHandler"/> class with the specified TShock instance and database connection. /// Initializes a new instance of the <see cref="RegionHandler"/> class with the specified <see cref="RegionManager"/> instance.
/// </summary> /// </summary>
/// <param name="plugin">The <see cref="TShock"/> instance.</param> /// <param name="regionManager">The <see cref="RegionManager"/> instance.</param>
/// <param name="connection">The database connection.</param> public RegionHandler(RegionManager regionManager)
public RegionHandler(TShock plugin, IDbConnection connection)
{ {
_regionManager = new RegionManager(connection); _regionManager = regionManager;
GetDataHandlers.GemLockToggle += OnGemLockToggle; GetDataHandlers.GemLockToggle += OnGemLockToggle;
GetDataHandlers.PlayerUpdate += OnPlayerUpdate;
GetDataHandlers.TileEdit += OnTileEdit; GetDataHandlers.TileEdit += OnTileEdit;
ServerApi.Hooks.GameUpdate.Register(plugin, OnGameUpdate);
} }
private void OnGameUpdate(EventArgs args) private void OnGemLockToggle(object sender, GetDataHandlers.GemLockToggleEventArgs e)
{ {
// Do not perform checks unless enough time has passed since the last execution. if (TShock.Config.RegionProtectGemLocks)
if ((DateTime.Now - _lastCheck).TotalSeconds < 1)
{ {
return; e.Handled = true;
}
} }
foreach (var player in TShock.Players.Where(p => p?.Active == true)) private void OnPlayerUpdate(object sender, GetDataHandlers.PlayerUpdateEventArgs e)
{ {
var player = e.Player;
// Store the player's last known region and update the current based on known regions at their coordinates. // Store the player's last known region and update the current based on known regions at their coordinates.
var oldRegion = player.CurrentRegion; var oldRegion = player.CurrentRegion;
player.CurrentRegion = _regionManager.GetTopRegion(_regionManager.InAreaRegion(player.TileX, player.TileY)); player.CurrentRegion = _regionManager.GetTopRegion(_regionManager.InAreaRegion(player.TileX, player.TileY));
@ -64,7 +62,7 @@ namespace TShockAPI
// Do not fire any hooks if the player has not left and/or entered a region. // Do not fire any hooks if the player has not left and/or entered a region.
if (player.CurrentRegion == oldRegion) if (player.CurrentRegion == oldRegion)
{ {
continue; return;
} }
// Ensure that the player has left a region before invoking the RegionLeft event // Ensure that the player has left a region before invoking the RegionLeft event
@ -80,29 +78,19 @@ namespace TShockAPI
} }
} }
// Set last execution time to this moment so we know when to execute the above code block again
_lastCheck = DateTime.Now;
}
private void OnGemLockToggle(object sender, GetDataHandlers.GemLockToggleEventArgs e)
{
if (TShock.Config.RegionProtectGemLocks)
{
e.Handled = true;
}
}
private void OnTileEdit(object sender, GetDataHandlers.TileEditEventArgs e) private void OnTileEdit(object sender, GetDataHandlers.TileEditEventArgs e)
{ {
var player = e.Player;
#region Region Information Display #region Region Information Display
if (e.Player.AwaitingName) if (player.AwaitingName)
{ {
bool includeUnprotected = false; bool includeUnprotected = false;
bool includeZIndexes = false; bool includeZIndexes = false;
bool persistentMode = false; bool persistentMode = false;
foreach (string nameParameter in e.Player.AwaitingNameParameters) foreach (string nameParameter in player.AwaitingNameParameters)
{ {
// If this flag is passed the final output will include unprotected regions, i.e regions // If this flag is passed the final output will include unprotected regions, i.e regions
// that have the DisableBuild flag set to false // that have the DisableBuild flag set to false
@ -149,28 +137,28 @@ namespace TShockAPI
if (output.Count == 0) if (output.Count == 0)
{ {
e.Player.SendInfoMessage(includeUnprotected player.SendInfoMessage(includeUnprotected
? "There are no regions at this point." ? "There are no regions at this point."
: "There are no regions at this point, or they are not protected."); : "There are no regions at this point, or they are not protected.");
} }
else else
{ {
e.Player.SendInfoMessage(includeUnprotected ? "Regions at this point: " : "Protected regions at this point: "); player.SendInfoMessage(includeUnprotected ? "Regions at this point: " : "Protected regions at this point: ");
foreach (string line in PaginationTools.BuildLinesFromTerms(output)) foreach (string line in PaginationTools.BuildLinesFromTerms(output))
{ {
e.Player.SendMessage(line, Color.White); player.SendMessage(line, Color.White);
} }
} }
if (!persistentMode) if (!persistentMode)
{ {
e.Player.AwaitingName = false; player.AwaitingName = false;
e.Player.AwaitingNameParameters = null; player.AwaitingNameParameters = null;
} }
// Revert all tile changes and handle the event // Revert all tile changes and handle the event
e.Player.SendTileSquare(e.X, e.Y, 4); player.SendTileSquare(e.X, e.Y, 4);
e.Handled = true; e.Handled = true;
} }
@ -178,17 +166,18 @@ namespace TShockAPI
#region TempPoints Setup #region TempPoints Setup
if (e.Player.AwaitingTempPoint != 0) if (player.AwaitingTempPoint != 0)
{ {
// Set temp point coordinates to current tile coordinates // Set temp point coordinates to current tile coordinates
e.Player.TempPoints[e.Player.AwaitingTempPoint - 1].X = e.X; player.TempPoints[player.AwaitingTempPoint - 1].X = e.X;
e.Player.TempPoints[e.Player.AwaitingTempPoint - 1].Y = e.Y; player.TempPoints[player.AwaitingTempPoint - 1].Y = e.Y;
e.Player.SendInfoMessage($"Set temp point {e.Player.AwaitingTempPoint}."); player.SendInfoMessage($"Set temp point {player.AwaitingTempPoint}.");
e.Player.AwaitingTempPoint = 0; // Reset the awaiting temp point
player.AwaitingTempPoint = 0;
// Revert all tile changes and handle the event // Revert all tile changes and handle the event
e.Player.SendTileSquare(e.X, e.Y, 4); player.SendTileSquare(e.X, e.Y, 4);
e.Handled = true; e.Handled = true;
} }

View file

@ -324,7 +324,7 @@ namespace TShockAPI
RestManager = new RestManager(RestApi); RestManager = new RestManager(RestApi);
RestManager.RegisterRestfulCommands(); RestManager.RegisterRestfulCommands();
Bouncer = new Bouncer(); Bouncer = new Bouncer();
RegionSystem = new RegionHandler(this, DB); RegionSystem = new RegionHandler(Regions);
var geoippath = "GeoIP.dat"; var geoippath = "GeoIP.dat";
if (Config.EnableGeoIP && File.Exists(geoippath)) if (Config.EnableGeoIP && File.Exists(geoippath))