Implementing client-only commands works like this:
1. Implement ICommand
I created an example command that will show the current time to a user like this:
public class GetTimeCommand implements ICommand {
@Override
public int compareTo(ICommand arg0) {
return 0;
}
@Override
public String getName() {
return "realtime";
}
@Override
public String getUsage(ICommandSender sender) {
return "/realtime";
}
@Override
public List<String> getAliases() {
List<String> aliases = Lists.<String>newArrayList();
aliases.add("/realtime");
return aliases;
}
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String time = dateFormat.format(new Date(System.currentTimeMillis()));
sender.sendMessage(format(net.minecraft.util.text.TextFormatting.DARK_GREEN, time));
}
@Override
public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
return true;
}
@Override
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args,
BlockPos targetPos) {
return null;
}
@Override
public boolean isUsernameIndex(String[] args, int index) {
return false;
}
private TextComponentTranslation format(TextFormatting color, String str, Object... args)
{
TextComponentTranslation ret = new TextComponentTranslation(str, args);
ret.getStyle().setColor(color);
return ret;
}
}
2. Register your command
Find a good place to register your command(s) at mod start up. I decided to first implement proxies for server and client like described in the Docs: https://mcforge.readthedocs.io/en/latest/concepts/sides/.
Then, in my ClientProxy class, I put command registrations into the preInit event handler:
public class ClientProxy extends CommonProxy {
@Override
public void preInit(FMLPreInitializationEvent e) {
ClientCommandHandler.instance.registerCommand(new GetTimeCommand());
}
...
}
Thats it. In game, I can now input "/realtime" in chat and get the current time displayed.