Implements checkpoints

This commit is contained in:
2023-04-15 23:37:27 +02:00
parent 8e4737a267
commit c68cafb925
4 changed files with 48 additions and 9 deletions

View File

@ -11,6 +11,7 @@ import net.knarcraft.minigames.util.PlayerTeleporter;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.logging.Level;
@ -25,6 +26,7 @@ public class ParkourArenaSession implements ArenaSession {
private int deaths;
private final long startTime;
private final PlayerEntryState entryState;
private Location reachedCheckpoint = null;
/**
* Instantiates a new parkour arena session
@ -62,6 +64,24 @@ public class ParkourArenaSession implements ArenaSession {
return this.entryState;
}
/**
* Registers the checkpoint this session's player has reached
*
* @param location <p>The location of the checkpoint</p>
*/
public void registerCheckpoint(@NotNull Location location) {
this.reachedCheckpoint = location;
}
/**
* Gets the checkpoint currently registered as the player's spawn location
*
* @return <p>The registered checkpoint, or null if not set</p>
*/
public @Nullable Location getRegisteredCheckpoint() {
return this.reachedCheckpoint;
}
/**
* Triggers a win for the player playing in this session
*/
@ -154,7 +174,8 @@ public class ParkourArenaSession implements ArenaSession {
public void triggerLoss() {
this.deaths++;
//Teleport the player back to the top
PlayerTeleporter.teleportPlayer(this.player, this.arena.getSpawnLocation(), true, false);
Location spawnLocation = this.reachedCheckpoint != null ? this.reachedCheckpoint : this.arena.getSpawnLocation();
PlayerTeleporter.teleportPlayer(this.player, spawnLocation, true, false);
this.entryState.setArenaState();
}

View File

@ -59,12 +59,22 @@ public class MoveListener implements Listener {
* @param arenaSession <p>The dropper session of the player triggering the event</p>
*/
private void doParkourArenaChecks(@NotNull PlayerMoveEvent event, ParkourArenaSession arenaSession) {
if (event.getTo() == null) {
// Ignore movement which won't cause the player's block to change
if (event.getTo() == null || event.getFrom().getBlock() == event.getTo().getBlock()) {
return;
}
// Only do block type checking if the block beneath the player changes
if (event.getFrom().getBlock() != event.getTo().getBlock()) {
checkForSpecialBlock(arenaSession, event.getTo());
if (checkForSpecialBlock(arenaSession, event.getTo())) {
return;
}
// Check if the player reached one of the checkpoints for the arena
for (Location checkpoint : arenaSession.getArena().getCheckpoints()) {
if (checkpoint.getBlock().equals(event.getTo().getBlock())) {
arenaSession.registerCheckpoint(checkpoint.clone());
return;
}
}
}

View File

@ -2,7 +2,9 @@ package net.knarcraft.minigames.listener;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.parkour.ParkourArenaSession;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -52,7 +54,8 @@ public class PlayerLeaveListener implements Listener {
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
if (event.getTo() == null || event.isCancelled()) {
Location targetLocation = event.getTo();
if (targetLocation == null || event.isCancelled()) {
return;
}
@ -61,7 +64,12 @@ public class PlayerLeaveListener implements Listener {
return;
}
if (event.getTo().equals(arenaSession.getArena().getSpawnLocation())) {
if (targetLocation.equals(arenaSession.getArena().getSpawnLocation())) {
return;
}
if (arenaSession instanceof ParkourArenaSession parkourArenaSession &&
targetLocation.equals(parkourArenaSession.getRegisteredCheckpoint())) {
return;
}