Now messages appropriately go to IRC opchannel, some other minor code cleanup

This commit is contained in:
UnknownShadow200 2018-06-11 23:21:12 +10:00
parent 8c6a18a096
commit 52d882cbc3
26 changed files with 151 additions and 137 deletions

View File

@ -123,7 +123,7 @@ namespace MCGalaxy {
Player[] players = PlayerInfo.Online.Items;
ChatMessageFilter scopeFilter = scopeFilters[(int)scope];
OnChatSysEvent.Call(scope, ref msg, arg, ref filter, irc);
OnChatSysEvent.Call(scope, msg, arg, ref filter, irc);
foreach (Player pl in players) {
if (!scopeFilter(pl, arg)) continue;
if (filter != null && !filter(pl, arg)) continue;
@ -157,7 +157,7 @@ namespace MCGalaxy {
ChatMessageFilter scopeFilter = scopeFilters[(int)scope];
if (source == null) source = ConsolePlayer.Instance;
OnChatFromEvent.Call(scope, source, ref msg, arg, ref filter, irc);
OnChatFromEvent.Call(scope, source, msg, arg, ref filter, irc);
foreach (Player pl in players) {
if (!scopeFilter(pl, arg)) continue;
if (filter != null && !filter(pl, arg)) continue;
@ -184,7 +184,7 @@ namespace MCGalaxy {
ChatMessageFilter scopeFilter = scopeFilters[(int)scope];
if (source == null) source = ConsolePlayer.Instance;
OnChatEvent.Call(scope, source, ref msg, arg, ref filter, irc);
OnChatEvent.Call(scope, source, msg, arg, ref filter, irc);
foreach (Player pl in players) {
if (!NotIgnoring(pl, source)) continue;
// Always show message to self too (unless ignoring self)

View File

@ -39,7 +39,7 @@ namespace MCGalaxy.Commands.Info {
TopStat stat = FindTopStat(args[1]);
if (stat == null) {
Player.Message(p, "/Top: Unrecognised type \"{0}\".", args[1]); return;
Player.Message(p, "&cUnrecognised type \"{0}\".", args[1]); return;
}
if (args.Length > 2) {

View File

@ -50,8 +50,8 @@ namespace MCGalaxy.Commands.Info {
}
public override void Help(Player p) {
Player.Message(p, "/viewranks [rank] - Shows all players who have [rank]");
Player.Message(p, "/viewranks banned - Shows all players who are banned.");
Player.Message(p, "%T/viewranks [rank] %H- Shows all players who have [rank]");
Player.Message(p, "%T/viewranks banned %H- Shows all players who are banned");
Player.Message(p, "Available ranks: " + Group.GroupList.Join(g => g.ColoredName));
}
}

View File

@ -38,8 +38,7 @@ namespace MCGalaxy.Commands.Moderation {
reason = ModActionCmd.ExpandReason(p, reason);
if (reason == null) return;
Player who = PlayerInfo.FindExact(target);
Group group = who != null ? who.group : Group.GroupIn(target);
Group group = PlayerInfo.GetGroup(target);
if (!CheckPerms(target, group, p)) return;
ModAction action = new ModAction(target, p, ModActionType.Ban, reason);

View File

@ -58,7 +58,7 @@ namespace MCGalaxy.Commands.Moderation {
if (accounts == null || accounts.Count == 0) return true;
foreach (string name in accounts) {
Group grp = Group.GroupIn(name);
Group grp = PlayerInfo.GetGroup(name);
if (grp == null || grp.Permission < p.Rank) continue;
Player.Message(p, "You can only IP ban IPs used by players with a lower rank.");

View File

@ -95,7 +95,7 @@ namespace MCGalaxy.Commands.Moderation {
Player.Message(p, "&9Players in the review queue:");
int pos = 1;
foreach (string name in Server.reviewlist) {
Group grp = Group.GroupIn(name);
Group grp = PlayerInfo.GetGroup(name);
Player.Message(p, "&a" + pos + ". &f" + name + "&a - Current Rank: " + grp.ColoredName);
pos++;
}

View File

@ -34,43 +34,44 @@ namespace MCGalaxy.Commands.Moderation {
public override void Use(Player p, string message) {
string[] args = message.SplitSpaces(3);
if (args.Length < 2) { Help(p); return; }
string rankName = null, name = null;
string rankName, target;
string reason = args.Length > 2 ? args[2] : null;
if (args[0].CaselessEq("+up")) {
rankName = args[0];
name = ModActionCmd.FindName(p, "promote", "Promote", "", args[1], ref reason);
target = ModActionCmd.FindName(p, "promote", "Promote", "", args[1], ref reason);
} else if (args[0].CaselessEq("-down")) {
rankName = args[0];
name = ModActionCmd.FindName(p, "demote", "Demote", "", args[1], ref reason);
target = ModActionCmd.FindName(p, "demote", "Demote", "", args[1], ref reason);
} else {
rankName = args[1];
name = ModActionCmd.FindName(p, "rank", "Rank", " " + rankName, args[0], ref reason);
target = ModActionCmd.FindName(p, "rank", "Rank", " " + rankName, args[0], ref reason);
}
if (name == null) return;
Player who = PlayerInfo.FindExact(name);
if (p == who && who != null) { Player.Message(p, "Cannot change your own rank."); return; }
if (target == null) return;
if (p != null && p.name.CaselessEq(target)) {
Player.Message(p, "Cannot change your own rank."); return;
}
Group curRank = who != null ? who.group : PlayerInfo.GetGroup(name);
Group curRank = PlayerInfo.GetGroup(target);
Group newRank = TargetRank(p, rankName, curRank);
if (newRank == null) return;
if (curRank == newRank) {
Player.Message(p, "{0} %Sis already ranked {1}",
PlayerInfo.GetColoredName(p, name), curRank.ColoredName);
PlayerInfo.GetColoredName(p, target), curRank.ColoredName);
return;
}
if (!CanChangeRank(name, curRank, newRank, who, p, ref reason)) return;
if (!CanChangeRank(target, curRank, newRank, p, ref reason)) return;
ModAction action = new ModAction(name, p, ModActionType.Rank, reason);
ModAction action = new ModAction(target, p, ModActionType.Rank, reason);
action.targetGroup = curRank;
action.Metadata = newRank;
OnModActionEvent.Call(action);
}
internal static bool CanChangeRank(string name, Group curRank, Group newRank,
Player who, Player p, ref string reason) {
Player p, ref string reason) {
Group banned = Group.BannedRank;
if (reason == null) {
reason = newRank.Permission >= curRank.Permission ?

View File

@ -34,9 +34,8 @@ namespace MCGalaxy.Commands.Moderation {
args.Length == 1 ? "" : " " + args[1],
args[0], ref reason);
if (target == null) return;
Player who = PlayerInfo.FindExact(target);
Group grp = who == null ? PlayerInfo.GetGroup(target) : who.group;
Group grp = PlayerInfo.GetGroup(target);
if (p != null && grp.Permission >= p.Rank) {
MessageTooHighRank(p, "temp ban", false); return;
}

View File

@ -46,7 +46,6 @@ namespace MCGalaxy.Commands.Moderation {
static void Assign(Player p, string[] args) {
string target = PlayerInfo.FindMatchesPreferOnline(p, args[0]);
if (target == null) return;
Player who = PlayerInfo.FindExact(target);
Group newRank = Matcher.FindRanks(p, args[1]);
if (newRank == null) return;
@ -57,12 +56,13 @@ namespace MCGalaxy.Commands.Moderation {
Player.Message(p, "&cThe player already has a temporary rank assigned!"); return;
}
if (p != null && who != null && p == who) {
if (p != null && p.name.CaselessEq(target)) {
Player.Message(p, "&cYou cannot assign yourself a temporary rank."); return;
}
Group curRank = who != null ? who.group : Group.GroupIn(target);
Group curRank = PlayerInfo.GetGroup(target);
string reason = args.Length > 3 ? args[3] : "assigning temp rank";
if (!CmdSetRank.CanChangeRank(target, curRank, newRank, who, p, ref reason)) return;
if (!CmdSetRank.CanChangeRank(target, curRank, newRank, p, ref reason)) return;
ModAction action = new ModAction(target, p, ModActionType.Rank, reason, duration);
action.targetGroup = curRank;
@ -79,14 +79,13 @@ namespace MCGalaxy.Commands.Moderation {
}
string[] parts = line.SplitSpaces();
Player who = PlayerInfo.FindExact(target);
Group curRank = who != null ? who.group : Group.GroupIn(target);
Group curRank = PlayerInfo.GetGroup(target);
Group oldRank = Group.Find(parts[4 - 1]); // -1 because data, not whole line
if (oldRank == null) return;
string reason = "temp rank unassigned";
if (!CmdSetRank.CanChangeRank(target, curRank, oldRank, who, p, ref reason)) return;
if (!CmdSetRank.CanChangeRank(target, curRank, oldRank, p, ref reason)) return;
ModAction action = new ModAction(target, p, ModActionType.Rank, reason);
action.Metadata = oldRank;

View File

@ -108,7 +108,7 @@ namespace MCGalaxy.Commands.Moderation {
names[i] = PlayerInfo.FindOfflineNameMatches(p, parts[i]);
if (names[i] == null) return null;
Group grp = Group.GroupIn(names[i]);
Group grp = PlayerInfo.GetGroup(names[i]);
bool canUndo = p == null || grp.Permission < p.Rank || p.name.CaselessEq(names[i]);
if (!canUndo) {
MessageTooHighRank(p, "undo", false); return null;

View File

@ -37,10 +37,10 @@ namespace MCGalaxy.Commands.Building {
BrushFactory brush = BrushFactory.Find(args[0]);
if (args[0].CaselessEq("list")) {
Player.Message(p, "%HAvailable brushes: %S" + BrushFactory.Available);
List(p);
} else if (brush == null) {
Player.Message(p, "No brush found with name \"{0}\".", args[0]);
Player.Message(p, "Available brushes: " + BrushFactory.Available);
List(p);
} else {
Player.Message(p, "Set your brush to: " + brush.Name);
p.BrushName = brush.Name;
@ -48,12 +48,16 @@ namespace MCGalaxy.Commands.Building {
}
}
internal static void List(Player p) {
Player.Message(p, "%HAvailable brushes: &f" + BrushFactory.Brushes.Join(b => b.Name));
}
public override void Help(Player p) {
Player.Message(p, "%T/Brush [name] <default brush args>");
Player.Message(p, "%HSets your current brush to the brush with that name.");
Player.Message(p, "%T/Help Brush [name]");
Player.Message(p, "%HOutputs the help for the brush with that name.");
Player.Message(p, "%HAvailable brushes: %S" + BrushFactory.Available);
List(p);
Player.Message(p, "%H- If \"skip\" is used for a block name, " +
"existing blocks in the map will not be replaced by this block.");
}
@ -62,7 +66,7 @@ namespace MCGalaxy.Commands.Building {
BrushFactory brush = BrushFactory.Find(message);
if (brush == null) {
Player.Message(p, "No brush found with name \"{0}\".", message);
Player.Message(p, "%HAvailable brushes: %S" + BrushFactory.Available);
List(p);
} else {
Player.MessageLines(p, brush.Help);
}

View File

@ -45,7 +45,7 @@ namespace MCGalaxy.Commands.Building {
BrushFactory factory = BrushFactory.Find(args[1]);
if (factory == null) {
Player.Message(p, "No brush found with name \"{0}\".", args[1]);
Player.Message(p, "Available brushes: " + BrushFactory.Available);
CmdBrush.List(p);
return null;
}

View File

@ -37,10 +37,10 @@ namespace MCGalaxy.Commands.Building {
TransformFactory transform = TransformFactory.Find(args[0]);
if (args[0].CaselessEq("list")) {
Player.Message(p, "%HAvailable transforms: %S" + TransformFactory.Available);
List(p);
} else if (transform == null) {
Player.Message(p, "No transform found with name \"{0}\".", args[0]);
Player.Message(p, "Available transforms: " + TransformFactory.Available);
List(p);
} else {
Player.Message(p, "Set your transform to: " + transform.Name);
Transform instance = transform.Construct(p, args.Length == 1 ? "" : args[1]);
@ -49,19 +49,23 @@ namespace MCGalaxy.Commands.Building {
}
}
static void List(Player p) {
Player.Message(p, "%HAvailable transforms: &f" + TransformFactory.Transforms.Join(t => t.Name));
}
public override void Help(Player p) {
Player.Message(p, "%T/Transform [name] <transform args>");
Player.Message(p, "%HSets your current transform to the transform with that name.");
Player.Message(p, "%T/Help Transform [name]");
Player.Message(p, "%HOutputs the help for the transform with that name.");
Player.Message(p, "%HAvailable transforms: %S" + TransformFactory.Available);
List(p);
}
public override void Help(Player p, string message) {
TransformFactory transform = TransformFactory.Find(message);
if (transform == null) {
Player.Message(p, "No transform found with name \"{0}\".", message);
Player.Message(p, "%HAvailable transforms: %S" + TransformFactory.Available);
List(p);
} else {
Player.MessageLines(p, transform.Help);
}

View File

@ -22,7 +22,7 @@ namespace MCGalaxy.Core {
internal static class ChatHandler {
internal static void HandleOnChat(ChatScope scope, Player source, ref string msg,
internal static void HandleOnChat(ChatScope scope, Player source, string msg,
object arg, ref ChatMessageFilter filter, bool irc) {
msg = msg.Replace("λFULL", source.name).Replace("λNICK", source.name);
LogType logType = LogType.PlayerChat;

View File

@ -146,7 +146,7 @@ namespace MCGalaxy.Core {
static void LogIPAction(ModAction e, string type) {
LevelPermission perm = CommandExtraPerms.MinPerm("WhoIs");
Chat.Message(ChatScope.BelowRank, e.FormatMessage("An IP", type), perm, null, true);
Chat.Message(ChatScope.AboveEqRank, e.FormatMessage(e.TargetName, type), perm, null, true);
Chat.Message(ChatScope.AboveEqRank, e.FormatMessage("An IP", type), perm, null, true);
}
static void DoBanIP(ModAction e) {

View File

@ -44,7 +44,7 @@ namespace MCGalaxy.DB {
string color = data.Color.Length == 0 ? group.Color : data.Color;
string prefix = data.Title.Length == 0 ? "" : color + "[" + data.TitleColor + data.Title + color + "] ";
string fullName = prefix + color + data.Name.RemoveLastPlus();
OnlineStat.CoreLine(p, fullName, data.Name, group);
OnlineStat.CoreLine(p, fullName, data.Name, group, data.Messages);
}
static void BlocksModifiedLine(Player p, PlayerData who) {

View File

@ -46,12 +46,12 @@ namespace MCGalaxy.DB {
static void OnlineCoreLine(Player p, Player who) {
string prefix = who.title.Length == 0 ? "" : who.color + "[" + who.titlecolor + who.title + who.color + "] ";
string fullName = prefix + who.ColoredName;
CoreLine(p, fullName, who.name, who.group);
CoreLine(p, fullName, who.name, who.group, who.TotalMessagesSent);
}
internal static void CoreLine(Player p, string fullName, string name, Group grp) {
Player.Message(p, fullName + " %S(" + name + ") has:");
Player.Message(p, " Rank of " + grp.ColoredName);
internal static void CoreLine(Player p, string fullName, string name, Group grp, int messages) {
Player.Message(p, "{0} %S({1}) has:", fullName, name);
Player.Message(p, " Rank of {0}%S, wrote &a{1} %Smessages", grp.ColoredName, messages);
}
internal static void MiscLine(Player p, string name, int deaths, int money) {

View File

@ -41,8 +41,6 @@ namespace MCGalaxy.Drawing.Brushes {
new RandomBrushFactory(), new CloudyBrushFactory(),
};
public static string Available { get { return Brushes.Join(b => b.Name); } }
public static BrushFactory Find(string name) {
foreach (BrushFactory entry in Brushes) {
if (entry.Name.CaselessEq(name)) return entry;

View File

@ -38,8 +38,6 @@ namespace MCGalaxy.Drawing.Transforms {
new RotateTransformFactory(),
};
public static string Available { get { return Transforms.Join(b => b.Name); } }
public static TransformFactory Find(string name) {
foreach (TransformFactory entry in Transforms) {
if (entry.Name.CaselessEq(name)) return entry;

View File

@ -35,8 +35,7 @@ namespace MCGalaxy.Events {
public Group TargetGroup {
get {
if (targetGroup != null) return targetGroup;
Player who = PlayerInfo.FindExact(Target);
targetGroup = who != null ? who.group : Group.GroupIn(Target);
targetGroup = PlayerInfo.GetGroup(Target);
return targetGroup;
}
}

View File

@ -43,43 +43,43 @@ namespace MCGalaxy.Events.ServerEvents {
}
}
public delegate void OnChatSys(ChatScope scope, ref string msg, object arg,
public delegate void OnChatSys(ChatScope scope, string msg, object arg,
ref ChatMessageFilter filter, bool irc);
public sealed class OnChatSysEvent : IEvent<OnChatSys> {
public static void Call(ChatScope scope, ref string msg, object arg,
public static void Call(ChatScope scope, string msg, object arg,
ref ChatMessageFilter filter, bool irc) {
IEvent<OnChatSys>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(scope, ref msg, arg, ref filter, irc); }
try { items[i].method(scope, msg, arg, ref filter, irc); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}
public delegate void OnChatFrom(ChatScope scope, Player source, ref string msg,
public delegate void OnChatFrom(ChatScope scope, Player source, string msg,
object arg, ref ChatMessageFilter filter, bool irc);
public sealed class OnChatFromEvent : IEvent<OnChatFrom> {
public static void Call(ChatScope scope,Player source, ref string msg,
public static void Call(ChatScope scope,Player source, string msg,
object arg, ref ChatMessageFilter filter, bool irc) {
IEvent<OnChatFrom>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(scope, source, ref msg, arg, ref filter, irc); }
try { items[i].method(scope, source, msg, arg, ref filter, irc); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}
}
public delegate void OnChat(ChatScope scope, Player source, ref string msg,
public delegate void OnChat(ChatScope scope, Player source, string msg,
object arg, ref ChatMessageFilter filter, bool irc);
public sealed class OnChatEvent : IEvent<OnChat> {
public static void Call(ChatScope scope, Player source, ref string msg,
public static void Call(ChatScope scope, Player source, string msg,
object arg, ref ChatMessageFilter filter, bool irc) {
IEvent<OnChat>[] items = handlers.Items;
for (int i = 0; i < items.Length; i++) {
try { items[i].method(scope, source, ref msg, arg, ref filter, irc); }
try { items[i].method(scope, source, msg, arg, ref filter, irc); }
catch (Exception ex) { LogHandlerException(ex, items[i]); }
}
}

View File

@ -50,11 +50,9 @@ namespace MCGalaxy {
/// <summary> Sends an IRC message to either the normal or operator IRC channel. </summary>
public void Say(string message, bool opchat = false) {
public void Say(string message, bool opchat) {
string[] chans = opchat ? opchannels : channels;
foreach (string chan in chans) {
Message(chan, message);
}
foreach (string chan in chans) { Message(chan, message); }
}
/// <summary> Sends an IRC private message to the given user. </summary>

View File

@ -19,6 +19,7 @@ using System;
using System.Collections.Generic;
using MCGalaxy.Commands;
using MCGalaxy.Events;
using MCGalaxy.Events.GroupEvents;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.DB;
@ -30,7 +31,12 @@ namespace MCGalaxy.Network {
public sealed class IRCHandlers {
readonly IRCBot bot;
public IRCHandlers(IRCBot bot) { this.bot = bot; }
readonly Player ircGuest = new Player("IRC");
public IRCHandlers(IRCBot bot) {
this.bot = bot;
ircGuest.group = Group.GuestRank;
}
volatile bool hookedEvents = false;
Dictionary<string, List<string>> userMap = new Dictionary<string, List<string>>();
@ -52,6 +58,7 @@ namespace MCGalaxy.Network {
OnPlayerActionEvent.Register(HandlePlayerAction, Priority.Low);
OnShuttingDownEvent.Register(HandleShutdown, Priority.Low);
OnGroupLoadEvent.Register(HandleGroupLoad, Priority.Low);
OnChatEvent.Register(HandleChat, Priority.Low);
OnChatSysEvent.Register(HandleChatSys, Priority.Low);
@ -83,6 +90,7 @@ namespace MCGalaxy.Network {
OnPlayerActionEvent.Unregister(HandlePlayerAction);
OnShuttingDownEvent.Unregister(HandleShutdown);
OnGroupLoadEvent.Unregister(HandleGroupLoad);
OnChatEvent.Unregister(HandleChat);
OnChatSysEvent.Unregister(HandleChatSys);
@ -107,40 +115,45 @@ namespace MCGalaxy.Network {
}
string Unescape(Player p, string msg) {
string full = ServerConfig.IRCShowPlayerTitles ? p.FullName : p.group.Prefix + p.ColoredName;
return msg.Replace("λFULL", full).Replace("λNICK", p.ColoredName);
}
void HandlePlayerAction(Player p, PlayerAction action, string message, bool stealth) {
if (!p.level.SeesServerWideChat || action != PlayerAction.Me) return;
bot.Say("*" + p.DisplayName + " " + message, stealth);
}
void HandleChatSys(ChatScope scope, ref string msg, object arg,
bool ToPublicChannel(ChatScope scope, object arg, ChatMessageFilter filter) {
return Chat.scopeFilters[(int)scope](ircGuest, arg)
&& (filter == null || filter(ircGuest, arg));
}
void HandleChatSys(ChatScope scope, string msg, object arg,
ref ChatMessageFilter filter, bool irc) {
if (!irc) return;
// TODO: Check filter
bot.Say(msg);
bot.Say(msg, !ToPublicChannel(scope, arg, filter));
}
void HandleChatFrom(ChatScope scope, Player source, ref string msg,
void HandleChatFrom(ChatScope scope, Player source, string msg,
object arg, ref ChatMessageFilter filter, bool irc) {
if (!irc) return;
// TODO: Check filter
string full = ServerConfig.IRCShowPlayerTitles ? source.FullName : source.group.Prefix + source.ColoredName;
msg = msg.Replace("λFULL", full).Replace("λNICK", source.ColoredName);
bot.Say(msg);
bot.Say(Unescape(source, msg), !ToPublicChannel(scope, arg, filter));
}
void HandleChat(ChatScope scope, Player source, ref string msg,
void HandleChat(ChatScope scope, Player source, string msg,
object arg, ref ChatMessageFilter filter, bool irc) {
if (!irc) return;
// TODO: Check filter
string full = ServerConfig.IRCShowPlayerTitles ? source.FullName : source.group.Prefix + source.ColoredName;
msg = msg.Replace("λFULL", full).Replace("λNICK", source.ColoredName);
bot.Say(msg);
bot.Say(Unescape(source, msg), !ToPublicChannel(scope, arg, filter));
}
void HandleShutdown(bool restarting, string message) {
bot.Disconnect(restarting ? "Server is restarting." : "Server is shutting down.");
}
void HandleGroupLoad() { ircGuest.group = Group.GuestRank; }
void Listener_OnAction(UserInfo user, string channel, string description) {
MessageInGame(user.Nick, string.Format("%I(IRC) * {0} {1}", user.Nick, description));

View File

@ -26,14 +26,16 @@ namespace MCGalaxy {
/// <remarks> Note this field is highly volatile, you should cache references to the items array. </remarks>
public static VolatileArray<Player> Online = new VolatileArray<Player>(true);
public static Group GetGroup(string name) { return Group.GroupIn(name); }
public static string GetColor(string name) { return GetGroup(name).Color; }
public static Group GetGroup(string name) {
Player target = FindExact(name);
return target != null ? target.group : Group.GroupIn(name);
}
public static string GetColoredName(Player p, string name) {
Player target = FindExact(name);
return target != null && Entities.CanSee(p, target) ?
target.ColoredName : GetColor(name) + name.RemoveLastPlus(); // TODO: select color from database?
// TODO: select color from database?
return target != null && Entities.CanSee(p, target) ? target.ColoredName
: Group.GroupIn(name).Color + name.RemoveLastPlus();
}
public static int NonHiddenCount() {