Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support damage entity with damage-causes in MC 1.20.4+ #7044

Merged
merged 31 commits into from
Dec 24, 2024
Merged
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a514e6b
feat: damage entity with damage-causes
0XPYEX0 Sep 6, 2024
1ff2d38
style: import
0XPYEX0 Sep 6, 2024
5f3d85f
fix: ArrayIndexOutOfBoundsException
0XPYEX0 Sep 8, 2024
5b7e469
perf: resolved pr mentioned
0XPYEX0 Sep 9, 2024
0896764
style: Add example
0XPYEX0 Sep 9, 2024
4bbaeef
style: sort codes
0XPYEX0 Sep 9, 2024
0f4b211
perf: resolved PR mentioned
0XPYEX0 Sep 10, 2024
d525ca7
style: depart to DamageUtils
0XPYEX0 Sep 10, 2024
ce426bd
style: remove licence
0XPYEX0 Sep 10, 2024
42b5f7e
perf: depart to HealthUtils
0XPYEX0 Sep 10, 2024
8e1305a
style: description
0XPYEX0 Sep 10, 2024
30ba088
style: to JetBrains annotation
0XPYEX0 Sep 10, 2024
7d9f9ea
perf: resolved PR mentioned
0XPYEX0 Sep 10, 2024
4777452
style: Suppress exactly; Add description
0XPYEX0 Sep 10, 2024
a08c132
perf: Deny using damage cause in old version
0XPYEX0 Sep 10, 2024
6b2e173
perf: resolved PR mentioned
0XPYEX0 Sep 10, 2024
dce2d89
perf: resolved PR mentioned
0XPYEX0 Sep 10, 2024
013d1fb
perf: 更新 EffHealth.java
0XPYEX0 Sep 10, 2024
71d4f98
style: sort code
0XPYEX0 Sep 11, 2024
1715e8b
Merge remote-tracking branch 'origin/dev/feature' into dev/feature
0XPYEX0 Sep 11, 2024
afa0d36
Merge branch 'dev/feature' into dev/feature
0XPYEX0 Sep 11, 2024
951a6bd
feat: test codes
0XPYEX0 Sep 11, 2024
95579f4
perf: test codes
0XPYEX0 Sep 21, 2024
7579963
perf: check version
0XPYEX0 Sep 21, 2024
4a8556e
fix: wrong health
0XPYEX0 Sep 21, 2024
6b0e862
Merge branch 'SkriptLang:dev/feature' into dev/feature
0XPYEX0 Oct 1, 2024
c73eae2
fix: test codes
0XPYEX0 Oct 1, 2024
69741af
Merge branch 'SkriptLang:dev/feature' into dev/feature
0XPYEX0 Oct 2, 2024
571042b
Merge branch 'dev/feature' into dev/feature
sovdeeth Oct 3, 2024
c1832a5
Merge branch 'dev/feature' into dev/feature
Efnilite Nov 30, 2024
05e22e7
Update src/main/java/ch/njol/skript/bukkitutil/HealthUtils.java
Efnilite Nov 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions src/main/java/ch/njol/skript/effects/EffHealth.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
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;
0XPYEX0 marked this conversation as resolved.
Show resolved Hide resolved
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -58,18 +61,23 @@ public class EffHealth extends Effect {
@Nullable
private Expression<Number> amount;
0XPYEX0 marked this conversation as resolved.
Show resolved Hide resolved
private boolean isHealing, isRepairing;
private final boolean canSetDamageCause = Skript.classExists("org.bukkit.damage.DamageSource");
@Nullable
private Expression<EntityDamageEvent.DamageCause> exprCause = null;
0XPYEX0 marked this conversation as resolved.
Show resolved Hide resolved

@SuppressWarnings("unchecked")
@Override
0XPYEX0 marked this conversation as resolved.
Show resolved Hide resolved
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+");
0XPYEX0 marked this conversation as resolved.
Show resolved Hide resolved

this.damageables = exprs[0];
this.isHealing = matchedPattern >= 1;
this.isRepairing = matchedPattern == 2;
this.amount = (Expression<Number>) exprs[1];
if (exprs.length > 2) {
this.exprCause = (Expression<EntityDamageEvent.DamageCause>) exprs[2];
}
0XPYEX0 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

Expand Down Expand Up @@ -109,14 +117,45 @@ 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) {
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());
0XPYEX0 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
}
HealthUtils.damage(damageable, amount);
}

Expand Down