Using the solution explorer to the right, find the "main" class of Terraria. It should be in Terraria>Main.cs. Now that we have the main class open, let's find out how the client handles chat messages. I encourage you to find it for yourself, but if you're lazy, the line number is about 11844 in the main class. It should look something like this:
Code:
if (Main.inputTextEnter && Main.chatRelease)
{
if (Main.chatText != "")
{
NetMessage.SendData(25, -1, -1, Main.chatText, Main.myPlayer, 0f, 0f, 0f, 0, 0, 0);
}
...
The "NetMessage.SendData" sends our message to the server, so we want to make sure to not do that when our client detects that it is a hack message. To actually hook our class into the chat, let's go back to our chat class and make a new method called onChat. The signature should be "public static bool onChat(string message)", returning true if we should send the message to our server, and false if we shouldn't. Now, we edit the code in the main class to make it:
Code:
if (Main.inputTextEnter && Main.chatRelease)
{
if (Main.chatText != "" && Hacks.onChat(Main.chatText))
{
NetMessage.SendData(25, -1, -1, Main.chatText, Main.myPlayer, 0f, 0f, 0f, 0, 0, 0);
}
...
Back in our hack class, our onChat method should look something like this:
Code:
public static bool onChat(string message)
{
if (message.StartsWith("~"))
{
if (message.StartsWith("~god"))
{
//In the next section!
}
else if (message.StartsWith("~infmana"))
{
//Are you excited?
}
//You can keep chaining these else if's for future code
return false; //Message should NOT go to the server, so we return false.
}
return true; //Message does NOT start with ~, so it is okay to send it to the server.
}
Now we have a method with communicating with our hacks, let's get started with the fun.