Skip to content

Commit

Permalink
Move code related to /notes acronyms into one place
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodlyay committed May 5, 2024
1 parent e163baf commit acdb29e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
13 changes: 1 addition & 12 deletions MCGalaxy/Modules/Moderation/Notes/CmdNotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,10 @@ class CmdNotes : Command2
if (args.Length > 5) long.TryParse(args[5], out duration);

p.Message("{0} by {1} &Son {2}{3}{4}",
Action(args[1]), p.FormatNick(args[2]), args[3],
NoteAcronym.GetAction(args[1]), p.FormatNick(args[2]), args[3],
duration == 0 ? "" : " for " + TimeSpan.FromTicks(duration).Shorten(true),
reason.Length == 0 ? "" : " - " + reason.Replace("%20", " "));
}

static string Action(string arg) {
if (arg.CaselessEq("W")) return "Warned";
if (arg.CaselessEq("K")) return "Kicked";
if (arg.CaselessEq("M")) return "Muted";
if (arg.CaselessEq("B")) return "Banned";
if (arg.CaselessEq("J")) return "Jailed";
if (arg.CaselessEq("F")) return "Frozen";
if (arg.CaselessEq("T")) return "Temp-Banned";
return arg;
}

public override void Help(Player p) {
p.Message("&T/Notes [name] &H- views that player's notes.");
Expand Down
73 changes: 59 additions & 14 deletions MCGalaxy/Modules/Moderation/Notes/NotesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using MCGalaxy.Events;
using MCGalaxy.Events.ServerEvents;

Expand All @@ -32,6 +33,7 @@ public sealed class NotesPlugin : Plugin
OnModActionEvent.Register(HandleModerationAction, Priority.Low);
Command.Register(cmdNotes);
Command.Register(cmdMyNotes);
NoteAcronym.Init();
}

public override void Unload(bool shutdown) {
Expand All @@ -41,21 +43,12 @@ public sealed class NotesPlugin : Plugin


static void HandleModerationAction(ModAction action) {
switch (action.Type) {
case ModActionType.Frozen:
AddNote(action, "F"); break;
case ModActionType.Kicked:
AddNote(action, "K"); break;
case ModActionType.Muted:
AddNote(action, "M"); break;
case ModActionType.Warned:
AddNote(action, "W"); break;
case ModActionType.Ban:
string banType = action.Duration.Ticks != 0 ? "T" : "B";
AddNote(action, banType); break;
}
string acronym = NoteAcronym.GetAcronym(action);
if (acronym == null) return;

AddNote(action, acronym);
}

static void AddNote(ModAction e, string type) {
if (!Server.Config.LogNotes) return;
string src = e.Actor.name;
Expand All @@ -66,4 +59,56 @@ public sealed class NotesPlugin : Plugin
Server.Notes.Append(data);
}
}

/// <summary>
/// Moderation note actions are logged to disk using single-letter acronyms. This class handles translating these to and from human-readable actions.
/// </summary>
public class NoteAcronym
{
readonly static NoteAcronym Warned = new NoteAcronym("W", "Warned");
readonly static NoteAcronym Kicked = new NoteAcronym("K", "Kicked");
readonly static NoteAcronym Muted = new NoteAcronym("M", "Muted");
readonly static NoteAcronym Banned = new NoteAcronym("B", "Banned");
readonly static NoteAcronym Jailed = new NoteAcronym("J", "Jailed"); //Jailing was removed, but still appears in notes for historical reasons
readonly static NoteAcronym Frozen = new NoteAcronym("F", "Frozen");
readonly static NoteAcronym TempBanned = new NoteAcronym("T", "Temp-Banned");

static NoteAcronym[] All;

internal static void Init() {
All = new NoteAcronym[] { Warned, Kicked, Muted, Banned, Jailed, Frozen, TempBanned };
}

/// <summary>
/// Returns the appropriate Acronym to log when a mod action occurs.
/// </summary>
public static string GetAcronym(ModAction action) {
if (action.Type == ModActionType.Ban) {
return action.Duration.Ticks != 0 ? TempBanned.Acronym : Banned.Acronym;
}

string modActionString = action.Type.ToString();
foreach (var na in All) {
if (na.Action == modActionString) { return na.Acronym; }
}
return null;
}
/// <summary>
/// Returns the appropriate Action from a mod note acronym. If none are found, returns the original argument.
/// </summary>
public static string GetAction(string acronym) {
foreach (var na in All) {
if (na.Acronym == acronym) { return na.Action; }
}
return acronym;
}

readonly string Acronym;
readonly string Action;

private NoteAcronym(string acronym, string action) {
Acronym = acronym;
Action = action;
}
}
}

0 comments on commit acdb29e

Please sign in to comment.