tModLoader v0.11.8.9
A mod to make and play Terraria mods
ModCommand.cs
Go to the documentation of this file.
1using Microsoft.Xna.Framework;
2using System;
3using Terraria.Localization;
4
5namespace Terraria.ModLoader
6{
8 [Flags]
9 public enum CommandType
10 {
12 Chat = 1,
14 Server = 2,
16 Console = 4,
18 World = 8
19 }
20
21 public interface CommandCaller
22 {
24
28 Player Player { get; }
29
35 void Reply(string text, Color color = default(Color));
36 }
37
41 public abstract class ModCommand
42 {
44 public Mod mod { get; internal set; }
46 public string Name { get; internal set; }
48 public abstract string Command { get; }
50 public abstract CommandType Type { get; }
52 public virtual string Usage => "/" + Command;
54 public virtual string Description => "";
56 public virtual bool Autoload(ref string name) => mod.Properties.Autoload;
58 public abstract void Action(CommandCaller caller, string input, string[] args);
59 }
60
62 {
63 internal string msg;
64 internal Color color = Color.Red;
65
66 public UsageException() { }
67
68 public UsageException(string msg) {
69 this.msg = msg;
70 }
71
72 public UsageException(string msg, Color color) {
73 this.msg = msg;
74 this.color = color;
75 }
76 }
77
78 internal class ChatCommandCaller : CommandCaller
79 {
80 public CommandType CommandType => CommandType.Chat;
81 public Player Player => Main.player[Main.myPlayer];
82
83 public void Reply(string text, Color color = default(Color)) {
84 if (color == default(Color))
85 color = Color.White;
86 foreach (var line in text.Split('\n'))
87 Main.NewText(line, color.R, color.G, color.B);
88 }
89 }
90
91 internal class PlayerCommandCaller : CommandCaller
92 {
93 public PlayerCommandCaller(Player player) {
94 Player = player;
95 }
96 public CommandType CommandType => CommandType.Server;
97
98 public Player Player { get; }
99
100 public void Reply(string text, Color color = default(Color)) {
101 if (color == default(Color))
102 color = Color.White;
103 foreach (var line in text.Split('\n'))
104 NetMessage.SendChatMessageToClient(NetworkText.FromLiteral(line), color, Player.whoAmI);
105 }
106 }
107
108 internal class ConsoleCommandCaller : CommandCaller
109 {
110 public CommandType CommandType => CommandType.Console;
111 public Player Player => null;
112
113 public void Reply(string text, Color color = default(Color)) {
114 foreach (var line in text.Split('\n'))
115 Console.WriteLine(line);
116 }
117 }
118}
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
string Name
Internal name of this command.
Definition: ModCommand.cs:46
abstract CommandType Type
A flag enum representing context where this command operates.
Definition: ModCommand.cs:50
abstract void Action(CommandCaller caller, string input, string[] args)
The code that is executed when the command is triggered.
virtual bool Autoload(ref string name)
Autoload this command, defaults to Mod.Properties.Autoload.
virtual string Usage
A short usage explanation for this command.
Definition: ModCommand.cs:52
virtual string Description
A short description of this command.
Definition: ModCommand.cs:54
Mod mod
The Mod this ModCommand belongs to.
Definition: ModCommand.cs:44
Mod is an abstract class that you will override. It serves as a central place from which the mod's co...
Definition: Mod.cs:25
ModProperties Properties
Definition: Mod.cs:52
UsageException(string msg, Color color)
Definition: ModCommand.cs:72
void Reply(string text, Color color=default(Color))
Use this to repond to the Player that invoked this command. This method handles writing to the consol...
Player Player
The Player object corresponding to the Player that invoked this command. Use this when the Player is ...
Definition: ModCommand.cs:28
CommandType
A flag enum representing context where this command operates.
Definition: ModCommand.cs:10
@ Chat
Command can be used in Chat in SP and MP.
@ Server
Command is executed by server in MP.
@ Console
Command can be used in server console during MP.
@ World
Command can be used in Chat in SP and MP, but executes on the Server in MP. (singleplayer ?...
bool Autoload
Whether or not this mod will autoload content by default. Autoloading content means you do not need t...