TommyHoogstra Posted March 29, 2016 Share Posted March 29, 2016 Hey everyone, I'm kinda new to forge but have a fair amount of java experience, I'm in a bit of a rush so I was hoping of someone could give me some pointers as google hasnt satisfied my needs. I'm trying to get the client to run a command as soon as it joins a server, e.g. /auth 123111717 I've tried using the ClientConnectToServer (forgot its exact name, forgive me), EntityJoinWorldEvent, PlayerLoggedInEvent (i think this is for server side mods, excuse me if I'm wrong) but have had no such luck at getting a command to execute. Any pointers, or examples? Thanks Quote Link to comment Share on other sites More sharing options...
Choonster Posted March 29, 2016 Share Posted March 29, 2016 FMLNetworkEvent.ClientConnectedToServerEvent or EntityJoinWorldEvent should work, though there's something to note about each event: All sub-events of FMLNetworkEvent are fired on the Netty thread, so you need to schedule a task on the main thread using IThreadListener before you can safely interact with normal Minecraft classes. This is explained in slightly more detail here. EntityJoinWorldEvent will be fired for every entity that joins the world and will also be fired when the player respawns or changes dimension. PlayerLoggedInEvent is only fired server-side, so it can't be used by a client-only mod in multiplayer (it will work in single player because the integrated server is running in the same JVM process as the client). Use ICommandManager#executeCommand to execute a command. The ICommandManager for client commands is stored in the ClientCommandHandler.instance field. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future. Link to comment Share on other sites More sharing options...
TommyHoogstra Posted March 29, 2016 Author Share Posted March 29, 2016 For the EntityJoinWorldEvent, couldn't I just check if the entity is instanceof EntityPlayerSP? Quote Link to comment Share on other sites More sharing options...
Choonster Posted March 29, 2016 Share Posted March 29, 2016 For the EntityJoinWorldEvent, couldn't I just check if the entity is instanceof EntityPlayerSP? Yes, that should work. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future. Link to comment Share on other sites More sharing options...
TommyHoogstra Posted March 29, 2016 Author Share Posted March 29, 2016 Ok, So I went with the ClientConnectedToServerEvent, and even though I haven't added the command to my server yet, I was expecting an 'unknown command' message to pop up. Am I missing something? @Mod(modid = "MAH", version = "1.0") public class MAH { @EventHandler public void init(FMLInitializationEvent e){ MinecraftForge.EVENT_BUS.register(new Events()); } @EventHandler public void postInit(FMLPostInitializationEvent event) { } @EventHandler public void preInit(FMLPreInitializationEvent event) { } public class Events { @SubscribeEvent public void onJoin(ClientConnectedToServerEvent event) { Minecraft.getMinecraft().addScheduledTask(new Auth()); } } public class Auth implements Runnable { @Override public void run() { ClientCommandHandler.instance.executeCommand(Minecraft.getMinecraft().thePlayer, "/auth"); } } Quote Link to comment Share on other sites More sharing options...
Choonster Posted March 29, 2016 Share Posted March 29, 2016 The FML bus has been merged with the Forge bus now, so FMLCommonHandler#bus is deprecated; though it will still work since it returns a reference to the Forge bus. If the command doesn't exist, ClientCommandHandler#executeCommand will return 0; it won't throw an exception or send a chat message. Is this command supposed to be handled on the client or the server? If it's a server command, you need to send a chat message packet to the server with the command in it. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future. Link to comment Share on other sites More sharing options...
TommyHoogstra Posted March 29, 2016 Author Share Posted March 29, 2016 Its a server command, is there any documentation on the Chat Message Packet, or an example around? Thanks Quote Link to comment Share on other sites More sharing options...
Choonster Posted March 29, 2016 Share Posted March 29, 2016 Use EntityPlayerSP#sendChatMessage to send a chat message packet to the server. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future. Link to comment Share on other sites More sharing options...
TommyHoogstra Posted March 29, 2016 Author Share Posted March 29, 2016 Ok, thanks very much. However, this is all that needed to change in my code, but it still doesn't send the command when I join, I think im missing something obvious, but I'm new Forge and its API so I'm not quite sure. public class Auth implements Runnable { @Override public void run() { Minecraft.getMinecraft().thePlayer.sendChatMessage("/auth"); } } Quote Link to comment Share on other sites More sharing options...
Choonster Posted March 29, 2016 Share Posted March 29, 2016 That should work, since it's the same method used by the chat GUI to send chat messages to the server. Does it work if you send some text that isn't a slash command? Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future. Link to comment Share on other sites More sharing options...
TommyHoogstra Posted March 29, 2016 Author Share Posted March 29, 2016 It doesn't seem to be working without the slash either I also tried addChatMessage, that didnt work either. I'm assuming its something wrong with the scheduled task? Quote Link to comment Share on other sites More sharing options...
TommyHoogstra Posted March 29, 2016 Author Share Posted March 29, 2016 That should work, since it's the same method used by the chat GUI to send chat messages to the server. Does it work if you send some text that isn't a slash command? Sorry for double post, i can confirm it works with EntityJoinWorldEvent, however, I would like to find out if theres a way to check the 'join' cause, e.g. Respawning, Logging in, etc. Right now im just using a boolean that gets set to false if the client logs out, and true when the entity joins. Quote Link to comment Share on other sites More sharing options...
Choonster Posted March 30, 2016 Share Posted March 30, 2016 It turns out that FMLNetworkEvent.ClientConnectedToServerEvent is fired before the client player is created and the server has finished the handshake process, so you can't use Minecraft#thePlayer or send packets. What you can do is have a separate event handler for EntityJoinWorldEvent that you register from the FMLNetworkEvent.ClientConnectedToServerEvent handler. When the client player joins the world, send the chat message and then unregister the event handler. I've written an example of this here. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future. Link to comment Share on other sites More sharing options...
TommyHoogstra Posted March 30, 2016 Author Share Posted March 30, 2016 Thanks very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.