Skip to content

Add world to Entity AddTo/RemoveFrom Events#10183

Merged
Owen1212055 merged 3 commits into
PaperMC:masterfrom
1stGlitch:add-world-to-entity-add-remove-events
Jan 23, 2024
Merged

Add world to Entity AddTo/RemoveFrom Events#10183
Owen1212055 merged 3 commits into
PaperMC:masterfrom
1stGlitch:add-world-to-entity-add-remove-events

Conversation

@1stGlitch
Copy link
Copy Markdown
Contributor

When a plugin listens to the EntityAddToWorld and EntityRemoveFromWorld events, I don't believe there is currently any method of directly obtaining which world the entity was actually added to/removed from. Using event.getEntity().getWorld() works in many cases, but not all. Specifically, when an entity is teleported from one world to another, the location of the entity is updated prior to the removal event being called. This means that when an entity goes through a nether/end portal or is teleported between worlds with a command, a plugin listening to the EntityRemoveFromWorldEvent has no way of determining which world the entity was actually removed from (without relying on other events).

To resolve this, I've added the world as a field in the events along with a getter to retrieve it. I also removed an unused import and made the documentation more clear on the event behaviour when chunks load/unload.

@1stGlitch 1stGlitch requested a review from a team as a code owner January 20, 2024 21:52
Copy link
Copy Markdown
Member

@Machine-Maker Machine-Maker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the javadocs include a note that it might not be the same as the entity's world? or is that not worth mentioning.

@Leguan16
Copy link
Copy Markdown
Contributor

I think it doesn't hurt to add it.

@1stGlitch
Copy link
Copy Markdown
Contributor Author

Sure, it certainly doesn't hurt to clarify things. I added a note to the javadoc in the EntityRemoveFromWorld event. Though I don't believe it's necessary to note it in the EntityAddToWorld event. While I did add a getWorld() method to the EntityAddToWorld event as a precaution, this issue mainly pertains to the EntityRemoveFromWorld event, and I can't think of a scenario where the world would differ from the entity's world when the EntityAddToWorld event is being fired.

@Owen1212055 Owen1212055 force-pushed the add-world-to-entity-add-remove-events branch from 902f313 to 512f017 Compare January 23, 2024 20:12
@Owen1212055 Owen1212055 merged commit 581b101 into PaperMC:master Jan 23, 2024
lynxplay pushed a commit to lynxplay/paper that referenced this pull request Feb 23, 2024
When a plugin listens to the EntityAddToWorld and EntityRemoveFromWorld events, I don't believe there is currently any method of directly obtaining which world the entity was actually added to/removed from. Using event.getEntity().getWorld() works in many cases, but not all. Specifically, when an entity is teleported from one world to another, the location of the entity is updated prior to the removal event being called. This means that when an entity goes through a nether/end portal or is teleported between worlds with a command, a plugin listening to the EntityRemoveFromWorldEvent has no way of determining which world the entity was actually removed from (without relying on other events).

To resolve this, I've added the world as a field in the events along with a getter to retrieve it. I also removed an unused import and made the documentation more clear on the event behaviour when chunks load/unload.
LeonTG pushed a commit to LeonTG/Paper that referenced this pull request May 17, 2026
When a plugin listens to the EntityAddToWorld and EntityRemoveFromWorld events, I don't believe there is currently any method of directly obtaining which world the entity was actually added to/removed from. Using event.getEntity().getWorld() works in many cases, but not all. Specifically, when an entity is teleported from one world to another, the location of the entity is updated prior to the removal event being called. This means that when an entity goes through a nether/end portal or is teleported between worlds with a command, a plugin listening to the EntityRemoveFromWorldEvent has no way of determining which world the entity was actually removed from (without relying on other events).

To resolve this, I've added the world as a field in the events along with a getter to retrieve it. I also removed an unused import and made the documentation more clear on the event behaviour when chunks load/unload.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants