From c00777e6eab01a98b3b2492bb56ae18222993ff4 Mon Sep 17 00:00:00 2001 From: Take Weiland Date: Mon, 4 Oct 2021 23:43:07 +0200 Subject: [PATCH] Added EntityEvent.EnteringSection event to replace EnteringChunk event. (#8078) --- .../PersistentEntitySectionManager.java.patch | 26 +++++++ .../TransientEntitySectionManager.java.patch | 32 ++++++++ .../net/minecraftforge/common/ForgeHooks.java | 7 ++ .../event/entity/EntityEvent.java | 73 +++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 patches/minecraft/net/minecraft/world/level/entity/TransientEntitySectionManager.java.patch diff --git a/patches/minecraft/net/minecraft/world/level/entity/PersistentEntitySectionManager.java.patch b/patches/minecraft/net/minecraft/world/level/entity/PersistentEntitySectionManager.java.patch index ac55755386..bf410c7bca 100644 --- a/patches/minecraft/net/minecraft/world/level/entity/PersistentEntitySectionManager.java.patch +++ b/patches/minecraft/net/minecraft/world/level/entity/PersistentEntitySectionManager.java.patch @@ -17,3 +17,29 @@ if (!this.m_157557_(p_157539_)) { return false; } else { +@@ -350,11 +_,13 @@ + + class Callback implements EntityInLevelCallback { + private final T f_157609_; ++ private final Entity realEntity; + private long f_157610_; + private EntitySection f_157611_; + + Callback(T p_157614_, long p_157615_, EntitySection p_157616_) { + this.f_157609_ = p_157614_; ++ this.realEntity = p_157614_ instanceof Entity ? (Entity) p_157614_ : null; + this.f_157610_ = p_157615_; + this.f_157611_ = p_157616_; + } +@@ -371,9 +_,11 @@ + PersistentEntitySectionManager.this.m_157509_(this.f_157610_, this.f_157611_); + EntitySection entitysection = PersistentEntitySectionManager.this.f_157495_.m_156893_(i); + entitysection.m_156840_(this.f_157609_); ++ long oldSectionKey = f_157610_; + this.f_157611_ = entitysection; + this.f_157610_ = i; + this.m_157620_(visibility, entitysection.m_156848_()); ++ if (this.realEntity != null) net.minecraftforge.common.ForgeHooks.onEntityEnterSection(this.realEntity, oldSectionKey, i); + } + + } diff --git a/patches/minecraft/net/minecraft/world/level/entity/TransientEntitySectionManager.java.patch b/patches/minecraft/net/minecraft/world/level/entity/TransientEntitySectionManager.java.patch new file mode 100644 index 0000000000..fdc28e09d8 --- /dev/null +++ b/patches/minecraft/net/minecraft/world/level/entity/TransientEntitySectionManager.java.patch @@ -0,0 +1,32 @@ +--- a/net/minecraft/world/level/entity/TransientEntitySectionManager.java ++++ b/net/minecraft/world/level/entity/TransientEntitySectionManager.java +@@ -92,11 +_,13 @@ + + class Callback implements EntityInLevelCallback { + private final T f_157668_; ++ private final Entity realEntity; + private long f_157669_; + private EntitySection f_157670_; + + Callback(T p_157673_, long p_157674_, EntitySection p_157675_) { + this.f_157668_ = p_157673_; ++ this.realEntity = p_157673_ instanceof Entity ? (Entity) p_157673_ : null; + this.f_157669_ = p_157674_; + this.f_157670_ = p_157675_; + } +@@ -113,6 +_,7 @@ + TransientEntitySectionManager.this.m_157648_(this.f_157669_, this.f_157670_); + EntitySection entitysection = TransientEntitySectionManager.this.f_157638_.m_156893_(i); + entitysection.m_156840_(this.f_157668_); ++ long oldSectionKey = f_157669_; + this.f_157670_ = entitysection; + this.f_157669_ = i; + if (!this.f_157668_.m_142389_()) { +@@ -124,6 +_,7 @@ + TransientEntitySectionManager.this.f_157636_.m_141987_(this.f_157668_); + } + } ++ if (this.realEntity != null) net.minecraftforge.common.ForgeHooks.onEntityEnterSection(this.realEntity, oldSectionKey, i); + } + + } diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index b2c5758d69..acd058ea89 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -135,6 +135,7 @@ import net.minecraftforge.event.ItemAttributeModifierEvent; import net.minecraftforge.event.ServerChatEvent; import net.minecraftforge.event.entity.EntityAttributeCreationEvent; import net.minecraftforge.event.entity.EntityAttributeModificationEvent; +import net.minecraftforge.event.entity.EntityEvent; import net.minecraftforge.event.entity.EntityTravelToDimensionEvent; import net.minecraftforge.event.entity.item.ItemTossEvent; import net.minecraftforge.event.entity.living.LivingAttackEvent; @@ -1349,4 +1350,10 @@ public class ForgeHooks FORGE_ATTRIBUTES.put(k, newBuilder.build()); }); } + + public static void onEntityEnterSection(Entity entity, long packedOldPos, long packedNewPos) + { + MinecraftForge.EVENT_BUS.post(new EntityEvent.EnteringSection(entity, packedOldPos, packedNewPos)); + } + } diff --git a/src/main/java/net/minecraftforge/event/entity/EntityEvent.java b/src/main/java/net/minecraftforge/event/entity/EntityEvent.java index 78af12b872..0523110f24 100644 --- a/src/main/java/net/minecraftforge/event/entity/EntityEvent.java +++ b/src/main/java/net/minecraftforge/event/entity/EntityEvent.java @@ -19,6 +19,7 @@ package net.minecraftforge.event.entity; +import net.minecraft.core.SectionPos; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityDimensions; import net.minecraft.world.entity.Pose; @@ -97,6 +98,78 @@ public class EntityEvent extends Event } } + /** + * This event is fired on server and client after an Entity has entered a different section.
+ * Sections are 16x16x16 block grids of the world.
+ * This event does not fire when a new entity is spawned, only when an entity moves from one section to another one. + * Use {@link EntityJoinWorldEvent} to detect new entities joining the world. + *
+ * This event is not {@link net.minecraftforge.eventbus.api.Cancelable}.
+ *
+ * This event does not have a result. {@link HasResult} + *
+ * This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.
+ **/ + public static class EnteringSection extends EntityEvent + { + + private final long packedOldPos; + private final long packedNewPos; + + public EnteringSection(Entity entity, long packedOldPos, long packedNewPos) + { + super(entity); + this.packedOldPos = packedOldPos; + this.packedNewPos = packedNewPos; + } + + /** + * A packed version of the old section's position. This is to be used with the various methods in {@link SectionPos}, + * such as {@link SectionPos#of(long)} or {@link SectionPos#x(long)} to avoid allocation. + * @return the packed position of the old section + */ + public long getPackedOldPos() + { + return packedOldPos; + } + + /** + * A packed version of the new section's position. This is to be used with the various methods in {@link SectionPos}, + * such as {@link SectionPos#of(long)} or {@link SectionPos#x(long)} to avoid allocation. + * @return the packed position of the new section + */ + public long getPackedNewPos() + { + return packedNewPos; + } + + /** + * @return the position of the old section + */ + public SectionPos getOldPos() + { + return SectionPos.of(packedOldPos); + } + + /** + * @return the position of the new section + */ + public SectionPos getNewPos() + { + return SectionPos.of(packedNewPos); + } + + /** + * Whether the chunk has changed as part of this event. If this method returns false, only the Y position of the + * section has changed. + */ + public boolean didChunkChange() + { + return SectionPos.x(packedOldPos) != SectionPos.x(packedNewPos) || SectionPos.z(packedOldPos) != SectionPos.z(packedNewPos); + } + + } + /** * This event is fired whenever the {@link Pose} changes, and in a few other hardcoded scenarios.
* CAREFUL: This is also fired in the Entity constructor. Therefore the entity(subclass) might not be fully initialized. Check Entity#isAddedToWorld() or !Entity#firstUpdate.