Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Solved] Only client-side chat, how to react to message send by player (me)?


DeadPix
 Share

Recommended Posts

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));
    }
}

Link to comment
Share on other sites

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));
    }
}

Link to comment
Share on other sites

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).

 

No, in Minecraft (and many other games) there is client side and server side. The client side does rendering and is different per player. The server side does updating tasks etc.

Link to comment
Share on other sites

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).

 

No, in Minecraft (and many other games) there is client side and server side. The client side does rendering and is different per player. The server side does updating tasks etc.

Link to comment
Share on other sites

Thank you for your reply.

 

So even when I want to make 'single player' mod, I have to count in the 'server side'.

 

If I want to react to message sent from the 'client' (I manually type in the game "/runbot"), I have to intercept this message "on server"? How will I do it then, please?

Link to comment
Share on other sites

Thank you for your reply.

 

So even when I want to make 'single player' mod, I have to count in the 'server side'.

 

If I want to react to message sent from the 'client' (I manually type in the game "/runbot"), I have to intercept this message "on server"? How will I do it then, please?

Link to comment
Share on other sites

No, in Minecraft (and many other games) there is client side and server side. The client side does rendering and is different per player. The server side does updating tasks etc.

 

Note that Survival Single Player is still a Client/Server relationship.

 

It's a local server thread with a single player connected.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

No, in Minecraft (and many other games) there is client side and server side. The client side does rendering and is different per player. The server side does updating tasks etc.

 

Note that Survival Single Player is still a Client/Server relationship.

 

It's a local server thread with a single player connected.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Again, thanks for your replies, Draco18s & diesieben07, I put to use both of them.

 

Now when I believe I solved my problem I also updated original post to be more descriptive and contained my solution. Would you mind looking at the facts I provided and validating them? I would like to avoid confusing visitors coming from search engine.

Link to comment
Share on other sites

Again, thanks for your replies, Draco18s & diesieben07, I put to use both of them.

 

Now when I believe I solved my problem I also updated original post to be more descriptive and contained my solution. Would you mind looking at the facts I provided and validating them? I would like to avoid confusing visitors coming from search engine.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.