From 33739c4f07205e7ad7a4daaf32f738ddbbff87c1 Mon Sep 17 00:00:00 2001 From: White Date: Sat, 12 Sep 2015 11:33:17 +0930 Subject: [PATCH] Added a new Disable method with flags to determine where the message is logged to. Obsoleted the old disable method Added a config option to disable OnSecondUpdate logs (disable message is written only to console if set to true) Updated all instances of the obsolete Disable method to the new Disable method --- TShockAPI/ConfigFile.cs | 5 +- TShockAPI/GetDataHandlers.cs | 40 +++++------ TShockAPI/TSPlayer.cs | 58 +++++++++++++-- TShockAPI/TShock.cs | 132 +++++++++++++++++------------------ 4 files changed, 142 insertions(+), 93 deletions(-) diff --git a/TShockAPI/ConfigFile.cs b/TShockAPI/ConfigFile.cs index 778ef046..fc2c697a 100755 --- a/TShockAPI/ConfigFile.cs +++ b/TShockAPI/ConfigFile.cs @@ -119,8 +119,8 @@ namespace TShockAPI [Description("Mediumcore players ONLY. This means softcore players cannot join.")] public bool MediumcoreOnly; - [Description("Kicks a mediumcore player on death.")] /// KickOnMediumcoreDeath - Whether or not to kick mediumcore players on death. + [Description("Kicks a mediumcore player on death.")] public bool KickOnMediumcoreDeath; /// BanOnMediumcoreDeath - Whether or not to ban mediumcore players on death. @@ -180,6 +180,9 @@ namespace TShockAPI [Description("Force-disable printing logs to players with the log permission.")] public bool DisableSpewLogs = true; + [Description("Prevents OnSecondUpdate checks from writing to the log file")] + public bool DisableSecondUpdateLogs = false; + [Description("Valid types are \"sha512\", \"sha256\", \"md5\", append with \"-xp\" for the xp supported algorithms.")] public string HashAlgorithm = "sha512"; diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 5bdce6ae..1439aead 100755 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1331,7 +1331,7 @@ namespace TShockAPI if (max > TShock.Config.MaxHP && !args.Player.Group.HasPermission(Permissions.ignorehp)) { - args.Player.Disable("Maximum HP beyond limit"); + args.Player.Disable("Maximum HP beyond limit", DisableFlags.WriteToLogAndConsole); return true; } @@ -1360,7 +1360,7 @@ namespace TShockAPI if (max > TShock.Config.MaxMP && !args.Player.Group.HasPermission(Permissions.ignoremp)) { - args.Player.Disable("Maximum MP beyond limit"); + args.Player.Disable("Maximum MP beyond limit", DisableFlags.WriteToLogAndConsole); return true; } @@ -2099,14 +2099,14 @@ namespace TShockAPI if (args.Player.TileKillThreshold >= TShock.Config.TileKillThreshold) { - args.Player.Disable("Reached TileKill threshold."); + args.Player.Disable("Reached TileKill threshold.", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 4); return true; } if (args.Player.TilePlaceThreshold >= TShock.Config.TilePlaceThreshold) { - args.Player.Disable("Reached TilePlace threshold."); + args.Player.Disable("Reached TilePlace threshold.", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 4); return true; } @@ -2214,7 +2214,7 @@ namespace TShockAPI if (args.Player.TilePlaceThreshold >= TShock.Config.TilePlaceThreshold) { - args.Player.Disable("Reached TilePlace threshold."); + args.Player.Disable("Reached TilePlace threshold.", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(x, y, 4); return true; } @@ -2443,7 +2443,7 @@ namespace TShockAPI if (TShock.Itembans.ItemIsBanned(itemName, args.Player)) { control[5] = false; - args.Player.Disable("using a banned item ({0})".SFormat(itemName)); + args.Player.Disable("using a banned item ({0})".SFormat(itemName), DisableFlags.WriteToLogAndConsole); args.Player.SendErrorMessage("You cannot use {0} on this server. Your actions are being ignored.", itemName); } @@ -2595,7 +2595,7 @@ namespace TShockAPI if (TShock.ProjectileBans.ProjectileIsBanned(type, args.Player)) { - args.Player.Disable("Player does not have permission to create that projectile.", true); + args.Player.Disable("Player does not have permission to create that projectile.", DisableFlags.WriteToLogAndConsole); args.Player.SendErrorMessage("You do not have permission to create that projectile."); args.Player.RemoveProjectile(ident, owner); return true; @@ -2603,7 +2603,7 @@ namespace TShockAPI if (dmg > TShock.Config.MaxProjDamage && !args.Player.Group.HasPermission(Permissions.ignoredamagecap)) { - args.Player.Disable(String.Format("Projectile damage is higher than {0}.", TShock.Config.MaxProjDamage)); + args.Player.Disable(String.Format("Projectile damage is higher than {0}.", TShock.Config.MaxProjDamage), DisableFlags.WriteToLogAndConsole); args.Player.RemoveProjectile(ident, owner); return true; } @@ -2633,7 +2633,7 @@ namespace TShockAPI } else { - args.Player.Disable(String.Format("Does not have projectile permission to update projectile. ({0})", type)); + args.Player.Disable(String.Format("Does not have projectile permission to update projectile. ({0})", type), DisableFlags.WriteToLogAndConsole); args.Player.RemoveProjectile(ident, owner); } return true; @@ -2641,7 +2641,7 @@ namespace TShockAPI if (args.Player.ProjectileThreshold >= TShock.Config.ProjectileThreshold) { - args.Player.Disable("Reached projectile update threshold."); + args.Player.Disable("Reached projectile update threshold.", DisableFlags.WriteToLogAndConsole); args.Player.RemoveProjectile(ident, owner); return true; } @@ -2708,7 +2708,7 @@ namespace TShockAPI if (TShock.CheckProjectilePermission(args.Player, index, type) && type != 102 && type != 100 && !TShock.Config.IgnoreProjKill) { - args.Player.Disable("Does not have projectile permission to kill projectile."); + args.Player.Disable("Does not have projectile permission to kill projectile.", DisableFlags.WriteToLogAndConsole); args.Player.RemoveProjectile(ident, owner); return true; } @@ -2810,7 +2810,7 @@ namespace TShockAPI if (args.Player.TileLiquidThreshold >= TShock.Config.TileLiquidThreshold) { - args.Player.Disable("Reached TileLiquid threshold."); + args.Player.Disable("Reached TileLiquid threshold.", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 1); return true; } @@ -2847,7 +2847,7 @@ namespace TShockAPI if (type == 1 && !(bucket == 2 || bucket == 0)) { args.Player.SendErrorMessage("You do not have permission to perform this action."); - args.Player.Disable("Spreading lava without holding a lava bucket"); + args.Player.Disable("Spreading lava without holding a lava bucket", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 1); return true; } @@ -2855,7 +2855,7 @@ namespace TShockAPI if (type == 1 && TShock.Itembans.ItemIsBanned("Lava Bucket", args.Player)) { args.Player.SendErrorMessage("You do not have permission to perform this action."); - args.Player.Disable("Using banned lava bucket without permissions"); + args.Player.Disable("Using banned lava bucket without permissions", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 1); return true; } @@ -2863,7 +2863,7 @@ namespace TShockAPI if (type == 0 && !(bucket == 1 || bucket == 0 || bucket == 4)) { args.Player.SendErrorMessage("You do not have permission to perform this action."); - args.Player.Disable("Spreading water without holding a water bucket"); + args.Player.Disable("Spreading water without holding a water bucket", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 1); return true; } @@ -2871,7 +2871,7 @@ namespace TShockAPI if (type == 0 && TShock.Itembans.ItemIsBanned("Water Bucket", args.Player)) { args.Player.SendErrorMessage("You do not have permission to perform this action."); - args.Player.Disable("Using banned water bucket without permissions"); + args.Player.Disable("Using banned water bucket without permissions", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 1); return true; } @@ -2879,7 +2879,7 @@ namespace TShockAPI if (type == 2 && !(bucket == 3 || bucket == 0)) { args.Player.SendErrorMessage("You do not have permission to perform this action."); - args.Player.Disable("Spreading honey without holding a honey bucket"); + args.Player.Disable("Spreading honey without holding a honey bucket", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 1); return true; } @@ -2887,7 +2887,7 @@ namespace TShockAPI if (type == 2 && TShock.Itembans.ItemIsBanned("Honey Bucket", args.Player)) { args.Player.SendErrorMessage("You do not have permission to perform this action."); - args.Player.Disable("Using banned honey bucket without permissions"); + args.Player.Disable("Using banned honey bucket without permissions", DisableFlags.WriteToLogAndConsole); args.Player.SendTileSquare(tileX, tileY, 1); return true; } @@ -3328,7 +3328,7 @@ namespace TShockAPI } else { - args.Player.Disable(String.Format("Player damage exceeded {0}.", TShock.Config.MaxDamage)); + args.Player.Disable(String.Format("Player damage exceeded {0}.", TShock.Config.MaxDamage), DisableFlags.WriteToLogAndConsole); } args.Player.SendData(PacketTypes.PlayerHp, "", id); args.Player.SendData(PacketTypes.PlayerUpdate, "", id); @@ -3394,7 +3394,7 @@ namespace TShockAPI } else { - args.Player.Disable(String.Format("NPC damage exceeded {0}.", TShock.Config.MaxDamage)); + args.Player.Disable(String.Format("NPC damage exceeded {0}.", TShock.Config.MaxDamage), DisableFlags.WriteToLogAndConsole); } args.Player.SendData(PacketTypes.NpcUpdate, "", id); return true; diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index b1dd80e4..4ed41fa8 100755 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -31,6 +31,30 @@ using Timer = System.Timers.Timer; namespace TShockAPI { + /// + /// Bitflags used with the method + /// + [Flags] + public enum DisableFlags + { + /// + /// Disable the player and leave no messages + /// + None, + /// + /// Write the Disable message to the console + /// + WriteToConsole, + /// + /// Write the Disable message to the log + /// + WriteToLog, + /// + /// Equivalent to WriteToConsole | WriteToLog + /// + WriteToLogAndConsole + } + public class TSPlayer { /// @@ -794,7 +818,25 @@ namespace TShockAPI /// /// The reason why the player was disabled. /// Whether or not to log this event to the console. + [Obsolete("Use Disable(string, DisableFlags)")] public virtual void Disable(string reason = "", bool displayConsole = true) + { + if (displayConsole) + { + Disable(reason, DisableFlags.WriteToConsole); + } + else + { + Disable(reason, DisableFlags.WriteToLog); + } + } + + /// + /// Disables the player for the given + /// + /// The reason why the player was disabled. + /// Flags to dictate where this event is logged to. + public virtual void Disable(string reason = "", DisableFlags flags = DisableFlags.WriteToLog) { LastThreat = DateTime.UtcNow; SetBuff(BuffID.Frozen, 330, true); @@ -810,14 +852,18 @@ namespace TShockAPI { if ((DateTime.UtcNow - LastDisableNotification).TotalMilliseconds > 5000) { - if (displayConsole) + if (flags.HasFlag(DisableFlags.WriteToConsole)) { - TShock.Log.ConsoleInfo("Player {0} has been disabled for {1}.", Name, reason); - } - else - { - TShock.Log.Info("Player {0} has been disabled for {1}.", Name, reason); + if (flags.HasFlag(DisableFlags.WriteToLog)) + { + TShock.Log.ConsoleInfo("Player {0} has been disabled for {1}.", Name, reason); + } + else + { + Server.SendInfoMessage("Player {0} has been disabled for {1}.", Name, reason); + } } + LastDisableNotification = DateTime.UtcNow; } } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 3f73cf7b..d9dafa7c 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -875,7 +875,7 @@ namespace TShockAPI { if (player.TileKillThreshold >= Config.TileKillThreshold) { - player.Disable("Reached TileKill threshold."); + player.Disable("Reached TileKill threshold.", DisableFlags.WriteToLogAndConsole); TSPlayer.Server.RevertTiles(player.TilesDestroyed); player.TilesDestroyed.Clear(); } @@ -891,7 +891,7 @@ namespace TShockAPI { if (player.TilePlaceThreshold >= Config.TilePlaceThreshold) { - player.Disable("Reached TilePlace threshold"); + player.Disable("Reached TilePlace threshold", DisableFlags.WriteToLogAndConsole); TSPlayer.Server.RevertTiles(player.TilesCreated); player.TilesCreated.Clear(); } @@ -932,7 +932,7 @@ namespace TShockAPI if (player.TileLiquidThreshold >= Config.TileLiquidThreshold) { - player.Disable("Reached TileLiquid threshold"); + player.Disable("Reached TileLiquid threshold", DisableFlags.WriteToLogAndConsole); } if (player.TileLiquidThreshold > 0) { @@ -941,7 +941,7 @@ namespace TShockAPI if (player.ProjectileThreshold >= Config.ProjectileThreshold) { - player.Disable("Reached projectile threshold"); + player.Disable("Reached projectile threshold", DisableFlags.WriteToLogAndConsole); } if (player.ProjectileThreshold > 0) { @@ -950,7 +950,7 @@ namespace TShockAPI if (player.PaintThreshold >= Config.TilePaintThreshold) { - player.Disable("Reached paint threshold"); + player.Disable("Reached paint threshold", DisableFlags.WriteToLogAndConsole); } if (player.PaintThreshold > 0) { @@ -961,80 +961,80 @@ namespace TShockAPI { player.Spawn(); } - string check = "none"; - foreach (Item item in player.TPlayer.inventory) + if (!Main.ServerSideCharacter || (Main.ServerSideCharacter && player.IsLoggedIn)) { - if (!player.Group.HasPermission(Permissions.ignorestackhackdetection) && (item.stack > item.maxStack || item.stack < 0) && - item.type != 0) + string check = "none"; + foreach (Item item in player.TPlayer.inventory) { - check = "Remove item " + item.name + " (" + item.stack + ") exceeds max stack of " + item.maxStack; - player.SendErrorMessage(check); - break; + if (!player.Group.HasPermission(Permissions.ignorestackhackdetection) && (item.stack > item.maxStack || item.stack < 0) && + item.type != 0) + { + check = "Remove item " + item.name + " (" + item.stack + ") exceeds max stack of " + item.maxStack; + player.SendErrorMessage(check); + break; + } } - } - player.IgnoreActionsForCheating = check; - check = "none"; - // Please don't remove this for the time being; without it, players wearing banned equipment will only get debuffed once - foreach (Item item in player.TPlayer.armor) - { - if (Itembans.ItemIsBanned(item.name, player)) + player.IgnoreActionsForCheating = check; + check = "none"; + // Please don't remove this for the time being; without it, players wearing banned equipment will only get debuffed once + foreach (Item item in player.TPlayer.armor) { - player.SetBuff(BuffID.Frozen, 330, true); - player.SetBuff(BuffID.Stoned, 330, true); - player.SetBuff(BuffID.Webbed, 330, true); - check = "Remove armor/accessory " + item.name; + if (Itembans.ItemIsBanned(item.name, player)) + { + player.SetBuff(BuffID.Frozen, 330, true); + player.SetBuff(BuffID.Stoned, 330, true); + player.SetBuff(BuffID.Webbed, 330, true); + check = "Remove armor/accessory " + item.name; - player.SendErrorMessage("You are wearing banned equipment. {0}", check); - break; + player.SendErrorMessage("You are wearing banned equipment. {0}", check); + break; + } } - } - foreach (Item item in player.TPlayer.dye) - { - if (Itembans.ItemIsBanned(item.name, player)) + foreach (Item item in player.TPlayer.dye) { - player.SetBuff(BuffID.Frozen, 330, true); - player.SetBuff(BuffID.Stoned, 330, true); - player.SetBuff(BuffID.Webbed, 330, true); - check = "Remove dye " + item.name; + if (Itembans.ItemIsBanned(item.name, player)) + { + player.SetBuff(BuffID.Frozen, 330, true); + player.SetBuff(BuffID.Stoned, 330, true); + player.SetBuff(BuffID.Webbed, 330, true); + check = "Remove dye " + item.name; - player.SendErrorMessage("You are wearing banned equipment. {0}", check); - break; + player.SendErrorMessage("You are wearing banned equipment. {0}", check); + break; + } } - } - foreach (Item item in player.TPlayer.miscEquips) - { - if (Itembans.ItemIsBanned(item.name, player)) + foreach (Item item in player.TPlayer.miscEquips) { - player.SetBuff(BuffID.Frozen, 330, true); - player.SetBuff(BuffID.Stoned, 330, true); - player.SetBuff(BuffID.Webbed, 330, true); - check = "Remove misc equip " + item.name; + if (Itembans.ItemIsBanned(item.name, player)) + { + player.SetBuff(BuffID.Frozen, 330, true); + player.SetBuff(BuffID.Stoned, 330, true); + player.SetBuff(BuffID.Webbed, 330, true); + check = "Remove misc equip " + item.name; - player.SendErrorMessage("You are wearing banned equipment. {0}", check); - break; + player.SendErrorMessage("You are wearing banned equipment. {0}", check); + break; + } } - } - foreach (Item item in player.TPlayer.miscDyes) - { - if (Itembans.ItemIsBanned(item.name, player)) + foreach (Item item in player.TPlayer.miscDyes) { - player.SetBuff(BuffID.Frozen, 330, true); - player.SetBuff(BuffID.Stoned, 330, true); - player.SetBuff(BuffID.Webbed, 330, true); - check = "Remove misc dye " + item.name; + if (Itembans.ItemIsBanned(item.name, player)) + { + player.SetBuff(BuffID.Frozen, 330, true); + player.SetBuff(BuffID.Stoned, 330, true); + player.SetBuff(BuffID.Webbed, 330, true); + check = "Remove misc dye " + item.name; - player.SendErrorMessage("You are wearing banned equipment. {0}", check); - break; + player.SendErrorMessage("You are wearing banned equipment. {0}", check); + break; + } + } + player.IgnoreActionsForDisabledArmor = check; + if (CheckIgnores(player) || Itembans.ItemIsBanned(player.TPlayer.inventory[player.TPlayer.selectedItem].name, player)) + { + DisableFlags flags = Config.DisableSecondUpdateLogs ? DisableFlags.WriteToConsole : DisableFlags.WriteToLogAndConsole; + player.Disable("check ignores failed in OnSecondUpdate()", flags); } - } - player.IgnoreActionsForDisabledArmor = check; - if (CheckIgnores(player)) - { - player.Disable("check ignores failed in OnSecondUpdate()", false); - } - else if (Itembans.ItemIsBanned(player.TPlayer.inventory[player.TPlayer.selectedItem].name, player)) - { - player.SetBuff(23, 120); //Cursed } var oldRegion = player.CurrentRegion; @@ -1044,12 +1044,12 @@ namespace TShockAPI { if (oldRegion != null) { - Hooks.RegionHooks.OnRegionLeft(player, oldRegion); + RegionHooks.OnRegionLeft(player, oldRegion); } if (player.CurrentRegion != null) { - Hooks.RegionHooks.OnRegionEntered(player, player.CurrentRegion); + RegionHooks.OnRegionEntered(player, player.CurrentRegion); } } }