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) { if (p.Game.Referee) {
p.Send(Packet.HackControl(true, true, true, true, true, -1)); p.Send(Packet.HackControl(true, true, true, true, true, -1));
} else { } 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 bool SuperUseable { get { return false; } }
public override void Use(Player p, string message, CommandData data) { 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.Message("You cannot use %T/Spawn %Son this map.");
p.isFlying = false; return; p.isFlying = false; return;
} }

View File

@ -28,7 +28,7 @@ namespace MCGalaxy.Commands.Misc {
public override bool SuperUseable { get { return false; } } public override bool SuperUseable { get { return false; } }
public override void Use(Player p, string message, CommandData data) { 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; p.Message("You cannot use %T/Ascend %Son this map."); return;
} }
int x = p.Pos.BlockX, y = p.Pos.BlockY, z = p.Pos.BlockZ; 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 bool SuperUseable { get { return false; } }
public override void Use(Player p, string message, CommandData data) { 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; 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 bool SuperUseable { get { return false; } }
public override void Use(Player p, string message, CommandData data) { 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.Message("You cannot use %T/Fly %Son this map.");
p.isFlying = false; return; p.isFlying = false; return;
} }

View File

@ -71,7 +71,7 @@ namespace MCGalaxy.Core {
if (p.Supports(CpeExt.InstantMOTD)) p.SendMapMotd(); if (p.Supports(CpeExt.InstantMOTD)) p.SendMapMotd();
p.SendCurrentEnv(); 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.Message("You cannot use %T/Fly %Son this map.");
p.isFlying = false; p.isFlying = false;
} }

View File

@ -209,12 +209,13 @@ namespace MCGalaxy.Events.PlayerEvents {
} }
} }
public delegate void OnSendingMotd(Player p, ref string motd); public delegate void OnGettingMotd(Player p, ref string motd);
/// <summary> Called when MOTD is being sent to a player. </summary> /// <summary> Called when MOTD is being retrieved for a player. </summary>
public sealed class OnSendingMotdEvent : IEvent<OnSendingMotd> { /// <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) { 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 // Can't use CallCommon because we need to pass arguments by ref
for (int i = 0; i < items.Length; i++) { for (int i = 0; i < items.Length; i++) {
try { items[i].method(p, ref motd); } try { items[i].method(p, ref motd); }

View File

@ -29,7 +29,7 @@ namespace MCGalaxy.Games {
static TimeSpan interval = TimeSpan.FromSeconds(5); static TimeSpan interval = TimeSpan.FromSeconds(5);
public static bool DetectNoclip(Player p, Position newPos) { 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; if (!p.IsLikelyInsideBlock() || p.Game.NoclipLog.AddSpamEntry(5, interval)) return false;
Warn(ref p.Game.LastNoclipWarn, p, "noclip"); Warn(ref p.Game.LastNoclipWarn, p, "noclip");
@ -37,7 +37,7 @@ namespace MCGalaxy.Games {
} }
public static bool DetectSpeedhack(Player p, Position newPos, float moveDist) { 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 dx = Math.Abs(p.Pos.X - newPos.X), dz = Math.Abs(p.Pos.Z - newPos.Z);
int maxMove = (int)(moveDist * 32); int maxMove = (int)(moveDist * 32);

View File

@ -24,29 +24,29 @@ namespace MCGalaxy {
public static class Hacks { public static class Hacks {
/// <summary> Returns whether the player is currently able to use any hacks at all. </summary> /// <summary> Returns whether the player is currently able to use any hacks at all. </summary>
public static bool CanUseHacks(Player p, Level lvl) { public static bool CanUseHacks(Player p) {
byte[] packet = MakeHackControl(p, lvl.GetMotd(p)); byte[] packet = MakeHackControl(p, p.GetMotd());
return packet[1] != 0 && packet[2] != 0 && packet[3] != 0 && packet[4] != 0 && packet[5] != 0; 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> /// <summary> Returns whether the player is currently able to fly. </summary>
public static bool CanUseFly(Player p, Level lvl) { public static bool CanUseFly(Player p) {
return MakeHackControl(p, lvl.GetMotd(p))[1] != 0; return MakeHackControl(p, p.GetMotd())[1] != 0;
} }
/// <summary> Returns whether the player is currently able to use noclip. </summary> /// <summary> Returns whether the player is currently able to use noclip. </summary>
public static bool CanUseNoclip(Player p, Level lvl) { public static bool CanUseNoclip(Player p) {
return MakeHackControl(p, lvl.GetMotd(p))[2] != 0; return MakeHackControl(p, p.GetMotd())[2] != 0;
} }
/// <summary> Returns whether the player is currently able to move at faster speeds. </summary> /// <summary> Returns whether the player is currently able to move at faster speeds. </summary>
public static bool CanUseSpeed(Player p, Level lvl) { public static bool CanUseSpeed(Player p) {
return MakeHackControl(p, lvl.GetMotd(p))[3] != 0; return MakeHackControl(p, p.GetMotd())[3] != 0;
} }
/// <summary> Returns whether the player is currently able to respawn. </summary> /// <summary> Returns whether the player is currently able to respawn. </summary>
public static bool CanUseRespawn(Player p, Level lvl) { public static bool CanUseRespawn(Player p) {
return MakeHackControl(p, lvl.GetMotd(p))[4] != 0; return MakeHackControl(p, p.GetMotd())[4] != 0;
} }
/// <summary> Parses the MOTD flags and returns resulting HackControl packet. </summary> /// <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.Commands;
using MCGalaxy.DB; using MCGalaxy.DB;
using MCGalaxy.Events.LevelEvents; using MCGalaxy.Events.LevelEvents;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Games; using MCGalaxy.Games;
using MCGalaxy.Generator; using MCGalaxy.Generator;
using MCGalaxy.Levels.IO; 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) { public Zone FindZoneExact(string name) {
Zone[] zones = Zones.Items; Zone[] zones = Zones.Items;
foreach (Zone zone in zones) { foreach (Zone zone in zones) {

View File

@ -150,10 +150,9 @@ namespace MCGalaxy {
} }
public void SendMapMotd() { public void SendMapMotd() {
string motd = level.GetMotd(this); string motd = GetMotd();
motd = Chat.Format(motd, this); motd = Chat.Format(motd, this);
OnSendingMotdEvent.Call(this, ref motd);
byte[] packet = Packet.Motd(this, motd); byte[] packet = Packet.Motd(this, motd);
Send(packet); Send(packet);

View File

@ -106,6 +106,18 @@ namespace MCGalaxy {
return BlockBindings[RawHeldBlock]; 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() { public void SetPrefix() {
prefix = Game.Referee ? "&2[Ref] " : ""; prefix = Game.Referee ? "&2[Ref] " : "";
if (GroupPrefix.Length > 0) { prefix += GroupPrefix + color; } if (GroupPrefix.Length > 0) { prefix += GroupPrefix + color; }