tModLoader v0.11.8.9
A mod to make and play Terraria mods
CommandManager.cs
Go to the documentation of this file.
1using Microsoft.Xna.Framework;
2using System;
3using System.Collections.Generic;
4using System.Linq;
5
6namespace Terraria.ModLoader
7{
8 //todo: further documentation
12 public static class CommandManager
13 {
14 internal static readonly IDictionary<string, List<ModCommand>> Commands = new Dictionary<string, List<ModCommand>>(StringComparer.OrdinalIgnoreCase);
15
16 public static bool Matches(CommandType commandType, CommandType callerType) {
17 if ((commandType & CommandType.World) != 0)
18 if (Main.netMode == 2)
19 commandType |= CommandType.Server;
20 else if (Main.netMode == 0)
21 commandType |= CommandType.Chat;
22
23 return (callerType & commandType) != 0;
24 }
25
26 internal static void Add(ModCommand cmd) {
27 List<ModCommand> cmdList;
28 if (!Commands.TryGetValue(cmd.Command, out cmdList))
29 Commands.Add(cmd.Command, cmdList = new List<ModCommand>());
30
31 cmdList.Add(cmd);
32 }
33
34 internal static void Unload() {
35 Commands.Clear();
36 }
42 internal static bool GetCommand(CommandCaller caller, string name, out ModCommand mc) {
43 string modName = null;
44 if (name.Contains(':')) {
45 var split = name.Split(':');
46 modName = split[0];
47 name = split[1];
48 }
49
50 mc = null;
51
52 List<ModCommand> cmdList;
53 if (!Commands.TryGetValue(name, out cmdList))
54 return false;
55
56 cmdList = cmdList.Where(c => Matches(c.Type, caller.CommandType)).ToList();
57 if (cmdList.Count == 0)
58 return false;
59
60 if (modName != null) {
61 Mod mod = ModLoader.GetMod(modName);
62 if (mod == null) {
63 caller.Reply("Unknown Mod: " + modName, Color.Red);
64 }
65 else {
66 mc = cmdList.SingleOrDefault(c => c.mod == mod);
67 if (mc == null)
68 caller.Reply("Mod: " + modName + " does not have a " + name + " command.", Color.Red);
69 }
70 }
71 else if (cmdList.Count > 1) {
72 caller.Reply("Multiple definitions of command /" + name + ". Try:", Color.Red);
73 foreach (var c in cmdList)
74 caller.Reply(c.mod.Name + ":" + c.Command, Color.LawnGreen);
75 }
76 else {
77 mc = cmdList[0];
78 }
79 return true;
80 }
81
82 internal static bool HandleCommand(string input, CommandCaller caller) {
83 var args = input.TrimEnd().Split(' ');
84 var name = args[0];
85 args = args.Skip(1).ToArray();
86
87 if (caller.CommandType != CommandType.Console) {
88 if (name[0] != '/')
89 return false;
90
91 name = name.Substring(1);
92 }
93
94 ModCommand mc;
95 if (!GetCommand(caller, name, out mc))
96 return false;
97
98 if (mc == null)//error in command name (multiple commands or missing mod etc)
99 return true;
100
101 try {
102 mc.Action(caller, input, args);
103 }
104 catch (Exception e) {
105 var ue = e as UsageException;
106 if (ue?.msg != null)
107 caller.Reply(ue.msg, ue.color);
108 else
109 caller.Reply("Usage: " + mc.Usage, Color.Red);
110 }
111 return true;
112 }
113
114 public static List<Tuple<string, string>> GetHelp(CommandType type) {
115 var list = new List<Tuple<string, string>>();
116 foreach (var entry in Commands) {
117 var cmdList = entry.Value.Where(mc => Matches(mc.Type, type)).ToList();
118 foreach (var mc in cmdList) {
119 string cmd = mc.Command;
120 if (cmdList.Count > 1)
121 cmd = mc.mod.Name + ":" + cmd;
122
123 list.Add(Tuple.Create(cmd, mc.Description));
124 }
125 }
126 return list;
127 }
128 }
129}
This serves as the central class from which ModCommand functions are supported and carried out.
static bool Matches(CommandType commandType, CommandType callerType)
static List< Tuple< string, string > > GetHelp(CommandType type)
This class represents a chat or console command. Use the CommandType to specify the scope of the comm...
Definition: ModCommand.cs:42
abstract string Command
The desired text to trigger this command.
Definition: ModCommand.cs:48
CommandType
A flag enum representing context where this command operates.
Definition: ModCommand.cs:10