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.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<PotionEffect> 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 <p>The id of the player whose state this should keep track of</p>
* @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p>
* @param originalAllowFlight <p>Whether the player was allowed flight 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 originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param playerId <p>The id of the player whose state this should keep track of</p>
* @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p>
* @param originalAllowFlight <p>Whether the player was allowed flight 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 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,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming,
boolean originalCollideAble) {
boolean originalCollideAble, Collection<PotionEffect> 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;
}

View File

@ -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 <p>The id of the player whose state this should keep track of</p>
* @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p>
* @param originalAllowFlight <p>Whether the player was allowed flight 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 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 originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param playerId <p>The id of the player whose state this should keep track of</p>
* @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p>
* @param originalAllowFlight <p>Whether the player was allowed flight 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 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 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,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming,
float originalFlySpeed, float horizontalVelocity,
DropperArenaGameMode arenaGameMode, boolean originalCollideAble) {
DropperArenaGameMode arenaGameMode, boolean originalCollideAble,
Collection<PotionEffect> 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 <p>The data to deserialize</p>
*/
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "unchecked"})
public static DropperPlayerEntryState deserialize(Map<String, Object> 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<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>());
return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying,
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.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 <p>The id of the player whose state this should keep track of</p>
* @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p>
* @param originalAllowFlight <p>Whether the player was allowed flight 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 originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param playerId <p>The id of the player whose state this should keep track of</p>
* @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p>
* @param originalAllowFlight <p>Whether the player was allowed flight 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 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,
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,
originalInvulnerable, originalIsSwimming, originalCollideAble);
originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects);
}
@Override
@ -60,7 +65,7 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
*
* @return <p>The data to deserialize</p>
*/
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "unchecked"})
public static ParkourPlayerEntryState deserialize(Map<String, Object> 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<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) 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);
}
}