Jump to content

Recommended Posts

Posted

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 :D

 

 

 

 

I don't keep an open mind lest someone try to fill it with garbage - Mark Twain

Posted

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.

1.7.10 is no longer supported by forge, you are on your own.

Posted

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 :D

 

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

 

 

 

I don't keep an open mind lest someone try to fill it with garbage - Mark Twain

Posted

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

1.7.10 is no longer supported by forge, you are on your own.

Posted

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.

 

 

 

I don't keep an open mind lest someone try to fill it with garbage - Mark Twain

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



×
×
  • Create New...

Important Information

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