Skip to content

Commit

Permalink
🛠 Fix keep inv/exp and other bugs related to death event (SkriptLang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AyhamAl-Ali committed Sep 14, 2023
1 parent 3f08853 commit 9ab489e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
25 changes: 14 additions & 11 deletions src/main/java/ch/njol/skript/effects/EffKeepInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package ch.njol.skript.effects;

import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -35,17 +36,19 @@

@Name("Keep Inventory / Experience")
@Description("Keeps the inventory or/and experiences of the dead player in a death event.")
@Examples({"on death of a player:",
@Examples({
"on death of a player:",
"\tif the victim is an op:",
"\t\tkeep the inventory and experiences"})
"\t\tkeep the inventory and experiences"
})
@Since("2.4")
@Events("death")
public class EffKeepInventory extends Effect {

static {
Skript.registerEffect(EffKeepInventory.class,
"keep [the] (inventory|items) [(1¦and [e]xp[erience][s] [point[s]])]",
"keep [the] [e]xp[erience][s] [point[s]] [(1¦and (inventory|items))]");
"keep [the] (inventory|items) [(1:and [e]xp[erience][s] [point[s]])]",
"keep [the] [e]xp[erience][s] [point[s]] [(1:and (inventory|items))]");
}

private boolean keepItems, keepExp;
Expand All @@ -54,7 +57,7 @@ public class EffKeepInventory extends Effect {
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
keepItems = matchedPattern == 0 || parseResult.mark == 1;
keepExp = matchedPattern == 1 || parseResult.mark == 1;
if (!getParser().isCurrentEvent(PlayerDeathEvent.class)) {
if (!getParser().isCurrentEvent(EntityDeathEvent.class)) {
Skript.error("The keep inventory/experience effect can't be used outside of a death event");
return false;
}
Expand All @@ -66,18 +69,18 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

@Override
protected void execute(Event e) {
if (e instanceof PlayerDeathEvent) {
PlayerDeathEvent event = (PlayerDeathEvent) e;
protected void execute(Event event) {
if (event instanceof PlayerDeathEvent) {
PlayerDeathEvent deathEvent = (PlayerDeathEvent) event;
if (keepItems)
event.setKeepInventory(true);
deathEvent.setKeepInventory(true);
if (keepExp)
event.setKeepLevel(true);
deathEvent.setKeepLevel(true);
}
}

@Override
public String toString(@Nullable Event e, boolean debug) {
public String toString(@Nullable Event event, boolean debug) {
if (keepItems && !keepExp)
return "keep the inventory";
else
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/effects/EffRespawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -62,7 +62,7 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
players = (Expression<Player>) exprs[0];
// Force a delay before respawning the player if we're in the death event and there isn't already a delay
// Unexpected behavior may occur if we don't do this
forceDelay = getParser().isCurrentEvent(PlayerDeathEvent.class) && isDelayed.isFalse();
forceDelay = getParser().isCurrentEvent(EntityDeathEvent.class) && isDelayed.isFalse();
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ch/njol/skript/expressions/ExprLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerLevelChangeEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
Expand Down Expand Up @@ -80,11 +81,11 @@ public Class<?>[] acceptChange(final ChangeMode mode) {
Skript.error("Cannot change a player's level in a respawn event. Add a delay of 1 tick or change the 'new level' in a death event.");
return null;
}
if (getParser().isCurrentEvent(PlayerDeathEvent.class) && getTime() == 0 && getExpr().isDefault() && !getParser().getHasDelayBefore().isTrue()) {
if (getParser().isCurrentEvent(EntityDeathEvent.class) && getTime() == 0 && getExpr().isDefault() && !getParser().getHasDelayBefore().isTrue()) {
Skript.warning("Changing the player's level in a death event will change the player's level before he dies. " +
"Use either 'past level of player' or 'new level of player' to clearly state whether to change the level before or after he dies.");
}
if (getTime() == -1 && !getParser().isCurrentEvent(PlayerDeathEvent.class))
if (getTime() == -1 && !getParser().isCurrentEvent(EntityDeathEvent.class))
return null;
return new Class[] {Number.class};
}
Expand Down

0 comments on commit 9ab489e

Please sign in to comment.