Skip to content

Commit

Permalink
Moved two functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lscgh committed Feb 8, 2024
1 parent 662c55b commit 1dd82b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String

if(!(sender instanceof Player)) return new ArrayList<String>();

ArrayList<String> argList = new ArrayList<String>();
int i = 0;
for(String arg: args) {
if(!arg.trim().isEmpty() || (i == args.length - 1)) {
argList.add(arg);
}

i++;
}
ArrayList<String> argList = CommandTicTacToe.removeEmptyElementsExceptForLast(args);

ArrayList<String> completions = new ArrayList<String>();

Expand Down Expand Up @@ -245,6 +237,20 @@ protected static int[] extractIntegerArgs(String args[]) {
return integerArguments;
}

private static ArrayList<String> removeEmptyElementsExceptForLast(String[] list) {
ArrayList<String> newList = new ArrayList<String>();
int i = 0;
for(String element: list) {
if(!element.trim().isEmpty() || (i == list.length - 1)) {
newList.add(element);
}

i++;
}

return newList;
}


public class InvalidArgCountException extends Exception {
private static final long serialVersionUID = 5946362337911270663L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
Expand Down Expand Up @@ -39,12 +38,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

String playerName = args[CommandTicTacToeAccept.PLAYER_NAME_ARG_INDEX];

Game targetGame = this.getQueuedGameWithPlayers(playerName, (Player)sender);
Game targetGame = Game.getQueuedGameWithPlayers(playerName, (Player)sender);


if(targetGame == null) {
try {
targetGame = this.getQueuedGameByUUID(args[CommandTicTacToeAccept.PLAYER_NAME_ARG_INDEX]);
targetGame = Game.getQueuedGameByUUID(args[CommandTicTacToeAccept.PLAYER_NAME_ARG_INDEX]);

if(targetGame == null) {
sender.sendMessage(ChatColor.RED + "This game is not available anymore." + ChatColor.RESET);
Expand All @@ -61,22 +60,6 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

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<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
Expand Down
16 changes: 16 additions & 0 deletions mavenmcserver/src/main/java/mavenmcserver/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public static List<Game> getRequestsTo(Player opponentPlayer) {
return result;
}

public static 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;
}

public static Game getQueuedGameByUUID(String uuidString) throws IllegalArgumentException {
UUID gameUUID = UUID.fromString(uuidString);
return Game.queuedGames.get(gameUUID);
}

/// Contains all games that are currently running in connection to their players (every game is in this map twice!)
public static HashMap<Player, Game> runningGames = new HashMap<Player, Game>();

Expand Down
2 changes: 2 additions & 0 deletions mavenmcserver/src/main/resources/classes.uml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class FieldPoint {
+{static}float FIELD_FALL_SOUND_PITCH
+{static}HashMap<UUID, Game> queuedGames
+{static}List<Game> getRequestsTo(Player opponentPlayer)
+{static}Game getQueuedGameWithPlayers(String mainPlayerName, Player opponentPlayer) /' For use in CommandTicTacToeAccept.java '/
+{static}Game getQueuedGameByUUID(String uuidString) /' For use in CommandTicTacToeAccept.java '/
+{static}HashMap<Player, Game> runningGames
+{static}HashMap<Player, GameConfig> lostGames

Expand Down

0 comments on commit 1dd82b2

Please sign in to comment.