Turn the player hooks into a tristate

This commit is contained in:
Ruby Rose 2017-07-11 10:37:17 +03:00
parent 1cdd33a78a
commit 79f66f554f
5 changed files with 85 additions and 25 deletions

View file

@ -201,8 +201,9 @@ namespace TShockAPI.DB
if (ply.HasPermission(Permissions.usebanneditem)) if (ply.HasPermission(Permissions.usebanneditem))
return true; return true;
if (PlayerHooks.OnPlayerItembanPermission(ply, this)) PermissionResult hookResult = PlayerHooks.OnPlayerItembanPermission(ply, this);
return true; if (hookResult != PermissionResult.Inconclusive)
return hookResult == PermissionResult.Granted;
var cur = ply.Group; var cur = ply.Group;
var traversed = new List<Group>(); var traversed = new List<Group>();

View file

@ -206,8 +206,9 @@ namespace TShockAPI.DB
if (ply.HasPermission(Permissions.canusebannedprojectiles)) if (ply.HasPermission(Permissions.canusebannedprojectiles))
return true; return true;
if (PlayerHooks.OnPlayerProjbanPermission(ply, this)) PermissionResult hookResult = PlayerHooks.OnPlayerProjbanPermission(ply, this);
return true; if (hookResult != PermissionResult.Inconclusive)
return hookResult == PermissionResult.Granted;
var cur = ply.Group; var cur = ply.Group;
var traversed = new List<Group>(); var traversed = new List<Group>();

View file

@ -206,8 +206,9 @@ namespace TShockAPI.DB
if (ply.HasPermission(Permissions.canusebannedtiles)) if (ply.HasPermission(Permissions.canusebannedtiles))
return true; return true;
if (PlayerHooks.OnPlayerTilebanPermission(ply, this)) PermissionResult hookResult = PlayerHooks.OnPlayerTilebanPermission(ply, this);
return true; if (hookResult != PermissionResult.Inconclusive)
return hookResult == PermissionResult.Granted;
var cur = ply.Group; var cur = ply.Group;
var traversed = new List<Group>(); var traversed = new List<Group>();

View file

@ -155,6 +155,11 @@ namespace TShockAPI.Hooks
/// </summary> /// </summary>
public string Permission { get; set; } public string Permission { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary> /// <summary>
/// Initializes a new instance of the PlayerPermissionEventArgs class. /// Initializes a new instance of the PlayerPermissionEventArgs class.
/// </summary> /// </summary>
@ -164,6 +169,7 @@ namespace TShockAPI.Hooks
{ {
Player = player; Player = player;
Permission = permission; Permission = permission;
Result = PermissionResult.Inconclusive;
} }
} }
@ -182,15 +188,21 @@ namespace TShockAPI.Hooks
/// </summary> /// </summary>
public ItemBan BannedItem { get; set; } public ItemBan BannedItem { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary> /// <summary>
/// Initializes a new instance of the PlayerItembanPermissionEventArgs class. /// Initializes a new instance of the PlayerItembanPermissionEventArgs class.
/// </summary> /// </summary>
/// <param name="player">The player who fired the event.</param> /// <param name="player">The player who fired the event.</param>
/// <param name="permission">The permission being checked.</param> /// <param name="bannedItem">The banned item being checked.</param>
public PlayerItembanPermissionEventArgs(TSPlayer player, ItemBan bannedItem) public PlayerItembanPermissionEventArgs(TSPlayer player, ItemBan bannedItem)
{ {
Player = player; Player = player;
BannedItem = bannedItem; BannedItem = bannedItem;
Result = PermissionResult.Inconclusive;
} }
} }
@ -209,6 +221,11 @@ namespace TShockAPI.Hooks
/// </summary> /// </summary>
public ProjectileBan BannedProjectile { get; set; } public ProjectileBan BannedProjectile { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary> /// <summary>
/// Initializes a new instance of the PlayerProjbanPermissionEventArgs class. /// Initializes a new instance of the PlayerProjbanPermissionEventArgs class.
/// </summary> /// </summary>
@ -218,6 +235,7 @@ namespace TShockAPI.Hooks
{ {
Player = player; Player = player;
BannedProjectile = checkedProjectile; BannedProjectile = checkedProjectile;
Result = PermissionResult.Inconclusive;
} }
} }
@ -236,6 +254,11 @@ namespace TShockAPI.Hooks
/// </summary> /// </summary>
public TileBan BannedTile { get; set; } public TileBan BannedTile { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary> /// <summary>
/// Initializes a new instance of the PlayerTilebanPermissionEventArgs class. /// Initializes a new instance of the PlayerTilebanPermissionEventArgs class.
/// </summary> /// </summary>
@ -245,6 +268,7 @@ namespace TShockAPI.Hooks
{ {
Player = player; Player = player;
BannedTile = checkedTile; BannedTile = checkedTile;
Result = PermissionResult.Inconclusive;
} }
} }
@ -439,60 +463,91 @@ namespace TShockAPI.Hooks
/// Fires the <see cref="PlayerPermission"/> event. /// Fires the <see cref="PlayerPermission"/> event.
/// </summary> /// </summary>
/// <param name="player">The player firing the event.</param> /// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns> /// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static bool OnPlayerPermission(TSPlayer player, string permission) public static PermissionResult OnPlayerPermission(TSPlayer player, string permission)
{ {
if (PlayerPermission == null) if (PlayerPermission == null)
return false; return PermissionResult.Inconclusive;
var args = new PlayerPermissionEventArgs(player, permission); var args = new PlayerPermissionEventArgs(player, permission);
PlayerPermission(args); PlayerPermission(args);
return args.Handled;
if (args.Handled)
return args.Result;
else
return PermissionResult.Inconclusive;
} }
/// <summary> /// <summary>
/// Fires the <see cref="PlayerItembanPermission"/> event. /// Fires the <see cref="PlayerItembanPermission"/> event.
/// </summary> /// </summary>
/// <param name="player">The player firing the event.</param> /// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns> /// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static bool OnPlayerItembanPermission(TSPlayer player, ItemBan bannedItem) public static PermissionResult OnPlayerItembanPermission(TSPlayer player, ItemBan bannedItem)
{ {
if (PlayerItembanPermission == null) if (PlayerItembanPermission == null)
return false; return PermissionResult.Inconclusive;
var args = new PlayerItembanPermissionEventArgs(player, bannedItem); var args = new PlayerItembanPermissionEventArgs(player, bannedItem);
PlayerItembanPermission(args); PlayerItembanPermission(args);
return args.Handled;
if (args.Handled)
return args.Result;
else
return PermissionResult.Inconclusive;
} }
/// <summary> /// <summary>
/// Fires the <see cref="PlayerProjbanPermission"/> event. /// Fires the <see cref="PlayerProjbanPermission"/> event.
/// </summary> /// </summary>
/// <param name="player">The player firing the event.</param> /// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns> /// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static bool OnPlayerProjbanPermission(TSPlayer player, ProjectileBan bannedProj) public static PermissionResult OnPlayerProjbanPermission(TSPlayer player, ProjectileBan bannedProj)
{ {
if (PlayerProjbanPermission == null) if (PlayerProjbanPermission == null)
return false; return PermissionResult.Inconclusive;
var args = new PlayerProjbanPermissionEventArgs(player, bannedProj); var args = new PlayerProjbanPermissionEventArgs(player, bannedProj);
PlayerProjbanPermission(args); PlayerProjbanPermission(args);
return args.Handled;
if (args.Handled)
return args.Result;
else
return PermissionResult.Inconclusive;
} }
/// <summary> /// <summary>
/// Fires the <see cref="PlayerTilebanPermission"/> event. /// Fires the <see cref="PlayerTilebanPermission"/> event.
/// </summary> /// </summary>
/// <param name="player">The player firing the event.</param> /// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns> /// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static bool OnPlayerTilebanPermission(TSPlayer player, TileBan bannedTile) public static PermissionResult OnPlayerTilebanPermission(TSPlayer player, TileBan bannedTile)
{ {
if (PlayerTilebanPermission == null) if (PlayerTilebanPermission == null)
return false; return PermissionResult.Inconclusive;
var args = new PlayerTilebanPermissionEventArgs(player, bannedTile); var args = new PlayerTilebanPermissionEventArgs(player, bannedTile);
PlayerTilebanPermission(args); PlayerTilebanPermission(args);
return args.Handled;
if (args.Handled)
return args.Result;
else
return PermissionResult.Inconclusive;
} }
} }
/// <summary>
/// Defines the possible outcomes of <see cref="PlayerHooks.PlayerPermission"/> handlers.
/// </summary>
public enum PermissionResult
{
/// <summary>Hook doesn't return a result on the permission check.</summary>
Inconclusive,
/// <summary>Permission is explicitly denied by a hook.</summary>
Denied,
/// <summary>Permission is explicitly granted by a hook.</summary>
Granted
}
} }

View file

@ -1265,8 +1265,10 @@ namespace TShockAPI
/// <returns>True if the player has that permission.</returns> /// <returns>True if the player has that permission.</returns>
public bool HasPermission(string permission) public bool HasPermission(string permission)
{ {
if (PlayerHooks.OnPlayerPermission(this, permission)) PermissionResult hookResult = PlayerHooks.OnPlayerPermission(this, permission);
return true;
if (hookResult != PermissionResult.Inconclusive)
return hookResult == PermissionResult.Granted;
if (tempGroup != null) if (tempGroup != null)
return tempGroup.HasPermission(permission); return tempGroup.HasPermission(permission);