DJD Posted July 30, 2015 Share Posted July 30, 2015 I'm still trying to wrap my head around the proper way to handle events (such as player entering a world etc)..... For some background: 1. I know Java. 2. I've got a working mod framework (e.g. handles commands etc). What would be the proper way to send a message to a player's chat when he first enters the world from my mod ? I think a good example would clarify the whole issue for me... kind of like a "hello world !" simple program is usually the 1st type of program most folks create when 1st learning a language Quote I don't keep an open mind lest someone try to fill it with garbage - Mark Twain Link to comment Share on other sites More sharing options...
NeoSup2130 Posted July 30, 2015 Share Posted July 30, 2015 Use the event: PlayerLoggedInEvent Whenever the player joins the world for the first time this event gets called. And this event is a FMLCommonHandler event Quote VampZ modder Link to comment Share on other sites More sharing options...
Ernio Posted July 30, 2015 Share Posted July 30, 2015 PlayerLoggedInEvent is fired not when you join world, but when you log in on server, once per session. What you want is EntityJoinWorldEvent. If you do not want to do checks for all entities (just for players), you can also use combination of: PlayerLoggedInEvent PlayerRespawnEvent PlayerChangedDimensionEvent Which will probably give you same results with better performance. As to events themselves - google it, there are more than enough tuts on this field. Quote 1.7.10 is no longer supported by forge, you are on your own. Link to comment Share on other sites More sharing options...
DJD Posted July 30, 2015 Author Share Posted July 30, 2015 Thanks for pointing me in the right direction. Getting which events fire at which times (I tried the PlayerLoggedInEvent and it didn't work) is the struggle... Nothing a little flailing around in code won't cure Got it working.. sort of..... public class ServerEventHandler { private MessageHandler msg = new MessageHandler(); @SubscribeEvent public void PlayerJoinsWorldEvent(EntityJoinWorldEvent e) { if (e.entity instanceof EntityPlayer) { msg.chat(e.entity, "Hello MINECRAFT !"); } } } I registered it in my serverStart(FMLServerStartingEvent e) by: MinecraftForge.EVENT_BUS.register(new ServerEventHandler()); but it sends the message twice. Its got to be something obvious I'm doing wrong....... BTW MessageHandler() is a custom messaging class my mod uses and its not the problem (same effect occurs if I just sysout it to the log).... Quote I don't keep an open mind lest someone try to fill it with garbage - Mark Twain Link to comment Share on other sites More sharing options...
Ernio Posted July 30, 2015 Share Posted July 30, 2015 1. Events should be registered in preInit of mod. 2. This event is fired for client and server logical side - thus 2 msgs. You can use !entity.world.isRemote to get server side. 3. Don't use this "chat stuff" you did above. You can send msg to player directly by using methods in EntityPlayerMP class (I think, will edit post if mistaken). EDIT 2 public class ServerEventHandler { @SubscribeEvent public void onJoinWorldEvent(EntityJoinWorldEvent event) { if (event.entity instanceof EntityPlayerMP) // This is 2 checks in one. EPMP can only exist on server logical side. { ((EntityPlayerMP) event.entity).addChatMessage(new ChatComponentText("YAY")); } } } Quote 1.7.10 is no longer supported by forge, you are on your own. Link to comment Share on other sites More sharing options...
DJD Posted July 30, 2015 Author Share Posted July 30, 2015 So that's what I wasn't figuring out from the tutorials ! I thought they had to belong to the ServerStart event (like the commands do)..... BTW "chat stuff" does some other things as well (or can if needed). Centralized coding helps when things seem to change from version to version (such as chat handling changing from previous versions)..... So to summarize if I got this correct: 1. register EventHandlers in preInit. 2. register Commands in serverStart. Quote I don't keep an open mind lest someone try to fill it with garbage - Mark Twain 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.