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

Commit b284880

Browse files
committed
fix(compatibility): change hook registration priorities to improve compatibility with other plugins
Fixes #56
1 parent 8f1d2f1 commit b284880

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

Implementation/ProtectorPlugin.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class ProtectorPlugin: TerrariaPlugin, IDisposable {
5252
protected PluginInfo PluginInfo { get; private set; }
5353
protected Configuration Config { get; private set; }
5454
protected GetDataHookHandler GetDataHookHandler { get; private set; }
55+
protected GetDataHookHandler GetDataHookHandlerLate { get; private set; }
5556
public ChestManager ChestManager { get; private set; }
5657
public ProtectionManager ProtectionManager { get; private set; }
5758
protected UserInteractionHandler UserInteractionHandler { get; private set; }
@@ -208,10 +209,9 @@ private void AddHooks() {
208209
if (this.GetDataHookHandler != null)
209210
throw new InvalidOperationException("Hooks already registered.");
210211

212+
// this hook should ideally be registered BEFORE all other plugins
211213
this.GetDataHookHandler = new GetDataHookHandler(this, true);
212-
this.GetDataHookHandler.InvokeTileOnObjectPlacement = false;
213214
this.GetDataHookHandler.TileEdit += this.Net_TileEdit;
214-
this.GetDataHookHandler.ObjectPlacement += this.Net_ObjectPlacement;
215215
this.GetDataHookHandler.SignEdit += this.Net_SignEdit;
216216
this.GetDataHookHandler.SignRead += this.Net_SignRead;
217217
this.GetDataHookHandler.ChestPlace += this.Net_ChestPlace;
@@ -224,6 +224,11 @@ private void AddHooks() {
224224
this.GetDataHookHandler.DoorUse += this.Net_DoorUse;
225225
this.GetDataHookHandler.QuickStackNearby += this.Net_QuickStackNearby;
226226

227+
// this hook should ideally be registered AFTER all other plugins
228+
this.GetDataHookHandlerLate = new GetDataHookHandler(this, true, -100);
229+
this.GetDataHookHandlerLate.TileEdit += this.Net_TileEditLate;
230+
this.GetDataHookHandlerLate.ObjectPlacement += this.Net_ObjectPlacement;
231+
227232
ServerApi.Hooks.GameUpdate.Register(this, this.Game_Update);
228233
ServerApi.Hooks.WorldSave.Register(this, this.World_SaveWorld);
229234
GetDataHandlers.PlayerSpawn += this.TShock_PlayerSpawn;
@@ -245,6 +250,13 @@ private void Net_TileEdit(object sender, TileEditEventArgs e) {
245250
e.Handled = this.UserInteractionHandler.HandleTileEdit(e.Player, e.EditType, e.BlockType, e.Location, e.ObjectStyle);
246251
}
247252

253+
private void Net_TileEditLate(object sender, TileEditEventArgs e) {
254+
if (this.isDisposed || !this.hooksEnabled || e.Handled)
255+
return;
256+
257+
e.Handled = this.UserInteractionHandler.HandleTileEdit(e.Player, e.EditType, e.BlockType, e.Location, e.ObjectStyle, true);
258+
}
259+
248260
private void Net_ObjectPlacement(object sender, ObjectPlacementEventArgs e) {
249261
if (this.isDisposed || !this.hooksEnabled || e.Handled)
250262
return;

Implementation/UserInteractionHandler.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,18 +2081,25 @@ private bool TradeChestCommand_HelpCallback(CommandArgs args) {
20812081
#endregion
20822082

20832083
#region [Hook Handlers]
2084-
public override bool HandleTileEdit(
2085-
TSPlayer player, TileEditType editType, int blockType, DPoint location, int objectStyle
2086-
) {
2084+
public override bool HandleTileEdit(TSPlayer player, TileEditType editType, int blockType, DPoint location, int objectStyle) {
2085+
return this.HandleTileEdit(player, editType, blockType, location, objectStyle, false);
2086+
}
2087+
2088+
/// <param name="isLate">
2089+
/// if <c>true</c>, then this tile edit handler was invoked after all other plugins.
2090+
/// </param>
2091+
public bool HandleTileEdit(TSPlayer player, TileEditType editType, int blockType, DPoint location, int objectStyle, bool isLate) {
20872092
if (this.IsDisposed)
20882093
return false;
20892094
if (base.HandleTileEdit(player, editType, blockType, location, objectStyle))
20902095
return true;
20912096

20922097
switch (editType) {
20932098
case TileEditType.PlaceTile: {
2099+
if (!isLate)
2100+
break;
2101+
20942102
WorldGen.PlaceTile(location.X, location.Y, blockType, false, true, -1, objectStyle);
2095-
20962103
NetMessage.SendData((int)PacketTypes.Tile, -1, player.Index, NetworkText.Empty, 1, location.X, location.Y, blockType, objectStyle);
20972104

20982105
if (this.Config.AutoProtectedTiles[blockType])

0 commit comments

Comments
 (0)