From 382b16d2b23240a148488e213cdf7f2d3acd8c8e Mon Sep 17 00:00:00 2001 From: levischnorr Date: Thu, 8 Feb 2024 13:01:52 +0100 Subject: [PATCH] Minor changes to CommandTicTacToeAccept for readability --- .../command/CommandTicTacToeAccept.java | 68 +++++++++++-------- .../main/java/mavenmcserver/game/Game.java | 13 ++++ 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java b/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java index 98be842..01d6840 100644 --- a/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java +++ b/mavenmcserver/src/main/java/mavenmcserver/command/CommandTicTacToeAccept.java @@ -20,6 +20,7 @@ public class CommandTicTacToeAccept implements CommandExecutor, TabCompleter { public static String COMMAND_NAME = "tictactoeaccept"; public static int ARG_COUNT = 1; 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; public CommandTicTacToeAccept(Plugin plugin) { plugin.getCommand(CommandTicTacToeAccept.COMMAND_NAME).setExecutor(this); @@ -30,7 +31,7 @@ public CommandTicTacToeAccept(Plugin plugin) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if(!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "this command may only be executed by players" + ChatColor.RESET); + sender.sendMessage(CommandTicTacToeAccept.ERROR_EXECUTION_IS_ONLY_ALLOWED_BY_PLAYERS); return true; } @@ -38,56 +39,57 @@ public boolean onCommand(CommandSender sender, Command command, String label, St String playerName = args[CommandTicTacToeAccept.PLAYER_NAME_ARG_INDEX]; - Game targetGame = null; + Game targetGame = this.getQueuedGameWithPlayers(playerName, (Player)sender); - for(Game queuedGame: Game.queuedGames.values()) { - if(queuedGame.config.opponentPlayer != (Player)sender) continue; - if(queuedGame.config.mainPlayer.getName().equals(playerName)) { - targetGame = queuedGame; - break; - } - } - - boolean validUUIDIsGiven = false; if(targetGame == null) { try { - UUID gameUUID = UUID.fromString(playerName); - validUUIDIsGiven = true; - targetGame = Game.queuedGames.get(gameUUID); - } catch(IllegalArgumentException e) {} - } - - if(targetGame == null) { - if(validUUIDIsGiven) { - sender.sendMessage(ChatColor.RED + "This game is not available anymore." + ChatColor.RESET); - } else { + targetGame = this.getQueuedGameByUUID(args[CommandTicTacToeAccept.PLAYER_NAME_ARG_INDEX]); + + if(targetGame == null) { + sender.sendMessage(ChatColor.RED + "This game is not available anymore." + ChatColor.RESET); + return true; + } + + } catch(IllegalArgumentException e) { sender.sendMessage(ChatColor.RED + "'" + playerName + "' hasn't sent any game request to you!" + ChatColor.RESET); + return true; } - return true; } targetGame.start(); return true; } + + private Game getQueuedGameWithPlayers(String mainPlayerName, Player opponentPlayer) { + for(Game queuedGame: Game.queuedGames.values()) { + if(queuedGame.config.opponentPlayer != opponentPlayer) continue; + if(queuedGame.config.mainPlayer.getName().equals(mainPlayerName)) { + return queuedGame; + } + } + + return null; + } + + private Game getQueuedGameByUUID(String uuidString) throws IllegalArgumentException { + UUID gameUUID = UUID.fromString(uuidString); + return Game.queuedGames.get(gameUUID); + } @Override public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { if(!(sender instanceof Player)) return new ArrayList(); - ArrayList argList = new ArrayList(); - for(String arg: args) argList.add(arg); - argList.removeIf((arg) -> arg.isEmpty() && !CommandTicTacToeAccept.listContainsNonEmptyString(argList.subList(0, Math.max(0, argList.indexOf(arg) - 1)))); + ArrayList argList = CommandTicTacToeAccept.removeEmptyStringsBeforeStringFromList(args); if(argList.size() >= CommandTicTacToeAccept.ARG_COUNT) return new ArrayList(); ArrayList completions = new ArrayList(); - for(Game queuedGame: Game.queuedGames.values()) { - if(queuedGame.config.opponentPlayer == (Player)sender) { - completions.add(queuedGame.config.mainPlayer.getName()); - } + for(Game queuedGame: Game.getRequestsTo((Player)sender)) { + completions.add(queuedGame.config.mainPlayer.getName()); } ArrayList filteredCompletions = new ArrayList(); @@ -96,6 +98,14 @@ public List onTabComplete(CommandSender sender, Command command, String return filteredCompletions; } + public static ArrayList removeEmptyStringsBeforeStringFromList(String[] list) { + ArrayList newList = new ArrayList(); + for(String arg: list) newList.add(arg); + + newList.removeIf((item) -> item.isEmpty() && !CommandTicTacToeAccept.listContainsNonEmptyString(newList.subList(0, Math.max(0, newList.indexOf(item) - 1)))); + + return newList; + } public static boolean listContainsNonEmptyString(List list) { for(String string: list) { diff --git a/mavenmcserver/src/main/java/mavenmcserver/game/Game.java b/mavenmcserver/src/main/java/mavenmcserver/game/Game.java index e1f22eb..07adabe 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.List; import java.util.Map.Entry; import java.util.UUID; @@ -47,6 +48,18 @@ public class Game { /// Contains all queued games that still have to be accepted / rejected public static HashMap queuedGames = new HashMap(); + public static List getRequestsTo(Player opponentPlayer) { + ArrayList result = new ArrayList(); + + for(Game queuedGame: Game.queuedGames.values()) { + if(queuedGame.config.opponentPlayer == opponentPlayer) { + result.add(queuedGame); + } + } + + return result; + } + /// 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();