EntityTickEvent and tick counter in Entity

This commit is contained in:
jglrxavpok 2020-05-07 16:00:52 +02:00
parent 9d9c158af0
commit 0daae4d831
2 changed files with 39 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.entity.EntitySpawnEvent;
import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback;
import net.minestom.server.event.entity.EntityTickEvent;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.CustomBlock;
@ -88,9 +89,15 @@ public abstract class Entity implements Viewable, DataContainer {
protected boolean silent;
protected boolean noGravity;
protected Pose pose = Pose.STANDING;
private long velocityUpdatePeriod;
protected boolean onGround;
// Tick related
private long ticks;
private final EntityTickEvent tickEvent = new EntityTickEvent(this);
public Entity(int entityType, Position spawnPosition) {
this.id = generateId();
this.entityType = entityType;
@ -315,6 +322,9 @@ public abstract class Entity implements Viewable, DataContainer {
// Call the abstract update method
update();
ticks++;
callEvent(EntityTickEvent.class, tickEvent); // reuse tickEvent to avoid recreating it each tick
// Scheduled synchronization
if (time - lastSynchronizationTime >= synchronizationDelay) {
lastSynchronizationTime = time;
@ -327,6 +337,14 @@ public abstract class Entity implements Viewable, DataContainer {
}
}
/**
* Returns the number of ticks this entity has been active for
* @return
*/
public long getAliveTicks() {
return ticks;
}
/**
* How does this entity handle being in the void?
*/

View File

@ -0,0 +1,21 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.Event;
/**
* Called when an entity ticks itself.
* Same instance used for all tick events for the same entity
*/
public class EntityTickEvent extends Event {
private final Entity entity;
public EntityTickEvent(Entity entity) {
this.entity = entity;
}
public Entity getEntity() {
return entity;
}
}