Skip to content

Commit

Permalink
Fix cause matching in EntityDamaged/KilledScriptEvent (#2392)
Browse files Browse the repository at this point in the history
* Fix `EntityDamagedScriptEvent` cause matching

* Fix `EntityKilledScriptEvent` cause matching
  • Loading branch information
tal5 committed Oct 22, 2022
1 parent a9b802a commit ba8762c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
Expand Up @@ -49,7 +49,7 @@ public class EntityDamagedScriptEvent extends BukkitScriptEvent implements Liste
// @Context
// <context.entity> returns the EntityTag that was damaged.
// <context.damager> returns the EntityTag damaging the other entity, if any.
// <context.cause> returns the an ElementTag of reason the entity was damaged - see <@link language damage cause> for causes.
// <context.cause> returns an ElementTag of reason the entity was damaged - see <@link language damage cause> for causes.
// <context.damage> returns an ElementTag(Decimal) of the amount of damage dealt.
// <context.final_damage> returns an ElementTag(Decimal) of the amount of damage dealt, after armor is calculated.
// <context.projectile> returns a EntityTag of the projectile, if one caused the event.
Expand Down Expand Up @@ -93,7 +93,6 @@ public EntityDamagedScriptEvent() {


public EntityTag entity;
public ElementTag cause;
public EntityTag damager;
public EntityTag projectile;
public ItemTag held;
Expand Down Expand Up @@ -129,12 +128,12 @@ public boolean matches(ScriptPath path) {
String target = cmd.equals("damages") ? path.eventArgLowerAt(2) : path.eventArgLowerAt(0);
if (!attacker.isEmpty()) {
if (damager != null) {
if (!cause.asString().equals(attacker) && (projectile == null || !projectile.tryAdvancedMatcher(attacker)) && (damager == null || !damager.tryAdvancedMatcher(attacker))) {
if (!runGenericCheck(attacker, event.getCause().name()) && (projectile == null || !projectile.tryAdvancedMatcher(attacker)) && (damager == null || !damager.tryAdvancedMatcher(attacker))) {
return false;
}
}
else {
if (!cause.asString().equals(attacker)) {
if (!runGenericCheck(attacker, event.getCause().name())) {
return false;
}
}
Expand Down Expand Up @@ -214,7 +213,7 @@ public ObjectTag getContext(String name) {
case "entity": return entity.getDenizenObject();
case "damage": return new ElementTag(event.getDamage());
case "final_damage": return new ElementTag(event.getFinalDamage());
case "cause": return cause;
case "cause": return new ElementTag(event.getCause());
case "damager":
if (damager != null) {
return damager.getDenizenObject();
Expand Down Expand Up @@ -249,7 +248,6 @@ public ObjectTag getContext(String name) {
@EventHandler
public void onEntityDamaged(EntityDamageEvent event) {
entity = new EntityTag(event.getEntity());
cause = new ElementTag(event.getCause());
damager = null;
projectile = null;
held = null;
Expand Down
Expand Up @@ -61,7 +61,6 @@ public EntityKilledScriptEvent() {


public EntityTag entity;
public ElementTag cause;
public ElementTag final_damage;
public EntityTag damager;
public EntityTag projectile;
Expand All @@ -77,11 +76,11 @@ public boolean matches(ScriptPath path) {
String target = cmd.equals("kills") ? arg2 : arg0;
if (!attacker.isEmpty()) {
if (damager != null) {
if (!cause.asString().equals(attacker) && (projectile == null || !projectile.tryAdvancedMatcher(attacker)) && (damager == null || !damager.tryAdvancedMatcher(attacker))) {
if (!runGenericCheck(attacker, event.getCause().name()) && (projectile == null || !projectile.tryAdvancedMatcher(attacker)) && (damager == null || !damager.tryAdvancedMatcher(attacker))) {
return false;
}
}
else if (!cause.asString().equals(attacker)) {
else if (!runGenericCheck(attacker, event.getCause().name())) {
return false;
}
}
Expand Down Expand Up @@ -118,32 +117,21 @@ public ScriptEntryData getScriptEntryData() {

@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity.getDenizenObject();
}
else if (name.equals("damage")) {
return new ElementTag(event.getDamage());
}
else if (name.equals("final_damage")) {
return final_damage;
}
else if (name.equals("cause")) {
return cause;
}
else if (name.equals("damager") && damager != null) {
return damager.getDenizenObject();
}
else if (name.equals("projectile") && projectile != null) {
return projectile.getDenizenObject();
}
else if (name.equals("damage_type_map")) {
MapTag map = new MapTag();
for (EntityDamageEvent.DamageModifier dm : EntityDamageEvent.DamageModifier.values()) {
map.putObject(dm.name(), new ElementTag(event.getDamage(dm)));
}
return map;
switch (name) {
case "entity": return entity.getDenizenEntity();
case "damage": return new ElementTag(event.getDamage());
case "final_damage": return final_damage;
case "cause": return new ElementTag(event.getCause());
case "damager": return damager != null ? damager.getDenizenObject() : null;
case "projectile": return projectile != null ? projectile.getDenizenObject() : null;
case "damage_type_map":
MapTag map = new MapTag();
for (EntityDamageEvent.DamageModifier dm : EntityDamageEvent.DamageModifier.values()) {
map.putObject(dm.name(), new ElementTag(event.getDamage(dm)));
}
return map;
}
else if (name.startsWith("damage_")) {
if (name.startsWith("damage_")) {
BukkitImplDeprecations.damageEventTypeMap.warn();
for (EntityDamageEvent.DamageModifier dm : EntityDamageEvent.DamageModifier.values()) {
if (name.equals("damage_" + CoreUtilities.toLowerCase(dm.name()))) {
Expand All @@ -167,7 +155,6 @@ public void onEntityKilled(EntityDamageEvent event) {
return;
}
final_damage = new ElementTag(event.getFinalDamage());
cause = new ElementTag(event.getCause());
damager = null;
projectile = null;
if (event instanceof EntityDamageByEntityEvent) {
Expand Down

0 comments on commit ba8762c

Please sign in to comment.