Skip to content

Commit

Permalink
Add EntityDamageEvent and implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed Aug 5, 2022
1 parent d05b320 commit cf2832a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/main/java/emu/grasscutter/game/entity/GameEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import emu.grasscutter.net.proto.MotionStateOuterClass.MotionState;
import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.grasscutter.net.proto.VectorOuterClass.Vector;
import emu.grasscutter.server.event.entity.EntityDamageEvent;
import emu.grasscutter.server.event.entity.EntityDeathEvent;
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
import emu.grasscutter.utils.Position;
Expand Down Expand Up @@ -50,7 +51,7 @@ public int getId() {
}

public int getEntityType() {
return getId() >> 24;
return this.getId() >> 24;
}

public World getWorld() {
Expand All @@ -66,7 +67,7 @@ public boolean isAlive() {
}

public LifeState getLifeState() {
return isAlive() ? LifeState.LIFE_ALIVE : LifeState.LIFE_DEAD;
return this.isAlive() ? LifeState.LIFE_ALIVE : LifeState.LIFE_DEAD;
}

public Map<String, Float> getMetaOverrideMap() {
Expand Down Expand Up @@ -122,15 +123,15 @@ private void setFightProperty(int id, float value) {
}

public void addFightProperty(FightProperty prop, float value) {
this.getFightProperties().put(prop.getId(), getFightProperty(prop) + value);
this.getFightProperties().put(prop.getId(), this.getFightProperty(prop) + value);
}

public float getFightProperty(FightProperty prop) {
return getFightProperties().getOrDefault(prop.getId(), 0f);
return this.getFightProperties().getOrDefault(prop.getId(), 0f);
}

public void addAllFightPropsToEntityInfo(SceneEntityInfo.Builder entityInfo) {
for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) {
for (Int2FloatMap.Entry entry : this.getFightProperties().int2FloatEntrySet()) {
if (entry.getIntKey() == 0) {
continue;
}
Expand Down Expand Up @@ -165,8 +166,8 @@ public void setGroupId(int groupId) {

protected MotionInfo getMotionInfo() {
MotionInfo proto = MotionInfo.newBuilder()
.setPos(getPosition().toProto())
.setRot(getRotation().toProto())
.setPos(this.getPosition().toProto())
.setRot(this.getRotation().toProto())
.setSpeed(Vector.newBuilder())
.setState(this.getMotionState())
.build();
Expand All @@ -187,8 +188,8 @@ public float heal(float amount) {
return 0f;
}

float curHp = getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
float maxHp = getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
float curHp = this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
float maxHp = this.getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);

if (curHp >= maxHp) {
return 0f;
Expand All @@ -197,37 +198,43 @@ public float heal(float amount) {
float healed = Math.min(maxHp - curHp, amount);
this.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, healed);

getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));
this.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));

return healed;
}

public void damage(float amount) {
damage(amount, 0);
this.damage(amount, 0);
}

public void damage(float amount, int killerId) {
// Sanity check
if (getFightProperties() == null) {
// Check if the entity has properties.
if (this.getFightProperties() == null) {
return;
}

// Lose hp
addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, -amount);
// Invoke entity damage event.
EntityDamageEvent event = new EntityDamageEvent(this, amount, this.getScene().getEntityById(killerId));
event.call(); if (event.isCanceled()) {
return; // If the event is canceled, do not damage the entity.
}

// Add negative HP to the current HP property.
this.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, -(event.getDamage()));

// Check if dead
boolean isDead = false;
if (getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) <= 0f) {
setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f);
if (this.getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) <= 0f) {
this.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f);
isDead = true;
}

// Packets
this.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));

// Check if dead
// Check if dead.
if (isDead) {
getScene().killEntity(this, killerId);
this.getScene().killEntity(this, killerId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package emu.grasscutter.server.event.entity;

import emu.grasscutter.game.entity.GameEntity;
import emu.grasscutter.server.event.Cancellable;
import emu.grasscutter.server.event.types.EntityEvent;

import javax.annotation.Nullable;

public final class EntityDamageEvent extends EntityEvent implements Cancellable {
private float damage;
@Nullable private final GameEntity damager;

public EntityDamageEvent(GameEntity entity, float damage, @Nullable GameEntity damager) {
super(entity);

this.damage = damage;
this.damager = damager;
}

public float getDamage() {
return this.damage;
}

public void setDamage(float damage) {
this.damage = damage;
}

@Nullable
public GameEntity getDamager() {
return this.damager;
}
}

0 comments on commit cf2832a

Please sign in to comment.