From a514e6b93a8b41a2078c7257b5de3ffb303a8ca2 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Fri, 6 Sep 2024 20:55:32 +0800 Subject: [PATCH 01/25] feat: damage entity with damage-causes --- .../ch/njol/skript/effects/EffHealth.java | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 2cd392e8810..02cfea062fd 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -32,8 +32,12 @@ import ch.njol.skript.util.slot.Slot; import ch.njol.util.Kleenean; import ch.njol.util.Math2; +import org.bukkit.damage.DamageSource; +import org.bukkit.damage.DamageType; import org.bukkit.entity.Damageable; +import org.bukkit.entity.Player; import org.bukkit.event.Event; +import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.inventory.ItemStack; import org.eclipse.jdt.annotation.Nullable; @@ -58,18 +62,21 @@ public class EffHealth extends Effect { @Nullable private Expression amount; private boolean isHealing, isRepairing; + private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); + @Nullable + private Expression exprCause; @SuppressWarnings("unchecked") @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (matchedPattern == 0 && exprs[2] != null) - Skript.warning("The fake damage cause extension of this effect has no functionality, " + - "and will be removed in the future"); + if (matchedPattern == 0 && exprs[2] != null && !canSetDamageCause) + Skript.warning("The fake damage cause extension of this effect has no functionality in your server, it need MC 1.20.4+"); this.damageables = exprs[0]; this.isHealing = matchedPattern >= 1; this.isRepairing = matchedPattern == 2; this.amount = (Expression) exprs[1]; + this.exprCause = (Expression) exprs[2]; return true; } @@ -117,6 +124,39 @@ protected void execute(Event event) { } else if (isHealing) { HealthUtils.heal(damageable, amount); } else { + if (canSetDamageCause && exprCause != null) { + EntityDamageEvent.DamageCause cause = exprCause.getSingle(event); + if (cause != null) { + damageable.damage(amount * 2, DamageSource.builder(switch (cause) { + case KILL, SUICIDE -> DamageType.GENERIC_KILL; + case WORLD_BORDER, VOID -> DamageType.OUT_OF_WORLD; + case CONTACT -> DamageType.CACTUS; + case SUFFOCATION -> DamageType.IN_WALL; + case FALL -> DamageType.FALL; + case FIRE -> DamageType.ON_FIRE; + case FIRE_TICK -> DamageType.IN_FIRE; + case LAVA -> DamageType.LAVA; + case DROWNING -> DamageType.DROWN; + case BLOCK_EXPLOSION, ENTITY_EXPLOSION -> DamageType.EXPLOSION; + case LIGHTNING -> DamageType.LIGHTNING_BOLT; + case STARVATION -> DamageType.STARVE; + case MAGIC, POISON -> DamageType.MAGIC; + case WITHER -> DamageType.WITHER; + case FALLING_BLOCK -> DamageType.FALLING_BLOCK; + case THORNS -> DamageType.THORNS; + case DRAGON_BREATH -> DamageType.DRAGON_BREATH; + case FLY_INTO_WALL -> DamageType.FLY_INTO_WALL; + case HOT_FLOOR -> DamageType.HOT_FLOOR; + case CAMPFIRE -> DamageType.CAMPFIRE; + case CRAMMING -> DamageType.CRAMMING; + case DRYOUT -> DamageType.DRY_OUT; + case FREEZE -> DamageType.FREEZE; + case SONIC_BOOM -> DamageType.SONIC_BOOM; + default -> DamageType.GENERIC; + }).build()); + return; + } + } HealthUtils.damage(damageable, amount); } From 1ff2d38dbe384b0e37eba8077bf960ec8f2823f3 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Fri, 6 Sep 2024 20:55:52 +0800 Subject: [PATCH 02/25] style: import --- src/main/java/ch/njol/skript/effects/EffHealth.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 02cfea062fd..16b8fa193be 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -35,7 +35,6 @@ import org.bukkit.damage.DamageSource; import org.bukkit.damage.DamageType; import org.bukkit.entity.Damageable; -import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.inventory.ItemStack; From 5f3d85fa7585b5e66396d6e5c95875bc0db6344e Mon Sep 17 00:00:00 2001 From: XPYEX Date: Sun, 8 Sep 2024 15:00:29 +0800 Subject: [PATCH 03/25] fix: ArrayIndexOutOfBoundsException --- src/main/java/ch/njol/skript/effects/EffHealth.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 16b8fa193be..b5e4b2be230 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -63,7 +63,7 @@ public class EffHealth extends Effect { private boolean isHealing, isRepairing; private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); @Nullable - private Expression exprCause; + private Expression exprCause = null; @SuppressWarnings("unchecked") @Override @@ -75,7 +75,9 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye this.isHealing = matchedPattern >= 1; this.isRepairing = matchedPattern == 2; this.amount = (Expression) exprs[1]; - this.exprCause = (Expression) exprs[2]; + if (exprs.length > 2) { + this.exprCause = (Expression) exprs[2]; + } return true; } @@ -115,9 +117,7 @@ protected void execute(Event event) { slot.setItem(itemStack); - } else if (obj instanceof Damageable) { - Damageable damageable = (Damageable) obj; - + } else if (obj instanceof Damageable damageable) { if (this.amount == null) { HealthUtils.heal(damageable, HealthUtils.getMaxHealth(damageable)); } else if (isHealing) { From 5b7e469834a45caae3cb7fd1a77f789a3a53651e Mon Sep 17 00:00:00 2001 From: XPYEX Date: Mon, 9 Sep 2024 16:41:21 +0800 Subject: [PATCH 04/25] perf: resolved pr mentioned --- .../njol/skript/bukkitutil/HealthUtils.java | 31 +++++++++++++ .../ch/njol/skript/effects/EffHealth.java | 45 ++++--------------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index be8675ccabb..f5ebb19830e 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -29,6 +29,7 @@ import org.bukkit.entity.LivingEntity; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.reflect.Constructor; @@ -142,4 +143,34 @@ public static void setDamageCause(Damageable e, DamageCause cause) { } } + @NotNull + public static DamageSource getDamageSourceFromCause(DamageCause cause) { + return DamageSource.builder(switch (cause) { + case KILL, SUICIDE -> DamageType.GENERIC_KILL; + case WORLD_BORDER, VOID -> DamageType.OUT_OF_WORLD; + case CONTACT -> DamageType.CACTUS; + case SUFFOCATION -> DamageType.IN_WALL; + case FALL -> DamageType.FALL; + case FIRE -> DamageType.ON_FIRE; + case FIRE_TICK -> DamageType.IN_FIRE; + case LAVA -> DamageType.LAVA; + case DROWNING -> DamageType.DROWN; + case BLOCK_EXPLOSION, ENTITY_EXPLOSION -> DamageType.EXPLOSION; + case LIGHTNING -> DamageType.LIGHTNING_BOLT; + case STARVATION -> DamageType.STARVE; + case MAGIC, POISON -> DamageType.MAGIC; + case WITHER -> DamageType.WITHER; + case FALLING_BLOCK -> DamageType.FALLING_BLOCK; + case THORNS -> DamageType.THORNS; + case DRAGON_BREATH -> DamageType.DRAGON_BREATH; + case FLY_INTO_WALL -> DamageType.FLY_INTO_WALL; + case HOT_FLOOR -> DamageType.HOT_FLOOR; + case CAMPFIRE -> DamageType.CAMPFIRE; + case CRAMMING -> DamageType.CRAMMING; + case DRYOUT -> DamageType.DRY_OUT; + case FREEZE -> DamageType.FREEZE; + case SONIC_BOOM -> DamageType.SONIC_BOOM; + default -> DamageType.GENERIC; + }).build(); + } } diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index b5e4b2be230..8617172edd2 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -32,13 +32,12 @@ import ch.njol.skript.util.slot.Slot; import ch.njol.util.Kleenean; import ch.njol.util.Math2; -import org.bukkit.damage.DamageSource; -import org.bukkit.damage.DamageType; import org.bukkit.entity.Damageable; import org.bukkit.event.Event; -import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.inventory.ItemStack; import org.eclipse.jdt.annotation.Nullable; +import org.jetbrains.annotations.UnknownNullability; @Name("Damage/Heal/Repair") @Description("Damage/Heal/Repair an entity, or item.") @@ -58,25 +57,25 @@ public class EffHealth extends Effect { } private Expression damageables; - @Nullable + @UnknownNullability private Expression amount; private boolean isHealing, isRepairing; private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); - @Nullable - private Expression exprCause = null; + @UnknownNullability + private Expression exprCause = null; @SuppressWarnings("unchecked") @Override public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { if (matchedPattern == 0 && exprs[2] != null && !canSetDamageCause) - Skript.warning("The fake damage cause extension of this effect has no functionality in your server, it need MC 1.20.4+"); + Skript.warning("Using the fake cause extension in effect 'damage' requires Spigot 1.20.4+"); this.damageables = exprs[0]; this.isHealing = matchedPattern >= 1; this.isRepairing = matchedPattern == 2; this.amount = (Expression) exprs[1]; if (exprs.length > 2) { - this.exprCause = (Expression) exprs[2]; + this.exprCause = (Expression) exprs[2]; } return true; } @@ -124,35 +123,9 @@ protected void execute(Event event) { HealthUtils.heal(damageable, amount); } else { if (canSetDamageCause && exprCause != null) { - EntityDamageEvent.DamageCause cause = exprCause.getSingle(event); + DamageCause cause = exprCause.getSingle(event); if (cause != null) { - damageable.damage(amount * 2, DamageSource.builder(switch (cause) { - case KILL, SUICIDE -> DamageType.GENERIC_KILL; - case WORLD_BORDER, VOID -> DamageType.OUT_OF_WORLD; - case CONTACT -> DamageType.CACTUS; - case SUFFOCATION -> DamageType.IN_WALL; - case FALL -> DamageType.FALL; - case FIRE -> DamageType.ON_FIRE; - case FIRE_TICK -> DamageType.IN_FIRE; - case LAVA -> DamageType.LAVA; - case DROWNING -> DamageType.DROWN; - case BLOCK_EXPLOSION, ENTITY_EXPLOSION -> DamageType.EXPLOSION; - case LIGHTNING -> DamageType.LIGHTNING_BOLT; - case STARVATION -> DamageType.STARVE; - case MAGIC, POISON -> DamageType.MAGIC; - case WITHER -> DamageType.WITHER; - case FALLING_BLOCK -> DamageType.FALLING_BLOCK; - case THORNS -> DamageType.THORNS; - case DRAGON_BREATH -> DamageType.DRAGON_BREATH; - case FLY_INTO_WALL -> DamageType.FLY_INTO_WALL; - case HOT_FLOOR -> DamageType.HOT_FLOOR; - case CAMPFIRE -> DamageType.CAMPFIRE; - case CRAMMING -> DamageType.CRAMMING; - case DRYOUT -> DamageType.DRY_OUT; - case FREEZE -> DamageType.FREEZE; - case SONIC_BOOM -> DamageType.SONIC_BOOM; - default -> DamageType.GENERIC; - }).build()); + damageable.damage(amount * 2, HealthUtils.getDamageSourceFromCause(cause)); return; } } From 0896764344acb982c7d45e6d170a1771ae2bfd7a Mon Sep 17 00:00:00 2001 From: XPYEX Date: Mon, 9 Sep 2024 16:51:40 +0800 Subject: [PATCH 05/25] style: Add example --- src/main/java/ch/njol/skript/effects/EffHealth.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 8617172edd2..201ca360f1b 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -43,6 +43,7 @@ @Description("Damage/Heal/Repair an entity, or item.") @Examples({ "damage player by 5 hearts", + "damage player by 3 hearts with fake cause fall", "heal the player", "repair tool of player" }) From 4bbaeefb10fce869fa0a5f3acfa5e8916aa014ba Mon Sep 17 00:00:00 2001 From: XPYEX Date: Mon, 9 Sep 2024 17:11:40 +0800 Subject: [PATCH 06/25] style: sort codes --- .../ch/njol/skript/effects/EffHealth.java | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 201ca360f1b..1e4f1a5dae7 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -1,19 +1,19 @@ /** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * + * This file is part of Skript. + *

+ * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *

+ * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + *

* Copyright Peter Güttinger, SkriptLang team and contributors */ package ch.njol.skript.effects; @@ -49,7 +49,6 @@ }) @Since("1.0") public class EffHealth extends Effect { - static { Skript.registerEffect(EffHealth.class, "damage %livingentities/itemtypes/slots% by %number% [heart[s]] [with fake cause %-damagecause%]", @@ -57,11 +56,11 @@ public class EffHealth extends Effect { "repair %itemtypes/slots% [by %-number%]"); } + private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); private Expression damageables; @UnknownNullability private Expression amount; private boolean isHealing, isRepairing; - private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); @UnknownNullability private Expression exprCause = null; @@ -92,8 +91,7 @@ protected void execute(Event event) { } for (Object obj : this.damageables.getArray(event)) { - if (obj instanceof ItemType) { - ItemType itemType = (ItemType) obj; + if (obj instanceof ItemType itemType) { if (this.amount == null) { ItemUtils.setDamage(itemType, 0); @@ -101,8 +99,7 @@ protected void execute(Event event) { ItemUtils.setDamage(itemType, (int) Math2.fit(0, (ItemUtils.getDamage(itemType) + (isHealing ? -amount : amount)), ItemUtils.getMaxDamage(itemType))); } - } else if (obj instanceof Slot) { - Slot slot = (Slot) obj; + } else if (obj instanceof Slot slot) { ItemStack itemStack = slot.getItem(); if (itemStack == null) @@ -139,14 +136,12 @@ protected void execute(Event event) { @Override public String toString(@Nullable Event event, boolean debug) { - String prefix = "damage "; if (isRepairing) { prefix = "repair "; } else if (isHealing) { prefix = "heal "; } - return prefix + damageables.toString(event, debug) + (amount != null ? " by " + amount.toString(event, debug) : ""); } From 0f4b21111a9969e26d87112bc942e942ad0c69ea Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 11:16:52 +0800 Subject: [PATCH 07/25] perf: resolved PR mentioned --- .../java/ch/njol/skript/effects/EffHealth.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 1e4f1a5dae7..07a04c4abef 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -51,18 +51,16 @@ public class EffHealth extends Effect { static { Skript.registerEffect(EffHealth.class, - "damage %livingentities/itemtypes/slots% by %number% [heart[s]] [with fake cause %-damagecause%]", + "damage %livingentities/itemtypes/slots% by %number% [heart[s]] [with [fake] [damage] cause %-damagecause%]", "heal %livingentities% [by %-number% [heart[s]]]", "repair %itemtypes/slots% [by %-number%]"); } private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); private Expression damageables; - @UnknownNullability - private Expression amount; + private @UnknownNullability Expression amount; private boolean isHealing, isRepairing; - @UnknownNullability - private Expression exprCause = null; + private @UnknownNullability Expression exprCause = null; @SuppressWarnings("unchecked") @Override @@ -74,9 +72,8 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye this.isHealing = matchedPattern >= 1; this.isRepairing = matchedPattern == 2; this.amount = (Expression) exprs[1]; - if (exprs.length > 2) { + if (exprs.length > 2) this.exprCause = (Expression) exprs[2]; - } return true; } @@ -142,7 +139,9 @@ public String toString(@Nullable Event event, boolean debug) { } else if (isHealing) { prefix = "heal "; } - return prefix + damageables.toString(event, debug) + (amount != null ? " by " + amount.toString(event, debug) : ""); + return prefix + damageables.toString(event, debug) + + (amount != null ? " by " + amount.toString(event, debug) : "") + + (exprCause != null && event != null ? "with damage cause " + exprCause.getSingle(event) : ""); } } From d525ca7d2ee3a5c892720dd604535f9cf1857a8a Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 11:39:22 +0800 Subject: [PATCH 08/25] style: depart to DamageUtils --- .../njol/skript/bukkitutil/DamageUtils.java | 40 +++++++++++++++++++ .../njol/skript/bukkitutil/HealthUtils.java | 32 --------------- .../ch/njol/skript/effects/EffHealth.java | 3 +- 3 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java diff --git a/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java new file mode 100644 index 00000000000..f348226490c --- /dev/null +++ b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java @@ -0,0 +1,40 @@ +package ch.njol.skript.bukkitutil; + +import org.bukkit.damage.DamageSource; +import org.bukkit.damage.DamageType; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.jetbrains.annotations.NotNull; + +public class DamageUtils { + @NotNull + @SuppressWarnings("all") + public static DamageSource getDamageSourceFromCause(DamageCause cause) { + return DamageSource.builder(switch (cause) { + case KILL, SUICIDE -> DamageType.GENERIC_KILL; + case WORLD_BORDER, VOID -> DamageType.OUT_OF_WORLD; + case CONTACT -> DamageType.CACTUS; + case SUFFOCATION -> DamageType.IN_WALL; + case FALL -> DamageType.FALL; + case FIRE -> DamageType.ON_FIRE; + case FIRE_TICK -> DamageType.IN_FIRE; + case LAVA -> DamageType.LAVA; + case DROWNING -> DamageType.DROWN; + case BLOCK_EXPLOSION, ENTITY_EXPLOSION -> DamageType.EXPLOSION; + case LIGHTNING -> DamageType.LIGHTNING_BOLT; + case STARVATION -> DamageType.STARVE; + case MAGIC, POISON -> DamageType.MAGIC; + case WITHER -> DamageType.WITHER; + case FALLING_BLOCK -> DamageType.FALLING_BLOCK; + case THORNS -> DamageType.THORNS; + case DRAGON_BREATH -> DamageType.DRAGON_BREATH; + case FLY_INTO_WALL -> DamageType.FLY_INTO_WALL; + case HOT_FLOOR -> DamageType.HOT_FLOOR; + case CAMPFIRE -> DamageType.CAMPFIRE; + case CRAMMING -> DamageType.CRAMMING; + case DRYOUT -> DamageType.DRY_OUT; + case FREEZE -> DamageType.FREEZE; + case SONIC_BOOM -> DamageType.SONIC_BOOM; + default -> DamageType.GENERIC; + }).build(); + } +} diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index f5ebb19830e..5372b1f50a6 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -29,7 +29,6 @@ import org.bukkit.entity.LivingEntity; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.reflect.Constructor; @@ -142,35 +141,4 @@ public static void setDamageCause(Damageable e, DamageCause cause) { e.setLastDamageCause(new EntityDamageEvent(e, cause, DamageSource.builder(DamageType.GENERIC).build(), 0)); } } - - @NotNull - public static DamageSource getDamageSourceFromCause(DamageCause cause) { - return DamageSource.builder(switch (cause) { - case KILL, SUICIDE -> DamageType.GENERIC_KILL; - case WORLD_BORDER, VOID -> DamageType.OUT_OF_WORLD; - case CONTACT -> DamageType.CACTUS; - case SUFFOCATION -> DamageType.IN_WALL; - case FALL -> DamageType.FALL; - case FIRE -> DamageType.ON_FIRE; - case FIRE_TICK -> DamageType.IN_FIRE; - case LAVA -> DamageType.LAVA; - case DROWNING -> DamageType.DROWN; - case BLOCK_EXPLOSION, ENTITY_EXPLOSION -> DamageType.EXPLOSION; - case LIGHTNING -> DamageType.LIGHTNING_BOLT; - case STARVATION -> DamageType.STARVE; - case MAGIC, POISON -> DamageType.MAGIC; - case WITHER -> DamageType.WITHER; - case FALLING_BLOCK -> DamageType.FALLING_BLOCK; - case THORNS -> DamageType.THORNS; - case DRAGON_BREATH -> DamageType.DRAGON_BREATH; - case FLY_INTO_WALL -> DamageType.FLY_INTO_WALL; - case HOT_FLOOR -> DamageType.HOT_FLOOR; - case CAMPFIRE -> DamageType.CAMPFIRE; - case CRAMMING -> DamageType.CRAMMING; - case DRYOUT -> DamageType.DRY_OUT; - case FREEZE -> DamageType.FREEZE; - case SONIC_BOOM -> DamageType.SONIC_BOOM; - default -> DamageType.GENERIC; - }).build(); - } } diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 07a04c4abef..cc03da723b4 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -20,6 +20,7 @@ import ch.njol.skript.Skript; import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.bukkitutil.DamageUtils; import ch.njol.skript.bukkitutil.HealthUtils; import ch.njol.skript.bukkitutil.ItemUtils; import ch.njol.skript.doc.Description; @@ -120,7 +121,7 @@ protected void execute(Event event) { if (canSetDamageCause && exprCause != null) { DamageCause cause = exprCause.getSingle(event); if (cause != null) { - damageable.damage(amount * 2, HealthUtils.getDamageSourceFromCause(cause)); + damageable.damage(amount * 2, DamageUtils.getDamageSourceFromCause(cause)); return; } } From ce426bd8b61d3640b392284dfb28dbd9958653bc Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 11:41:42 +0800 Subject: [PATCH 09/25] style: remove licence --- .../ch/njol/skript/bukkitutil/DamageUtils.java | 3 +-- .../ch/njol/skript/bukkitutil/HealthUtils.java | 18 ------------------ .../java/ch/njol/skript/effects/EffHealth.java | 18 ------------------ 3 files changed, 1 insertion(+), 38 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java index f348226490c..f0d3e10a95f 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java @@ -6,9 +6,8 @@ import org.jetbrains.annotations.NotNull; public class DamageUtils { - @NotNull @SuppressWarnings("all") - public static DamageSource getDamageSourceFromCause(DamageCause cause) { + public static @NotNull DamageSource getDamageSourceFromCause(DamageCause cause) { return DamageSource.builder(switch (cause) { case KILL, SUICIDE -> DamageType.GENERIC_KILL; case WORLD_BORDER, VOID -> DamageType.OUT_OF_WORLD; diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index 5372b1f50a6..5e9ca0aa386 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -1,21 +1,3 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.bukkitutil; import ch.njol.skript.Skript; diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index cc03da723b4..b6026b8944f 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -1,21 +1,3 @@ -/** - * This file is part of Skript. - *

- * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - *

- * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - *

- * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - *

- * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.effects; import ch.njol.skript.Skript; From 42b5f7ee3d9d8a5a0a78c49ca5f311d55218f44f Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 11:52:20 +0800 Subject: [PATCH 10/25] perf: depart to HealthUtils --- .../njol/skript/bukkitutil/HealthUtils.java | 61 ++++++++++++------- .../ch/njol/skript/effects/EffHealth.java | 4 +- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index 5e9ca0aa386..98896110fdd 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -2,6 +2,8 @@ import ch.njol.skript.Skript; import ch.njol.util.Math2; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import org.bukkit.attribute.Attributable; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; @@ -13,13 +15,22 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.jetbrains.annotations.Nullable; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; - public class HealthUtils { + @Nullable + private static final Constructor OLD_DAMAGE_EVENT_CONSTRUCTOR; + + static { + Constructor constructor = null; + try { + constructor = EntityDamageEvent.class.getConstructor(Damageable.class, DamageCause.class, double.class); + } catch (NoSuchMethodException ignored) { + } + OLD_DAMAGE_EVENT_CONSTRUCTOR = constructor; + } /** * Get the health of an entity + * * @param e Entity to get health from * @return The amount of hearts the entity has left */ @@ -28,18 +39,20 @@ public static double getHealth(Damageable e) { return 0; return e.getHealth() / 2; } - + /** * Set the health of an entity - * @param e Entity to set health for + * + * @param e Entity to set health for * @param health The amount of hearts to set */ public static void setHealth(Damageable e, double health) { e.setHealth(Math2.fit(0, health, getMaxHealth(e)) * 2); } - + /** * Get the max health an entity has + * * @param e Entity to get max health from * @return How many hearts the entity can have at most */ @@ -48,10 +61,11 @@ public static double getMaxHealth(Damageable e) { assert attributeInstance != null; return attributeInstance.getValue() / 2; } - + /** * Set the max health an entity can have - * @param e Entity to set max health for + * + * @param e Entity to set max health for * @param health How many hearts the entity can have at most */ public static void setMaxHealth(Damageable e, double health) { @@ -59,9 +73,10 @@ public static void setMaxHealth(Damageable e, double health) { assert attributeInstance != null; attributeInstance.setBaseValue(health * 2); } - + /** * Apply damage to an entity + * * @param e Entity to apply damage to * @param d Amount of hearts to damage */ @@ -73,8 +88,17 @@ public static void damage(Damageable e, double d) { e.damage(d * 2); } + public static void damage(Damageable e, double d, DamageSource cause) { + if (d < 0) { + heal(e, -d); + return; + } + e.damage(d * 2, cause); + } + /** * Heal an entity + * * @param e Entity to heal * @param h Amount of hearts to heal */ @@ -85,15 +109,17 @@ public static void heal(Damageable e, double h) { } setHealth(e, getHealth(e) + h); } - + public static double getDamage(EntityDamageEvent e) { return e.getDamage() / 2; + // } - + public static double getFinalDamage(EntityDamageEvent e) { return e.getFinalDamage() / 2; + // } - + public static void setDamage(EntityDamageEvent event, double damage) { event.setDamage(damage * 2); // Set last damage manually as Bukkit doesn't appear to do that @@ -101,17 +127,6 @@ public static void setDamage(EntityDamageEvent event, double damage) { ((LivingEntity) event.getEntity()).setLastDamage(damage * 2); } - @Nullable - private static final Constructor OLD_DAMAGE_EVENT_CONSTRUCTOR; - - static { - Constructor constructor = null; - try { - constructor = EntityDamageEvent.class.getConstructor(Damageable.class, DamageCause.class, double.class); - } catch (NoSuchMethodException ignored) { } - OLD_DAMAGE_EVENT_CONSTRUCTOR = constructor; - } - public static void setDamageCause(Damageable e, DamageCause cause) { if (OLD_DAMAGE_EVENT_CONSTRUCTOR != null) { try { diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index b6026b8944f..07007ac37fa 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -103,7 +103,7 @@ protected void execute(Event event) { if (canSetDamageCause && exprCause != null) { DamageCause cause = exprCause.getSingle(event); if (cause != null) { - damageable.damage(amount * 2, DamageUtils.getDamageSourceFromCause(cause)); + HealthUtils.damage(damageable, amount, DamageUtils.getDamageSourceFromCause(cause)); return; } } @@ -124,7 +124,7 @@ public String toString(@Nullable Event event, boolean debug) { } return prefix + damageables.toString(event, debug) + (amount != null ? " by " + amount.toString(event, debug) : "") - + (exprCause != null && event != null ? "with damage cause " + exprCause.getSingle(event) : ""); + + (exprCause != null && event != null ? " with damage cause " + exprCause.getSingle(event) : ""); } } From 8e1305aaba7e043b91f300fb49684c78be79dc4a Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 11:53:36 +0800 Subject: [PATCH 11/25] style: description --- src/main/java/ch/njol/skript/effects/EffHealth.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 07007ac37fa..a9fb3a0ebb2 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -23,7 +23,7 @@ import org.jetbrains.annotations.UnknownNullability; @Name("Damage/Heal/Repair") -@Description("Damage/Heal/Repair an entity, or item.") +@Description("Damage, heal, or repair an entity or item.") @Examples({ "damage player by 5 hearts", "damage player by 3 hearts with fake cause fall", From 30ba08832e21544e13a02aa762a289c7cbf8ffdc Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 11:55:11 +0800 Subject: [PATCH 12/25] style: to JetBrains annotation --- src/main/java/ch/njol/skript/effects/EffHealth.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index a9fb3a0ebb2..eab90446282 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -19,7 +19,7 @@ import org.bukkit.event.Event; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.inventory.ItemStack; -import org.eclipse.jdt.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnknownNullability; @Name("Damage/Heal/Repair") From 7d9f9ea27a5d06d8df2a3b82694175e4038a07da Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 12:51:22 +0800 Subject: [PATCH 13/25] perf: resolved PR mentioned --- src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java | 7 +++---- src/main/java/ch/njol/skript/effects/EffHealth.java | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index 98896110fdd..28dcf953bdb 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -16,15 +16,13 @@ import org.jetbrains.annotations.Nullable; public class HealthUtils { - @Nullable - private static final Constructor OLD_DAMAGE_EVENT_CONSTRUCTOR; + private static final @Nullable Constructor OLD_DAMAGE_EVENT_CONSTRUCTOR; static { Constructor constructor = null; try { constructor = EntityDamageEvent.class.getConstructor(Damageable.class, DamageCause.class, double.class); - } catch (NoSuchMethodException ignored) { - } + } catch (NoSuchMethodException ignored) {} OLD_DAMAGE_EVENT_CONSTRUCTOR = constructor; } @@ -48,6 +46,7 @@ public static double getHealth(Damageable e) { */ public static void setHealth(Damageable e, double health) { e.setHealth(Math2.fit(0, health, getMaxHealth(e)) * 2); + // } /** diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index eab90446282..0d2759b24b2 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -8,6 +8,7 @@ import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.RequiredPlugins; import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Effect; import ch.njol.skript.lang.Expression; @@ -31,6 +32,7 @@ "repair tool of player" }) @Since("1.0") +@RequiredPlugins("Spigot 1.20.4+ (for damage cause)") public class EffHealth extends Effect { static { Skript.registerEffect(EffHealth.class, @@ -45,8 +47,8 @@ public class EffHealth extends Effect { private boolean isHealing, isRepairing; private @UnknownNullability Expression exprCause = null; - @SuppressWarnings("unchecked") @Override + @SuppressWarnings("unchecked") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { if (matchedPattern == 0 && exprs[2] != null && !canSetDamageCause) Skript.warning("Using the fake cause extension in effect 'damage' requires Spigot 1.20.4+"); From 4777452bf647dfb5c5ccd71320b13db1e05d8298 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 12:59:07 +0800 Subject: [PATCH 14/25] style: Suppress exactly; Add description --- src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java | 2 +- src/main/java/ch/njol/skript/effects/EffHealth.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java index f0d3e10a95f..5751f312f48 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java @@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull; public class DamageUtils { - @SuppressWarnings("all") + @SuppressWarnings("UnstableApiUsage") public static @NotNull DamageSource getDamageSourceFromCause(DamageCause cause) { return DamageSource.builder(switch (cause) { case KILL, SUICIDE -> DamageType.GENERIC_KILL; diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 0d2759b24b2..afaf74c680e 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -24,7 +24,10 @@ import org.jetbrains.annotations.UnknownNullability; @Name("Damage/Heal/Repair") -@Description("Damage, heal, or repair an entity or item.") +@Description({ + "Damage, heal, or repair an entity or item.", + "We can use damage entity with a fake damage cause in Spigot 1.20.4+" +}) @Examples({ "damage player by 5 hearts", "damage player by 3 hearts with fake cause fall", From a08c132b3da27c2971e1b6dc2323d6bb2dbd3636 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 13:00:54 +0800 Subject: [PATCH 15/25] perf: Deny using damage cause in old version --- src/main/java/ch/njol/skript/effects/EffHealth.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index afaf74c680e..4c0b0144ecf 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -53,8 +53,10 @@ public class EffHealth extends Effect { @Override @SuppressWarnings("unchecked") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (matchedPattern == 0 && exprs[2] != null && !canSetDamageCause) - Skript.warning("Using the fake cause extension in effect 'damage' requires Spigot 1.20.4+"); + if (matchedPattern == 0 && exprs[2] != null && !canSetDamageCause) { + Skript.error("Using the fake cause extension in effect 'damage' requires Spigot 1.20.4+"); + return false; + } this.damageables = exprs[0]; this.isHealing = matchedPattern >= 1; From 6b2e1737eeaf7bf4ed5ae8f511ed035f740e4664 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 10 Sep 2024 20:09:10 +0800 Subject: [PATCH 16/25] perf: resolved PR mentioned --- .../njol/skript/bukkitutil/DamageUtils.java | 2 + .../njol/skript/bukkitutil/HealthUtils.java | 75 +++++++++---------- .../ch/njol/skript/effects/EffHealth.java | 8 +- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java index 5751f312f48..02f74bef6bb 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/DamageUtils.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull; public class DamageUtils { + @SuppressWarnings("UnstableApiUsage") public static @NotNull DamageSource getDamageSourceFromCause(DamageCause cause) { return DamageSource.builder(switch (cause) { @@ -36,4 +37,5 @@ public class DamageUtils { default -> DamageType.GENERIC; }).build(); } + } diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index 28dcf953bdb..3ddc0182d25 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -29,34 +29,33 @@ public class HealthUtils { /** * Get the health of an entity * - * @param e Entity to get health from + * @param entity Entity to get health from * @return The amount of hearts the entity has left */ - public static double getHealth(Damageable e) { - if (e.isDead()) + public static double getHealth(Damageable entity) { + if (entity.isDead()) return 0; - return e.getHealth() / 2; + return entity.getHealth() / 2; } /** * Set the health of an entity * - * @param e Entity to set health for + * @param entity Entity to set health for * @param health The amount of hearts to set */ - public static void setHealth(Damageable e, double health) { - e.setHealth(Math2.fit(0, health, getMaxHealth(e)) * 2); - // + public static void setHealth(Damageable entity, double health) { + entity.setHealth(Math2.fit(0, health, getMaxHealth(entity)) * 2); } /** * Get the max health an entity has * - * @param e Entity to get max health from + * @param entity Entity to get max health from * @return How many hearts the entity can have at most */ - public static double getMaxHealth(Damageable e) { - AttributeInstance attributeInstance = ((Attributable) e).getAttribute(Attribute.GENERIC_MAX_HEALTH); + public static double getMaxHealth(Damageable entity) { + AttributeInstance attributeInstance = ((Attributable) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH); assert attributeInstance != null; return attributeInstance.getValue() / 2; } @@ -64,11 +63,11 @@ public static double getMaxHealth(Damageable e) { /** * Set the max health an entity can have * - * @param e Entity to set max health for + * @param entity Entity to set max health for * @param health How many hearts the entity can have at most */ - public static void setMaxHealth(Damageable e, double health) { - AttributeInstance attributeInstance = ((Attributable) e).getAttribute(Attribute.GENERIC_MAX_HEALTH); + public static void setMaxHealth(Damageable entity, double health) { + AttributeInstance attributeInstance = ((Attributable) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH); assert attributeInstance != null; attributeInstance.setBaseValue(health * 2); } @@ -76,47 +75,45 @@ public static void setMaxHealth(Damageable e, double health) { /** * Apply damage to an entity * - * @param e Entity to apply damage to - * @param d Amount of hearts to damage + * @param entity Entity to apply damage to + * @param damage Amount of hearts to damage */ - public static void damage(Damageable e, double d) { - if (d < 0) { - heal(e, -d); + public static void damage(Damageable entity, double damage) { + if (damage < 0) { + heal(entity, -damage); return; } - e.damage(d * 2); + entity.damage(damage * 2); } - public static void damage(Damageable e, double d, DamageSource cause) { - if (d < 0) { - heal(e, -d); + public static void damage(Damageable entity, double damage, DamageSource cause) { + if (damage < 0) { + heal(entity, -damage); return; } - e.damage(d * 2, cause); + entity.damage(damage * 2, cause); } /** * Heal an entity * - * @param e Entity to heal - * @param h Amount of hearts to heal + * @param entity Entity to heal + * @param health Amount of hearts to heal */ - public static void heal(Damageable e, double h) { - if (h < 0) { - damage(e, -h); + public static void heal(Damageable entity, double health) { + if (health < 0) { + damage(entity, -health); return; } - setHealth(e, getHealth(e) + h); + setHealth(entity, getHealth(entity) + health); } - public static double getDamage(EntityDamageEvent e) { - return e.getDamage() / 2; - // + public static double getDamage(EntityDamageEvent event) { + return event.getDamage() / 2; } - public static double getFinalDamage(EntityDamageEvent e) { - return e.getFinalDamage() / 2; - // + public static double getFinalDamage(EntityDamageEvent event) { + return event.getFinalDamage() / 2; } public static void setDamage(EntityDamageEvent event, double damage) { @@ -126,15 +123,15 @@ public static void setDamage(EntityDamageEvent event, double damage) { ((LivingEntity) event.getEntity()).setLastDamage(damage * 2); } - public static void setDamageCause(Damageable e, DamageCause cause) { + public static void setDamageCause(Damageable entity, DamageCause cause) { if (OLD_DAMAGE_EVENT_CONSTRUCTOR != null) { try { - e.setLastDamageCause(OLD_DAMAGE_EVENT_CONSTRUCTOR.newInstance(e, cause, 0)); + entity.setLastDamageCause(OLD_DAMAGE_EVENT_CONSTRUCTOR.newInstance(entity, cause, 0)); } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) { Skript.exception("Failed to set last damage cause"); } } else { - e.setLastDamageCause(new EntityDamageEvent(e, cause, DamageSource.builder(DamageType.GENERIC).build(), 0)); + entity.setLastDamageCause(new EntityDamageEvent(entity, cause, DamageSource.builder(DamageType.GENERIC).build(), 0)); } } } diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 4c0b0144ecf..7a22662b896 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -26,7 +26,7 @@ @Name("Damage/Heal/Repair") @Description({ "Damage, heal, or repair an entity or item.", - "We can use damage entity with a fake damage cause in Spigot 1.20.4+" + "Servers running Spigot 1.20.4+ can optionally choose to specify a fake damage cause." }) @Examples({ "damage player by 5 hearts", @@ -44,7 +44,7 @@ public class EffHealth extends Effect { "repair %itemtypes/slots% [by %-number%]"); } - private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); + private static final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); private Expression damageables; private @UnknownNullability Expression amount; private boolean isHealing, isRepairing; @@ -107,8 +107,8 @@ protected void execute(Event event) { } else if (isHealing) { HealthUtils.heal(damageable, amount); } else { - if (canSetDamageCause && exprCause != null) { - DamageCause cause = exprCause.getSingle(event); + if (canSetDamageCause) { + DamageCause cause = exprCause == null ? null : exprCause.getSingle(event); if (cause != null) { HealthUtils.damage(damageable, amount, DamageUtils.getDamageSourceFromCause(cause)); return; From dce2d898d769d2916b217ed7de3162111fbf4aa2 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Wed, 11 Sep 2024 00:01:20 +0800 Subject: [PATCH 17/25] perf: resolved PR mentioned --- .../njol/skript/bukkitutil/HealthUtils.java | 54 +++++++++---------- .../ch/njol/skript/effects/EffHealth.java | 8 +-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index 3ddc0182d25..2f0e0dd3afd 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -29,33 +29,33 @@ public class HealthUtils { /** * Get the health of an entity * - * @param entity Entity to get health from + * @param damageable Entity to get health from * @return The amount of hearts the entity has left */ - public static double getHealth(Damageable entity) { - if (entity.isDead()) + public static double getHealth(Damageable damageable) { + if (damageable.isDead()) return 0; - return entity.getHealth() / 2; + return damageable.getHealth() / 2; } /** * Set the health of an entity * - * @param entity Entity to set health for + * @param damageable Entity to set health for * @param health The amount of hearts to set */ - public static void setHealth(Damageable entity, double health) { - entity.setHealth(Math2.fit(0, health, getMaxHealth(entity)) * 2); + public static void setHealth(Damageable damageable, double health) { + damageable.setHealth(Math2.fit(0, health, getMaxHealth(damageable)) * 2); } /** * Get the max health an entity has * - * @param entity Entity to get max health from + * @param damageable Entity to get max health from * @return How many hearts the entity can have at most */ - public static double getMaxHealth(Damageable entity) { - AttributeInstance attributeInstance = ((Attributable) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH); + public static double getMaxHealth(Damageable damageable) { + AttributeInstance attributeInstance = ((Attributable) damageable).getAttribute(Attribute.GENERIC_MAX_HEALTH); assert attributeInstance != null; return attributeInstance.getValue() / 2; } @@ -63,11 +63,11 @@ public static double getMaxHealth(Damageable entity) { /** * Set the max health an entity can have * - * @param entity Entity to set max health for + * @param damageable Entity to set max health for * @param health How many hearts the entity can have at most */ - public static void setMaxHealth(Damageable entity, double health) { - AttributeInstance attributeInstance = ((Attributable) entity).getAttribute(Attribute.GENERIC_MAX_HEALTH); + public static void setMaxHealth(Damageable damageable, double health) { + AttributeInstance attributeInstance = ((Attributable) damageable).getAttribute(Attribute.GENERIC_MAX_HEALTH); assert attributeInstance != null; attributeInstance.setBaseValue(health * 2); } @@ -75,37 +75,37 @@ public static void setMaxHealth(Damageable entity, double health) { /** * Apply damage to an entity * - * @param entity Entity to apply damage to + * @param damageable Entity to apply damage to * @param damage Amount of hearts to damage */ - public static void damage(Damageable entity, double damage) { + public static void damage(Damageable damageable, double damage) { if (damage < 0) { - heal(entity, -damage); + heal(damageable, -damage); return; } - entity.damage(damage * 2); + damageable.damage(damage * 2); } - public static void damage(Damageable entity, double damage, DamageSource cause) { + public static void damage(Damageable damageable, double damage, DamageSource cause) { if (damage < 0) { - heal(entity, -damage); + heal(damageable, -damage); return; } - entity.damage(damage * 2, cause); + damageable.damage(damage * 2, cause); } /** * Heal an entity * - * @param entity Entity to heal + * @param damageable Entity to heal * @param health Amount of hearts to heal */ - public static void heal(Damageable entity, double health) { + public static void heal(Damageable damageable, double health) { if (health < 0) { - damage(entity, -health); + damage(damageable, -health); return; } - setHealth(entity, getHealth(entity) + health); + setHealth(damageable, getHealth(damageable) + health); } public static double getDamage(EntityDamageEvent event) { @@ -123,15 +123,15 @@ public static void setDamage(EntityDamageEvent event, double damage) { ((LivingEntity) event.getEntity()).setLastDamage(damage * 2); } - public static void setDamageCause(Damageable entity, DamageCause cause) { + public static void setDamageCause(Damageable damageable, DamageCause cause) { if (OLD_DAMAGE_EVENT_CONSTRUCTOR != null) { try { - entity.setLastDamageCause(OLD_DAMAGE_EVENT_CONSTRUCTOR.newInstance(entity, cause, 0)); + damageable.setLastDamageCause(OLD_DAMAGE_EVENT_CONSTRUCTOR.newInstance(damageable, cause, 0)); } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) { Skript.exception("Failed to set last damage cause"); } } else { - entity.setLastDamageCause(new EntityDamageEvent(entity, cause, DamageSource.builder(DamageType.GENERIC).build(), 0)); + damageable.setLastDamageCause(new EntityDamageEvent(damageable, cause, DamageSource.builder(DamageType.GENERIC).build(), 0)); } } } diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 7a22662b896..96e664035e1 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -34,7 +34,7 @@ "heal the player", "repair tool of player" }) -@Since("1.0") +@Since("1.0, 2.10 (damage cause)") @RequiredPlugins("Spigot 1.20.4+ (for damage cause)") public class EffHealth extends Effect { static { @@ -44,7 +44,7 @@ public class EffHealth extends Effect { "repair %itemtypes/slots% [by %-number%]"); } - private static final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource"); + private static final boolean SUPPORTS_DAMAGE_SOURCE = Skript.classExists("org.bukkit.damage.DamageSource"); private Expression damageables; private @UnknownNullability Expression amount; private boolean isHealing, isRepairing; @@ -53,7 +53,7 @@ public class EffHealth extends Effect { @Override @SuppressWarnings("unchecked") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (matchedPattern == 0 && exprs[2] != null && !canSetDamageCause) { + if (matchedPattern == 0 && exprs[2] != null && !SUPPORTS_DAMAGE_SOURCE) { Skript.error("Using the fake cause extension in effect 'damage' requires Spigot 1.20.4+"); return false; } @@ -107,7 +107,7 @@ protected void execute(Event event) { } else if (isHealing) { HealthUtils.heal(damageable, amount); } else { - if (canSetDamageCause) { + if (SUPPORTS_DAMAGE_SOURCE) { DamageCause cause = exprCause == null ? null : exprCause.getSingle(event); if (cause != null) { HealthUtils.damage(damageable, amount, DamageUtils.getDamageSourceFromCause(cause)); From 013d1fbb93e772aadd366c8d5a1aa4c68c0f6517 Mon Sep 17 00:00:00 2001 From: XPYEX <50171612+0XPYEX0@users.noreply.github.com> Date: Wed, 11 Sep 2024 00:46:04 +0800 Subject: [PATCH 18/25] =?UTF-8?q?perf:=20=E6=9B=B4=E6=96=B0=20EffHealth.ja?= =?UTF-8?q?va?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ch/njol/skript/effects/EffHealth.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 96e664035e1..81e7b8891c3 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -34,7 +34,7 @@ "heal the player", "repair tool of player" }) -@Since("1.0, 2.10 (damage cause)") +@Since("1.0, INSERT VERSION (damage cause)") @RequiredPlugins("Spigot 1.20.4+ (for damage cause)") public class EffHealth extends Effect { static { From 71d4f987630cb62513903307a935c9c69ac585b3 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Wed, 11 Sep 2024 10:18:46 +0800 Subject: [PATCH 19/25] style: sort code --- src/main/java/ch/njol/skript/effects/EffHealth.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffHealth.java b/src/main/java/ch/njol/skript/effects/EffHealth.java index 96e664035e1..28d21dd87b9 100644 --- a/src/main/java/ch/njol/skript/effects/EffHealth.java +++ b/src/main/java/ch/njol/skript/effects/EffHealth.java @@ -37,6 +37,8 @@ @Since("1.0, 2.10 (damage cause)") @RequiredPlugins("Spigot 1.20.4+ (for damage cause)") public class EffHealth extends Effect { + private static final boolean SUPPORTS_DAMAGE_SOURCE = Skript.classExists("org.bukkit.damage.DamageSource"); + static { Skript.registerEffect(EffHealth.class, "damage %livingentities/itemtypes/slots% by %number% [heart[s]] [with [fake] [damage] cause %-damagecause%]", @@ -44,7 +46,6 @@ public class EffHealth extends Effect { "repair %itemtypes/slots% [by %-number%]"); } - private static final boolean SUPPORTS_DAMAGE_SOURCE = Skript.classExists("org.bukkit.damage.DamageSource"); private Expression damageables; private @UnknownNullability Expression amount; private boolean isHealing, isRepairing; From 951a6bd73d58549795119117e021902cc67892eb Mon Sep 17 00:00:00 2001 From: XPYEX Date: Wed, 11 Sep 2024 17:42:47 +0800 Subject: [PATCH 20/25] feat: test codes --- src/test/skript/tests/syntaxes/effects/EffHealth.sk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/skript/tests/syntaxes/effects/EffHealth.sk b/src/test/skript/tests/syntaxes/effects/EffHealth.sk index b77c566076b..223cd02ef4c 100644 --- a/src/test/skript/tests/syntaxes/effects/EffHealth.sk +++ b/src/test/skript/tests/syntaxes/effects/EffHealth.sk @@ -20,4 +20,7 @@ test "health effect": assert health of {_m} is 1.5 with "heal cow failed" heal {_m} by 99 assert health of {_m} is 5 with "heal cow failed" + wait 1 tick + damage {_m} by 2 hearts with damage cause fall + assert health of {_m} is 3 with "damage cow failed" clear all entities From 95579f4dd89a77c5e05e5dbb1e75f62fb63e5ca6 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Sat, 21 Sep 2024 18:28:36 +0800 Subject: [PATCH 21/25] perf: test codes --- src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java | 1 + src/test/skript/tests/syntaxes/effects/EffHealth.sk | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index 2f0e0dd3afd..49d6264aa24 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -86,6 +86,7 @@ public static void damage(Damageable damageable, double damage) { damageable.damage(damage * 2); } + @SuppressWarnings("UnstableApiUsage") public static void damage(Damageable damageable, double damage, DamageSource cause) { if (damage < 0) { heal(damageable, -damage); diff --git a/src/test/skript/tests/syntaxes/effects/EffHealth.sk b/src/test/skript/tests/syntaxes/effects/EffHealth.sk index 223cd02ef4c..fee14980b8e 100644 --- a/src/test/skript/tests/syntaxes/effects/EffHealth.sk +++ b/src/test/skript/tests/syntaxes/effects/EffHealth.sk @@ -20,7 +20,8 @@ test "health effect": assert health of {_m} is 1.5 with "heal cow failed" heal {_m} by 99 assert health of {_m} is 5 with "heal cow failed" - wait 1 tick + damage {_m} by 2 hearts with damage cause fall + wait 1 tick assert health of {_m} is 3 with "damage cow failed" clear all entities From 7579963b938bfdad2e31b5b047f74b02a4be45c1 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Sat, 21 Sep 2024 19:19:41 +0800 Subject: [PATCH 22/25] perf: check version --- .../tests/syntaxes/effects/EffHealth.sk | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/test/skript/tests/syntaxes/effects/EffHealth.sk b/src/test/skript/tests/syntaxes/effects/EffHealth.sk index fee14980b8e..54057157304 100644 --- a/src/test/skript/tests/syntaxes/effects/EffHealth.sk +++ b/src/test/skript/tests/syntaxes/effects/EffHealth.sk @@ -1,27 +1,31 @@ test "health effect": - clear all entities - set {_i} to diamond sword - assert {_i}'s damage value is 0 with "new item durability failed" - damage {_i} by 50 - assert {_i}'s damage value is 50 with "damage item failed" - repair {_i} by 49 - assert {_i}'s damage value is 1 with "repair item failed" + clear all entities + set {_i} to diamond sword + assert {_i}'s damage value is 0 with "new item durability failed" + damage {_i} by 50 + assert {_i}'s damage value is 50 with "damage item failed" + repair {_i} by 49 + assert {_i}'s damage value is 1 with "repair item failed" - spawn cow at location(0, 64, 0, world "world") - set {_m} to last spawned cow - assert health of {_m} is 5 with "default cow health failed" - damage {_m} by 0.5 - assert health of {_m} is 4.5 with "damage cow failed" - damage {_m} by 99 - assert health of {_m} is 0 with "damage cow failed" - heal {_m} by 1 - assert health of {_m} is 1 with "heal cow failed" - heal {_m} by 0.5 - assert health of {_m} is 1.5 with "heal cow failed" - heal {_m} by 99 - assert health of {_m} is 5 with "heal cow failed" + spawn cow at location(0, 64, 0, world "world") + set {_m} to last spawned cow + assert health of {_m} is 5 with "default cow health failed" + damage {_m} by 0.5 + assert health of {_m} is 4.5 with "damage cow failed" + damage {_m} by 99 + assert health of {_m} is 0 with "damage cow failed" + heal {_m} by 1 + assert health of {_m} is 1 with "heal cow failed" + heal {_m} by 0.5 + assert health of {_m} is 1.5 with "heal cow failed" + heal {_m} by 99 + assert health of {_m} is 5 with "heal cow failed" - damage {_m} by 2 hearts with damage cause fall - wait 1 tick - assert health of {_m} is 3 with "damage cow failed" - clear all entities + set {_v::*} to "%minecraft version%" split by "." + set {_result} to "%{_v::2}%%{_v::3} ? 0%" parsed as number + parse if {_result} >= 204: + send "1.20.4+ and test damage cause" to console + damage {_m} by 2 hearts with damage cause fall + wait 1 tick + assert health of {_m} is 4 with "damage cow failed" + clear all entities From 4a8556ec00a96aeb17dafa870e973c3ffa9c8988 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Sat, 21 Sep 2024 19:59:13 +0800 Subject: [PATCH 23/25] fix: wrong health --- src/test/skript/tests/syntaxes/effects/EffHealth.sk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/skript/tests/syntaxes/effects/EffHealth.sk b/src/test/skript/tests/syntaxes/effects/EffHealth.sk index 54057157304..7242dd9d533 100644 --- a/src/test/skript/tests/syntaxes/effects/EffHealth.sk +++ b/src/test/skript/tests/syntaxes/effects/EffHealth.sk @@ -26,6 +26,6 @@ test "health effect": parse if {_result} >= 204: send "1.20.4+ and test damage cause" to console damage {_m} by 2 hearts with damage cause fall - wait 1 tick - assert health of {_m} is 4 with "damage cow failed" + assert health of {_m} is 3 with "damage cow failed" + send "Check passed" to console clear all entities From c73eae20a364dd531895a7cca1b1c64666e317a6 Mon Sep 17 00:00:00 2001 From: XPYEX Date: Tue, 1 Oct 2024 18:07:29 +0800 Subject: [PATCH 24/25] fix: test codes --- src/test/skript/tests/syntaxes/effects/EffHealth.sk | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test/skript/tests/syntaxes/effects/EffHealth.sk b/src/test/skript/tests/syntaxes/effects/EffHealth.sk index 7242dd9d533..be5dc2a448f 100644 --- a/src/test/skript/tests/syntaxes/effects/EffHealth.sk +++ b/src/test/skript/tests/syntaxes/effects/EffHealth.sk @@ -21,11 +21,7 @@ test "health effect": heal {_m} by 99 assert health of {_m} is 5 with "heal cow failed" - set {_v::*} to "%minecraft version%" split by "." - set {_result} to "%{_v::2}%%{_v::3} ? 0%" parsed as number - parse if {_result} >= 204: - send "1.20.4+ and test damage cause" to console + parse if running minecraft "1.20.4": damage {_m} by 2 hearts with damage cause fall assert health of {_m} is 3 with "damage cow failed" - send "Check passed" to console clear all entities From 05e22e79814d6f67a7ff62da68bc5956fdf498c8 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Sat, 30 Nov 2024 23:38:14 +0100 Subject: [PATCH 25/25] Update src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java --- src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java index 0045d5b7617..a6955604cc1 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java @@ -76,7 +76,7 @@ public static double getMaxHealth(Damageable damageable) { * @param health How many hearts the entity can have at most */ public static void setMaxHealth(Damageable damageable, double health) { - AttributeInstance attributeInstance = ((Attributable) e).getAttribute(MAX_HEALTH); + AttributeInstance attributeInstance = ((Attributable) damageable).getAttribute(MAX_HEALTH); assert attributeInstance != null; attributeInstance.setBaseValue(health * 2); }