PlayerHasBuildPermission hook (#8)

This commit is contained in:
Anzhelika 2021-01-20 18:54:26 +03:00 committed by Killia0
parent 4ceb039f82
commit b19a928fd8
2 changed files with 58 additions and 0 deletions

View file

@ -272,6 +272,32 @@ namespace TShockAPI.Hooks
}
}
/// <summary>
/// EventArgs used for the <see cref="PlayerHooks.PlayerHasBuildPermission"/> event.
/// </summary>
public class PlayerHasBuildPermissionEventArgs
{
/// <summary>
/// The player who fired the event.
/// </summary>
public TSPlayer Player { get; set; }
/// <summary>
/// The X coordinate being checked.
/// </summary>
public int X { get; set; }
/// <summary>
/// The Y coordinate being checked.
/// </summary>
public int Y { get; set; }
/// <summary>
/// <see cref="PermissionHookResult"/> of the hook.
/// </summary>
public PermissionHookResult Result { get; set; }
}
/// <summary>
/// A collection of events fired by players that can be hooked to.
/// </summary>
@ -368,6 +394,16 @@ namespace TShockAPI.Hooks
/// </summary>
public static event PlayerTilebanPermissionD PlayerTilebanPermission;
/// <summary>
/// The delegate of the <see cref="PlayerHasBuildPermission"/> event.
/// </summary>
/// <param name="e">The EventArgs for this event.</param>
public delegate void PlayerHasBuildPermissionD(PlayerHasBuildPermissionEventArgs e);
/// <summary>
/// Fired by players every time a build permission check occurs.
/// </summary>
public static event PlayerHasBuildPermissionD PlayerHasBuildPermission;
/// <summary>
/// Fires the <see cref="PlayerPostLogin"/> event.
@ -525,6 +561,22 @@ namespace TShockAPI.Hooks
return args.Result;
}
/// <summary>
/// Fires the <see cref="PlayerHasBuildPermission"/> event.
/// </summary>
/// <param name="player">The player firing the event.</param>
/// <returns>Event result if the event has been handled, otherwise <see cref="PermissionHookResult.Unhandled"/>.</returns>
public static PermissionHookResult OnPlayerHasBuildPermission(TSPlayer player, int x, int y)
{
if (PlayerHasBuildPermission == null)
return PermissionHookResult.Unhandled;
var args = new PlayerHasBuildPermissionEventArgs {Player = player, X = x, Y = y};
PlayerHasBuildPermission(args);
return args.Result;
}
}
/// <summary>

View file

@ -651,6 +651,12 @@ namespace TShockAPI
/// <returns>True if the player can build at the given point from build, spawn, and region protection.</returns>
public bool HasBuildPermission(int x, int y, bool shouldWarnPlayer = true)
{
PermissionHookResult hookResult = PlayerHooks.OnPlayerHasBuildPermission(this, x, y);
if (hookResult != PermissionHookResult.Unhandled)
{
return hookResult == PermissionHookResult.Granted;
}
BuildPermissionFailPoint failure = BuildPermissionFailPoint.GeneralBuild;
// The goal is to short circuit on easy stuff as much as possible.
// Don't compute permissions unless needed, and don't compute taxing stuff unless needed.