Terraria ModLoader  0.11.7.8
A mod to make and play Terraria mods
ModCommand.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework;
2 using System;
3 using Terraria.Localization;
4 
5 namespace 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 
61  public class UsageException : Exception
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 }
CommandType
A flag enum representing context where this command operates.
Definition: ModCommand.cs:9
Command can be used in Chat in SP and MP, but executes on the Server in MP. (singleplayer ...
UsageException(string msg, Color color)
Definition: ModCommand.cs:72
Command can be used in Chat in SP and MP.
Command can be used in server console during MP.
Command is executed by server in MP.
ModProperties Properties
Definition: Mod.cs:52
bool Autoload
Whether or not this mod will autoload content by default. Autoloading content means you do not need t...
Mod is an abstract class that you will override. It serves as a central place from which the mod's co...
Definition: Mod.cs:24
This class represents a chat or console command. Use the CommandType to specify the scope of the comm...
Definition: ModCommand.cs:41