mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 02:57:37 +01:00
Added EntityDamageEvent
This commit is contained in:
parent
eaf9349613
commit
bf795c1442
@ -4,6 +4,7 @@ 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.DeathEvent;
|
||||
import net.minestom.server.event.EntityDamageEvent;
|
||||
import net.minestom.server.event.PickupItemEvent;
|
||||
import net.minestom.server.instance.Chunk;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
@ -105,18 +106,25 @@ public abstract class LivingEntity extends Entity {
|
||||
}
|
||||
|
||||
public void damage(DamageType type, float value) {
|
||||
if(isImmune(type)) {
|
||||
if (isImmune(type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
EntityDamageEvent entityDamageEvent = new EntityDamageEvent(type, value);
|
||||
callCancellableEvent(EntityDamageEvent.class, entityDamageEvent, () -> {
|
||||
float damage = entityDamageEvent.getDamage();
|
||||
|
||||
EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket();
|
||||
entityAnimationPacket.entityId = getEntityId();
|
||||
entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE;
|
||||
sendPacketToViewersAndSelf(entityAnimationPacket);
|
||||
setHealth(getHealth() - value);
|
||||
setHealth(getHealth() - damage);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this entity immune to the given type of damage?
|
||||
*
|
||||
* @param type the type of damage
|
||||
* @return true iff this entity is immune to the given type of damage
|
||||
*/
|
||||
@ -212,7 +220,7 @@ public abstract class LivingEntity extends Entity {
|
||||
@Override
|
||||
protected void handleVoid() {
|
||||
// Kill if in void
|
||||
if(getInstance().isInVoid(this.position)) {
|
||||
if (getInstance().isInVoid(this.position)) {
|
||||
damage(DamageType.VOID, 10f);
|
||||
}
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ public class Player extends LivingEntity {
|
||||
|
||||
@Override
|
||||
public void damage(DamageType type, float value) {
|
||||
if(!isImmune(type)) {
|
||||
if (!isImmune(type)) {
|
||||
lastDamageSource = type;
|
||||
}
|
||||
super.damage(type, value);
|
||||
@ -245,10 +245,10 @@ public class Player extends LivingEntity {
|
||||
|
||||
@Override
|
||||
public void kill() {
|
||||
if(!isDead()) {
|
||||
if (!isDead()) {
|
||||
// send death message to player
|
||||
TextObject deathMessage;
|
||||
if(lastDamageSource != null) {
|
||||
if (lastDamageSource != null) {
|
||||
deathMessage = lastDamageSource.buildDeathMessage();
|
||||
} else { // may happen if killed by the server without applying damage
|
||||
deathMessage = TextBuilder.of("Killed by poor programming.").build();
|
||||
@ -258,10 +258,10 @@ public class Player extends LivingEntity {
|
||||
|
||||
// send death message to chat
|
||||
TextObject chatMessage;
|
||||
if(lastDamageSource != null) {
|
||||
if (lastDamageSource != null) {
|
||||
chatMessage = lastDamageSource.buildChatMessage(this);
|
||||
} else { // may happen if killed by the server without applying damage
|
||||
chatMessage = TextBuilder.of(getUsername()+" was killed by poor programming.").build();
|
||||
chatMessage = TextBuilder.of(getUsername() + " was killed by poor programming.").build();
|
||||
}
|
||||
MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> {
|
||||
player.sendMessage(chatMessage);
|
||||
@ -455,7 +455,7 @@ public class Player extends LivingEntity {
|
||||
|
||||
@Override
|
||||
public boolean isImmune(DamageType type) {
|
||||
if(getGameMode().canTakeDamage()) {
|
||||
if (getGameMode().canTakeDamage()) {
|
||||
return type != DamageType.VOID;
|
||||
}
|
||||
return super.isImmune(type);
|
||||
@ -659,6 +659,7 @@ public class Player extends LivingEntity {
|
||||
|
||||
/**
|
||||
* Returns true iff this player is in creative. Used for code readability
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isCreative() {
|
||||
|
@ -22,19 +22,19 @@ public class DamageType {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public TextObject buildChatMessage(Player killed) {
|
||||
return TextBuilder.of(killed.getUsername()+" was killed by damage of type "+identifier).build();
|
||||
public static DamageType fromProjectile(Entity shooter, Entity projectile) {
|
||||
return new EntityProjectileDamage(shooter, projectile);
|
||||
}
|
||||
|
||||
public TextObject buildDeathMessage() {
|
||||
return TextBuilder.of("Killed by damage of type "+identifier).build();
|
||||
public TextObject buildChatMessage(Player killed) {
|
||||
return TextBuilder.of(killed.getUsername() + " was killed by damage of type " + identifier).build();
|
||||
}
|
||||
|
||||
public static DamageType fromPlayer(Player player) {
|
||||
return new EntityDamage(player);
|
||||
}
|
||||
|
||||
public static DamageType fromProjectile(Entity shooter, Entity projectile) {
|
||||
return new EntityProjectileDamage(shooter, projectile);
|
||||
public TextObject buildDeathMessage() {
|
||||
return TextBuilder.of("Killed by damage of type " + identifier).build();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.entity.damage.DamageType;
|
||||
|
||||
public class EntityDamageEvent extends CancellableEvent {
|
||||
|
||||
private DamageType damageType;
|
||||
private float damage;
|
||||
|
||||
public EntityDamageEvent(DamageType damageType, float damage) {
|
||||
this.damageType = damageType;
|
||||
this.damage = damage;
|
||||
}
|
||||
|
||||
public DamageType getDamageType() {
|
||||
return damageType;
|
||||
}
|
||||
|
||||
public float getDamage() {
|
||||
return damage;
|
||||
}
|
||||
|
||||
public void setDamage(float damage) {
|
||||
this.damage = damage;
|
||||
}
|
||||
}
|
@ -95,6 +95,7 @@ public abstract class Instance implements BlockModifier, DataContainer {
|
||||
/**
|
||||
* Determines whether a position in the void. If true, entities should take damage and die.
|
||||
* Always returning false allow entities to survive in the void
|
||||
*
|
||||
* @param position the position in the world
|
||||
* @return true iif position is inside the void
|
||||
*/
|
||||
|
@ -33,6 +33,7 @@ public class PlayerConnection {
|
||||
public void sendPacket(ServerPacket serverPacket) {
|
||||
ByteBuf buffer = PacketUtils.writePacket(serverPacket);
|
||||
sendPacket(buffer);
|
||||
buffer.release();
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
|
Loading…
Reference in New Issue
Block a user