From ddcd20c57a32386f328a6c406b5d1fc4c82af7ae Mon Sep 17 00:00:00 2001 From: levischnorr Date: Fri, 9 Feb 2024 08:29:12 +0100 Subject: [PATCH] Code changes for readability --- .../src/main/java/mavenmcserver/Plugin.java | 19 +- .../command/CommandTicTacToe.java | 181 ++++++++++-------- .../command/CommandTicTacToeAccept.java | 10 +- .../main/java/mavenmcserver/game/Game.java | 10 + .../java/mavenmcserver/game/GameConfig.java | 22 ++- mavenmcserver/src/main/resources/classes.uml | 11 +- .../mavenmcserver/game/GameConfigTest.java | 10 +- 7 files changed, 156 insertions(+), 107 deletions(-) diff --git a/mavenmcserver/src/main/java/mavenmcserver/Plugin.java b/mavenmcserver/src/main/java/mavenmcserver/Plugin.java index a14a6db..c0215ee 100644 --- a/mavenmcserver/src/main/java/mavenmcserver/Plugin.java +++ b/mavenmcserver/src/main/java/mavenmcserver/Plugin.java @@ -1,13 +1,10 @@ package mavenmcserver; -import java.util.HashSet; - import org.bukkit.plugin.java.JavaPlugin; import mavenmcserver.command.CommandTicTacToe; import mavenmcserver.command.CommandTicTacToeAccept; import mavenmcserver.game.Game; -import mavenmcserver.game.Game.GameEndCause; public class Plugin extends JavaPlugin { @@ -19,21 +16,15 @@ public void onLoad() { @Override public void onEnable() { // Register command /tictactoe - new CommandTicTacToe(this); - new CommandTicTacToeAccept(this); + new CommandTicTacToe(this).registerToPlugin(); + // Register command /tictactoeaccept + new CommandTicTacToeAccept(this).registerToPlugin(); } @Override public void onDisable() { - - // Cancel all running games - HashSet runningGames = new HashSet(); - runningGames.addAll(Game.runningGames.values()); - - for(Game runningGame: runningGames) { - runningGame.end(GameEndCause.CANCEL); - } - + this.getLogger().info("TicTacToe disabled! Cancelling all games..."); + Game.cancelAllGames(); } } diff --git a/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToe.java b/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToe.java index 32ead9e..cdc1fc7 100644 --- a/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToe.java +++ b/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToe.java @@ -32,11 +32,19 @@ public class CommandTicTacToe implements CommandExecutor, TabCompleter { public static int SIZE_Z_INDEX = 2; public static int WIN_REQUIRED_AMOUNT_INDEX = 3; + public static String CANCEL_KEYWORD = "cancel"; + public static String REQUEST_RETURN_MATCH_KEYWORD = "requestReturnMatch"; + + public static int NO_AVAILABLE_PLAYERS_MIN_ARG_COUNT = 3; + public static String NO_AVAILABLE_PLAYERS_ARGS[] = {"(no", "available", "players)"}; + private Plugin plugin; public CommandTicTacToe(Plugin plugin) { this.plugin = plugin; - + } + + public void registerToPlugin() { this.plugin.getCommand(CommandTicTacToe.COMMAND_NAME).setExecutor(this); this.plugin.getCommand(CommandTicTacToe.COMMAND_NAME).setTabCompleter(this); } @@ -49,94 +57,113 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; } - if(args.length > 0) { - - boolean playerIsCurrentlyInAGame = Game.runningGames.containsKey((Player)sender); - if(playerIsCurrentlyInAGame && args[0].equals("cancel")) { - Game.runningGames.get((Player)sender).end(GameEndCause.CANCEL); - return true; - } + if(args.length < CommandTicTacToe.MIN_VALID_ARG_COUNT) { + return false; + } - if(args.length > CommandTicTacToe.MAX_VALID_ARG_COUNT) { - sender.sendMessage(ChatColor.RED + "Too many arguments for command '/" + label + "'!" + ChatColor.RESET); - return false; - } + boolean playerIsCurrentlyInAGame = Game.runningGames.containsKey((Player)sender); + boolean playerProvidedCancelKeyword = args[CommandTicTacToe.OPPONENT_ARG_INDEX].equals(CommandTicTacToe.CANCEL_KEYWORD); + if(playerIsCurrentlyInAGame && playerProvidedCancelKeyword) { + Game gameToCancel = Game.runningGames.get((Player)sender); + gameToCancel.end(GameEndCause.CANCEL); + return true; + } + + if(args.length > CommandTicTacToe.MAX_VALID_ARG_COUNT) { + sender.sendMessage(ChatColor.RED + "Too many arguments for command '/" + label + "'!" + ChatColor.RESET); + return false; + } + + if(CommandTicTacToe.isNoAvailablePlayersPlaceholder(args)) { + return true; + } + + if(args[CommandTicTacToe.OPPONENT_ARG_INDEX].equals(CommandTicTacToe.REQUEST_RETURN_MATCH_KEYWORD)) { - int noAvailablePlayersMinArgCount = 3; - if(args.length >= noAvailablePlayersMinArgCount) { - String noAvailablePlayersPlaceholder[] = {"(no", "available", "players)"}; - if(args[0].equals(noAvailablePlayersPlaceholder[0]) && args[1].equals(noAvailablePlayersPlaceholder[1]) && args[2].equals(noAvailablePlayersPlaceholder[2])) return true; - } + GameConfig configOfReturnMatch = Game.lostGames.get((Player)sender); - if(args[0].equals("requestReturnMatch")) { + if(configOfReturnMatch != null) { - GameConfig returnConfig = Game.lostGames.get((Player)sender); + // Show the confirmation to the player + sender.sendMessage("You've just asked " + ChatColor.AQUA + ChatColor.BOLD + configOfReturnMatch.opponentPlayer.getName() + ChatColor.RESET + " to play a return match with you!"); - if(returnConfig != null) { - - // Show the confirmation to the player - sender.sendMessage("You've just asked " + ChatColor.AQUA + ChatColor.BOLD + returnConfig.opponentPlayer.getName() + ChatColor.RESET + " to play a return match with you!"); - - // Remove config from list! - Game.lostGames.remove((Player)sender); - Game.lostGames.remove(returnConfig.opponentPlayer); - - new Game(returnConfig, this.plugin, true); - return true; - } - } - - // Create the game's config from the command's args - GameConfig config; - - try { - config = this.createGameConfigFromCommand((Player)sender, args); - } catch(InvalidArgCountException | OpponentPlayerNotFoundException | OpponentIsMainPlayerException e) { - sender.sendMessage(ChatColor.RED + e.getMessage() + ChatColor.RESET); - return true; - } catch(NumberFormatException e) { - String nonNumberString = e.getMessage().substring(19, e.getMessage().length() - 1); - sender.sendMessage(ChatColor.RED + "Error: expected number at '" + nonNumberString + "'." + ChatColor.RESET); - return true; - } - - // Check for errors in the game's config - List configErrors = config.validate(); - if(!configErrors.isEmpty()) { - for(String error: configErrors) { - sender.sendMessage(ChatColor.RED + error + ChatColor.RESET); - } + // Remove config from list! + Game.lostGames.remove((Player)sender); + Game.lostGames.remove(configOfReturnMatch.opponentPlayer); - // Don't continue on error + new Game(configOfReturnMatch, this.plugin, true); return true; } + } + + // Create the game's config from the command's args + GameConfig config; + + try { - ArrayList queuedGames = new ArrayList(); - queuedGames.addAll(Game.queuedGames.values()); + config = this.createGameConfigFromCommand((Player)sender, args); - for(Game queuedGame: queuedGames) { - if(queuedGame.config.mainPlayer == config.mainPlayer) { - Game.queuedGames.remove(queuedGame.uuid); - - String revokeMessage; - if(queuedGame.config.opponentPlayer == config.opponentPlayer) { - revokeMessage = ChatColor.AQUA + "" + ChatColor.BOLD + config.mainPlayer.getName() + ChatColor.RESET + " has updated their tic-tac-toe-game request. See below."; - } else { - revokeMessage = ChatColor.AQUA + "" + ChatColor.BOLD + config.mainPlayer.getName() + ChatColor.RESET + " has revoked their tic-tac-toe-game request to you."; - } - - queuedGame.config.opponentPlayer.sendMessage(revokeMessage); - } + } catch(InvalidArgCountException | OpponentPlayerNotFoundException | OpponentIsMainPlayerException e) { + sender.sendMessage(ChatColor.RED + e.getMessage() + ChatColor.RESET); + return true; + } catch(NumberFormatException e) { + String nonNumberString = e.getMessage().substring(19, e.getMessage().length() - 1); + sender.sendMessage(ChatColor.RED + "Error: expected number at '" + nonNumberString + "'." + ChatColor.RESET); + return true; + } + + // Check for errors in the game's config + List configErrors = config.validateReturningErrors(); + if(!configErrors.isEmpty()) { + for(String error: configErrors) { + sender.sendMessage(ChatColor.RED + error + ChatColor.RESET); } - // Show the confirmation to the player - sender.sendMessage("You've just asked " + ChatColor.AQUA + ChatColor.BOLD + config.opponentPlayer.getName() + ChatColor.RESET + " to play a game of tic-tac-toe with you!"); - - new Game(config, this.plugin, false); + // Don't continue on error + return true; + } + + this.tellAffectedPlayersThatPlayerChangedTheirRequest(config); + + // Show the confirmation to the player + sender.sendMessage("You've just asked " + ChatColor.AQUA + ChatColor.BOLD + config.opponentPlayer.getName() + ChatColor.RESET + " to play a game of tic-tac-toe with you!"); + + new Game(config, this.plugin, false); + + return false; + } + + private static boolean isNoAvailablePlayersPlaceholder(String[] args) { + if(args.length < CommandTicTacToe.NO_AVAILABLE_PLAYERS_MIN_ARG_COUNT) { + return false; } - boolean shouldShowUsage = args.length <= 0; - return !shouldShowUsage; + boolean firstArgumentIsPlaceholder = args[0].equals(CommandTicTacToe.NO_AVAILABLE_PLAYERS_ARGS[0]); + boolean secondArgumentIsPlaceholder = args[1].equals(CommandTicTacToe.NO_AVAILABLE_PLAYERS_ARGS[1]); + boolean thirdArgumentIsPlaceholder = args[2].equals(CommandTicTacToe.NO_AVAILABLE_PLAYERS_ARGS[2]); + boolean argumentsArePlaceholder = firstArgumentIsPlaceholder && secondArgumentIsPlaceholder && thirdArgumentIsPlaceholder; + + return argumentsArePlaceholder; + } + + private void tellAffectedPlayersThatPlayerChangedTheirRequest(GameConfig config) { + ArrayList queuedGames = new ArrayList(); + queuedGames.addAll(Game.queuedGames.values()); + + for(Game queuedGame: queuedGames) { + if(queuedGame.config.mainPlayer == config.mainPlayer) { + Game.queuedGames.remove(queuedGame.uuid); + + String revokeMessage; + if(queuedGame.config.opponentPlayer == config.opponentPlayer) { + revokeMessage = ChatColor.AQUA + "" + ChatColor.BOLD + config.mainPlayer.getName() + ChatColor.RESET + " has updated their tic-tac-toe-game request. See below."; + } else { + revokeMessage = ChatColor.AQUA + "" + ChatColor.BOLD + config.mainPlayer.getName() + ChatColor.RESET + " has revoked their tic-tac-toe-game request to you."; + } + + queuedGame.config.opponentPlayer.sendMessage(revokeMessage); + } + } } @Override @@ -151,7 +178,7 @@ public List onTabComplete(CommandSender sender, Command command, String boolean playerIsCurrentlyInAGame = Game.runningGames.containsKey((Player)sender); if(playerIsCurrentlyInAGame) { if(argList.size() == CommandTicTacToe.OPPONENT_ARG_INDEX + 1) { - completions.add("cancel"); + completions.add(CommandTicTacToe.CANCEL_KEYWORD); } } else { if(argList.size() == CommandTicTacToe.OPPONENT_ARG_INDEX + 1) { @@ -162,7 +189,7 @@ public List onTabComplete(CommandSender sender, Command command, String } if(Game.lostGames.containsKey((Player)sender)) { - completions.add("requestReturnMatch"); + completions.add(CommandTicTacToe.REQUEST_RETURN_MATCH_KEYWORD); } if(completions.isEmpty()) completions.add("(no available players)"); diff --git a/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java b/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java index ad41e32..ffda24c 100644 --- a/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java +++ b/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java @@ -21,9 +21,15 @@ public class CommandTicTacToeAccept implements CommandExecutor, TabCompleter { public static int PLAYER_NAME_ARG_INDEX = 0; public static String ERROR_EXECUTION_IS_ONLY_ALLOWED_BY_PLAYERS = ChatColor.RED + "this command may only be executed by players" + ChatColor.RESET; + private Plugin plugin; + public CommandTicTacToeAccept(Plugin plugin) { - plugin.getCommand(CommandTicTacToeAccept.COMMAND_NAME).setExecutor(this); - plugin.getCommand(CommandTicTacToeAccept.COMMAND_NAME).setTabCompleter(this); + this.plugin = plugin; + } + + public void registerToPlugin() { + this.plugin.getCommand(CommandTicTacToeAccept.COMMAND_NAME).setExecutor(this); + this.plugin.getCommand(CommandTicTacToeAccept.COMMAND_NAME).setTabCompleter(this); } @Override diff --git a/mavenmcserver/src/main/java/mavenmcserver/game/Game.java b/mavenmcserver/src/main/java/mavenmcserver/game/Game.java index a574fc9..817800d 100644 --- a/mavenmcserver/src/main/java/mavenmcserver/game/Game.java +++ b/mavenmcserver/src/main/java/mavenmcserver/game/Game.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.UUID; @@ -78,6 +79,15 @@ public static Game getQueuedGameByUUID(String uuidString) throws IllegalArgument /// Contains all games that are currently running in connection to their players (every game is in this map twice!) public static HashMap runningGames = new HashMap(); + public static void cancelAllGames() { + HashSet runningGames = new HashSet(); + runningGames.addAll(Game.runningGames.values()); + + for(Game runningGame: runningGames) { + runningGame.end(GameEndCause.CANCEL); + } + } + /// Contains a list of game setups (value) that were lost by player (key) (ties count as well). public static HashMap lostGames = new HashMap(); diff --git a/mavenmcserver/src/main/java/mavenmcserver/game/GameConfig.java b/mavenmcserver/src/main/java/mavenmcserver/game/GameConfig.java index 93defde..2f40642 100644 --- a/mavenmcserver/src/main/java/mavenmcserver/game/GameConfig.java +++ b/mavenmcserver/src/main/java/mavenmcserver/game/GameConfig.java @@ -8,6 +8,8 @@ public class GameConfig { + public static int MIN_DIMENSION_SIZE = 1; + /** * The minimum X and Z size of a game */ @@ -55,7 +57,7 @@ public GameConfig(Player mainPlayer, Player opponentPlayer, Vector3i size, int w * Checks the config for errors. * @return a list with all errors found on this config. If no errors are found, an empty list is returned. */ - public List validate() { + public List validateReturningErrors() { ArrayList errors = new ArrayList(); if(this.mainPlayer == null) { @@ -78,7 +80,7 @@ public List validate() { return errors; } - errors.addAll(this.validateNumbers()); + errors.addAll(this.validateNumbersReturningErrors()); return errors; } @@ -87,24 +89,32 @@ public List validate() { * Checks the config for errors with the size and winRequiredAmount (part of validate()). * @return a list with all errors found on this config regarding the number values. If no errors are found, an empty list is returned. */ - List validateNumbers() { + List validateNumbersReturningErrors() { ArrayList errors = new ArrayList(); - if(Math.min(this.size.x, Math.min(this.size.y, this.size.z)) <= 0) { - errors.add("No dimension of the game can be smaller than 1. The smallest possible game is (" + GameConfig.MIN_X_Z_SIZE + ", " + GameConfig.MIN_HEIGHT + ", " + GameConfig.MIN_X_Z_SIZE + ")."); + if(this.getSmallestDimension() < GameConfig.MIN_DIMENSION_SIZE) { + errors.add("No dimension of the game can be smaller than " + GameConfig.MIN_DIMENSION_SIZE + ". The smallest possible game is (" + GameConfig.MIN_X_Z_SIZE + ", " + GameConfig.MIN_HEIGHT + ", " + GameConfig.MIN_X_Z_SIZE + ")."); } if(Math.min(this.size.x, this.size.z) < GameConfig.MIN_X_Z_SIZE) { errors.add("The X and Z size of the game must not be smaller than " + GameConfig.MIN_X_Z_SIZE + "."); } - if(this.winRequiredAmount > Math.max(this.size.x, Math.max(this.size.y, this.size.z))) { + if(this.winRequiredAmount > this.getLargestDimension()) { errors.add(GameConfig.ERROR_WIN_REQUIRED_AMOUNT_TOO_LARGE); } return errors; } + public int getSmallestDimension() { + return Math.min(this.size.x, Math.min(this.size.y, this.size.z)); + } + + public int getLargestDimension() { + return Math.max(this.size.x, Math.max(this.size.y, this.size.z)); + } + @Override public String toString() { diff --git a/mavenmcserver/src/main/resources/classes.uml b/mavenmcserver/src/main/resources/classes.uml index 0a82b34..efbb53f 100644 --- a/mavenmcserver/src/main/resources/classes.uml +++ b/mavenmcserver/src/main/resources/classes.uml @@ -87,17 +87,22 @@ class FieldPoint { +{static}int MIN_X_Z_SIZE +{static}int MIN_HEIGHT {static}String ERROR_MAIN_PLAYER_NULL + {static}String ERROR_OPPONENT_PLAYER_NULL + {static}String ERROR_PLAYER_ALREADY_IN_GAME + {static}String ERROR_WIN_REQUIRED_AMOUNT_TOO_LARGE .. Fields .. - +{final}Player mainPlayer + +Player mainPlayer +Player opponentPlayer +Vector3i size +int winRequiredAmount .. Methods .. +GameConfig(Player mainPlayer, Player opponentPlayer, Vector3i size, int winRequiredAmount) - +List validate() /' Returns a list of errors in the config! Empty means OK '/ - List validateNumbers() /' sub-part of validate() '/ + +List validateReturningErrors() /' Returns a list of errors in the config! Empty means OK '/ + List validateNumbersReturningErrors() /' sub-part of validateReturningErrors() '/ + +int getSmallestDimenson() + +int getLargestDimension() } diff --git a/mavenmcserver/src/test/java/mavenmcserver/game/GameConfigTest.java b/mavenmcserver/src/test/java/mavenmcserver/game/GameConfigTest.java index 6768556..be690e9 100644 --- a/mavenmcserver/src/test/java/mavenmcserver/game/GameConfigTest.java +++ b/mavenmcserver/src/test/java/mavenmcserver/game/GameConfigTest.java @@ -13,7 +13,7 @@ public class GameConfigTest { @Test public void testValidate() { GameConfig config = new GameConfig(null, null, new Vector3i(2, 1, 2), 2); - List errors = config.validate(); + List errors = config.validateReturningErrors(); assertTrue(errors.contains(GameConfig.ERROR_MAIN_PLAYER_NULL)); } @@ -21,21 +21,21 @@ public void testValidate() { @Test public void testValidateNumbers() { GameConfig config = new GameConfig(null, null, new Vector3i(0, 0, 0), 2); - List errors = config.validateNumbers(); + List errors = config.validateNumbersReturningErrors(); assertTrue(errors.contains("No dimension of the game can be smaller than 1. The smallest possible game is (" + GameConfig.MIN_X_Z_SIZE + ", " + GameConfig.MIN_HEIGHT + ", " + GameConfig.MIN_X_Z_SIZE + ").")); assertTrue(errors.contains("The X and Z size of the game must not be smaller than " + GameConfig.MIN_X_Z_SIZE + ".")); assertTrue(errors.contains("The required win amount must not be larger than the size's largest dimension.")); config = new GameConfig(null, null, new Vector3i(0, 1, 2), 2); - errors = config.validateNumbers(); + errors = config.validateNumbersReturningErrors(); assertTrue(errors.contains("No dimension of the game can be smaller than 1. The smallest possible game is (" + GameConfig.MIN_X_Z_SIZE + ", " + GameConfig.MIN_HEIGHT + ", " + GameConfig.MIN_X_Z_SIZE + ").")); config = new GameConfig(null, null, new Vector3i(3, 1, 2), 4); - errors = config.validateNumbers(); + errors = config.validateNumbersReturningErrors(); assertTrue(errors.contains(GameConfig.ERROR_WIN_REQUIRED_AMOUNT_TOO_LARGE)); config = new GameConfig(null, null, new Vector3i(1, 1, 2), 3); - errors = config.validateNumbers(); + errors = config.validateNumbersReturningErrors(); assertTrue(errors.contains("The X and Z size of the game must not be smaller than " + GameConfig.MIN_X_Z_SIZE + ".")); }