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.damage.DamageType;
|
||||||
import net.minestom.server.entity.property.Attribute;
|
import net.minestom.server.entity.property.Attribute;
|
||||||
import net.minestom.server.event.DeathEvent;
|
import net.minestom.server.event.DeathEvent;
|
||||||
|
import net.minestom.server.event.EntityDamageEvent;
|
||||||
import net.minestom.server.event.PickupItemEvent;
|
import net.minestom.server.event.PickupItemEvent;
|
||||||
import net.minestom.server.instance.Chunk;
|
import net.minestom.server.instance.Chunk;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
@ -105,18 +106,25 @@ public abstract class LivingEntity extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void damage(DamageType type, float value) {
|
public void damage(DamageType type, float value) {
|
||||||
if(isImmune(type)) {
|
if (isImmune(type)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket();
|
|
||||||
entityAnimationPacket.entityId = getEntityId();
|
EntityDamageEvent entityDamageEvent = new EntityDamageEvent(type, value);
|
||||||
entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE;
|
callCancellableEvent(EntityDamageEvent.class, entityDamageEvent, () -> {
|
||||||
sendPacketToViewersAndSelf(entityAnimationPacket);
|
float damage = entityDamageEvent.getDamage();
|
||||||
setHealth(getHealth() - value);
|
|
||||||
|
EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket();
|
||||||
|
entityAnimationPacket.entityId = getEntityId();
|
||||||
|
entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE;
|
||||||
|
sendPacketToViewersAndSelf(entityAnimationPacket);
|
||||||
|
setHealth(getHealth() - damage);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is this entity immune to the given type of damage?
|
* Is this entity immune to the given type of damage?
|
||||||
|
*
|
||||||
* @param type the type of damage
|
* @param type the type of damage
|
||||||
* @return true iff this entity is immune to the given 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
|
@Override
|
||||||
protected void handleVoid() {
|
protected void handleVoid() {
|
||||||
// Kill if in void
|
// Kill if in void
|
||||||
if(getInstance().isInVoid(this.position)) {
|
if (getInstance().isInVoid(this.position)) {
|
||||||
damage(DamageType.VOID, 10f);
|
damage(DamageType.VOID, 10f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,7 +117,7 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void damage(DamageType type, float value) {
|
public void damage(DamageType type, float value) {
|
||||||
if(!isImmune(type)) {
|
if (!isImmune(type)) {
|
||||||
lastDamageSource = type;
|
lastDamageSource = type;
|
||||||
}
|
}
|
||||||
super.damage(type, value);
|
super.damage(type, value);
|
||||||
@ -245,10 +245,10 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void kill() {
|
public void kill() {
|
||||||
if(!isDead()) {
|
if (!isDead()) {
|
||||||
// send death message to player
|
// send death message to player
|
||||||
TextObject deathMessage;
|
TextObject deathMessage;
|
||||||
if(lastDamageSource != null) {
|
if (lastDamageSource != null) {
|
||||||
deathMessage = lastDamageSource.buildDeathMessage();
|
deathMessage = lastDamageSource.buildDeathMessage();
|
||||||
} else { // may happen if killed by the server without applying damage
|
} else { // may happen if killed by the server without applying damage
|
||||||
deathMessage = TextBuilder.of("Killed by poor programming.").build();
|
deathMessage = TextBuilder.of("Killed by poor programming.").build();
|
||||||
@ -258,10 +258,10 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
// send death message to chat
|
// send death message to chat
|
||||||
TextObject chatMessage;
|
TextObject chatMessage;
|
||||||
if(lastDamageSource != null) {
|
if (lastDamageSource != null) {
|
||||||
chatMessage = lastDamageSource.buildChatMessage(this);
|
chatMessage = lastDamageSource.buildChatMessage(this);
|
||||||
} else { // may happen if killed by the server without applying damage
|
} 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 -> {
|
MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> {
|
||||||
player.sendMessage(chatMessage);
|
player.sendMessage(chatMessage);
|
||||||
@ -455,7 +455,7 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isImmune(DamageType type) {
|
public boolean isImmune(DamageType type) {
|
||||||
if(getGameMode().canTakeDamage()) {
|
if (getGameMode().canTakeDamage()) {
|
||||||
return type != DamageType.VOID;
|
return type != DamageType.VOID;
|
||||||
}
|
}
|
||||||
return super.isImmune(type);
|
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
|
* Returns true iff this player is in creative. Used for code readability
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean isCreative() {
|
public boolean isCreative() {
|
||||||
|
@ -22,19 +22,19 @@ public class DamageType {
|
|||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextObject buildChatMessage(Player killed) {
|
public static DamageType fromProjectile(Entity shooter, Entity projectile) {
|
||||||
return TextBuilder.of(killed.getUsername()+" was killed by damage of type "+identifier).build();
|
return new EntityProjectileDamage(shooter, projectile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextObject buildDeathMessage() {
|
public TextObject buildChatMessage(Player killed) {
|
||||||
return TextBuilder.of("Killed by damage of type "+identifier).build();
|
return TextBuilder.of(killed.getUsername() + " was killed by damage of type " + identifier).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DamageType fromPlayer(Player player) {
|
public static DamageType fromPlayer(Player player) {
|
||||||
return new EntityDamage(player);
|
return new EntityDamage(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DamageType fromProjectile(Entity shooter, Entity projectile) {
|
public TextObject buildDeathMessage() {
|
||||||
return new EntityProjectileDamage(shooter, projectile);
|
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.
|
* 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
|
* Always returning false allow entities to survive in the void
|
||||||
|
*
|
||||||
* @param position the position in the world
|
* @param position the position in the world
|
||||||
* @return true iif position is inside the void
|
* @return true iif position is inside the void
|
||||||
*/
|
*/
|
||||||
|
@ -33,6 +33,7 @@ public class PlayerConnection {
|
|||||||
public void sendPacket(ServerPacket serverPacket) {
|
public void sendPacket(ServerPacket serverPacket) {
|
||||||
ByteBuf buffer = PacketUtils.writePacket(serverPacket);
|
ByteBuf buffer = PacketUtils.writePacket(serverPacket);
|
||||||
sendPacket(buffer);
|
sendPacket(buffer);
|
||||||
|
buffer.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() {
|
public void flush() {
|
||||||
|
Loading…
Reference in New Issue
Block a user