Restores player health upon arena exit

This commit is contained in:
Kristian Knarvik 2024-05-16 13:32:58 +02:00
parent d101c7ed02
commit 2ec15c69c7
3 changed files with 24 additions and 10 deletions

View File

@ -36,6 +36,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
private final boolean originalInvulnerable; private final boolean originalInvulnerable;
private final boolean originalIsSwimming; private final boolean originalIsSwimming;
private final boolean originalCollideAble; private final boolean originalCollideAble;
private final double originalHealth;
private final Collection<PotionEffect> originalPotionEffects; private final Collection<PotionEffect> originalPotionEffects;
/** /**
@ -52,6 +53,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
this.originalInvulnerable = player.isInvulnerable(); this.originalInvulnerable = player.isInvulnerable();
this.originalIsSwimming = player.isSwimming(); this.originalIsSwimming = player.isSwimming();
this.originalCollideAble = player.isCollidable(); this.originalCollideAble = player.isCollidable();
this.originalHealth = player.getHealth();
// Store and clear potion effects // Store and clear potion effects
this.originalPotionEffects = getPlayer().getActivePotionEffects(); this.originalPotionEffects = getPlayer().getActivePotionEffects();
@ -72,11 +74,13 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
* @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p> * @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p>
* @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p> * @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param originalPotionEffects <p>The potion effects applied to the player when joining</p> * @param originalPotionEffects <p>The potion effects applied to the player when joining</p>
* @param originalHealth <p>The health of the player when joining the arena</p>
*/ */
public AbstractPlayerEntryState(@NotNull UUID playerId, Location entryLocation, public AbstractPlayerEntryState(@NotNull UUID playerId, @NotNull Location entryLocation,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming, boolean originalInvulnerable, boolean originalIsSwimming,
boolean originalCollideAble, Collection<PotionEffect> originalPotionEffects) { boolean originalCollideAble, @NotNull Collection<PotionEffect> originalPotionEffects,
double originalHealth) {
this.playerId = playerId; this.playerId = playerId;
this.entryLocation = entryLocation; this.entryLocation = entryLocation;
this.originalIsFlying = originalIsFlying; this.originalIsFlying = originalIsFlying;
@ -86,6 +90,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
this.originalIsSwimming = originalIsSwimming; this.originalIsSwimming = originalIsSwimming;
this.originalCollideAble = originalCollideAble; this.originalCollideAble = originalCollideAble;
this.originalPotionEffects = originalPotionEffects; this.originalPotionEffects = originalPotionEffects;
this.originalHealth = originalHealth;
} }
@Override @Override
@ -115,6 +120,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
player.addPotionEffect(potionEffect); player.addPotionEffect(potionEffect);
} }
removeMenuItem(player); removeMenuItem(player);
player.setHealth(originalHealth);
} }
@Override @Override
@ -149,6 +155,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
data.put("originalIsSwimming", this.originalIsSwimming); data.put("originalIsSwimming", this.originalIsSwimming);
data.put("originalCollideAble", this.originalCollideAble); data.put("originalCollideAble", this.originalCollideAble);
data.put("originalPotionEffects", this.originalPotionEffects); data.put("originalPotionEffects", this.originalPotionEffects);
data.put("originalHealth", this.originalHealth);
return data; return data;
} }

View File

@ -49,15 +49,16 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
* @param horizontalVelocity <p>The horizontal velocity of the player before entering the arena</p> * @param horizontalVelocity <p>The horizontal velocity of the player before entering the arena</p>
* @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p> * @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param originalPotionEffects <p>The potion effects applied to the player when joining</p> * @param originalPotionEffects <p>The potion effects applied to the player when joining</p>
* @param originalHealth <p>The health of the player when joining the arena</p>
*/ */
public DropperPlayerEntryState(@NotNull UUID playerId, Location entryLocation, public DropperPlayerEntryState(@NotNull UUID playerId, @NotNull Location entryLocation,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming, boolean originalInvulnerable, boolean originalIsSwimming,
float originalFlySpeed, float horizontalVelocity, float originalFlySpeed, float horizontalVelocity,
DropperArenaGameMode arenaGameMode, boolean originalCollideAble, @NotNull DropperArenaGameMode arenaGameMode, boolean originalCollideAble,
Collection<PotionEffect> originalPotionEffects) { @NotNull Collection<PotionEffect> originalPotionEffects, double originalHealth) {
super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight,
originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects); originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects, originalHealth);
this.originalFlySpeed = originalFlySpeed; this.originalFlySpeed = originalFlySpeed;
this.horizontalVelocity = horizontalVelocity; this.horizontalVelocity = horizontalVelocity;
this.arenaGameMode = arenaGameMode; this.arenaGameMode = arenaGameMode;
@ -128,10 +129,12 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
boolean originalCollideAble = getBoolean(data, "originalCollideAble"); boolean originalCollideAble = getBoolean(data, "originalCollideAble");
Collection<PotionEffect> originalPotionEffect = Collection<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>()); (Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>());
double originalHealth = ((Number) data.get("originalHealth")).doubleValue();
return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying, return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying,
originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming,
originalFlySpeed, horizontalVelocity, arenaGameMode, originalCollideAble, originalPotionEffect); originalFlySpeed, horizontalVelocity, arenaGameMode, originalCollideAble, originalPotionEffect,
originalHealth);
} }
} }

View File

@ -39,13 +39,15 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
* @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p> * @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p>
* @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p> * @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param originalPotionEffects <p>The potion effects applied to the player when joining</p> * @param originalPotionEffects <p>The potion effects applied to the player when joining</p>
* @param originalHealth <p>The health of the player when joining the arena</p>
*/ */
public ParkourPlayerEntryState(@NotNull UUID playerId, Location entryLocation, public ParkourPlayerEntryState(@NotNull UUID playerId, Location entryLocation,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming, boolean originalInvulnerable, boolean originalIsSwimming,
boolean originalCollideAble, Collection<PotionEffect> originalPotionEffects) { boolean originalCollideAble, Collection<PotionEffect> originalPotionEffects,
double originalHealth) {
super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight,
originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects); originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects, originalHealth);
} }
@Override @Override
@ -77,9 +79,11 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
boolean originalCollideAble = getBoolean(data, "originalCollideAble"); boolean originalCollideAble = getBoolean(data, "originalCollideAble");
Collection<PotionEffect> originalPotionEffect = Collection<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>()); (Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>());
double originalHealth = ((Number) data.get("originalHealth")).doubleValue();
return new ParkourPlayerEntryState(playerId, entryLocation, originalIsFlying, originalGameMode, return new ParkourPlayerEntryState(playerId, entryLocation, originalIsFlying, originalGameMode,
originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffect); originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble,
originalPotionEffect, originalHealth);
} }
} }