package net.knarcraft.minigames.arena.dropper; import net.knarcraft.minigames.arena.AbstractPlayerEntryState; import net.knarcraft.minigames.container.SerializableUUID; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.util.Map; import java.util.UUID; /** * The state of a player before entering a dropper arena */ public class DropperPlayerEntryState extends AbstractPlayerEntryState { private final float originalFlySpeed; private final float horizontalVelocity; private final DropperArenaGameMode arenaGameMode; /** * Instantiates a new player state * * @param player

The player whose state should be stored

*/ public DropperPlayerEntryState(@NotNull Player player, @NotNull DropperArenaGameMode arenaGameMode, float horizontalVelocity) { super(player); this.originalFlySpeed = player.getFlySpeed(); this.arenaGameMode = arenaGameMode; this.horizontalVelocity = horizontalVelocity; } /** * 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

*/ public DropperPlayerEntryState(@NotNull UUID playerId, Location entryLocation, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalInvulnerable, boolean originalIsSwimming, float originalFlySpeed, float horizontalVelocity, DropperArenaGameMode arenaGameMode) { super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming); this.originalFlySpeed = originalFlySpeed; this.horizontalVelocity = horizontalVelocity; this.arenaGameMode = arenaGameMode; } @Override public void setArenaState() { Player player = getPlayer(); if (player == null) { return; } player.setAllowFlight(true); player.setFlying(true); player.setGameMode(GameMode.ADVENTURE); player.setSwimming(false); // If playing on the inverted game-mode, negate the horizontal velocity to swap the controls if (this.arenaGameMode == DropperArenaGameMode.INVERTED) { player.setFlySpeed(-this.horizontalVelocity); } else { player.setFlySpeed(this.horizontalVelocity); } } @Override public boolean restore() { Player player = getPlayer(); if (player == null) { return false; } this.restore(player); return true; } @Override public void restore(@NotNull Player player) { super.restore(player); player.setFlySpeed(this.originalFlySpeed); } @NotNull @Override public Map serialize() { Map data = super.serialize(); data.put("originalFlySpeed", this.originalFlySpeed); data.put("horizontalVelocity", this.horizontalVelocity); data.put("arenaGameMode", this.arenaGameMode); return data; } /** * Deserializes a ParkourPlayerEntryState from the given data * * @return

The data to deserialize

*/ @SuppressWarnings("unused") public static DropperPlayerEntryState deserialize(Map data) { UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue(); Location entryLocation = (Location) data.get("entryLocation"); boolean originalIsFlying = (boolean) data.get("originalIsFlying"); GameMode originalGameMode = GameMode.valueOf((String) data.get("originalGameMode")); boolean originalAllowFlight = (boolean) data.get("originalAllowFlight"); boolean originalInvulnerable = (boolean) data.get("originalInvulnerable"); boolean originalIsSwimming = (boolean) data.get("originalIsSwimming"); float originalFlySpeed = ((Number) data.get("originalFlySpeed")).floatValue(); float horizontalVelocity = ((Number) data.get("horizontalVelocity")).floatValue(); DropperArenaGameMode arenaGameMode = (DropperArenaGameMode) data.get("arenaGameMode"); return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, originalFlySpeed, horizontalVelocity, arenaGameMode); } }