Hello,
I am at really early phase of learning how to mod Minecraft with forge, but I was unable to google and learn few things. One of them is how to work with chat.
From what I understand, there are two ways how to write a mod. One way is client only (I suppose there is no interaction with server), and the other way is mod communicating with server.
I would like to create the mod that will work only in my single player game (I, for now, have no intentions of leaving this working on any server). My mod should take over player and control his movement, be able to mine, craft, eventually, etc. But I am only at the beginning now. Mod would react to command posted to chat "/runbot".
I understand that in order to find out that message "/runbot" was sent to chat, I should @SubscribeEvent? But what event would it be? I would imagine maybe overriding some method, that would take string as parameter, which I would then compared to "/runbot" and if comparison succeeded, I would start my bot. Is that how would I do that? And what event(s) should I subscribe to in order to read chat message send by me and myself only.
I searched this forum thoroughly, but haven't find satisfactory answer. Thanks in advance for any help provided!
Edit(s):
* Apparently I was confused about how the 'clientside' and 'serverside' are working. It is perfectly described in this tutorial.
Working solution: Only client-side chat reaction to commands send by player
First of all, there is no need to subscribe any event or anything like this - only to register command via proxy. In my design (inspired probably by wuppy's tutorials), I have class ClientProxy that contains public void registerRenderers() method with contents as follows:
public class ClientProxy extends CommonProxy {
@Override
public void registerRenderers() {
ClientCommandHandler.instance.registerCommand(new TestCommand()); // This is the chat command registration I will talk about a bit bellow
}
}
In my main mod class (for example: ExampleMod.java) I used following code to make my ClientProxy accessible:
@SidedProxy(clientSide = "com.example.examplemod.ClientProxy")
public static CommonProxy proxy;
And in the very same class in init method I called proxy.registerRenderers().
Registration of the command in the first snippet of code is provided by ClientCommandHandler method registerCommand(). Command definition is then defined in the class TestCommand, which has to extend from CommandBase class. There are, of course, some methods to override. See the code bellow.
public class TestCommand extends CommandBase {
@Override
public String getCommandName()
{
return "TestCommand";
}
@Override
public String getCommandUsage(ICommandSender p_71518_1_)
{
return "/TestCommand <FirstParam> <SecondParam> [ThirdParam]";
}
// Older versions (<1.9) had instead of execute method processCommand method (without MinecraftServer parameter)
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws WrongUsageException {
// Makes minecraft show returned content of getCommandUsage() method
if (args.length < 2)
wrongUsage(sender);
// Display something to chat || do something else entirely
// In versions < 1.9, there was new ChatTextComponent instantiated instead of TextComponentString
sender.addChatMessage(new TextComponentString("Something to be displayed in chat..."));
}
public void wrongUsage(ICommandSender sender) throws WrongUsageException {
throw new WrongUsageException(getCommandUsage(sender));
}
}