Jump to content

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


bigbang

Recommended Posts

************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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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



×
×
  • Create New...

Important Information

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