From 0efe8e9d4b2d233e763651bf86fe6cad4cc44b39 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 15 Jan 2019 23:29:46 +1100 Subject: [PATCH] Move motd event into GetMotd This allows plugins to change MOTD for /fly,/ascend etc checks --- MCGalaxy/Commands/Fun/CmdReferee.cs | 2 +- MCGalaxy/Commands/World/CmdSpawn.cs | 2 +- MCGalaxy/Commands/other/CmdAscend.cs | 2 +- MCGalaxy/Commands/other/CmdDescend.cs | 2 +- MCGalaxy/Commands/other/CmdFly.cs | 2 +- MCGalaxy/CorePlugin/MiscHandlers.cs | 2 +- MCGalaxy/Events/PlayerEvents.cs | 9 +++---- MCGalaxy/Games/MovementCheck.cs | 4 ++-- MCGalaxy/Levels/Hacks.cs | 20 ++++++++-------- MCGalaxy/Levels/Level.cs | 34 ++++++++++----------------- MCGalaxy/Network/Player.Networking.cs | 3 +-- MCGalaxy/Player/Player.cs | 12 ++++++++++ 12 files changed, 49 insertions(+), 45 deletions(-) diff --git a/MCGalaxy/Commands/Fun/CmdReferee.cs b/MCGalaxy/Commands/Fun/CmdReferee.cs index d13635afe..b4149d2dd 100644 --- a/MCGalaxy/Commands/Fun/CmdReferee.cs +++ b/MCGalaxy/Commands/Fun/CmdReferee.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.Fun { if (p.Game.Referee) { p.Send(Packet.HackControl(true, true, true, true, true, -1)); } else { - p.Send(Hacks.MakeHackControl(p, p.level.GetMotd(p))); + p.Send(Hacks.MakeHackControl(p, p.GetMotd())); } } } diff --git a/MCGalaxy/Commands/World/CmdSpawn.cs b/MCGalaxy/Commands/World/CmdSpawn.cs index 25cd0e428..46964361b 100644 --- a/MCGalaxy/Commands/World/CmdSpawn.cs +++ b/MCGalaxy/Commands/World/CmdSpawn.cs @@ -25,7 +25,7 @@ namespace MCGalaxy.Commands.World { public override bool SuperUseable { get { return false; } } public override void Use(Player p, string message, CommandData data) { - if (!Hacks.CanUseRespawn(p, p.level)) { + if (!Hacks.CanUseRespawn(p)) { p.Message("You cannot use %T/Spawn %Son this map."); p.isFlying = false; return; } diff --git a/MCGalaxy/Commands/other/CmdAscend.cs b/MCGalaxy/Commands/other/CmdAscend.cs index a98d156f4..760dab1f2 100644 --- a/MCGalaxy/Commands/other/CmdAscend.cs +++ b/MCGalaxy/Commands/other/CmdAscend.cs @@ -28,7 +28,7 @@ namespace MCGalaxy.Commands.Misc { public override bool SuperUseable { get { return false; } } public override void Use(Player p, string message, CommandData data) { - if (!Hacks.CanUseHacks(p, p.level)) { + if (!Hacks.CanUseHacks(p)) { p.Message("You cannot use %T/Ascend %Son this map."); return; } int x = p.Pos.BlockX, y = p.Pos.BlockY, z = p.Pos.BlockZ; diff --git a/MCGalaxy/Commands/other/CmdDescend.cs b/MCGalaxy/Commands/other/CmdDescend.cs index 933d5451f..740d2fae7 100644 --- a/MCGalaxy/Commands/other/CmdDescend.cs +++ b/MCGalaxy/Commands/other/CmdDescend.cs @@ -28,7 +28,7 @@ namespace MCGalaxy.Commands.Misc { public override bool SuperUseable { get { return false; } } public override void Use(Player p, string message, CommandData data) { - if (!Hacks.CanUseHacks(p, p.level)) { + if (!Hacks.CanUseHacks(p)) { p.Message("You cannot use %T/Descend %Son this map."); return; } diff --git a/MCGalaxy/Commands/other/CmdFly.cs b/MCGalaxy/Commands/other/CmdFly.cs index 6c52d0d5f..17e6a66c7 100644 --- a/MCGalaxy/Commands/other/CmdFly.cs +++ b/MCGalaxy/Commands/other/CmdFly.cs @@ -28,7 +28,7 @@ namespace MCGalaxy.Commands.Misc { public override bool SuperUseable { get { return false; } } public override void Use(Player p, string message, CommandData data) { - if (!Hacks.CanUseFly(p, p.level)) { + if (!Hacks.CanUseFly(p)) { p.Message("You cannot use %T/Fly %Son this map."); p.isFlying = false; return; } diff --git a/MCGalaxy/CorePlugin/MiscHandlers.cs b/MCGalaxy/CorePlugin/MiscHandlers.cs index e3bac86b2..6c3111032 100644 --- a/MCGalaxy/CorePlugin/MiscHandlers.cs +++ b/MCGalaxy/CorePlugin/MiscHandlers.cs @@ -71,7 +71,7 @@ namespace MCGalaxy.Core { if (p.Supports(CpeExt.InstantMOTD)) p.SendMapMotd(); p.SendCurrentEnv(); - if (p.isFlying && !Hacks.CanUseFly(p, p.level)) { + if (p.isFlying && !Hacks.CanUseFly(p)) { p.Message("You cannot use %T/Fly %Son this map."); p.isFlying = false; } diff --git a/MCGalaxy/Events/PlayerEvents.cs b/MCGalaxy/Events/PlayerEvents.cs index 0982f5de4..1f45ed280 100644 --- a/MCGalaxy/Events/PlayerEvents.cs +++ b/MCGalaxy/Events/PlayerEvents.cs @@ -209,12 +209,13 @@ namespace MCGalaxy.Events.PlayerEvents { } } - public delegate void OnSendingMotd(Player p, ref string motd); - /// Called when MOTD is being sent to a player. - public sealed class OnSendingMotdEvent : IEvent { + public delegate void OnGettingMotd(Player p, ref string motd); + /// Called when MOTD is being retrieved for a player. + /// e.g. You can use this event to make one player always have +hax motd. + public sealed class OnGettingMotdEvent : IEvent { public static void Call(Player p, ref string motd) { - IEvent[] items = handlers.Items; + IEvent[] items = handlers.Items; // Can't use CallCommon because we need to pass arguments by ref for (int i = 0; i < items.Length; i++) { try { items[i].method(p, ref motd); } diff --git a/MCGalaxy/Games/MovementCheck.cs b/MCGalaxy/Games/MovementCheck.cs index 8a75fa54b..0be27b0c5 100644 --- a/MCGalaxy/Games/MovementCheck.cs +++ b/MCGalaxy/Games/MovementCheck.cs @@ -29,7 +29,7 @@ namespace MCGalaxy.Games { static TimeSpan interval = TimeSpan.FromSeconds(5); public static bool DetectNoclip(Player p, Position newPos) { - if (p.Game.Referee || Hacks.CanUseNoclip(p, p.level)) return false; + if (p.Game.Referee || Hacks.CanUseNoclip(p)) return false; if (!p.IsLikelyInsideBlock() || p.Game.NoclipLog.AddSpamEntry(5, interval)) return false; Warn(ref p.Game.LastNoclipWarn, p, "noclip"); @@ -37,7 +37,7 @@ namespace MCGalaxy.Games { } public static bool DetectSpeedhack(Player p, Position newPos, float moveDist) { - if (p.Game.Referee || Hacks.CanUseSpeed(p, p.level)) return false; + if (p.Game.Referee || Hacks.CanUseSpeed(p)) return false; int dx = Math.Abs(p.Pos.X - newPos.X), dz = Math.Abs(p.Pos.Z - newPos.Z); int maxMove = (int)(moveDist * 32); diff --git a/MCGalaxy/Levels/Hacks.cs b/MCGalaxy/Levels/Hacks.cs index ab142d658..af510927d 100644 --- a/MCGalaxy/Levels/Hacks.cs +++ b/MCGalaxy/Levels/Hacks.cs @@ -24,29 +24,29 @@ namespace MCGalaxy { public static class Hacks { /// Returns whether the player is currently able to use any hacks at all. - public static bool CanUseHacks(Player p, Level lvl) { - byte[] packet = MakeHackControl(p, lvl.GetMotd(p)); + public static bool CanUseHacks(Player p) { + byte[] packet = MakeHackControl(p, p.GetMotd()); return packet[1] != 0 && packet[2] != 0 && packet[3] != 0 && packet[4] != 0 && packet[5] != 0; } /// Returns whether the player is currently able to fly. - public static bool CanUseFly(Player p, Level lvl) { - return MakeHackControl(p, lvl.GetMotd(p))[1] != 0; + public static bool CanUseFly(Player p) { + return MakeHackControl(p, p.GetMotd())[1] != 0; } /// Returns whether the player is currently able to use noclip. - public static bool CanUseNoclip(Player p, Level lvl) { - return MakeHackControl(p, lvl.GetMotd(p))[2] != 0; + public static bool CanUseNoclip(Player p) { + return MakeHackControl(p, p.GetMotd())[2] != 0; } /// Returns whether the player is currently able to move at faster speeds. - public static bool CanUseSpeed(Player p, Level lvl) { - return MakeHackControl(p, lvl.GetMotd(p))[3] != 0; + public static bool CanUseSpeed(Player p) { + return MakeHackControl(p, p.GetMotd())[3] != 0; } /// Returns whether the player is currently able to respawn. - public static bool CanUseRespawn(Player p, Level lvl) { - return MakeHackControl(p, lvl.GetMotd(p))[4] != 0; + public static bool CanUseRespawn(Player p) { + return MakeHackControl(p, p.GetMotd())[4] != 0; } /// Parses the MOTD flags and returns resulting HackControl packet. diff --git a/MCGalaxy/Levels/Level.cs b/MCGalaxy/Levels/Level.cs index 792368a12..fb4beee6d 100644 --- a/MCGalaxy/Levels/Level.cs +++ b/MCGalaxy/Levels/Level.cs @@ -24,6 +24,7 @@ using MCGalaxy.Bots; using MCGalaxy.Commands; using MCGalaxy.DB; using MCGalaxy.Events.LevelEvents; +using MCGalaxy.Events.PlayerEvents; using MCGalaxy.Games; using MCGalaxy.Generator; using MCGalaxy.Levels.IO; @@ -96,15 +97,6 @@ namespace MCGalaxy { } } - public string GetMotd(Player p) { - Zone zone = p.ZoneIn; - string zoneMOTD = zone == null ? null : zone.Config.MOTD; - if (zoneMOTD != null && zoneMOTD != "ignore") return zoneMOTD; - - if (Config.MOTD != "ignore") return Config.MOTD; - return String.IsNullOrEmpty(p.group.MOTD) ? Server.Config.MOTD : p.group.MOTD; - } - public Zone FindZoneExact(string name) { Zone[] zones = Zones.Items; foreach (Zone zone in zones) { @@ -152,7 +144,7 @@ namespace MCGalaxy { BotsFile.Save(this); PlayerBot.RemoveLoadedBots(this, false); } - } catch (Exception ex) { + } catch (Exception ex) { Logger.LogError("Error saving bots", ex); } @@ -234,7 +226,7 @@ namespace MCGalaxy { File.Copy(path + ".backup", path); SaveSettings(); - Logger.Log(LogType.SystemActivity, "SAVED: Level \"{0}\". ({1}/{2}/{3})", + Logger.Log(LogType.SystemActivity, "SAVED: Level \"{0}\". ({1}/{2}/{3})", name, players.Count, PlayerInfo.Online.Count, Server.Config.MaxPlayers); Changed = false; } @@ -246,7 +238,7 @@ namespace MCGalaxy { public string Backup(bool force = false, string backup = "") { if (!backedup || force) { string backupPath = LevelInfo.BackupBasePath(name); - if (!Directory.Exists(backupPath)) Directory.CreateDirectory(backupPath); + if (!Directory.Exists(backupPath)) Directory.CreateDirectory(backupPath); int next = LevelInfo.LatestBackup(name) + 1; if (backup.Length == 0) backup = next.ToString(); @@ -299,7 +291,7 @@ namespace MCGalaxy { lvl.Config.JailZ = (ushort)(lvl.spawnz * 32); lvl.Config.jailrotx = lvl.rotx; lvl.Config.jailroty = lvl.roty; - + try { string propsPath = LevelInfo.PropsPath(lvl.MapName); bool propsExisted = lvl.Config.Load(propsPath); @@ -364,18 +356,18 @@ namespace MCGalaxy { [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct UndoPos { - public int Index; + public int Index; int flags; BlockRaw oldRaw, newRaw; - public BlockID OldBlock { + public BlockID OldBlock { get { return (BlockID)(oldRaw | ((flags & 0x03) << Block.ExtendedShift)); } } - public BlockID NewBlock { + public BlockID NewBlock { get { return (BlockID)(newRaw | (((flags & 0xC >> 2)) << Block.ExtendedShift)); } } - public DateTime Time { - get { return Server.StartTime.AddTicks((flags >> 4) * TimeSpan.TicksPerSecond); } + public DateTime Time { + get { return Server.StartTime.AddTicks((flags >> 4) * TimeSpan.TicksPerSecond); } } public void SetData(BlockID oldBlock, BlockID newBlock) { @@ -396,13 +388,13 @@ namespace MCGalaxy { public void UpdateBlockProps() { LoadDefaultProps(); string propsPath = BlockProps.PropsPath("_" + MapName); - + // backwards compatibility with older versions if (!File.Exists(propsPath)) { BlockProps.Load("lvl_" + MapName, Props, 2, true); } else { BlockProps.Load("_" + MapName, Props, 2, false); - } + } } public void UpdateBlockHandlers() { @@ -412,7 +404,7 @@ namespace MCGalaxy { } public void UpdateBlockHandler(BlockID block) { - bool nonSolid = !MCGalaxy.Blocks.CollideType.IsSolid(CollideType(block)); + bool nonSolid = !MCGalaxy.Blocks.CollideType.IsSolid(CollideType(block)); deleteHandlers[block] = BlockBehaviour.GetDeleteHandler(block, Props); placeHandlers[block] = BlockBehaviour.GetPlaceHandler(block, Props); walkthroughHandlers[block] = BlockBehaviour.GetWalkthroughHandler(block, Props, nonSolid); diff --git a/MCGalaxy/Network/Player.Networking.cs b/MCGalaxy/Network/Player.Networking.cs index b69cb71b8..f70449083 100644 --- a/MCGalaxy/Network/Player.Networking.cs +++ b/MCGalaxy/Network/Player.Networking.cs @@ -150,10 +150,9 @@ namespace MCGalaxy { } public void SendMapMotd() { - string motd = level.GetMotd(this); + string motd = GetMotd(); motd = Chat.Format(motd, this); - OnSendingMotdEvent.Call(this, ref motd); byte[] packet = Packet.Motd(this, motd); Send(packet); diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 8862c3f22..0f11d1ae1 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -106,6 +106,18 @@ namespace MCGalaxy { return BlockBindings[RawHeldBlock]; } + public string GetMotd() { + Zone zone = ZoneIn; + string motd = zone == null ? "ignore" : zone.Config.MOTD; + + // fallback to level MOTD, then rank MOTD, then server MOTD + if (motd == "ignore") motd = level.Config.MOTD; + if (motd == "ignore") motd = String.IsNullOrEmpty(group.MOTD) ? Server.Config.MOTD : group.MOTD; + + OnGettingMotdEvent.Call(this, ref motd); + return motd; + } + public void SetPrefix() { prefix = Game.Referee ? "&2[Ref] " : ""; if (GroupPrefix.Length > 0) { prefix += GroupPrefix + color; }