Migrate most of CheckTilePermission to TSPlayer

TShock.CheckTilePermission(ply, x, y, paint) has been replaced with
TSPlayer.HasPaintPermission and TSPlayer.HasBuildPermission
respectively. These two methods dramatically simplify the logic required
to process build permissions.

Still todo after this commit:

1. Implement warnings for when a player fails a permission check on
build. This is probably going to be accomplished by a default argument
to the HasBuildPermission signature.

2. Create a variant that checks for ice tile permission and use like
HasPaintPermission() so as to remove the other
TShock.CheckTilePermission method.

Annoyingly, all of the existing methods that start with Check seem to
"check" if they player _doesn't_ have permission (true = no permission),
which makes nearly every call negative.
This commit is contained in:
Lucas Nicodemus 2017-12-22 00:31:02 -07:00
parent fbfb509cc9
commit 195a23a7e5
4 changed files with 64 additions and 83 deletions

View file

@ -76,7 +76,7 @@ namespace TShockAPI
return;
}
if (TShock.CheckTilePermission(args.Player, args.X, args.Y))
if (!args.Player.HasBuildPermission(args.X, args.Y))
{
NetMessage.SendData((int)PacketTypes.UpdateTileEntity, -1, -1, NetworkText.Empty, args.ItemFrame.ID, 0, 1);
args.Handled = true;
@ -114,7 +114,7 @@ namespace TShockAPI
return;
}
if (TShock.CheckTilePermission(args.Player, args.X, args.Y))
if (!args.Player.HasBuildPermission(args.X, args.Y))
{
args.Handled = true;
return;
@ -132,7 +132,7 @@ namespace TShockAPI
return;
}
if (TShock.CheckTilePermission(args.Player, args.X, args.Y))
if (!args.Player.HasBuildPermission(args.X, args.Y))
{
args.Handled = true;
return;
@ -183,7 +183,7 @@ namespace TShockAPI
return;
}
if (TShock.CheckTilePermission(args.Player, x, y))
if (!args.Player.HasBuildPermission(x, y))
{
args.Handled = true;
return;
@ -510,7 +510,7 @@ namespace TShockAPI
return;
}
if (TShock.CheckTilePermission(args.Player, Main.chest[id].x, Main.chest[id].y) && TShock.Config.RegionProtectChests)
if (!args.Player.HasBuildPermission(Main.chest[id].x, Main.chest[id].y) && TShock.Config.RegionProtectChests)
{
args.Handled = true;
return;
@ -535,7 +535,7 @@ namespace TShockAPI
// Calls to TShock.CheckTilePermission need to be broken up into different subsystems
// In particular, this handles both regions and other things. Ouch.
if (TShock.CheckTilePermission(args.Player, x, y))
if (!args.Player.HasBuildPermission(x, y))
{
args.Player.SendErrorMessage("You do not have access to modify this area.");
args.Player.SendData(PacketTypes.UpdateNPCHome, "", id, Main.npc[id].homeTileX, Main.npc[id].homeTileY,
@ -570,7 +570,7 @@ namespace TShockAPI
return;
}
if (TShock.CheckTilePermission(args.Player, args.X, args.Y) && TShock.Config.RegionProtectChests)
if (!args.Player.HasBuildPermission(args.X, args.Y) && TShock.Config.RegionProtectChests)
{
args.Handled = true;
return;
@ -625,7 +625,7 @@ namespace TShockAPI
}
}
if (TShock.CheckTilePermission(args.Player, tileX, tileY))
if (!args.Player.HasBuildPermission(tileX, tileY))
{
args.Player.SendTileSquare(tileX, tileY, 3);
args.Handled = true;
@ -758,7 +758,7 @@ namespace TShockAPI
}
}
if (TShock.CheckTilePermission(args.Player, tileX, tileY))
if (!args.Player.HasBuildPermission(tileX, tileY))
{
args.Player.SendTileSquare(tileX, tileY, 1);
args.Handled = true;
@ -1660,7 +1660,7 @@ namespace TShockAPI
var tile = Main.tile[realx, realy];
var newtile = tiles[x, y];
if (TShock.CheckTilePermission(args.Player, realx, realy) ||
if (!args.Player.HasBuildPermission(realx, realy) ||
!args.Player.IsInRange(realx, realy))
{
continue;

View file

@ -2532,7 +2532,7 @@ namespace TShockAPI
args.Player.ActiveChest = id;
if (TShock.CheckTilePermission(args.Player, x, y) && TShock.Config.RegionProtectChests)
if (!args.Player.HasBuildPermission(x, y) && TShock.Config.RegionProtectChests)
{
args.Player.SendData(PacketTypes.ChestOpen, "", -1);
return true;
@ -2572,7 +2572,7 @@ namespace TShockAPI
if (OnSignEvent(id, x, y))
return true;
if (TShock.CheckTilePermission(args.Player, x, y))
if (!args.Player.HasBuildPermission(x, y))
{
args.Player.SendData(PacketTypes.SignNew, "", id);
return true;
@ -2897,7 +2897,7 @@ namespace TShockAPI
}
if (args.Player.IsBouncerThrottled() ||
TShock.CheckTilePermission(args.Player, x, y, true) ||
!args.Player.HasPaintPermission(x, y) ||
!args.Player.IsInRange(x, y))
{
args.Player.SendData(PacketTypes.PaintTile, "", x, y, Main.tile[x, y].color());
@ -2941,7 +2941,7 @@ namespace TShockAPI
}
if (args.Player.IsBouncerThrottled() ||
TShock.CheckTilePermission(args.Player, x, y, true) ||
!args.Player.HasPaintPermission(x, y) ||
!args.Player.IsInRange(x, y))
{
args.Player.SendData(PacketTypes.PaintWall, "", x, y, Main.tile[x, y].wallColor());

View file

@ -335,6 +335,56 @@ namespace TShockAPI
return true;
}
private enum BuildPermissionFailPoint
{
GeneralBuild,
SpawnProtect,
Regions
}
/// <summary>Determines if the player can build on a given point.</summary>
/// <param name="x">The x coordinate they want to build at.</param>
/// <param name="y">The y coordinate they want to paint at.</param>
/// <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)
{
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.
// If the player has bypass on build protection or building is enabled; continue
// (General build protection takes precedence over spawn protection)
if (!TShock.Config.DisableBuild || HasPermission(Permissions.antibuild))
{
// If they have spawn protect bypass, or it isn't spawn, or it isn't in spawn; continue
// (If they have spawn protect bypass, we don't care if it's spawn or not)
if (!TShock.Config.SpawnProtection || HasPermission(Permissions.editspawn) || !Utils.IsInSpawn(x, y))
{
// If they have build permission in this region, then they're allowed to continue
if (TShock.Regions.CanBuild(x, y, this))
{
return true;
}
}
}
// TODO: Implement warning system.
// If they lack build permission, they end up here.
// If they have build permission but lack the ability to edit spawn and it's spawn, they end up here.
// If they have build, it isn't spawn, or they can edit spawn, but they fail the region check, they end up here.
return false;
}
/// <summary>Determines if the player can paint on a given point. Checks general build permissions, then paint.</summary>
/// <param name="x">The x coordinate they want to paint at.</param>
/// <param name="y">The y coordinate they want to paint at.</param>
/// <returns>True if they can paint.</returns>
public bool HasPaintPermission(int x, int y)
{
return HasBuildPermission(x, y) || HasPermission(Permissions.canpaint);
}
/// <summary>
/// A list of points where ice tiles have been placed.
/// </summary>

View file

@ -1842,75 +1842,6 @@ namespace TShockAPI
return false;
}
/// <summary>CheckTilePermission - Checks to see if a player has the ability to modify a tile at a given position.</summary>
/// <param name="player">player - The TSPlayer object.</param>
/// <param name="tileX">tileX - The x coordinate of the tile.</param>
/// <param name="tileY">tileY - The y coordinate of the tile.</param>
/// <param name="paint">paint - Whether or not the tile is paint.</param>
/// <returns>bool - True if the player should not be able to modify the tile.</returns>
public static bool CheckTilePermission(TSPlayer player, int tileX, int tileY, bool paint = false)
{
if ((!paint && !player.HasPermission(Permissions.canbuild)) ||
(paint && !player.HasPermission(Permissions.canpaint)))
{
if (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - player.BPm) > 2000)
{
if (paint)
{
player.SendErrorMessage("You do not have permission to paint!");
}
else
{
player.SendErrorMessage("You do not have permission to build!");
}
player.BPm = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
return true;
}
if (!Regions.CanBuild(tileX, tileY, player))
{
if (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - player.RPm) > 2000)
{
player.SendErrorMessage("This region is protected from changes.");
player.RPm = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
return true;
}
if (Config.DisableBuild)
{
if (!player.HasPermission(Permissions.antibuild))
{
if (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - player.WPm) > 2000)
{
player.SendErrorMessage("The world is protected from changes.");
player.WPm = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
return true;
}
}
if (Config.SpawnProtection)
{
if (!player.HasPermission(Permissions.editspawn))
{
if (Utils.IsInSpawn(tileX, tileY))
{
if (((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - player.SPm) > 1000)
{
player.SendErrorMessage("Spawn is protected from changes.");
player.SPm = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
}
return true;
}
}
}
return false;
}
/// <summary>Distance - Determines the distance between two vectors.</summary>
/// <param name="value1">value1 - The first vector location.</param>
/// <param name="value2">value2 - The second vector location.</param>