Move motd event into GetMotd

This allows plugins to change MOTD for /fly,/ascend etc checks
This commit is contained in:
UnknownShadow200 2019-01-15 23:29:46 +11:00
parent 05a3de6fbe
commit 0efe8e9d4b
12 changed files with 49 additions and 45 deletions

View File

@ -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()));
}
}
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -209,12 +209,13 @@ namespace MCGalaxy.Events.PlayerEvents {
}
}
public delegate void OnSendingMotd(Player p, ref string motd);
/// <summary> Called when MOTD is being sent to a player. </summary>
public sealed class OnSendingMotdEvent : IEvent<OnSendingMotd> {
public delegate void OnGettingMotd(Player p, ref string motd);
/// <summary> Called when MOTD is being retrieved for a player. </summary>
/// <remarks> e.g. You can use this event to make one player always have +hax motd. </remarks>
public sealed class OnGettingMotdEvent : IEvent<OnGettingMotd> {
public static void Call(Player p, ref string motd) {
IEvent<OnSendingMotd>[] items = handlers.Items;
IEvent<OnGettingMotd>[] 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); }

View File

@ -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);

View File

@ -24,29 +24,29 @@ namespace MCGalaxy {
public static class Hacks {
/// <summary> Returns whether the player is currently able to use any hacks at all. </summary>
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;
}
/// <summary> Returns whether the player is currently able to fly. </summary>
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;
}
/// <summary> Returns whether the player is currently able to use noclip. </summary>
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;
}
/// <summary> Returns whether the player is currently able to move at faster speeds. </summary>
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;
}
/// <summary> Returns whether the player is currently able to respawn. </summary>
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;
}
/// <summary> Parses the MOTD flags and returns resulting HackControl packet. </summary>

View File

@ -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);

View File

@ -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);

View File

@ -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; }