Stores potion effects for players in arenas

This commit is contained in:
Kristian Knarvik 2024-04-04 23:29:56 +02:00
parent d7e950c53e
commit 81e652b4ff
3 changed files with 65 additions and 36 deletions

View File

@ -11,8 +11,10 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@ -34,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 Collection<PotionEffect> originalPotionEffects;
/** /**
* Instantiates a new abstract player entry state * Instantiates a new abstract player entry state
@ -49,6 +52,12 @@ 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();
// Store and clear potion effects
this.originalPotionEffects = getPlayer().getActivePotionEffects();
for (PotionEffect potionEffect : this.originalPotionEffects) {
player.removePotionEffect(potionEffect.getType());
}
} }
/** /**
@ -62,11 +71,12 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
* @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p> * @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p>
* @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>
*/ */
public AbstractPlayerEntryState(@NotNull UUID playerId, Location entryLocation, public AbstractPlayerEntryState(@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) { boolean originalCollideAble, Collection<PotionEffect> originalPotionEffects) {
this.playerId = playerId; this.playerId = playerId;
this.entryLocation = entryLocation; this.entryLocation = entryLocation;
this.originalIsFlying = originalIsFlying; this.originalIsFlying = originalIsFlying;
@ -75,6 +85,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
this.originalInvulnerable = originalInvulnerable; this.originalInvulnerable = originalInvulnerable;
this.originalIsSwimming = originalIsSwimming; this.originalIsSwimming = originalIsSwimming;
this.originalCollideAble = originalCollideAble; this.originalCollideAble = originalCollideAble;
this.originalPotionEffects = originalPotionEffects;
} }
@Override @Override
@ -100,6 +111,9 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
player.setGameMode(this.originalGameMode); player.setGameMode(this.originalGameMode);
player.setInvulnerable(this.originalInvulnerable); player.setInvulnerable(this.originalInvulnerable);
player.setSwimming(this.originalIsSwimming); player.setSwimming(this.originalIsSwimming);
for (PotionEffect potionEffect : originalPotionEffects) {
player.addPotionEffect(potionEffect);
}
removeMenuItem(player); removeMenuItem(player);
} }
@ -134,6 +148,7 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
data.put("originalInvulnerable", this.originalInvulnerable); data.put("originalInvulnerable", this.originalInvulnerable);
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);
return data; return data;
} }

View File

@ -5,8 +5,11 @@ import net.knarcraft.minigames.container.SerializableUUID;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -45,14 +48,16 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
* @param originalFlySpeed <p>The fly-speed of the player before entering the arena</p> * @param originalFlySpeed <p>The fly-speed of the player before entering the arena</p>
* @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>
*/ */
public DropperPlayerEntryState(@NotNull UUID playerId, Location entryLocation, public DropperPlayerEntryState(@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,
float originalFlySpeed, float horizontalVelocity, float originalFlySpeed, float horizontalVelocity,
DropperArenaGameMode arenaGameMode, boolean originalCollideAble) { DropperArenaGameMode arenaGameMode, boolean originalCollideAble,
Collection<PotionEffect> originalPotionEffects) {
super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight,
originalInvulnerable, originalIsSwimming, originalCollideAble); originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects);
this.originalFlySpeed = originalFlySpeed; this.originalFlySpeed = originalFlySpeed;
this.horizontalVelocity = horizontalVelocity; this.horizontalVelocity = horizontalVelocity;
this.arenaGameMode = arenaGameMode; this.arenaGameMode = arenaGameMode;
@ -108,7 +113,7 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
* *
* @return <p>The data to deserialize</p> * @return <p>The data to deserialize</p>
*/ */
@SuppressWarnings("unused") @SuppressWarnings({"unused", "unchecked"})
public static DropperPlayerEntryState deserialize(Map<String, Object> data) { public static DropperPlayerEntryState deserialize(Map<String, Object> data) {
UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue(); UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue();
Location entryLocation = (Location) data.get("entryLocation"); Location entryLocation = (Location) data.get("entryLocation");
@ -121,10 +126,12 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
float horizontalVelocity = ((Number) data.get("horizontalVelocity")).floatValue(); float horizontalVelocity = ((Number) data.get("horizontalVelocity")).floatValue();
DropperArenaGameMode arenaGameMode = (DropperArenaGameMode) data.get("arenaGameMode"); DropperArenaGameMode arenaGameMode = (DropperArenaGameMode) data.get("arenaGameMode");
boolean originalCollideAble = getBoolean(data, "originalCollideAble"); boolean originalCollideAble = getBoolean(data, "originalCollideAble");
Collection<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>());
return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying, return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying,
originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming,
originalFlySpeed, horizontalVelocity, arenaGameMode, originalCollideAble); originalFlySpeed, horizontalVelocity, arenaGameMode, originalCollideAble, originalPotionEffect);
} }
} }

View File

@ -5,8 +5,11 @@ import net.knarcraft.minigames.container.SerializableUUID;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -35,12 +38,14 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
* @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p> * @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p>
* @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>
*/ */
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 originalCollideAble) { boolean originalInvulnerable, boolean originalIsSwimming,
boolean originalCollideAble, Collection<PotionEffect> originalPotionEffects) {
super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight,
originalInvulnerable, originalIsSwimming, originalCollideAble); originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects);
} }
@Override @Override
@ -60,7 +65,7 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
* *
* @return <p>The data to deserialize</p> * @return <p>The data to deserialize</p>
*/ */
@SuppressWarnings("unused") @SuppressWarnings({"unused", "unchecked"})
public static ParkourPlayerEntryState deserialize(Map<String, Object> data) { public static ParkourPlayerEntryState deserialize(Map<String, Object> data) {
UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue(); UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue();
Location entryLocation = (Location) data.get("entryLocation"); Location entryLocation = (Location) data.get("entryLocation");
@ -70,9 +75,11 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
boolean originalInvulnerable = getBoolean(data, "originalInvulnerable"); boolean originalInvulnerable = getBoolean(data, "originalInvulnerable");
boolean originalIsSwimming = getBoolean(data, "originalIsSwimming"); boolean originalIsSwimming = getBoolean(data, "originalIsSwimming");
boolean originalCollideAble = getBoolean(data, "originalCollideAble"); boolean originalCollideAble = getBoolean(data, "originalCollideAble");
Collection<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>());
return new ParkourPlayerEntryState(playerId, entryLocation, originalIsFlying, return new ParkourPlayerEntryState(playerId, entryLocation, originalIsFlying, originalGameMode,
originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble); originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffect);
} }
} }