From 906543f0171912ed068fe3307eb63403d23ac36c Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 24 Mar 2023 02:03:18 +0100 Subject: [PATCH] Prepares for command implementation Removes some finished TODOs Registers all listeners Registers all commands Adds more descriptive TODO comments to each command Removes unintended double storage of dropper arena sessions in DropperArenaHandler --- .../java/net/knarcraft/dropper/Dropper.java | 67 ++++++++++++++----- .../dropper/arena/DropperArenaHandler.java | 22 ------ .../arena/DropperArenaPlayerRegistry.java | 14 +--- .../dropper/arena/DropperArenaSession.java | 2 +- .../dropper/command/CreateArenaCommand.java | 9 ++- .../dropper/command/EditArenaCommand.java | 7 +- .../command/EditArenaTabCompleter.java | 24 +++++++ .../dropper/command/JoinArenaCommand.java | 4 +- .../dropper/command/LeaveArenaCommand.java | 6 +- .../dropper/command/ListArenaCommand.java | 20 ++++++ .../dropper/command/RemoveArenaCommand.java | 9 ++- 11 files changed, 126 insertions(+), 58 deletions(-) create mode 100644 src/main/java/net/knarcraft/dropper/command/EditArenaTabCompleter.java create mode 100644 src/main/java/net/knarcraft/dropper/command/ListArenaCommand.java diff --git a/src/main/java/net/knarcraft/dropper/Dropper.java b/src/main/java/net/knarcraft/dropper/Dropper.java index feb6735..5843a92 100644 --- a/src/main/java/net/knarcraft/dropper/Dropper.java +++ b/src/main/java/net/knarcraft/dropper/Dropper.java @@ -2,7 +2,25 @@ package net.knarcraft.dropper; import net.knarcraft.dropper.arena.DropperArenaHandler; import net.knarcraft.dropper.arena.DropperArenaPlayerRegistry; +import net.knarcraft.dropper.command.CreateArenaCommand; +import net.knarcraft.dropper.command.EditArenaCommand; +import net.knarcraft.dropper.command.EditArenaTabCompleter; +import net.knarcraft.dropper.command.JoinArenaCommand; +import net.knarcraft.dropper.command.LeaveArenaCommand; +import net.knarcraft.dropper.command.ListArenaCommand; +import net.knarcraft.dropper.command.RemoveArenaCommand; +import net.knarcraft.dropper.listener.DamageListener; +import net.knarcraft.dropper.listener.MoveListener; +import net.knarcraft.dropper.listener.PlayerLeaveListener; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabCompleter; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.logging.Level; /** * The dropper plugin's main class @@ -49,34 +67,49 @@ public final class Dropper extends JavaPlugin { this.arenaHandler = new DropperArenaHandler(); this.arenaHandler.loadArenas(); - //TODO: Keep track of whether players are in a dropper arena, and which arena they are in - //TODO: Make an event listener that kicks players from an arena if they take damage (EntityDamageEvent). - // Remember to cancel the event so they don't die. - //TODO: Make a listener for whether someone in an arena is about to hit a block (for cobwebs or similar). Use - // another check in the listener to check if a player is hitting water -> do whatever should be done when winning. - - //TODO: Arena settings: Spawn (where players are teleported to), Velocity (the downwards speed added to players. - // Might need a scheduler to maintain the speed), Stage (a numeric integer. if set, only allow access if the - // previous stage has been cleared), A configurable reward of some sort?, A name (just for easy differentiation), - // possibly a leave location to make sure pressure plates won't create an infinite loop - //TODO: Add a command for joining a specific arena. Only teleport if the stage check succeeds (The server can // use something like https://www.spigotmc.org/resources/commandblocks.62720/ for immersion) //TODO: Store various information about players' performance, and hook into PlaceholderAPI - //TODO: Implement optional time trial/least deaths game-mode somehow //TODO: Possibly implement an optional queue mode, which only allows one player inside one dropper arena at any - // time (to prevent players from pushing each-other) + // time (to prevent players from pushing each-other)? - //TODO: Set player.setAllowFlight to true while in the arena to avoid flight blocking for high velocities + PluginManager pluginManager = getServer().getPluginManager(); + pluginManager.registerEvents(new DamageListener(), this); + pluginManager.registerEvents(new MoveListener(), this); + pluginManager.registerEvents(new PlayerLeaveListener(), this); - - //TODO: Register event listeners - //TODO: Register commands + registerCommand("droppercreate", new CreateArenaCommand(), null); + registerCommand("dropperlist", new ListArenaCommand(), null); + registerCommand("dropperjoin", new JoinArenaCommand(), null); + registerCommand("dropperleave", new LeaveArenaCommand(), null); + registerCommand("dropperedit", new EditArenaCommand(), new EditArenaTabCompleter()); + registerCommand("dropperremove", new RemoveArenaCommand(), null); } @Override public void onDisable() { } + + /** + * Registers a command + * + * @param commandName

The name of the command to register (defined in plugin.yml)

+ * @param commandExecutor

The executor for the command

+ * @param tabCompleter

The tab-completer to use, or null

+ */ + private void registerCommand(@NotNull String commandName, @NotNull CommandExecutor commandExecutor, + @Nullable TabCompleter tabCompleter) { + PluginCommand command = this.getCommand(commandName); + if (command != null) { + command.setExecutor(commandExecutor); + if (tabCompleter != null) { + command.setTabCompleter(tabCompleter); + } + } else { + getLogger().log(Level.SEVERE, "Unable to register the command " + commandName); + } + } + } diff --git a/src/main/java/net/knarcraft/dropper/arena/DropperArenaHandler.java b/src/main/java/net/knarcraft/dropper/arena/DropperArenaHandler.java index 5bfddaf..f9196a9 100644 --- a/src/main/java/net/knarcraft/dropper/arena/DropperArenaHandler.java +++ b/src/main/java/net/knarcraft/dropper/arena/DropperArenaHandler.java @@ -9,10 +9,8 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.logging.Level; /** @@ -23,28 +21,8 @@ public class DropperArenaHandler { private static final File arenaFile = new File(Dropper.getInstance().getDataFolder(), "arenas.yml"); private List arenas = new ArrayList<>(); - private final Set activeSessions = new HashSet<>(); private final Map stagesCleared = new HashMap<>(); - /** - * Registers the given arena session to known active arena sessions - * - * @param session

The arena session to register

- */ - public void registerArenaSession(@NotNull DropperArenaSession session) { - this.activeSessions.add(session); - } - - /** - * Unregisters the given arena session from known active arena sessions - * - * @param session

The session to remove

- * @return

True if the session was removed

- */ - public boolean unregisterArenaSession(@NotNull DropperArenaSession session) { - return this.activeSessions.remove(session); - } - /** * Tries to register the given stage as cleared * diff --git a/src/main/java/net/knarcraft/dropper/arena/DropperArenaPlayerRegistry.java b/src/main/java/net/knarcraft/dropper/arena/DropperArenaPlayerRegistry.java index 2426039..acc1caa 100644 --- a/src/main/java/net/knarcraft/dropper/arena/DropperArenaPlayerRegistry.java +++ b/src/main/java/net/knarcraft/dropper/arena/DropperArenaPlayerRegistry.java @@ -29,18 +29,8 @@ public class DropperArenaPlayerRegistry { * * @param player

The player to remove

*/ - public void removePlayer(@NotNull Player player) { - this.arenaPlayers.remove(player); - } - - /** - * Gets whether the given player is currently playing in an arena - * - * @param player

The player to check

- * @return

True if the player is currently in an arena

- */ - public boolean isInArena(@NotNull Player player) { - return getArenaSession(player) != null; + public boolean removePlayer(@NotNull Player player) { + return this.arenaPlayers.remove(player) != null; } /** diff --git a/src/main/java/net/knarcraft/dropper/arena/DropperArenaSession.java b/src/main/java/net/knarcraft/dropper/arena/DropperArenaSession.java index e298d6c..cdb0d9d 100644 --- a/src/main/java/net/knarcraft/dropper/arena/DropperArenaSession.java +++ b/src/main/java/net/knarcraft/dropper/arena/DropperArenaSession.java @@ -88,7 +88,7 @@ public class DropperArenaSession { */ private void removeSession() { // Remove this session for game sessions to stop listeners from fiddling more with the player - boolean removedSession = Dropper.getInstance().getArenaHandler().unregisterArenaSession(this); + boolean removedSession = Dropper.getInstance().getPlayerRegistry().removePlayer(player); if (!removedSession) { Dropper.getInstance().getLogger().log(Level.SEVERE, "Unable to remove dropper arena session for " + player.getName() + ". This will have unintended consequences."); diff --git a/src/main/java/net/knarcraft/dropper/command/CreateArenaCommand.java b/src/main/java/net/knarcraft/dropper/command/CreateArenaCommand.java index cc35982..99db88d 100644 --- a/src/main/java/net/knarcraft/dropper/command/CreateArenaCommand.java +++ b/src/main/java/net/knarcraft/dropper/command/CreateArenaCommand.java @@ -5,12 +5,19 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +/** + * The command for creating a new dropper arena + */ public class CreateArenaCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { - //TODO: Implement command behavior + //TODO: Make sure the console cannot run this + //TODO: Make sure the arena name isn't a duplicate and doesn't contain any unwanted characters + //TODO: Create a new arena + //TODO: Register the new arena in the arena handler + //TODO: Tell the user of success return false; } diff --git a/src/main/java/net/knarcraft/dropper/command/EditArenaCommand.java b/src/main/java/net/knarcraft/dropper/command/EditArenaCommand.java index 50e3de2..164d175 100644 --- a/src/main/java/net/knarcraft/dropper/command/EditArenaCommand.java +++ b/src/main/java/net/knarcraft/dropper/command/EditArenaCommand.java @@ -5,12 +5,17 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +/** + * The command for editing an existing dropper arena + */ public class EditArenaCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { - //TODO: Implement command behavior + //TODO: Make sure the console cannot run this + //TODO: If an arena name and a property is given, display the current value + //TODO: If an arena name, a property and a value is given, check if it's valid, and update the property return false; } diff --git a/src/main/java/net/knarcraft/dropper/command/EditArenaTabCompleter.java b/src/main/java/net/knarcraft/dropper/command/EditArenaTabCompleter.java new file mode 100644 index 0000000..7205e4b --- /dev/null +++ b/src/main/java/net/knarcraft/dropper/command/EditArenaTabCompleter.java @@ -0,0 +1,24 @@ +package net.knarcraft.dropper.command; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * The tab-completer for the edit arena command + */ +public class EditArenaTabCompleter implements TabCompleter { + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, + @NotNull String label, @NotNull String[] args) { + //TODO: Tab-complete existing arena names + //TODO: If an arena name is given, tab-complete change-able properties + return null; + } + +} diff --git a/src/main/java/net/knarcraft/dropper/command/JoinArenaCommand.java b/src/main/java/net/knarcraft/dropper/command/JoinArenaCommand.java index 1f5f34a..28fa46d 100644 --- a/src/main/java/net/knarcraft/dropper/command/JoinArenaCommand.java +++ b/src/main/java/net/knarcraft/dropper/command/JoinArenaCommand.java @@ -18,8 +18,10 @@ public class JoinArenaCommand implements CommandExecutor { commandSender.sendMessage("This command must be used by a player"); return false; } - //TODO: Implement command behavior //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; } diff --git a/src/main/java/net/knarcraft/dropper/command/LeaveArenaCommand.java b/src/main/java/net/knarcraft/dropper/command/LeaveArenaCommand.java index fa4f6b4..2f5f57d 100644 --- a/src/main/java/net/knarcraft/dropper/command/LeaveArenaCommand.java +++ b/src/main/java/net/knarcraft/dropper/command/LeaveArenaCommand.java @@ -5,13 +5,17 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +/** + * The command used to leave the current dropper arena + */ public class LeaveArenaCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { - //TODO: Implement command behavior + //TODO: Make sure the console cannot run this //TODO: If the player isn't currently in an arena, just display an error message + //TODO: Trigger the player's session's triggerQuit() method return false; } diff --git a/src/main/java/net/knarcraft/dropper/command/ListArenaCommand.java b/src/main/java/net/knarcraft/dropper/command/ListArenaCommand.java new file mode 100644 index 0000000..de462cc --- /dev/null +++ b/src/main/java/net/knarcraft/dropper/command/ListArenaCommand.java @@ -0,0 +1,20 @@ +package net.knarcraft.dropper.command; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +/** + * A command for listing existing dropper arenas + */ +public class ListArenaCommand implements CommandExecutor { + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, + @NotNull String[] args) { + //TODO: List all existing arenas, and possibly information about a specified arena + return false; + } + +} diff --git a/src/main/java/net/knarcraft/dropper/command/RemoveArenaCommand.java b/src/main/java/net/knarcraft/dropper/command/RemoveArenaCommand.java index 3b8e811..7c64dff 100644 --- a/src/main/java/net/knarcraft/dropper/command/RemoveArenaCommand.java +++ b/src/main/java/net/knarcraft/dropper/command/RemoveArenaCommand.java @@ -5,13 +5,18 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +/** + * The method used for removing an existing arena + */ public class RemoveArenaCommand implements CommandExecutor { @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { - //TODO: Implement command behavior - //TODO: Make sure to kick players if the arena is currently in use + //TODO: Make sure to kick any playing players if the arena is currently in use, by triggering their sessions' + // triggerQuit() method + //TODO: Remove the arena from DropperArenaHandler + //TODO: Notify the user of success return false; }