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.

[SOLVED] [1.11.2] EntityJoinWorldEvent fires multiple times for the same entity

Featured Replies

Posted

************SOLUTION************

The solution is LivingSpawnEvent.SpecialSpawn instead of EntityEvent, so proper event hook for animals spawn should look like this:

 

@SubscribeEvent(priority = EventPriority.NORMAL)
public void onEntitySpawn(SpecialSpawn e) {
    Entity squid = e.getEntity();
    if (squid instanceof EntitySquid) {
        //do stuff
    }
}

 

************QUESTION************

 

 public class EventHandler {
 +	
 +	@SubscribeEvent(priority = EventPriority.NORMAL)
 +	public void EntityJoinWorldEvent(EntityEvent e) {
 +		Entity spawnedEntity = e.getEntity();
 +		if (spawnedEntity instanceof EntitySquid) {
 +			System.out.println(spawnedEntity.getUniqueID());
 +		}
 +	}
 }

Hi,

What I want to achieve is replace half of Squids on my custom mob. So everytime Squid is spawned I want to replace it with a chance of 50%. When I subscribed to EntityJoinWorldEvent I noticed that this event is fired multiple times for the same entity. My code is above, nothing too fancy, very simple handler. Here is console output, you can see the same UUID repeat over and over again (i colorcoded the same UUIDs). I expect EntityJoinWorldEvent should be fired once per entity.

 

Edit:

Maybe I should try onJoinWorldEvent(EntityJoinWorldEvent e) instead of EntityJoinWorldEvent(EntityEvent e), this could be my problem?

 

Quote

[00:41:53] [Server thread/INFO]: [STDOUT]: 2f0b34e7-f396-4ff4-b986-bfb65217d06a - Squid A
[00:41:53] [Server thread/INFO]: [STDOUT]: f9b18086-201d-4986-a342-f356f403eba3 - Squid B
[00:41:53] [Server thread/INFO]: [STDOUT]: 5baaec49-4bf2-4052-99a7-6e087d2eb15a - Squid C
[00:41:53] [Server thread/INFO]: [STDOUT]: e5287093-dbdb-4286-bd5b-e9ab09ff7298 - Squid D
[00:41:53] [Server thread/INFO]: [STDOUT]: 5dca64e1-557a-4860-ad3d-b34d72ad6412 - Squid E
[00:41:53] [Server thread/INFO]: [STDOUT]: 4fc4b076-a626-41d2-9e96-8a88c9fc4796 - Squid F
[00:41:53] [Server thread/INFO]: [STDOUT]: 2f0b34e7-f396-4ff4-b986-bfb65217d06a - Squid A
[00:41:53] [Server thread/INFO]: [STDOUT]: f9b18086-201d-4986-a342-f356f403eba3 - Squid B
[00:41:53] [Server thread/INFO]: [STDOUT]: 5baaec49-4bf2-4052-99a7-6e087d2eb15a - Squid C
[00:41:53] [Server thread/INFO]: [STDOUT]: e5287093-dbdb-4286-bd5b-e9ab09ff7298 - Squid D
[00:41:53] [Server thread/INFO]: [STDOUT]: 5dca64e1-557a-4860-ad3d-b34d72ad6412 - Squid E
[00:41:53] [Server thread/INFO]: [STDOUT]: 4fc4b076-a626-41d2-9e96-8a88c9fc4796 - Squid F
[00:41:53] [Server thread/INFO]: [STDOUT]: 2f0b34e7-f396-4ff4-b986-bfb65217d06a - Squid A
[00:41:53] [Server thread/INFO]: [STDOUT]: f9b18086-201d-4986-a342-f356f403eba3 - Squid B
[00:41:53] [Server thread/INFO]: [STDOUT]: 5baaec49-4bf2-4052-99a7-6e087d2eb15a - Squid C
[00:41:53] [Server thread/INFO]: [STDOUT]: e5287093-dbdb-4286-bd5b-e9ab09ff7298 - Squid D
[00:41:53] [Server thread/INFO]: [STDOUT]: 5dca64e1-557a-4860-ad3d-b34d72ad6412 - Squid E
[00:41:53] [Server thread/INFO]: [STDOUT]: 4fc4b076-a626-41d2-9e96-8a88c9fc4796 - Squid F

 

Edited by bigbang

1 hour ago, bigbang said:

Maybe I should try onJoinWorldEvent(EntityJoinWorldEvent e) instead of EntityJoinWorldEvent(EntityEvent e), this could be my problem?

Try it, it sounds more logical (I haven't messed with entity joining events at all, so not sure myself), EntityEvent makes it sound like it will fire for ANYTHING related to entities, spawning, dying, joining the world, maybe even moving.

 

The name of the method doesn't matter, it's having the @SubscribeEvent notation and the proper parameters that make it work.

2 hours ago, bigbang said:

EntityJoinWorldEvent(EntityEvent e)

EntityEvent fires for any updates with entities.

 

2 hours ago, bigbang said:

onJoinWorldEvent(EntityJoinWorldEvent e)

Yes, you should...

  • Author

So LivingSpawnEvent.CheckSpawnLivingSpawnEvent.SpecialSpawn and EntityJoinWorldEvent should be called only once? I assume that the only exception may be EntityJoinWorldEvent, it seems like this event should be fired also on level load for already extisting entities.

41 minutes ago, bigbang said:

So LivingSpawnEvent.CheckSpawnLivingSpawnEvent.SpecialSpawn and EntityJoinWorldEvent should be called only once? I assume that the only exception may be EntityJoinWorldEvent, it seems like this event should be fired also on level load for already extisting entities.

If you want to check for when a squid spawns, use your initial code with LivingSpawnEvent.

Edited by Differentiation

  • Author

I found the best solution which is SpecialSpawn event, problem was solved.

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.