Makes sure all arena sessions are exited regardless

This commit is contained in:
Kristian Knarvik 2023-04-17 22:20:42 +02:00
parent ce0a0dbaa7
commit cf0962ef70
2 changed files with 19 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package net.knarcraft.minigames; package net.knarcraft.minigames;
import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaData; import net.knarcraft.minigames.arena.dropper.DropperArenaData;
import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode; import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode;
import net.knarcraft.minigames.arena.dropper.DropperArenaGroup; import net.knarcraft.minigames.arena.dropper.DropperArenaGroup;
@ -8,6 +9,7 @@ import net.knarcraft.minigames.arena.dropper.DropperArenaHandler;
import net.knarcraft.minigames.arena.dropper.DropperArenaPlayerRegistry; import net.knarcraft.minigames.arena.dropper.DropperArenaPlayerRegistry;
import net.knarcraft.minigames.arena.dropper.DropperArenaRecordsRegistry; import net.knarcraft.minigames.arena.dropper.DropperArenaRecordsRegistry;
import net.knarcraft.minigames.arena.dropper.DropperArenaSession; import net.knarcraft.minigames.arena.dropper.DropperArenaSession;
import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaData; import net.knarcraft.minigames.arena.parkour.ParkourArenaData;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode; import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup; import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup;
@ -56,7 +58,6 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -277,12 +278,12 @@ public final class MiniGames extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
// Throw out currently playing players before exiting // Kill all sessions before exiting
for (Player player : getServer().getOnlinePlayers()) { for (DropperArena arena : dropperArenaHandler.getArenas().values()) {
ArenaSession session = getSession(player.getUniqueId()); dropperArenaPlayerRegistry.removeForArena(arena);
if (session != null) { }
session.triggerQuit(true); for (ParkourArena arena : parkourArenaHandler.getArenas().values()) {
} parkourArenaPlayerRegistry.removeForArena(arena);
} }
} }

View File

@ -52,28 +52,36 @@ public class PlayerLeaveListener implements Listener {
} }
} }
@EventHandler /**
* Prevent the player from teleporting away from an arena for any reason
*
* @param event <p>The triggered teleport event</p>
*/
@EventHandler(ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent event) { public void onPlayerTeleport(PlayerTeleportEvent event) {
Location targetLocation = event.getTo(); Location targetLocation = event.getTo();
if (targetLocation == null || event.isCancelled()) { if (targetLocation == null) {
return; return;
} }
// Ignore if not in an arena session
ArenaSession arenaSession = MiniGames.getInstance().getSession(event.getPlayer().getUniqueId()); ArenaSession arenaSession = MiniGames.getInstance().getSession(event.getPlayer().getUniqueId());
if (arenaSession == null) { if (arenaSession == null) {
return; return;
} }
// If teleported to the arena's spawn, it's fine
if (targetLocation.equals(arenaSession.getArena().getSpawnLocation())) { if (targetLocation.equals(arenaSession.getArena().getSpawnLocation())) {
return; return;
} }
// If teleported to the arena's checkpoint, it's fine
if (arenaSession instanceof ParkourArenaSession parkourArenaSession && if (arenaSession instanceof ParkourArenaSession parkourArenaSession &&
targetLocation.equals(parkourArenaSession.getRegisteredCheckpoint())) { targetLocation.equals(parkourArenaSession.getRegisteredCheckpoint())) {
return; return;
} }
arenaSession.triggerQuit(false); event.setCancelled(true);
} }
} }