From d7b7740bcc4e84b051a7fc7f6a2c1b4482b8b2d5 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Fri, 5 May 2023 20:08:25 +0300 Subject: [PATCH 1/3] Add Item Damge event --- .../njol/skript/expressions/ExprDamage.java | 88 ++++++++++++------- 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprDamage.java b/src/main/java/ch/njol/skript/expressions/ExprDamage.java index 2b0a5bd5c0a..720041f3ecc 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprDamage.java +++ b/src/main/java/ch/njol/skript/expressions/ExprDamage.java @@ -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; @@ -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", + "\tdelete damage # unbreakable tools", + "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 { static { @@ -57,9 +65,9 @@ public class ExprDamage extends SimpleExpression { 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; @@ -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: @@ -128,7 +152,7 @@ public Class getReturnType() { } @Override - public String toString(final @Nullable Event e, final boolean debug) { + public String toString(@Nullable Event event, boolean debug) { return "the damage"; } From c5888ca57a3c878f8793ca125dbc0170300396c4 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Fri, 5 May 2023 20:22:06 +0300 Subject: [PATCH 2/3] Events Annotation Naming --- src/main/java/ch/njol/skript/expressions/ExprDamage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprDamage.java b/src/main/java/ch/njol/skript/expressions/ExprDamage.java index 720041f3ecc..91c00c8323c 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprDamage.java +++ b/src/main/java/ch/njol/skript/expressions/ExprDamage.java @@ -54,7 +54,7 @@ "\tincrease the damage by 2" }) @Since("1.3.5, INSERT VERSION (item damage event)") -@Events({"damage", "vehicle damage", "item damage"}) +@Events({"Damage", "Vehicle Damage", "Item Damage"}) public class ExprDamage extends SimpleExpression { static { From 9b2e3e178ffb0046b4623ed864bec4cb003f17aa Mon Sep 17 00:00:00 2001 From: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 27 May 2023 12:20:16 +0300 Subject: [PATCH 3/3] Update src/main/java/ch/njol/skript/expressions/ExprDamage.java Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- src/main/java/ch/njol/skript/expressions/ExprDamage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprDamage.java b/src/main/java/ch/njol/skript/expressions/ExprDamage.java index 91c00c8323c..20305cb05be 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprDamage.java +++ b/src/main/java/ch/njol/skript/expressions/ExprDamage.java @@ -49,7 +49,7 @@ @Examples({ "on item damage:", "\tevent-item is any tool", - "\tdelete damage # unbreakable tools", + "\tclear damage # unbreakable tools as the damage will be 0", "on damage:", "\tincrease the damage by 2" })