Jump to content

[1.9] Executing a Client command when client joins a server


TommyHoogstra

Recommended Posts

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

Link to comment
Share on other sites

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.

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

For the EntityJoinWorldEvent, couldn't I just check if the entity is instanceof EntityPlayerSP?

 

Yes, that should work.

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

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

}

}

Link to comment
Share on other sites

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.

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

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

}

}

Link to comment
Share on other sites

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?

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

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.

Link to comment
Share on other sites

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.

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

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • As a seasoned investor, I’ve learned that the financial world can be exciting and treacherous. My experiences with investments have taught me the importance of diligence and thorough research, particularly when dealing with newer and high-risk assets like Bitcoin. Unfortunately, my journey with Bitcoin was marred by a painful lesson in the form of a scam, which I hope to share to help others avoid similar pitfalls. My introduction to Bitcoin investing seemed promising. The allure of high returns and the buzz surrounding cryptocurrency were captivating. I was drawn to several Bitcoin investment platforms that promised substantial profits. These platforms presented themselves with professional websites, attractive promotions, and genuine testimonials. I was led to believe that these sites were reliable and that I was making a sound investment. However, this trust proved to be misplaced. The platform I initially invested in showcased fake success stories and substantial profits to entice investors. They used these fabricated examples to build credibility and persuade new investors like me to commit more funds. Their tactics were sophisticated; they knew exactly how to create an illusion of success and security. I, too, was lured by their promises and gradually invested a considerable amount of money, totaling 68,000 USD.The initial investments went smoothly. My account appeared to grow with impressive returns, and I felt a sense of validation in my investment strategy. But things took a drastic turn when I decided to make a more significant investment, believing that the returns would only get better. Once I deposited a substantial sum, the platform’s behavior changed abruptly. My account was frozen without warning, and I was faced with a barrage of demands for additional payments before I could access my funds or the supposed profits. The situation was both distressing and bewildering. I was met with excuses and obstructions at every turn. It became clear that I was dealing with a fraudulent company that had no intention of honoring its commitments. The realization that I had been deceived was crushing. The emotional and financial toll of the situation was overwhelming. Determined to recover my funds, I reached out to Trust Geeks Hack Expert, a service recommended by a trusted friend who had faced a similar ordeal. My initial skepticism was tempered by desperation and hope. Trust Geeks Hack Expert took immediate action. Their team worked tirelessly to investigate the fraudulent platform and recover my lost funds. Their dedication and expertise were evident throughout the process. In just a few weeks, Trust Geeks Hack Expert managed to successfully retrieve the full amount of 68,000 USD. Their assistance was thorough and professional, and they kept me informed every step of the way. Their efforts not only resulted in the recovery of my funds but also provided me with invaluable insights into how these scams operate. This. This experience has been a harsh lesson in the importance of conducting thorough research before investing in any platform, particularly in the cryptocurrency space. The allure of high returns can be overwhelming, but it’s crucial to approach such investments with caution. Verify the legitimacy of the platform, seek out reviews from reliable sources, and ensure that any investment opportunity has a track record of transparency and reliability. To anyone who finds themselves in a similar predicament, I cannot recommend Trust Geeks Hack Expert  enough. Their professionalism and commitment to recovering my funds were exemplary:: E>mail: trustgeekshackexpert {@} fastservice{.}com -----> Tele>gram : Trustgeekshackexpert, And also  What's>App   + 1-7-1-9-4-9-2-2-6-9-3
    • As a seasoned investor, I’ve learned that the financial world can be exciting and treacherous. My experiences with investments have taught me the importance of diligence and thorough research, particularly when dealing with newer and high-risk assets like Bitcoin. Unfortunately, my journey with Bitcoin was marred by a painful lesson in the form of a scam, which I hope to share to help others avoid similar pitfalls. My introduction to Bitcoin investing seemed promising. The allure of high returns and the buzz surrounding cryptocurrency were captivating. I was drawn to several Bitcoin investment platforms that promised substantial profits. These platforms presented themselves with professional websites, attractive promotions, and genuine testimonials. I was led to believe that these sites were reliable and that I was making a sound investment. However, this trust proved to be misplaced. The platform I initially invested in showcased fake success stories and substantial profits to entice investors. They used these fabricated examples to build credibility and persuade new investors like me to commit more funds. Their tactics were sophisticated; they knew exactly how to create an illusion of success and security. I, too, was lured by their promises and gradually invested a considerable amount of money, totaling 68,000 USD.The initial investments went smoothly. My account appeared to grow with impressive returns, and I felt a sense of validation in my investment strategy. But things took a drastic turn when I decided to make a more significant investment, believing that the returns would only get better. Once I deposited a substantial sum, the platform’s behavior changed abruptly. My account was frozen without warning, and I was faced with a barrage of demands for additional payments before I could access my funds or the supposed profits. The situation was both distressing and bewildering. I was met with excuses and obstructions at every turn. It became clear that I was dealing with a fraudulent company that had no intention of honoring its commitments. The realization that I had been deceived was crushing. The emotional and financial toll of the situation was overwhelming. Determined to recover my funds, I reached out to Trust Geeks Hack Expert, a service recommended by a trusted friend who had faced a similar ordeal. My initial skepticism was tempered by desperation and hope. Trust Geeks Hack Expert took immediate action. Their team worked tirelessly to investigate the fraudulent platform and recover my lost funds. Their dedication and expertise were evident throughout the process. In just a few weeks, Trust Geeks Hack Expert managed to successfully retrieve the full amount of 68,000 USD. Their assistance was thorough and professional, and they kept me informed every step of the way. Their efforts not only resulted in the recovery of my funds but also provided me with invaluable insights into how these scams operate. This. This experience has been a harsh lesson in the importance of conducting thorough research before investing in any platform, particularly in the cryptocurrency space. The allure of high returns can be overwhelming, but it’s crucial to approach such investments with caution. Verify the legitimacy of the platform, seek out reviews from reliable sources, and ensure that any investment opportunity has a track record of transparency and reliability. To anyone who finds themselves in a similar predicament, I cannot recommend Trust Geeks Hack Expert  enough. Their professionalism and commitment to recovering my funds were exemplary:: E>mail: trustgeekshackexpert{@}fastservice{.}com -----> Tele>gram : Trustgeekshackexpert, And also  What's>App   + 1-7-1-9-4-9-2-2-6-9-3
    • I've been playing it for only 1 day and it doesn't want to go into the game anymore. I've tried reinstalling the game and repairing the game files (on curseforge) but it still doesn't work. Whenever i try to click on the Singleplayer icon, it just flickers into the world creation page and flickers back 
    • https://gist.github.com/RealMangoBot/03ce10d60ce10f126dcf2c033c3a4f46  
  • Topics

×
×
  • Create New...

Important Information

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