Added EntityEvent.EnteringSection event to replace EnteringChunk event. (#8078)

This commit is contained in:
Take Weiland authored and GitHub committed 2021-10-04 14:43:07 -07:00
1 parent b2cea78006
commit c00777e6ea
4 files changed
+138

No files matched your search

@@ -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<T> f_157611_;
Callback(T p_157614_, long p_157615_, EntitySection<T> 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<T> 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);
}
}
@@ -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<T> f_157670_;
Callback(T p_157673_, long p_157674_, EntitySection<T> 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<T> 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);
}
}
@@ -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));
}
}
@@ -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. <br>
* Sections are 16x16x16 block grids of the world.<br>
* 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.
* <br>
* This event is not {@link net.minecraftforge.eventbus.api.Cancelable}.<br>
* <br>
* This event does not have a result. {@link HasResult}
* <br>
* This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.<br>
**/
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.<br>
* CAREFUL: This is also fired in the Entity constructor. Therefore the entity(subclass) might not be fully initialized. Check Entity#isAddedToWorld() or !Entity#firstUpdate.<br>