Skip to content

Commit

Permalink
🚀 Add Item Damge event to ExprDamage (#5678)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyhamAl-Ali authored Sep 25, 2023
1 parent 7152d75 commit 5941679
Showing 1 changed file with 56 additions and 32 deletions.
88 changes: 56 additions & 32 deletions src/main/java/ch/njol/skript/expressions/ExprDamage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -39,14 +40,21 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

/**
* @author Peter Güttinger
*/
@Name("Damage")
@Description("How much damage is done in a damage event, possibly ignoring armour, criticals and/or enchantments. Can be changed (remember that in Skript '1' is one full heart, not half a heart).")
@Examples({"increase the damage by 2"})
@Since("1.3.5")
@Events("damage")
@Description({
"How much damage is done in a entity/vehicle/item damage events.",
"For entity damage events, possibly ignoring armour, criticals and/or enchantments (remember that in Skript '1' is one full heart, not half a heart).",
"For items, it's the amount of durability damage the item will be taking."
})
@Examples({
"on item damage:",
"\tevent-item is any tool",
"\tclear damage # unbreakable tools as the damage will be 0",
"on damage:",
"\tincrease the damage by 2"
})
@Since("1.3.5, INSERT VERSION (item damage event)")
@Events({"Damage", "Vehicle Damage", "Item Damage"})
public class ExprDamage extends SimpleExpression<Number> {

static {
Expand All @@ -57,9 +65,9 @@ public class ExprDamage extends SimpleExpression<Number> {
private Kleenean delay;

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
if (!getParser().isCurrentEvent(EntityDamageEvent.class, VehicleDamageEvent.class)) {
Skript.error("The expression 'damage' may only be used in damage events", ErrorQuality.SEMANTIC_ERROR);
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!getParser().isCurrentEvent(EntityDamageEvent.class, VehicleDamageEvent.class, PlayerItemDamageEvent.class)) {
Skript.error("The 'damage' expression may only be used in damage events");
return false;
}
delay = isDelayed;
Expand All @@ -68,48 +76,64 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final

@Override
@Nullable
protected Number[] get(final Event e) {
if (!(e instanceof EntityDamageEvent || e instanceof VehicleDamageEvent))
protected Number[] get(Event event) {
if (!(event instanceof EntityDamageEvent || event instanceof VehicleDamageEvent || event instanceof PlayerItemDamageEvent))
return new Number[0];

if (e instanceof VehicleDamageEvent)
return CollectionUtils.array(((VehicleDamageEvent) e).getDamage());
return CollectionUtils.array(HealthUtils.getDamage((EntityDamageEvent) e));
if (event instanceof VehicleDamageEvent)
return CollectionUtils.array(((VehicleDamageEvent) event).getDamage());
if (event instanceof PlayerItemDamageEvent)
return CollectionUtils.array(((PlayerItemDamageEvent) event).getDamage());

return CollectionUtils.array(HealthUtils.getDamage((EntityDamageEvent) event));
}

@Override
@Nullable
public Class<?>[] acceptChange(final ChangeMode mode) {
public Class<?>[] acceptChange(ChangeMode mode) {
if (delay != Kleenean.FALSE) {
Skript.error("Can't change the damage anymore after the event has already passed");
return null;
}
if (mode == ChangeMode.REMOVE_ALL)
return null;
return CollectionUtils.array(Number.class);
switch (mode) {
case ADD:
case SET:
case DELETE:
case REMOVE:
return CollectionUtils.array(Number.class);
default:
return null;
}
}

@Override
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) throws UnsupportedOperationException {
if (!(e instanceof EntityDamageEvent || e instanceof VehicleDamageEvent))
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) throws UnsupportedOperationException {
if (!(event instanceof EntityDamageEvent || event instanceof VehicleDamageEvent || event instanceof PlayerItemDamageEvent))
return;
double d = delta == null ? 0 : ((Number) delta[0]).doubleValue();

double value = delta == null ? 0 : ((Number) delta[0]).doubleValue();
switch (mode) {
case SET:
case DELETE:
if (e instanceof VehicleDamageEvent)
((VehicleDamageEvent) e).setDamage(d);
else
HealthUtils.setDamage((EntityDamageEvent) e, d);
if (event instanceof VehicleDamageEvent) {
((VehicleDamageEvent) event).setDamage(value);
} else if (event instanceof PlayerItemDamageEvent) {
((PlayerItemDamageEvent) event).setDamage((int) value);
} else {
HealthUtils.setDamage((EntityDamageEvent) event, value);
}
break;
case REMOVE:
d = -d;
value = -value;
//$FALL-THROUGH$
case ADD:
if (e instanceof VehicleDamageEvent)
((VehicleDamageEvent) e).setDamage(((VehicleDamageEvent) e).getDamage() + d);
else
HealthUtils.setDamage((EntityDamageEvent) e, HealthUtils.getDamage((EntityDamageEvent) e) + d);
if (event instanceof VehicleDamageEvent) {
((VehicleDamageEvent) event).setDamage(((VehicleDamageEvent) event).getDamage() + value);
} else if (event instanceof PlayerItemDamageEvent) {
((PlayerItemDamageEvent) event).setDamage((int) (((PlayerItemDamageEvent) event).getDamage() + value));
} else {
HealthUtils.setDamage((EntityDamageEvent) event, HealthUtils.getDamage((EntityDamageEvent) event) + value);
}
break;
case REMOVE_ALL:
case RESET:
Expand All @@ -128,7 +152,7 @@ public Class<? extends Number> getReturnType() {
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
public String toString(@Nullable Event event, boolean debug) {
return "the damage";
}

Expand Down

0 comments on commit 5941679

Please sign in to comment.