From 81e652b4ff96f2949208e4a32f6fa0e6d6412681 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 4 Apr 2024 23:29:56 +0200 Subject: [PATCH] Stores potion effects for players in arenas --- .../arena/AbstractPlayerEntryState.java | 33 ++++++++++++----- .../dropper/DropperPlayerEntryState.java | 35 +++++++++++-------- .../parkour/ParkourPlayerEntryState.java | 33 ++++++++++------- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java b/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java index 8c06be1..2ce326f 100644 --- a/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java +++ b/src/main/java/net/knarcraft/minigames/arena/AbstractPlayerEntryState.java @@ -11,8 +11,10 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataType; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -34,6 +36,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState { private final boolean originalInvulnerable; private final boolean originalIsSwimming; private final boolean originalCollideAble; + private final Collection originalPotionEffects; /** * Instantiates a new abstract player entry state @@ -49,24 +52,31 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState { this.originalInvulnerable = player.isInvulnerable(); this.originalIsSwimming = player.isSwimming(); this.originalCollideAble = player.isCollidable(); + + // Store and clear potion effects + this.originalPotionEffects = getPlayer().getActivePotionEffects(); + for (PotionEffect potionEffect : this.originalPotionEffects) { + player.removePotionEffect(potionEffect.getType()); + } } /** * Instantiates a new abstract player entry state * - * @param playerId

The id of the player whose state this should keep track of

- * @param entryLocation

The location the player entered from

- * @param originalIsFlying

Whether the player was flying before entering the arena

- * @param originalGameMode

The game-mode of the player before entering the arena

- * @param originalAllowFlight

Whether the player was allowed flight before entering the arena

- * @param originalInvulnerable

Whether the player was invulnerable before entering the arena

- * @param originalIsSwimming

Whether the player was swimming before entering the arena

- * @param originalCollideAble

Whether the player was collide-able before entering the arena

+ * @param playerId

The id of the player whose state this should keep track of

+ * @param entryLocation

The location the player entered from

+ * @param originalIsFlying

Whether the player was flying before entering the arena

+ * @param originalGameMode

The game-mode of the player before entering the arena

+ * @param originalAllowFlight

Whether the player was allowed flight before entering the arena

+ * @param originalInvulnerable

Whether the player was invulnerable before entering the arena

+ * @param originalIsSwimming

Whether the player was swimming before entering the arena

+ * @param originalCollideAble

Whether the player was collide-able before entering the arena

+ * @param originalPotionEffects

The potion effects applied to the player when joining

*/ public AbstractPlayerEntryState(@NotNull UUID playerId, Location entryLocation, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalInvulnerable, boolean originalIsSwimming, - boolean originalCollideAble) { + boolean originalCollideAble, Collection originalPotionEffects) { this.playerId = playerId; this.entryLocation = entryLocation; this.originalIsFlying = originalIsFlying; @@ -75,6 +85,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState { this.originalInvulnerable = originalInvulnerable; this.originalIsSwimming = originalIsSwimming; this.originalCollideAble = originalCollideAble; + this.originalPotionEffects = originalPotionEffects; } @Override @@ -100,6 +111,9 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState { player.setGameMode(this.originalGameMode); player.setInvulnerable(this.originalInvulnerable); player.setSwimming(this.originalIsSwimming); + for (PotionEffect potionEffect : originalPotionEffects) { + player.addPotionEffect(potionEffect); + } removeMenuItem(player); } @@ -134,6 +148,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState { data.put("originalInvulnerable", this.originalInvulnerable); data.put("originalIsSwimming", this.originalIsSwimming); data.put("originalCollideAble", this.originalCollideAble); + data.put("originalPotionEffects", this.originalPotionEffects); return data; } diff --git a/src/main/java/net/knarcraft/minigames/arena/dropper/DropperPlayerEntryState.java b/src/main/java/net/knarcraft/minigames/arena/dropper/DropperPlayerEntryState.java index 48e764c..7edf216 100644 --- a/src/main/java/net/knarcraft/minigames/arena/dropper/DropperPlayerEntryState.java +++ b/src/main/java/net/knarcraft/minigames/arena/dropper/DropperPlayerEntryState.java @@ -5,8 +5,11 @@ import net.knarcraft.minigames.container.SerializableUUID; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.Collection; import java.util.Map; import java.util.UUID; @@ -35,24 +38,26 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState { /** * Instantiates a new parkour player entry state * - * @param playerId

The id of the player whose state this should keep track of

- * @param entryLocation

The location the player entered from

- * @param originalIsFlying

Whether the player was flying before entering the arena

- * @param originalGameMode

The game-mode of the player before entering the arena

- * @param originalAllowFlight

Whether the player was allowed flight before entering the arena

- * @param originalInvulnerable

Whether the player was invulnerable before entering the arena

- * @param originalIsSwimming

Whether the player was swimming before entering the arena

- * @param originalFlySpeed

The fly-speed of the player before entering the arena

- * @param horizontalVelocity

The horizontal velocity of the player before entering the arena

- * @param originalCollideAble

Whether the player was collide-able before entering the arena

+ * @param playerId

The id of the player whose state this should keep track of

+ * @param entryLocation

The location the player entered from

+ * @param originalIsFlying

Whether the player was flying before entering the arena

+ * @param originalGameMode

The game-mode of the player before entering the arena

+ * @param originalAllowFlight

Whether the player was allowed flight before entering the arena

+ * @param originalInvulnerable

Whether the player was invulnerable before entering the arena

+ * @param originalIsSwimming

Whether the player was swimming before entering the arena

+ * @param originalFlySpeed

The fly-speed of the player before entering the arena

+ * @param horizontalVelocity

The horizontal velocity of the player before entering the arena

+ * @param originalCollideAble

Whether the player was collide-able before entering the arena

+ * @param originalPotionEffects

The potion effects applied to the player when joining

*/ public DropperPlayerEntryState(@NotNull UUID playerId, Location entryLocation, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalInvulnerable, boolean originalIsSwimming, float originalFlySpeed, float horizontalVelocity, - DropperArenaGameMode arenaGameMode, boolean originalCollideAble) { + DropperArenaGameMode arenaGameMode, boolean originalCollideAble, + Collection originalPotionEffects) { super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, - originalInvulnerable, originalIsSwimming, originalCollideAble); + originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects); this.originalFlySpeed = originalFlySpeed; this.horizontalVelocity = horizontalVelocity; this.arenaGameMode = arenaGameMode; @@ -108,7 +113,7 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState { * * @return

The data to deserialize

*/ - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "unchecked"}) public static DropperPlayerEntryState deserialize(Map data) { UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue(); Location entryLocation = (Location) data.get("entryLocation"); @@ -121,10 +126,12 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState { float horizontalVelocity = ((Number) data.get("horizontalVelocity")).floatValue(); DropperArenaGameMode arenaGameMode = (DropperArenaGameMode) data.get("arenaGameMode"); boolean originalCollideAble = getBoolean(data, "originalCollideAble"); + Collection originalPotionEffect = + (Collection) data.getOrDefault("originalPotionEffects", new ArrayList<>()); return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, - originalFlySpeed, horizontalVelocity, arenaGameMode, originalCollideAble); + originalFlySpeed, horizontalVelocity, arenaGameMode, originalCollideAble, originalPotionEffect); } } diff --git a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourPlayerEntryState.java b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourPlayerEntryState.java index fa00dff..c95b8bf 100644 --- a/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourPlayerEntryState.java +++ b/src/main/java/net/knarcraft/minigames/arena/parkour/ParkourPlayerEntryState.java @@ -5,8 +5,11 @@ import net.knarcraft.minigames.container.SerializableUUID; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.Collection; import java.util.Map; import java.util.UUID; @@ -27,20 +30,22 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState { /** * Instantiates a new parkour player entry state * - * @param playerId

The id of the player whose state this should keep track of

- * @param entryLocation

The location the player entered from

- * @param originalIsFlying

Whether the player was flying before entering the arena

- * @param originalGameMode

The game-mode of the player before entering the arena

- * @param originalAllowFlight

Whether the player was allowed flight before entering the arena

- * @param originalInvulnerable

Whether the player was invulnerable before entering the arena

- * @param originalIsSwimming

Whether the player was swimming before entering the arena

- * @param originalCollideAble

Whether the player was collide-able before entering the arena

+ * @param playerId

The id of the player whose state this should keep track of

+ * @param entryLocation

The location the player entered from

+ * @param originalIsFlying

Whether the player was flying before entering the arena

+ * @param originalGameMode

The game-mode of the player before entering the arena

+ * @param originalAllowFlight

Whether the player was allowed flight before entering the arena

+ * @param originalInvulnerable

Whether the player was invulnerable before entering the arena

+ * @param originalIsSwimming

Whether the player was swimming before entering the arena

+ * @param originalCollideAble

Whether the player was collide-able before entering the arena

+ * @param originalPotionEffects

The potion effects applied to the player when joining

*/ public ParkourPlayerEntryState(@NotNull UUID playerId, Location entryLocation, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, - boolean originalInvulnerable, boolean originalIsSwimming, boolean originalCollideAble) { + boolean originalInvulnerable, boolean originalIsSwimming, + boolean originalCollideAble, Collection originalPotionEffects) { super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, - originalInvulnerable, originalIsSwimming, originalCollideAble); + originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects); } @Override @@ -60,7 +65,7 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState { * * @return

The data to deserialize

*/ - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "unchecked"}) public static ParkourPlayerEntryState deserialize(Map data) { UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue(); Location entryLocation = (Location) data.get("entryLocation"); @@ -70,9 +75,11 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState { boolean originalInvulnerable = getBoolean(data, "originalInvulnerable"); boolean originalIsSwimming = getBoolean(data, "originalIsSwimming"); boolean originalCollideAble = getBoolean(data, "originalCollideAble"); + Collection originalPotionEffect = + (Collection) data.getOrDefault("originalPotionEffects", new ArrayList<>()); - return new ParkourPlayerEntryState(playerId, entryLocation, originalIsFlying, - originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble); + return new ParkourPlayerEntryState(playerId, entryLocation, originalIsFlying, originalGameMode, + originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffect); } }