Added EntityVelocityEvent + minor refactoring

This commit is contained in:
Felix Cravic 2020-05-14 18:57:44 +02:00
parent da0df5afe3
commit aeeb9f08a4
12 changed files with 92 additions and 25 deletions

View File

@ -8,7 +8,7 @@ import net.minestom.server.benchmark.BenchmarkManager;
import net.minestom.server.benchmark.ThreadResult;
import net.minestom.server.entity.*;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.event.entity.AttackEvent;
import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.ItemUpdateStateEvent;
import net.minestom.server.event.item.PickupItemEvent;
@ -100,7 +100,7 @@ public class PlayerInit {
connectionManager.addPlayerInitialization(player -> {
player.addEventCallback(AttackEvent.class, event -> {
player.addEventCallback(EntityAttackEvent.class, event -> {
Entity entity = event.getTarget();
if (entity instanceof EntityCreature) {
EntityCreature creature = (EntityCreature) entity;

View File

@ -10,6 +10,7 @@ import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback;
import net.minestom.server.event.entity.EntitySpawnEvent;
import net.minestom.server.event.entity.EntityTickEvent;
import net.minestom.server.event.entity.EntityVelocityEvent;
import net.minestom.server.event.handler.EventHandler;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
@ -423,7 +424,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
}
public void setVelocity(Vector velocity) {
this.velocity.copy(velocity);
EntityVelocityEvent entityVelocityEvent = new EntityVelocityEvent(this, velocity);
callCancellableEvent(EntityVelocityEvent.class, entityVelocityEvent, () -> {
this.velocity.copy(entityVelocityEvent.getVelocity());
});
}
public void setGravity(float gravityDragPerTick) {

View File

@ -3,8 +3,8 @@ package net.minestom.server.entity;
import net.minestom.server.collision.BoundingBox;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.property.Attribute;
import net.minestom.server.event.entity.DeathEvent;
import net.minestom.server.event.entity.EntityDamageEvent;
import net.minestom.server.event.entity.EntityDeathEvent;
import net.minestom.server.event.entity.EntityFireEvent;
import net.minestom.server.event.item.PickupItemEvent;
import net.minestom.server.instance.Chunk;
@ -146,8 +146,8 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
refreshIsDead(true); // So the entity isn't killed over and over again
triggerStatus((byte) 3); // Start death animation status
setHealth(0);
DeathEvent deathEvent = new DeathEvent();
callEvent(DeathEvent.class, deathEvent);
EntityDeathEvent entityDeathEvent = new EntityDeathEvent(this);
callEvent(EntityDeathEvent.class, entityDeathEvent);
}
/**
@ -166,7 +166,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
* @param unit unit used to express the duration
*/
public void setFireForDuration(int duration, TimeUnit unit) {
EntityFireEvent entityFireEvent = new EntityFireEvent(duration, unit);
EntityFireEvent entityFireEvent = new EntityFireEvent(this, duration, unit);
callCancellableEvent(EntityFireEvent.class, entityFireEvent, () -> {
long fireTime = entityFireEvent.getFireTime(TimeUnit.MILLISECOND);
setOnFire(true);

View File

@ -5,12 +5,18 @@ import net.minestom.server.event.CancellableEvent;
public class AnimationEvent extends CancellableEvent {
private Player player;
private Player.Hand hand;
public AnimationEvent(Player.Hand hand) {
public AnimationEvent(Player player, Player.Hand hand) {
this.player = player;
this.hand = hand;
}
public Player getPlayer() {
return player;
}
public Player.Hand getHand() {
return hand;
}

View File

@ -1,9 +0,0 @@
package net.minestom.server.event.entity;
import net.minestom.server.event.Event;
public class DeathEvent extends Event {
// TODO cause
}

View File

@ -3,14 +3,20 @@ package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.Event;
public class AttackEvent extends Event {
public class EntityAttackEvent extends Event {
private Entity source;
private Entity target;
public AttackEvent(Entity target) {
public EntityAttackEvent(Entity source, Entity target) {
this.source = source;
this.target = target;
}
public Entity getSource() {
return source;
}
public Entity getTarget() {
return target;
}

View File

@ -0,0 +1,18 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.Event;
public class EntityDeathEvent extends Event {
private Entity entity;
// TODO cause
public EntityDeathEvent(Entity entity) {
this.entity = entity;
}
public Entity getEntity() {
return entity;
}
}

View File

@ -1,17 +1,24 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.utils.time.TimeUnit;
public class EntityFireEvent extends CancellableEvent {
private Entity entity;
private int duration;
private TimeUnit timeUnit;
public EntityFireEvent(int duration, TimeUnit timeUnit) {
public EntityFireEvent(Entity entity, int duration, TimeUnit timeUnit) {
this.entity = entity;
setFireTime(duration, timeUnit);
}
public Entity getEntity() {
return entity;
}
public long getFireTime(TimeUnit timeUnit) {
switch (timeUnit) {
case TICK:

View File

@ -0,0 +1,28 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.utils.Vector;
public class EntityVelocityEvent extends CancellableEvent {
private Entity entity;
private Vector velocity;
public EntityVelocityEvent(Entity entity, Vector velocity) {
this.entity = entity;
this.velocity = velocity;
}
public Entity getEntity() {
return entity;
}
public Vector getVelocity() {
return velocity;
}
public void setVelocity(Vector velocity) {
this.velocity = velocity;
}
}

View File

@ -1,19 +1,26 @@
package net.minestom.server.event.item;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.event.Event;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
public class ArmorEquipEvent extends Event {
private LivingEntity livingEntity;
private ItemStack armorItem;
private ArmorSlot armorSlot;
public ArmorEquipEvent(ItemStack armorItem, ArmorSlot armorSlot) {
public ArmorEquipEvent(LivingEntity livingEntity, ItemStack armorItem, ArmorSlot armorSlot) {
this.livingEntity = livingEntity;
this.armorItem = armorItem;
this.armorSlot = armorSlot;
}
public LivingEntity getEntity() {
return livingEntity;
}
public ItemStack getArmorItem() {
return armorItem;
}

View File

@ -8,7 +8,7 @@ import net.minestom.server.network.packet.server.play.EntityAnimationPacket;
public class AnimationListener {
public static void animationListener(ClientAnimationPacket packet, Player player) {
AnimationEvent animationEvent = new AnimationEvent(packet.hand);
AnimationEvent animationEvent = new AnimationEvent(player, packet.hand);
player.callCancellableEvent(AnimationEvent.class, animationEvent, () -> {
EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket();
entityAnimationPacket.entityId = player.getEntityId();

View File

@ -3,7 +3,7 @@ package net.minestom.server.listener;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.Player;
import net.minestom.server.event.entity.AttackEvent;
import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.event.player.PlayerInteractEvent;
import net.minestom.server.network.packet.client.play.ClientInteractEntityPacket;
@ -18,8 +18,8 @@ public class UseEntityListener {
if (entity instanceof LivingEntity && ((LivingEntity) entity).isDead()) // Can't attack dead entities
return;
AttackEvent attackEvent = new AttackEvent(entity);
player.callEvent(AttackEvent.class, attackEvent);
EntityAttackEvent entityAttackEvent = new EntityAttackEvent(player, entity);
player.callEvent(EntityAttackEvent.class, entityAttackEvent);
} else if (type == ClientInteractEntityPacket.Type.INTERACT) {
PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(entity, packet.hand);
player.callEvent(PlayerInteractEvent.class, playerInteractEvent);