Implements the join command

This commit is contained in:
Kristian Knarvik 2023-03-24 11:23:56 +01:00
parent a9e14497d8
commit 52de9e5161
2 changed files with 79 additions and 7 deletions

View File

@ -1,5 +1,11 @@
package net.knarcraft.dropper.command;
import net.knarcraft.dropper.Dropper;
import net.knarcraft.dropper.arena.DropperArena;
import net.knarcraft.dropper.arena.DropperArenaPlayerRegistry;
import net.knarcraft.dropper.arena.DropperArenaSession;
import net.knarcraft.dropper.property.ArenaGameMode;
import net.knarcraft.dropper.util.PlayerTeleporter;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -13,16 +19,62 @@ public class JoinArenaCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] strings) {
if (!(commandSender instanceof Player)) {
@NotNull String[] arguments) {
if (!(commandSender instanceof Player player)) {
commandSender.sendMessage("This command must be used by a player");
return false;
}
//TODO: Remember to check if the player is already in an arena first!
//TODO: Create a new arena session
//TODO: Register the session in the arena player registry
//TODO: Teleport the player to the arena's start location
return true;
if (arguments.length < 1) {
return false;
}
// Disallow joining if the player is already in a dropper arena
DropperArenaSession existingSession = Dropper.getInstance().getPlayerRegistry().getArenaSession(player);
if (existingSession != null) {
commandSender.sendMessage("You are already in a dropper arena!");
return false;
}
// Make sure the arena exists
String arenaName = arguments[0].trim();
DropperArena specifiedArena = null;
for (DropperArena arena : Dropper.getInstance().getArenaHandler().getArenas()) {
if (arena.getArenaName().equalsIgnoreCase(arenaName)) {
specifiedArena = arena;
break;
}
}
if (specifiedArena == null) {
commandSender.sendMessage("Unable to find the specified dropper arena.");
return false;
}
// Find the specified game-mode
ArenaGameMode gameMode;
if (arguments.length > 1) {
gameMode = ArenaGameMode.matchGamemode(arguments[1]);
} else {
gameMode = ArenaGameMode.DEFAULT;
}
//TODO: Check if the arena has been beaten if the non-default game-mode has been chosen
// Register the player's session
DropperArenaSession newSession = new DropperArenaSession(specifiedArena, player, gameMode);
DropperArenaPlayerRegistry playerRegistry = Dropper.getInstance().getPlayerRegistry();
playerRegistry.registerPlayer(player, newSession);
// Try to teleport the player to the arena
boolean teleported = PlayerTeleporter.teleportPlayer(player, specifiedArena.getSpawnLocation(), false);
if (!teleported) {
commandSender.sendMessage("Unable to teleport you to the dropper arena. Make sure you're not in a vehicle," +
"and is not carrying a passenger!");
newSession.triggerQuit();
return false;
} else {
return true;
}
}
}

View File

@ -1,5 +1,7 @@
package net.knarcraft.dropper.property;
import org.jetbrains.annotations.NotNull;
/**
* A representation of possible arena game-modes
*/
@ -20,5 +22,23 @@ public enum ArenaGameMode {
* The least-time game-mode. Player plays until they manage to win. The total time of the session is recorded.
*/
LEAST_TIME,
;
/**
* Tries to match the correct game-mode according to the given string
*
* @param gameMode <p>The game-mode string to match</p>
* @return <p>The specified arena game-mode</p>
*/
public static @NotNull ArenaGameMode matchGamemode(@NotNull String gameMode) {
String sanitized = gameMode.trim().toLowerCase();
if (sanitized.matches("(least)?deaths?")) {
return ArenaGameMode.LEAST_DEATHS;
} else if (sanitized.matches("(least)?time")) {
return ArenaGameMode.LEAST_TIME;
} else {
return ArenaGameMode.DEFAULT;
}
}
}