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

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