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))
return true;
if (PlayerHooks.OnPlayerItembanPermission(ply, this))
return true;
PermissionResult hookResult = PlayerHooks.OnPlayerItembanPermission(ply, this);
if (hookResult != PermissionResult.Inconclusive)
return hookResult == PermissionResult.Granted;
var cur = ply.Group;
var traversed = new List<Group>();

View file

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

View file

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

View file

@ -155,6 +155,11 @@ namespace TShockAPI.Hooks
/// </summary>
public string Permission { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary>
/// Initializes a new instance of the PlayerPermissionEventArgs class.
/// </summary>
@ -164,6 +169,7 @@ namespace TShockAPI.Hooks
{
Player = player;
Permission = permission;
Result = PermissionResult.Inconclusive;
}
}
@ -182,15 +188,21 @@ namespace TShockAPI.Hooks
/// </summary>
public ItemBan BannedItem { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary>
/// Initializes a new instance of the PlayerItembanPermissionEventArgs class.
/// </summary>
/// <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)
{
Player = player;
BannedItem = bannedItem;
Result = PermissionResult.Inconclusive;
}
}
@ -209,6 +221,11 @@ namespace TShockAPI.Hooks
/// </summary>
public ProjectileBan BannedProjectile { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary>
/// Initializes a new instance of the PlayerProjbanPermissionEventArgs class.
/// </summary>
@ -218,6 +235,7 @@ namespace TShockAPI.Hooks
{
Player = player;
BannedProjectile = checkedProjectile;
Result = PermissionResult.Inconclusive;
}
}
@ -236,6 +254,11 @@ namespace TShockAPI.Hooks
/// </summary>
public TileBan BannedTile { get; set; }
/// <summary>
/// <see cref="PermissionResult"/> of the hook.
/// </summary>
public PermissionResult Result { get; set; }
/// <summary>
/// Initializes a new instance of the PlayerTilebanPermissionEventArgs class.
/// </summary>
@ -245,6 +268,7 @@ namespace TShockAPI.Hooks
{
Player = player;
BannedTile = checkedTile;
Result = PermissionResult.Inconclusive;
}
}
@ -439,60 +463,91 @@ namespace TShockAPI.Hooks
/// Fires the <see cref="PlayerPermission"/> event.
/// </summary>
/// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns>
public static bool OnPlayerPermission(TSPlayer player, string permission)
/// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static PermissionResult OnPlayerPermission(TSPlayer player, string permission)
{
if (PlayerPermission == null)
return false;
return PermissionResult.Inconclusive;
var args = new PlayerPermissionEventArgs(player, permission);
PlayerPermission(args);
return args.Handled;
if (args.Handled)
return args.Result;
else
return PermissionResult.Inconclusive;
}
/// <summary>
/// Fires the <see cref="PlayerItembanPermission"/> event.
/// </summary>
/// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns>
public static bool OnPlayerItembanPermission(TSPlayer player, ItemBan bannedItem)
/// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static PermissionResult OnPlayerItembanPermission(TSPlayer player, ItemBan bannedItem)
{
if (PlayerItembanPermission == null)
return false;
return PermissionResult.Inconclusive;
var args = new PlayerItembanPermissionEventArgs(player, bannedItem);
PlayerItembanPermission(args);
return args.Handled;
if (args.Handled)
return args.Result;
else
return PermissionResult.Inconclusive;
}
/// <summary>
/// Fires the <see cref="PlayerProjbanPermission"/> event.
/// </summary>
/// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns>
public static bool OnPlayerProjbanPermission(TSPlayer player, ProjectileBan bannedProj)
/// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static PermissionResult OnPlayerProjbanPermission(TSPlayer player, ProjectileBan bannedProj)
{
if (PlayerProjbanPermission == null)
return false;
return PermissionResult.Inconclusive;
var args = new PlayerProjbanPermissionEventArgs(player, bannedProj);
PlayerProjbanPermission(args);
return args.Handled;
if (args.Handled)
return args.Result;
else
return PermissionResult.Inconclusive;
}
/// <summary>
/// Fires the <see cref="PlayerTilebanPermission"/> event.
/// </summary>
/// <param name="player">The player firing the event.</param>
/// <returns>True if the event has been handled.</returns>
public static bool OnPlayerTilebanPermission(TSPlayer player, TileBan bannedTile)
/// <returns>Event result if the event has been handled, otherwise <see cref="PermissionResult.Inconclusive"/>.</returns>
public static PermissionResult OnPlayerTilebanPermission(TSPlayer player, TileBan bannedTile)
{
if (PlayerTilebanPermission == null)
return false;
return PermissionResult.Inconclusive;
var args = new PlayerTilebanPermissionEventArgs(player, bannedTile);
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>
public bool HasPermission(string permission)
{
if (PlayerHooks.OnPlayerPermission(this, permission))
return true;
PermissionResult hookResult = PlayerHooks.OnPlayerPermission(this, permission);
if (hookResult != PermissionResult.Inconclusive)
return hookResult == PermissionResult.Granted;
if (tempGroup != null)
return tempGroup.HasPermission(permission);