Skip to content

Commit

Permalink
Method renames and NBT cleanup for entities (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon authored Jul 10, 2024
1 parent bd5ce7f commit 8de9b1e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
+ if (net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.entity.EntityJoinLevelEvent(p_104741_, this)).isCanceled()) return;
this.removeEntity(p_104741_.getId(), Entity.RemovalReason.DISCARDED);
this.entityStorage.addEntity(p_104741_);
+ p_104741_.onAddedToWorld();
+ p_104741_.onAddedToLevel();
}

public void removeEntity(int p_171643_, Entity.RemovalReason p_171644_) {
Expand Down Expand Up @@ -115,7 +115,7 @@
p_171716_.unRide();
ClientLevel.this.players.remove(p_171716_);
+
+ p_171716_.onRemovedFromWorld();
+ p_171716_.onRemovedFromLevel();
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.entity.EntityLeaveLevelEvent(p_171716_, ClientLevel.this));
+
+ if (p_171716_.isMultipartEntity()) {
Expand Down
6 changes: 3 additions & 3 deletions patches/net/minecraft/server/level/ServerLevel.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@

- this.entityManager.addNewEntity(p_8854_);
+ this.entityManager.addNewEntityWithoutEvent(p_8854_);
+ p_8854_.onAddedToWorld();
+ p_8854_.onAddedToLevel();
}

private boolean addEntity(Entity p_8873_) {
Expand All @@ -124,7 +124,7 @@
} else {
- return this.entityManager.addNewEntity(p_8873_);
+ if (this.entityManager.addNewEntity(p_8873_)) {
+ p_8873_.onAddedToWorld();
+ p_8873_.onAddedToLevel();
+ return true;
+ } else {
+ return false;
Expand Down Expand Up @@ -216,7 +216,7 @@

p_143375_.updateDynamicGameEventListener(DynamicGameEventListener::remove);
+
+ p_143375_.onRemovedFromWorld();
+ p_143375_.onRemovedFromLevel();
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.entity.EntityLeaveLevelEvent(p_143375_, ServerLevel.this));
}

Expand Down
10 changes: 5 additions & 5 deletions patches/net/minecraft/world/entity/Entity.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@

this.levelCallback.onMove();
}
+ if (this.isAddedToWorld() && !this.level.isClientSide && !this.isRemoved()) this.level.getChunk((int) Math.floor(p_20344_) >> 4, (int) Math.floor(p_20346_) >> 4); // Forge - ensure target chunk is loaded.
+ if (this.isAddedToLevel() && !this.level.isClientSide && !this.isRemoved()) this.level.getChunk((int) Math.floor(p_20344_) >> 4, (int) Math.floor(p_20346_) >> 4); // Forge - ensure target chunk is loaded.
}

public void checkDespawn() {
Expand Down Expand Up @@ -502,16 +502,16 @@
+ * allow guarantees that entity position changes will force a chunk load, avoiding
+ * potential issues with entity desyncing and bad chunk data.
+ */
+ private boolean isAddedToWorld;
+ private boolean isAddedToLevel;
+
+ @Override
+ public final boolean isAddedToWorld() { return this.isAddedToWorld; }
+ public final boolean isAddedToLevel() { return this.isAddedToLevel; }
+
+ @Override
+ public void onAddedToWorld() { this.isAddedToWorld = true; }
+ public void onAddedToLevel() { this.isAddedToLevel = true; }
+
+ @Override
+ public void onRemovedFromWorld() { this.isAddedToWorld = false; }
+ public void onRemovedFromLevel() { this.isAddedToLevel = false; }
+
+ // Neo: Helper method to stop an entity from being removed if already marked for removal
+ @Override
Expand Down
4 changes: 2 additions & 2 deletions patches/net/minecraft/world/entity/Mob.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@
+
+ /**
+ * Marks this mob as being disallowed to spawn during {@link Level#addFreshEntity(Entity)}.<p>
+ * @throws UnsupportedOperationException if this entity has already been {@link Entity#isAddedToWorld() added to the world}.
+ * @throws UnsupportedOperationException if this entity has already been {@link Entity#isAddedToLevel()} added to the level.
+ * @apiNote Not public-facing API.
+ */
+ @org.jetbrains.annotations.ApiStatus.Internal
+ public final void setSpawnCancelled(boolean cancel) {
+ if (this.isAddedToWorld()) {
+ if (this.isAddedToLevel()) {
+ throw new UnsupportedOperationException("Late invocations of Mob#setSpawnCancelled are not permitted.");
+ }
+ this.spawnCancelled = cancel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
- p_157553_.forEach(p_157607_ -> this.addEntity((T)p_157607_, true));
+ p_157553_.forEach(p_157607_ -> {
+ this.addEntity(p_157607_, true);
+ if (p_157607_ instanceof Entity entity) entity.onAddedToWorld();
+ if (p_157607_ instanceof Entity entity) entity.onAddedToLevel();
+ });
}

public void addWorldGenChunkEntities(Stream<T> p_157560_) {
- p_157560_.forEach(p_157605_ -> this.addEntity((T)p_157605_, false));
+ p_157560_.forEach(p_157605_ -> {
+ this.addEntity(p_157605_, false);
+ if (p_157605_ instanceof Entity entity) entity.onAddedToWorld();
+ if (p_157605_ instanceof Entity entity) entity.onAddedToLevel();
+ });
}

Expand All @@ -44,7 +44,7 @@
- chunkentities.getEntities().forEach(p_157593_ -> this.addEntity((T)p_157593_, true));
+ chunkentities.getEntities().forEach(p_157593_ -> {
+ this.addEntity(p_157593_, true);
+ if (p_157593_ instanceof Entity entity) entity.onAddedToWorld();
+ if (p_157593_ instanceof Entity entity) entity.onAddedToLevel();
+ });
this.chunkLoadStatuses.put(chunkentities.getPos().toLong(), PersistentEntitySectionManager.ChunkLoadStatus.LOADED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ private Entity self() {
return (Entity) this;
}

/**
* Deserialize from a compound tag.
*
* @deprecated Use a data component/attachment to store info, or {@link Entity#load(CompoundTag)} as a replacement.
*/
@Override
@Deprecated(forRemoval = true, since = "21.0")
default void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) {
self().load(nbt);
}

/**
* Serialize metadata into a compound tag.
*
* @deprecated Use a data component/attachment to store info. ({@link Entity#setData(AttachmentType, Object)})
*/
@Override
@Deprecated(forRemoval = true, since = "21.0")
default CompoundTag serializeNBT(HolderLookup.Provider provider) {
CompoundTag ret = new CompoundTag();
String id = self().getEncodeId();
Expand Down Expand Up @@ -135,24 +147,17 @@ default MobCategory getClassification(boolean forSpawnCount) {
*
* @return True if this entity is being tracked by a world
*/
// TODO: rename in 1.19 to isAddedToLevel
boolean isAddedToWorld();
boolean isAddedToLevel();

/**
* Called after the entity has been added to the world's
* ticking list. Can be overriden, but needs to call super
* to prevent MC-136995.
* Called after the entity has been added to the world's ticking list.
*/
// TODO: rename in 1.19 to onAddedToLevel
void onAddedToWorld();
void onAddedToLevel();

/**
* Called after the entity has been removed to the world's
* ticking list. Can be overriden, but needs to call super
* to prevent MC-136995.
* Called after the entity has been removed to the world's ticking list.
*/
// TODO: rename in 1.19 to onRemovedFromLevel
void onRemovedFromWorld();
void onRemovedFromLevel();

/**
* Revives an entity that has been removed from a world.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public static boolean checkSpawnPositionSpawner(Mob mob, ServerLevelAccessor lev
* var zombie = new Zombie(level);
* zombie.finalizeSpawn(level, difficulty, spawnType, spawnData);
* level.tryAddFreshEntityWithPassengers(zombie);
* if (zombie.isAddedToWorld()) {
* if (zombie.isAddedToLevel()) {
* // Do stuff with your new zombie
* }
* </pre>
Expand All @@ -288,7 +288,7 @@ public static boolean checkSpawnPositionSpawner(Mob mob, ServerLevelAccessor lev
* var zombie = new Zombie(level);
* EventHooks.finalizeMobSpawn(zombie, level, difficulty, spawnType, spawnData);
* level.tryAddFreshEntityWithPassengers(zombie);
* if (zombie.isAddedToWorld()) {
* if (zombie.isAddedToLevel()) {
* // Do stuff with your new zombie
* }
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public boolean didChunkChange() {
*
* <p><strong>Note:</strong> This event is fired from the {@code Entity} constructor, and therefore the entity instance
* might not be fully initialized. Be cautious in using methods and fields from the instance, and check
* {@link Entity#isAddedToWorld()} or {@link Entity#firstTick}.
* {@link Entity#isAddedToLevel()} or {@link Entity#firstTick}.
*
* <p>This event is not {@linkplain net.neoforged.bus.api.ICancellableEvent cancellable}, and is fired on the
* {@linkplain NeoForge#EVENT_BUS game event bus}.
Expand Down

0 comments on commit 8de9b1e

Please sign in to comment.