Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

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

VampZ modder

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.

  • Author

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

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.

  • Author

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.