From f991dbca586f0feb4de42bd4fe45a9a6c67d70ec Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 9 Jan 2023 16:10:03 +0100 Subject: [PATCH 01/13] Refactoring, Registering new command class (SetupCommands) --- .../butzlabben/missilewars/MissileWars.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java index 0c97fb0c..945e9a77 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java @@ -20,6 +20,7 @@ import co.aikar.commands.PaperCommandManager; import de.butzlabben.missilewars.commands.MWCommands; +import de.butzlabben.missilewars.commands.SetupCommands; import de.butzlabben.missilewars.commands.StatsCommands; import de.butzlabben.missilewars.commands.UserCommands; import de.butzlabben.missilewars.configuration.Config; @@ -59,6 +60,9 @@ public class MissileWars extends JavaPlugin { private SignRepository signRepository; private boolean foundFAWE; + + private PlayerListener playerListener; + private SignListener signListener; public MissileWars() { instance = this; @@ -99,7 +103,7 @@ public void onEnable() { Arenas.load(); SetupUtil.checkShields(); - GameManager.getInstance().loadGames(); + GameManager.getInstance().loadGamesOnStartup(); new Metrics(this, 3749); @@ -143,8 +147,11 @@ public void onDisable() { * This method registers all events of the missilewars event listener. */ private void registerEvents() { - Bukkit.getPluginManager().registerEvents(new PlayerListener(), this); - Bukkit.getPluginManager().registerEvents(new SignListener(), this); + playerListener = new PlayerListener(); + signListener = new SignListener(); + + Bukkit.getPluginManager().registerEvents(playerListener, this); + Bukkit.getPluginManager().registerEvents(signListener, this); } /** @@ -157,10 +164,11 @@ private void registerCommands() { // It simply lets it take advantage of Paper specific features if available, // such as Asynchronous Tab Completions. PaperCommandManager manager = new PaperCommandManager(this); - + manager.registerCommand(new MWCommands()); manager.registerCommand(new StatsCommands()); manager.registerCommand(new UserCommands()); + manager.registerCommand(new SetupCommands()); } /** @@ -220,4 +228,12 @@ private void sendPluginInfo() { Logger.BOOT.log("Other authors: " + sb); } } + + public PlayerListener getPlayerListener() { + return playerListener; + } + + public SignListener getSignListener() { + return signListener; + } } From 459483def7cf214e779bc408f2259b52ce4a370a Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 9 Jan 2023 16:12:53 +0100 Subject: [PATCH 02/13] Removing duplicate / unnecessary actions --- .../java/de/butzlabben/missilewars/game/Game.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java index ca85ae14..ec3351f9 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java @@ -242,7 +242,7 @@ private void stopTimer() { public void stopGame() { if (Config.isSetup()) return; - + Logger.DEBUG.log("Stopping"); for (BukkitTask bt : playerTasks.values()) { @@ -284,7 +284,7 @@ public void reset() { return; } - GameManager.getInstance().restartGame(lobby); + GameManager.getInstance().restartGame(lobby, false); } public void appendRestart() { @@ -416,11 +416,6 @@ private void checkTeamSize(Team team) { } public void resetGame() { - if (state == GameState.INGAME) { - stopGame(); - return; - } - HandlerList.unregisterAll(listener); stopTimer(); @@ -436,9 +431,6 @@ public void resetGame() { if (scoreboardManager != null) { scoreboardManager.removeScoreboard(); } - - team1 = null; - team2 = null; } public boolean isInLobbyArea(Location location) { From 9f6ae2267e6856e1dd7e1e34b72fe80a8e357856 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 9 Jan 2023 16:13:33 +0100 Subject: [PATCH 03/13] Adding new "auto_load" configuration --- .../java/de/butzlabben/missilewars/configuration/Lobby.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java index 82ad52a6..3fdb30ac 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java @@ -45,6 +45,7 @@ public class Lobby { private String name = "lobby0"; @SerializedName("display_name") private String displayName = "&eDefault game"; + @SerializedName("auto_load") private boolean autoLoad = true; @SerializedName("world") private String world = Bukkit.getWorlds().get(0).getName(); @SerializedName("lobby_time") private int lobbyTime = 60; @SerializedName("join_ongoing_game") private boolean joinOngoingGame = false; @@ -54,8 +55,8 @@ public class Lobby { @SerializedName("team1_color") private String team1Color = "&c"; @SerializedName("team2_name") private String team2Name = "Team2"; @SerializedName("team2_color") private String team2Color = "&a"; - @SerializedName("spawn_point") private Location spawnPoint = Bukkit.getWorlds().get(0).getSpawnLocation(); - @SerializedName("after_game_spawn") private Location afterGameSpawn = Bukkit.getWorlds().get(0).getSpawnLocation(); + @Setter @SerializedName("spawn_point") private Location spawnPoint = Bukkit.getWorlds().get(0).getSpawnLocation(); + @Setter @SerializedName("after_game_spawn") private Location afterGameSpawn = Bukkit.getWorlds().get(0).getSpawnLocation(); private Area area = Area.defaultAreaAround(Bukkit.getWorlds().get(0).getSpawnLocation()); @SerializedName("map_choose_procedure") private MapChooseProcedure mapChooseProcedure = MapChooseProcedure.FIRST; @SerializedName("possible_arenas") private List possibleArenas = new ArrayList<>() {{ From c233c036714b932ad612b2490474defa64499078 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 9 Jan 2023 16:13:57 +0100 Subject: [PATCH 04/13] Refactoring --- .../missilewars/game/GameManager.java | 115 +++++++++++------- 1 file changed, 72 insertions(+), 43 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java index 48c257f9..f6b72da0 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java @@ -43,8 +43,16 @@ public void disableAll() { games.values().forEach(Game::disableGameOnServerStop); games.clear(); } + + public void restartAll() { + games.values().forEach(game -> restartGame(game.getLobby(), false)); + } - public void loadGames() { + /** + * This method is for starting up the server. The game lobby configurations + * are loaded here. + */ + public void loadGamesOnStartup() { File[] lobbyFiles = null; if (Config.isMultipleLobbies()) { lobbyFiles = new File(Config.getLobbiesFolder()).listFiles(); @@ -72,59 +80,80 @@ public void loadGames() { lobbyFiles = new File[] {file}; } - loadGames(lobbyFiles); - } - - private void loadGames(File[] lobbyFileList) { - - for (File lobbyFile : lobbyFileList) { - if (lobbyFile == null) - continue; + for (File lobbyFile : lobbyFiles) { + if (lobbyFile == null) continue; if (!lobbyFile.getName().endsWith(".yml") && !lobbyFile.getName().endsWith(".yaml")) continue; - Logger.BOOT.log("Loading lobby " + lobbyFile.getName()); - try { - Lobby lobby = Serializer.deserialize(lobbyFile, Lobby.class); - if (lobby == null) { - Logger.ERROR.log("Could not load lobby " + lobbyFile.getName()); - continue; - } - Game potentialOtherGame = getGame(lobby.getName()); - if (potentialOtherGame != null) { - Logger.ERROR.log("A lobby with the same name was already loaded. Names of lobbies must be unique, this lobby will not be loaded"); - continue; - } - lobby.setFile(lobbyFile); - restartGame(lobby); - Logger.BOOTDONE.log("Loaded lobby " + lobbyFile.getName()); - } catch (IOException exception) { - Logger.ERROR.log("Could not load lobby " + lobbyFile.getName()); - exception.printStackTrace(); + debugStart(lobbyFile); + } + } + + /** + * This method attempts to read the game lobby configuration and build a game + * from it. Config mistakes are recognized and the config is saved again. + * + * @param lobbyFile (File) the arena configuration file + */ + private void debugStart(File lobbyFile) { + Logger.BOOT.log("Try to loading lobby of \"" + lobbyFile.getName() + "\""); + + try { + Lobby lobby = Serializer.deserialize(lobbyFile, Lobby.class); + + if (lobby == null) { + Logger.ERROR.log("Could not load lobby of \"" + lobbyFile.getName() + "\""); + return; } + + if (getGame(lobby.getName()) != null) { + Logger.ERROR.log("A lobby with the same name was already loaded. Names of lobbies must be unique, this lobby will not be loaded"); + return; + } + + lobby.setFile(lobbyFile); + restartGame(lobby, false); + + } catch (IOException exception) { + Logger.ERROR.log("Could not load lobby of \"" + lobbyFile.getName() + "\""); + exception.printStackTrace(); } } - public void disableGame(String lobbyName) { - Game game = getGame(lobbyName); - if (game == null) return; + /** + * This method (re)starts a MissileWars game. + * + * @param targetLobby (Lobby) the existing lobby of the game + * @param forceStart true, if it should also (re)start, if it's not an automatically + * starting game according to the lobby configuration + */ + public void restartGame(Lobby targetLobby, boolean forceStart) { + if (!targetLobby.isAutoLoad() && !forceStart) return; + + String targetLobbyName = targetLobby.getName(); + + // reset the old game + Game game = getGame(targetLobbyName); + if (game != null) { + game.resetGame(); + } - game.resetGame(); - games.remove(lobbyName); + // delete the old game from the list + if (games.get(targetLobbyName) != null) { + games.remove(targetLobby); + } Logger.DEBUG.log("Old Game disabled."); - } - - public void restartGame(Lobby oldLobby) { - String oldLobbyName = oldLobby.getName(); - disableGame(oldLobbyName); + + // read the game lobby configuration and build a new game and lobby from it try { - Lobby lobby = Serializer.deserialize(oldLobby.getFile(), Lobby.class); - lobby.setFile(oldLobby.getFile()); - // Save for possible new values - Serializer.serialize(oldLobby.getFile(), lobby); - games.put(oldLobbyName, new Game(lobby)); + Lobby lobby = Serializer.deserialize(targetLobby.getFile(), Lobby.class); + lobby.setFile(targetLobby.getFile()); + + Logger.BOOTDONE.log("Reloaded lobby \"" + targetLobbyName + "\" (" + targetLobby.getFile().getName() + ")"); + games.put(targetLobbyName, new Game(lobby)); + } catch (IOException exception) { - Logger.ERROR.log("Could not load lobby " + oldLobby.getName()); + Logger.ERROR.log("Could not load lobby of \"" + targetLobby.getFile().getName() + "\""); exception.printStackTrace(); } } From 317385f7e48277b9d3b2909cfdd3ede418652609 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Mon, 9 Jan 2023 16:59:05 +0100 Subject: [PATCH 05/13] Small typo --- .../src/main/java/de/butzlabben/missilewars/game/Game.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java index ec3351f9..d3780131 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java @@ -276,9 +276,8 @@ public void stopGame() { } public void reset() { - if (Config.isSetup()) - return; - + if (Config.isSetup()) return; + if (restart) { Bukkit.getServer().spigot().restart(); return; From bd2efe2f27584b7057e79950a29a3ebfb3aeb871 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sat, 14 Jan 2023 15:05:01 +0100 Subject: [PATCH 06/13] Fix github check for PR --- .../src/main/java/de/butzlabben/missilewars/MissileWars.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java index 945e9a77..31b9158a 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java @@ -20,7 +20,6 @@ import co.aikar.commands.PaperCommandManager; import de.butzlabben.missilewars.commands.MWCommands; -import de.butzlabben.missilewars.commands.SetupCommands; import de.butzlabben.missilewars.commands.StatsCommands; import de.butzlabben.missilewars.commands.UserCommands; import de.butzlabben.missilewars.configuration.Config; @@ -168,7 +167,7 @@ private void registerCommands() { manager.registerCommand(new MWCommands()); manager.registerCommand(new StatsCommands()); manager.registerCommand(new UserCommands()); - manager.registerCommand(new SetupCommands()); + //manager.registerCommand(new SetupCommands()); } /** From 1e1515d523f2bc20a27fa94079f064923a65b541 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sat, 14 Jan 2023 16:47:44 +0100 Subject: [PATCH 07/13] Reworking Command System --- .../butzlabben/missilewars/MissileWars.java | 3 +- .../missilewars/commands/MWCommands.java | 191 +++++++----- .../missilewars/commands/SetupCommands.java | 277 ++++++++++++++++++ .../missilewars/commands/StatsCommands.java | 15 +- .../missilewars/commands/UserCommands.java | 62 ++-- 5 files changed, 426 insertions(+), 122 deletions(-) create mode 100644 missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java index 31b9158a..945e9a77 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java @@ -20,6 +20,7 @@ import co.aikar.commands.PaperCommandManager; import de.butzlabben.missilewars.commands.MWCommands; +import de.butzlabben.missilewars.commands.SetupCommands; import de.butzlabben.missilewars.commands.StatsCommands; import de.butzlabben.missilewars.commands.UserCommands; import de.butzlabben.missilewars.configuration.Config; @@ -167,7 +168,7 @@ private void registerCommands() { manager.registerCommand(new MWCommands()); manager.registerCommand(new StatsCommands()); manager.registerCommand(new UserCommands()); - //manager.registerCommand(new SetupCommands()); + manager.registerCommand(new SetupCommands()); } /** diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java index 0195761c..b273c0f4 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java @@ -23,24 +23,21 @@ import de.butzlabben.missilewars.Logger; import de.butzlabben.missilewars.MissileWars; import de.butzlabben.missilewars.configuration.Config; -import de.butzlabben.missilewars.configuration.Lobby; import de.butzlabben.missilewars.configuration.Messages; import de.butzlabben.missilewars.configuration.arena.Arena; import de.butzlabben.missilewars.game.Arenas; import de.butzlabben.missilewars.game.Game; import de.butzlabben.missilewars.game.GameManager; +import de.butzlabben.missilewars.game.enums.GameResult; import de.butzlabben.missilewars.game.enums.GameState; import de.butzlabben.missilewars.game.enums.MapChooseProcedure; import de.butzlabben.missilewars.game.missile.Missile; import de.butzlabben.missilewars.game.missile.MissileFacing; -import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; @CommandAlias("mw|missilewars") public class MWCommands extends BaseCommand { @@ -58,7 +55,7 @@ public void mwCommand(CommandSender sender) { if (sender.hasPermission("mw.stop")) sender.sendMessage(Messages.getPrefix() + "/mw stop - Stops the game"); if (sender.hasPermission("mw.restart")) - sender.sendMessage(Messages.getPrefix() + "/mw start - Restarts the game"); + sender.sendMessage(Messages.getPrefix() + "/mw restart - Restarts the game"); if (sender.hasPermission("mw.appendrestart")) sender.sendMessage(Messages.getPrefix() + "/mw appendrestart - Appends a restart after the next game ends"); @@ -76,6 +73,28 @@ public void mwCommand(CommandSender sender) { sender.sendMessage(Messages.getPrefix() + "/mw stats list - Lists history of games"); } + @Subcommand("listgames|list|games") + @Description("List the active games.") + @Syntax("/mw listgames") + @CommandCompletion("@nothing") + @CommandPermission("mw.listgames") + public void listgamesCommand(CommandSender sender, String[] args) { + + sender.sendMessage(Messages.getPrefix() + "Current games:"); + + for (Game game : GameManager.getInstance().getGames().values()) { + sender.sendMessage("§e " + game.getLobby().getName() + "§7 -- Name: »" + game.getLobby().getDisplayName() + "§7« | Status: " + game.getState()); + sender.sendMessage("§8 - §f" + "Load with startup: §7" + game.getLobby().isAutoLoad()); + sender.sendMessage("§8 - §f" + "Current Arena: §7" + game.getArena().getName() + "§7 -- Name: »" + game.getArena().getDisplayName() + "§7«"); + sender.sendMessage("§8 - §f" + "Total players: §7" + game.getPlayers().size() + "x"); + sender.sendMessage("§8 - §f" + "Team 1: §7" + game.getTeam1().getColor() + game.getTeam1().getName() + + " §7with " + game.getTeam1().getMembers().size() + " players"); + sender.sendMessage("§8 - §f" + "Team 2: §7" + game.getTeam2().getColor() + game.getTeam2().getName() + + " §7with " + game.getTeam2().getMembers().size() + " players"); + } + + } + @Subcommand("paste") @Description("Pastes a missile.") @Syntax("/mw paste ") @@ -85,21 +104,31 @@ public void pasteCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; Player player = (Player) sender; + + if (args.length < 1) { + player.sendMessage(Messages.getPrefix() + "§cMissile needed."); + return; + } + + if (args.length > 1) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } Game game = GameManager.getInstance().getGame(player.getLocation()); if (game == null) { player.sendMessage(Messages.getMessage("not_in_arena")); return; } - - String arguments = getAllNextArgumentsAsString(args, false); - Missile m = game.getArena().getMissileConfiguration().getMissileFromName(arguments.trim()); - if (m == null) { - player.sendMessage(Messages.getPrefix() + "§cUnknown missile"); + + Missile missile = game.getArena().getMissileConfiguration().getMissileFromName(args[0]); + if (missile == null) { + player.sendMessage(Messages.getPrefix() + "§cUnknown missile."); return; } - MissileFacing mf = MissileFacing.getFacingPlayer(player, game.getArena().getMissileConfiguration()); - m.paste(player, mf, game); + + MissileFacing missileFacing = MissileFacing.getFacingPlayer(player, game.getArena().getMissileConfiguration()); + missile.paste(player, missileFacing, game); } @Subcommand("start") @@ -112,16 +141,22 @@ public void startCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; Player player = (Player) sender; + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + Game game = GameManager.getInstance().getGame(player.getLocation()); if (game == null) { player.sendMessage(Messages.getMessage("not_in_arena")); return; } - + if (game.getState() != GameState.LOBBY) { player.sendMessage(Messages.getPrefix() + "§cGame already started"); return; } + if (game.isReady()) game.startGame(); else { @@ -147,7 +182,7 @@ public void startCommand(CommandSender sender, String[] args) { @Subcommand("stop") @Description("Stops the game.") - @Syntax("/mw stop") + @Syntax("/mw stop [lobby]") @CommandCompletion("@nothing") @CommandPermission("mw.stop") public void stopCommand(CommandSender sender, String[] args) { @@ -155,48 +190,52 @@ public void stopCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; Player player = (Player) sender; - Game game = GameManager.getInstance().getGame(player.getLocation()); - if (game == null) { - player.sendMessage(Messages.getMessage("not_in_arena")); + if (args.length > 1) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - - // TODO more arguments to get "game.sendGameResult();" - Bukkit.getScheduler().runTask(MissileWars.getInstance(), () -> { - if (game.getState() == GameState.INGAME) game.stopGame(); - }); + + Game game; + if (args.length == 1) { + game = GameManager.getInstance().getGame(args[0]); + if (game == null) { + player.sendMessage(Messages.getPrefix() + "§cGame not found."); + return; + } + } else { + game = GameManager.getInstance().getGame(player.getLocation()); + if (game == null) { + player.sendMessage(Messages.getMessage("not_in_arena")); + return; + } + } + + game.getTeam1().setGameResult(GameResult.DRAW); + game.getTeam2().setGameResult(GameResult.DRAW); + if (game.getState() == GameState.INGAME) game.stopGame(); } - - @Subcommand("restart") - @Description("Restarts the game.") - @Syntax("/mw restart") + + @Subcommand("appendrestart") + @Description("Appends a restart after the next game ends.") + @Syntax("/mw appendrestart") @CommandCompletion("@nothing") - @CommandPermission("mw.restart") - public void restartCommand(CommandSender sender, String[] args) { + @CommandPermission("mw.appendrestart") + public void appendrestartCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; Player player = (Player) sender; - Game game = GameManager.getInstance().getGame(player.getLocation()); + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + Game game = GameManager.getInstance().getGame(player.getLocation()); if (game == null) { player.sendMessage(Messages.getMessage("not_in_arena")); return; } - - Bukkit.getScheduler().runTask(MissileWars.getInstance(), () -> { - if (game.getState() == GameState.INGAME) game.stopGame(); - game.reset(); - }); - } - - @Subcommand("appendrestart") - @Description("Appends a restart after the next game ends.") - @Syntax("/mw appendrestart") - @CommandCompletion("@nothing") - @CommandPermission("mw.appendrestart") - public void appendRestartCommand(CommandSender sender, String[] args) { - + GameManager.getInstance().getGames().values().forEach(Game::appendRestart); sender.sendMessage(Messages.getMessage("restart_after_game")); } @@ -206,8 +245,16 @@ public void appendRestartCommand(CommandSender sender, String[] args) { @Syntax("/mw reload") @CommandCompletion("@nothing") @CommandPermission("mw.reload") - public void onReload(CommandSender sender, String[] args) { + public void reloadCommand(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + Player player = (Player) sender; + + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + Config.load(); Messages.load(); Arenas.load(); @@ -219,8 +266,16 @@ public void onReload(CommandSender sender, String[] args) { @Syntax("/mw debug") @CommandCompletion("@nothing") @CommandPermission("mw.debug") - public void onDebug(CommandSender sender, String[] args) { + public void debugCommand(CommandSender sender, String[] args) { + + if (!senderIsPlayer(sender)) return; + Player player = (Player) sender; + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + int i = 0; Logger.NORMAL.log("Starting to print debug information for MissileWars v" + MissileWars.getInstance().version); for (Game game : GameManager.getInstance().getGames().values()) { @@ -235,41 +290,31 @@ public void onDebug(CommandSender sender, String[] args) { @Syntax("/mw restartall") @CommandCompletion("@nothing") @CommandPermission("mw.reload") - public void onRestartAll(CommandSender sender, String[] args) { + public void restartallCommand(CommandSender sender, String[] args) { + + if (!senderIsPlayer(sender)) return; + Player player = (Player) sender; + + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + + Game game = GameManager.getInstance().getGame(player.getLocation()); + if (game == null) { + player.sendMessage(Messages.getMessage("not_in_arena")); + return; + } sender.sendMessage(Messages.getPrefix() + "§cWarning - Restarting all games. This may take a while"); - List arenaPropertiesList = GameManager.getInstance().getGames().values() - .stream().map(Game::getLobby).collect(Collectors.toList()); - arenaPropertiesList.forEach(GameManager.getInstance()::restartGame); + GameManager.getInstance().restartAll(); sender.sendMessage(Messages.getPrefix() + "Reloaded configs"); } - - private boolean senderIsPlayer(CommandSender sender) { + + static boolean senderIsPlayer(CommandSender sender) { if (sender instanceof Player) return true; sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); return false; } - - /** - * This method returns all next command arguments as one String line. - * Separated with a " " between the arguments. It also removes all - * unnecessary "&" signs. - * - * @param args Argument Array - * @return (String) all next arguments - */ - private String getAllNextArgumentsAsString(String[] args, boolean filterColorCode) { - StringBuilder sb = new StringBuilder(); - String arguments; - - for (int i = 0; i < args.length; i++) { - sb.append(" "); - } - arguments = sb.toString(); - - if (filterColorCode) arguments.replaceAll("&.", ""); - - return arguments; - } } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java new file mode 100644 index 00000000..98bfd022 --- /dev/null +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java @@ -0,0 +1,277 @@ +/* + * This file is part of MissileWars (https://github.com/Butzlabben/missilewars). + * Copyright (c) 2018-2021 Daniel Nägele. + * + * MissileWars is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * MissileWars is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MissileWars. If not, see . + */ + +package de.butzlabben.missilewars.commands; + +import co.aikar.commands.BaseCommand; +import co.aikar.commands.annotation.*; +import de.butzlabben.missilewars.configuration.Config; +import de.butzlabben.missilewars.configuration.Messages; +import de.butzlabben.missilewars.game.Game; +import de.butzlabben.missilewars.game.GameManager; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +@CommandAlias("mw|missilewars") +@Subcommand("setup") +public class SetupCommands extends BaseCommand { + + private Game game; + private Player player; + + @Default + @Description("Setup the game lobby and game area.") + @Syntax("/mw stats ") + @CommandPermission("mw.setup") + public void setupCommands(CommandSender sender, String[] args) { + sender.sendMessage(Messages.getPrefix() + "§fSetup usage: §7/mw stats "); + } + + @Subcommand("main") + @Description("Main Setup") + public class mainSetupCommands extends BaseCommand { + + @Subcommand("fallbackspawn") + public class fallbackspawnSetup extends BaseCommand { + + @Subcommand("set") + public void set(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + + Config.setFallbackSpawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'fallbackSpawn' to " + player.getLocation() + "."); + } + + @Subcommand("teleport|tp") + public void teleport(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + + player.teleport(Config.getFallbackSpawn()); + player.sendMessage(Messages.getPrefix() + "§fTeleported to 'fallbackSpawn'."); + } + + } + } + + @Subcommand("lobby") + @Description("Setup the lobby") + public class lobbySetupCommands extends BaseCommand { + + @Subcommand("spawnpoint") + public class spawnpointSetup extends BaseCommand { + + @Subcommand("set") + public void set(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getLobby().setSpawnPoint(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'spawnPoint' to " + player.getLocation() + "."); + } + + @Subcommand("teleport|tp") + public void teleport(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + player.teleport(game.getLobby().getSpawnPoint()); + player.sendMessage(Messages.getPrefix() + "§fTeleported to 'spawnPoint'."); + } + + } + + @Subcommand("aftergamespawn") + public class aftergamespawnSetup extends BaseCommand { + + @Subcommand("set") + public void set(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getLobby().setAfterGameSpawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); + } + + @Subcommand("teleport|tp") + public void teleport(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + player.teleport(game.getLobby().getAfterGameSpawn()); + player.sendMessage(Messages.getPrefix() + "§fTeleported to 'afterGameSpawn'."); + } + + } + + @Subcommand("area") + public class areaSetup extends BaseCommand { + + @Subcommand("set") + public class set extends BaseCommand { + + @Subcommand("pos1") + public void pos1(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getLobby().setAfterGameSpawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); + } + + @Subcommand("pos2") + public void pos2(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getLobby().setAfterGameSpawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); + } + + } + + @Subcommand("teleport|tp") + public class teleport extends BaseCommand { + + @Subcommand("pos1") + public void pos1(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getLobby().setAfterGameSpawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); + } + + @Subcommand("pos2") + public void pos2(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getLobby().setAfterGameSpawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); + } + + } + } + } + + @Subcommand("arena") + @Description("Setup the arena") + public class arenaSetupCommands extends BaseCommand { + + @Subcommand("spectatorspawn") + public class spectatorspawnSetup extends BaseCommand { + + @Subcommand("set") + public void set(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getArena().setSpectatorSpawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'spectatorSpawn' to " + player.getLocation() + "."); + } + + @Subcommand("teleport|tp") + public void teleport(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + player.teleport(game.getArena().getSpectatorSpawn()); + player.sendMessage(Messages.getPrefix() + "§fTeleported to 'spectatorSpawn'."); + } + + } + + @Subcommand("team1spawn") + public class team1spawnSetup extends BaseCommand { + + @Subcommand("set") + public void set(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getArena().setTeam1Spawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'team1Spawn' to " + player.getLocation() + "."); + } + + @Subcommand("teleport|tp") + public void teleport(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + player.teleport(game.getArena().getTeam1Spawn()); + player.sendMessage(Messages.getPrefix() + "§fTeleported to 'team1Spawn'."); + } + + } + + @Subcommand("team2spawn") + public class team2spawnSetup extends BaseCommand { + + @Subcommand("set") + public void set(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + game.getArena().setTeam2Spawn(player.getLocation()); + player.sendMessage(Messages.getPrefix() + "§fSet new 'team2Spawn' to " + player.getLocation() + "."); + } + + @Subcommand("teleport|tp") + public void teleport(CommandSender sender, String[] args) { + if (!senderIsPlayer(sender)) return; + if (!isValidGame()) return; + + player.teleport(game.getArena().getTeam2Spawn()); + player.sendMessage(Messages.getPrefix() + "§fTeleported to 'team2Spawn'."); + } + + } + } + + /** + * This method checks if the command sender is a valid ingame player. + * + * @param sender = the command sender + * @return true, if it's an ingame player + */ + private boolean senderIsPlayer(CommandSender sender) { + if (sender instanceof Player) { + player = (Player) sender; + return true; + } + + sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); + return false; + } + + /** + * This method checks if the player execute the command on a valid + * game world (lobby or area). + * + * @return true, if it's a MissileWars game world + */ + private boolean isValidGame() { + game = GameManager.getInstance().getGame(player.getLocation()); + + if (game != null) return true; + + player.sendMessage(Messages.getMessage("not_in_arena")); + return false; + } + +} diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java index 25c54fcb..1aea8468 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java @@ -58,7 +58,7 @@ public class StatsCommands extends BaseCommand { @CommandPermission("mw.stats") public void onStats(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; + if (!MWCommands.senderIsPlayer(sender)) return; Player player = (Player) sender; StatsFetcher fetcher = getFetcher(player, args); @@ -111,7 +111,7 @@ public void onStats(CommandSender sender, String[] args) { @CommandPermission("mw.stats.recommendations") public void onRecommendations(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; + if (!MWCommands.senderIsPlayer(sender)) return; Player player = (Player) sender; StatsFetcher fetcher = getFetcher(player, args); @@ -151,7 +151,7 @@ public void onRecommendations(CommandSender sender, String[] args) { @CommandPermission("mw.stats.players") public void onPlayers(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; + if (!MWCommands.senderIsPlayer(sender)) return; Player player = (Player) sender; StatsFetcher fetcher = getFetcher(player, args); @@ -169,7 +169,7 @@ public void onPlayers(CommandSender sender, String[] args) { @CommandPermission("mw.stats.list") public void onList(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; + if (!MWCommands.senderIsPlayer(sender)) return; Player player = (Player) sender; StatsFetcher fetcher = getFetcher(player, args); @@ -213,11 +213,4 @@ private StatsFetcher getFetcher(Player player, String[] args) { player.sendMessage(Messages.getPrefix() + "Loading data..."); return fetcher; } - - private boolean senderIsPlayer(CommandSender sender) { - if (sender instanceof Player) return true; - - sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); - return false; - } } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java index ea4fa772..5a91f744 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java @@ -20,7 +20,7 @@ import co.aikar.commands.BaseCommand; import co.aikar.commands.annotation.*; -import de.butzlabben.missilewars.configuration.Config; +import de.butzlabben.missilewars.MissileWars; import de.butzlabben.missilewars.configuration.Messages; import de.butzlabben.missilewars.configuration.arena.Arena; import de.butzlabben.missilewars.game.Arenas; @@ -30,7 +30,6 @@ import de.butzlabben.missilewars.game.enums.GameState; import de.butzlabben.missilewars.game.enums.MapChooseProcedure; import de.butzlabben.missilewars.player.MWPlayer; -import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -46,9 +45,14 @@ public class UserCommands extends BaseCommand { @CommandPermission("mw.change") public void changeCommand(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; + if (!MWCommands.senderIsPlayer(sender)) return; Player player = (Player) sender; + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + Game game = GameManager.getInstance().getGame(player.getLocation()); if (game == null) { player.sendMessage(Messages.getMessage("not_in_arena")); @@ -59,11 +63,7 @@ public void changeCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cThe game is not in the right state to change your team right now"); return; } - - if (args.length != 1) { - player.sendMessage(Messages.getPrefix() + "§c/mw change <1|2>"); - return; - } + try { MWPlayer mwPlayer = game.getPlayer(player); int teamNumber = Integer.parseInt(args[0]); @@ -91,10 +91,14 @@ public void changeCommand(CommandSender sender, String[] args) { @CommandPermission("mw.vote") public void voteCommand(CommandSender sender, String[] args) { - // TODO more messageconfig - if (!senderIsPlayer(sender)) return; + if (!MWCommands.senderIsPlayer(sender)) return; Player player = (Player) sender; + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + Game game = GameManager.getInstance().getGame(player.getLocation()); if (game == null) { player.sendMessage(Messages.getMessage("not_in_arena")); @@ -115,12 +119,7 @@ public void voteCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cA map was already elected"); return; } - - if (args.length != 1) { - player.sendMessage(Messages.getPrefix() + "§c/mw vote "); - return; - } - + String arenaName = args[0]; Optional arena = Arenas.getFromName(arenaName); if (!game.getVotes().containsKey(arenaName) || arena.isEmpty()) { @@ -137,34 +136,23 @@ public void voteCommand(CommandSender sender, String[] args) { @Syntax("/mw quit") @CommandCompletion("@nothing") @CommandPermission("mw.quit") - public void onQuit(CommandSender sender, String[] args) { + public void quitCommand(CommandSender sender, String[] args) { - // TODO message config - if (!senderIsPlayer(sender)) return; + if (!MWCommands.senderIsPlayer(sender)) return; Player player = (Player) sender; + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + Game game = GameManager.getInstance().getGame(player.getLocation()); if (game == null) { player.sendMessage(Messages.getMessage("not_in_arena")); return; } - MWPlayer mwPlayer = game.getPlayer(player); - if (mwPlayer == null) { - player.sendMessage(Messages.getPrefix() + "§cYou are not a member in this arena. Something went wrong pretty badly :("); - return; - } - Location endSpawn = game.getLobby().getAfterGameSpawn(); - if (GameManager.getInstance().getGame(endSpawn) != null) { - endSpawn = Config.getFallbackSpawn(); - } - player.teleport(endSpawn); - player.sendMessage(Messages.getMessage("game_quit")); - } - - private boolean senderIsPlayer(CommandSender sender) { - if (sender instanceof Player) return true; - - sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); - return false; + + MissileWars.getInstance().getPlayerListener().registerPlayerArenaLeaveEvent(player, game); + game.teleportToFallbackSpawn(player); } } From 6012a97235e0c750aa2a7971e0a48a31cda1237b Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sat, 14 Jan 2023 18:46:35 +0100 Subject: [PATCH 08/13] Refactoring: removing unused annotations and reworking help command --- .../missilewars/commands/MWCommands.java | 71 +++++------- .../missilewars/commands/SetupCommands.java | 2 - .../missilewars/commands/StatsCommands.java | 13 +-- .../missilewars/commands/UserCommands.java | 101 +++++++++--------- 4 files changed, 82 insertions(+), 105 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java index b273c0f4..a7ba6f69 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java @@ -47,35 +47,29 @@ public class MWCommands extends BaseCommand { public void mwCommand(CommandSender sender) { sender.sendMessage(Messages.getPrefix() + "MissileWars v" + MissileWars.getInstance().version + " by Butzlabben"); - - if (sender.hasPermission("mw.quit")) - sender.sendMessage(Messages.getPrefix() + "/mw quit - Quit a game"); - if (sender.hasPermission("mw.start")) - sender.sendMessage(Messages.getPrefix() + "/mw start - Starts the game"); - if (sender.hasPermission("mw.stop")) - sender.sendMessage(Messages.getPrefix() + "/mw stop - Stops the game"); - if (sender.hasPermission("mw.restart")) - sender.sendMessage(Messages.getPrefix() + "/mw restart - Restarts the game"); - if (sender.hasPermission("mw.appendrestart")) - sender.sendMessage(Messages.getPrefix() - + "/mw appendrestart - Appends a restart after the next game ends"); - if (sender.hasPermission("mw.paste")) - sender.sendMessage(Messages.getPrefix() + "/mw paste - Pastes a missile"); - if (sender.hasPermission("mw.reload")) - sender.sendMessage(Messages.getPrefix() + "/mw reload - Reloads configurations"); - if (sender.hasPermission("mw.stats")) - sender.sendMessage(Messages.getPrefix() + "/mw stats - Shows stats"); - if (sender.hasPermission("mw.stats.recommendations")) - sender.sendMessage(Messages.getPrefix() + "/mw stats recommendations - Shows recommendations"); - if (sender.hasPermission("mw.stats.players")) - sender.sendMessage(Messages.getPrefix() + "/mw stats players - Shows player list"); - if (sender.hasPermission("mw.stats.list")) - sender.sendMessage(Messages.getPrefix() + "/mw stats list - Lists history of games"); + + sendHelpMessage(sender, "mw.vote", "/mw vote", "Vote for a arena."); + sendHelpMessage(sender, "mw.change", "/mw change <1|2>", "Changes your team."); + sendHelpMessage(sender, "mw.quit", "/mw quit", "Quit a game."); + + sendHelpMessage(sender, "mw.stats", "/mw stats [from] [arena]", "Shows stats."); + sendHelpMessage(sender, "mw.stats.recommendations", "/mw stats recommendations [from] [arena]", "Shows recommendations."); + sendHelpMessage(sender, "mw.stats.players", "/mw stats players [from] [arena]", "Shows player list."); + sendHelpMessage(sender, "mw.stats.list", "/mw stats list [from] [arena]", "Lists history of games."); + + sendHelpMessage(sender, "mw.listgames", "/mw listgames", "List the active games."); + sendHelpMessage(sender, "mw.paste", "/mw paste ", "Pastes a missile."); + sendHelpMessage(sender, "mw.start", "/mw start", "Starts the game."); + sendHelpMessage(sender, "mw.stop", "/mw stop [lobby]", "Stops the game."); + sendHelpMessage(sender, "mw.appendrestart", "/mw appendrestart", "Appends a restart after the next game ends."); + sendHelpMessage(sender, "mw.reload", "/mw reload", "Reload the plugin."); + sendHelpMessage(sender, "mw.debug", "/mw debug", "Show debug info."); + sendHelpMessage(sender, "mw.restartall", "/mw restartall", "Restart all games."); + + sendHelpMessage(sender, "mw.setup", "/mw setup ", "Setup the game lobby and game area."); } - + @Subcommand("listgames|list|games") - @Description("List the active games.") - @Syntax("/mw listgames") @CommandCompletion("@nothing") @CommandPermission("mw.listgames") public void listgamesCommand(CommandSender sender, String[] args) { @@ -96,8 +90,6 @@ public void listgamesCommand(CommandSender sender, String[] args) { } @Subcommand("paste") - @Description("Pastes a missile.") - @Syntax("/mw paste ") @CommandCompletion("@nothing") @CommandPermission("mw.paste") public void pasteCommand(CommandSender sender, String[] args) { @@ -132,8 +124,6 @@ public void pasteCommand(CommandSender sender, String[] args) { } @Subcommand("start") - @Description("Starts the game.") - @Syntax("/mw start") @CommandCompletion("@nothing") @CommandPermission("mw.start") public void startCommand(CommandSender sender, String[] args) { @@ -181,8 +171,6 @@ public void startCommand(CommandSender sender, String[] args) { } @Subcommand("stop") - @Description("Stops the game.") - @Syntax("/mw stop [lobby]") @CommandCompletion("@nothing") @CommandPermission("mw.stop") public void stopCommand(CommandSender sender, String[] args) { @@ -216,8 +204,6 @@ public void stopCommand(CommandSender sender, String[] args) { } @Subcommand("appendrestart") - @Description("Appends a restart after the next game ends.") - @Syntax("/mw appendrestart") @CommandCompletion("@nothing") @CommandPermission("mw.appendrestart") public void appendrestartCommand(CommandSender sender, String[] args) { @@ -241,8 +227,6 @@ public void appendrestartCommand(CommandSender sender, String[] args) { } @Subcommand("reload") - @Description("Reload the plugin.") - @Syntax("/mw reload") @CommandCompletion("@nothing") @CommandPermission("mw.reload") public void reloadCommand(CommandSender sender, String[] args) { @@ -262,8 +246,6 @@ public void reloadCommand(CommandSender sender, String[] args) { } @Subcommand("debug") - @Description("Show debug info.") - @Syntax("/mw debug") @CommandCompletion("@nothing") @CommandPermission("mw.debug") public void debugCommand(CommandSender sender, String[] args) { @@ -286,10 +268,8 @@ public void debugCommand(CommandSender sender, String[] args) { } @Subcommand("restartall") - @Description("Restart all games.") - @Syntax("/mw restartall") @CommandCompletion("@nothing") - @CommandPermission("mw.reload") + @CommandPermission("mw.restartall") public void restartallCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; @@ -317,4 +297,11 @@ static boolean senderIsPlayer(CommandSender sender) { sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); return false; } + + static void sendHelpMessage(CommandSender sender, String permission, String command, String description) { + if (sender instanceof Player) { + if (!sender.hasPermission(permission)) return; + } + sender.sendMessage(Messages.getPrefix() + command + " - " + description); + } } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java index 98bfd022..6df3a11d 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java @@ -35,8 +35,6 @@ public class SetupCommands extends BaseCommand { private Player player; @Default - @Description("Setup the game lobby and game area.") - @Syntax("/mw stats ") @CommandPermission("mw.setup") public void setupCommands(CommandSender sender, String[] args) { sender.sendMessage(Messages.getPrefix() + "§fSetup usage: §7/mw stats "); diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java index 1aea8468..e2e2c64b 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java @@ -19,7 +19,10 @@ package de.butzlabben.missilewars.commands; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.*; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Default; +import co.aikar.commands.annotation.Subcommand; import de.butzlabben.missilewars.configuration.Config; import de.butzlabben.missilewars.configuration.Messages; import de.butzlabben.missilewars.game.stats.PlayerStats; @@ -53,8 +56,6 @@ public class StatsCommands extends BaseCommand { private final SimpleDateFormat preciseFormat = new SimpleDateFormat("hh:mm dd.MM.yyyy"); @Default - @Description("Shows stats.") - @Syntax("/mw stats [from] [arena]") @CommandPermission("mw.stats") public void onStats(CommandSender sender, String[] args) { @@ -106,8 +107,6 @@ public void onStats(CommandSender sender, String[] args) { } @Subcommand("recommendations") - @Description("Shows recommendations.") - @Syntax("/mw stats recommendations [from] [arena]") @CommandPermission("mw.stats.recommendations") public void onRecommendations(CommandSender sender, String[] args) { @@ -146,8 +145,6 @@ public void onRecommendations(CommandSender sender, String[] args) { } @Subcommand("players") - @Description("Shows player list.") - @Syntax("/mw stats players [from] [arena]") @CommandPermission("mw.stats.players") public void onPlayers(CommandSender sender, String[] args) { @@ -164,8 +161,6 @@ public void onPlayers(CommandSender sender, String[] args) { } @Subcommand("list") - @Description("Lists history of games.") - @Syntax("/mw stats list [from] [arena]") @CommandPermission("mw.stats.list") public void onList(CommandSender sender, String[] args) { diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java index 5a91f744..d94550cc 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java @@ -19,7 +19,10 @@ package de.butzlabben.missilewars.commands; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.*; +import co.aikar.commands.annotation.CommandAlias; +import co.aikar.commands.annotation.CommandCompletion; +import co.aikar.commands.annotation.CommandPermission; +import co.aikar.commands.annotation.Subcommand; import de.butzlabben.missilewars.MissileWars; import de.butzlabben.missilewars.configuration.Messages; import de.butzlabben.missilewars.configuration.arena.Arena; @@ -37,10 +40,52 @@ @CommandAlias("mw|missilewars") public class UserCommands extends BaseCommand { + + @Subcommand("vote") + @CommandPermission("mw.vote") + public void voteCommand(CommandSender sender, String[] args) { + + if (!MWCommands.senderIsPlayer(sender)) return; + Player player = (Player) sender; + + if (args.length > 0) { + player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); + return; + } + + Game game = GameManager.getInstance().getGame(player.getLocation()); + if (game == null) { + player.sendMessage(Messages.getMessage("not_in_arena")); + return; + } + + if (game.getState() != GameState.LOBBY) { + player.sendMessage(Messages.getPrefix() + "§cThe game is not in the right state to vote right now"); + return; + } + if (game.getLobby().getMapChooseProcedure() != MapChooseProcedure.MAPVOTING) { + player.sendMessage(Messages.getPrefix() + "§cYou can't vote in this game"); + return; + } + + if (game.getArena() != null) { + player.sendMessage(Messages.getPrefix() + "§cA map was already elected"); + return; + } + + String arenaName = args[0]; + Optional arena = Arenas.getFromName(arenaName); + if (!game.getVotes().containsKey(arenaName) || arena.isEmpty()) { + player.sendMessage(Messages.getPrefix() + "§cNo map with this title was found"); + return; + } + + game.getVotes().put(arenaName, game.getVotes().get(arenaName) + 1); + player.sendMessage(Messages.getMessage("vote.success").replace("%map%", arena.get().getDisplayName())); + } + @Subcommand("change") - @Description("Changes your team.") - @Syntax("/mw change <1|2>") @CommandCompletion("@range:1-2") @CommandPermission("mw.change") public void changeCommand(CommandSender sender, String[] args) { @@ -84,56 +129,8 @@ public void changeCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§c/mw change <1|2>"); } } - - @Subcommand("vote") - @Description("Stops the game.") - @Syntax("/mw vote ") - @CommandPermission("mw.vote") - public void voteCommand(CommandSender sender, String[] args) { - - if (!MWCommands.senderIsPlayer(sender)) return; - Player player = (Player) sender; - - if (args.length > 0) { - player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); - return; - } - - Game game = GameManager.getInstance().getGame(player.getLocation()); - if (game == null) { - player.sendMessage(Messages.getMessage("not_in_arena")); - return; - } - - if (game.getState() != GameState.LOBBY) { - player.sendMessage(Messages.getPrefix() + "§cThe game is not in the right state to vote right now"); - return; - } - - if (game.getLobby().getMapChooseProcedure() != MapChooseProcedure.MAPVOTING) { - player.sendMessage(Messages.getPrefix() + "§cYou can't vote in this game"); - return; - } - - if (game.getArena() != null) { - player.sendMessage(Messages.getPrefix() + "§cA map was already elected"); - return; - } - - String arenaName = args[0]; - Optional arena = Arenas.getFromName(arenaName); - if (!game.getVotes().containsKey(arenaName) || arena.isEmpty()) { - player.sendMessage(Messages.getPrefix() + "§cNo map with this title was found"); - return; - } - - game.getVotes().put(arenaName, game.getVotes().get(arenaName) + 1); - player.sendMessage(Messages.getMessage("vote.success").replace("%map%", arena.get().getDisplayName())); - } - + @Subcommand("quit|leave") - @Description("Quit a game.") - @Syntax("/mw quit") @CommandCompletion("@nothing") @CommandPermission("mw.quit") public void quitCommand(CommandSender sender, String[] args) { From 32b24ec66077fb8218981d85275f0173342c26a9 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sun, 15 Jan 2023 14:29:55 +0100 Subject: [PATCH 09/13] Adding method to update the arena file --- .../missilewars/configuration/arena/Arena.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/Arena.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/Arena.java index 818d0a59..7df438ee 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/Arena.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/Arena.java @@ -21,12 +21,16 @@ import com.google.gson.annotations.SerializedName; import de.butzlabben.missilewars.util.geometry.FlatArea; import de.butzlabben.missilewars.util.geometry.Plane; +import de.butzlabben.missilewars.util.serialization.Serializer; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.bukkit.Location; import org.bukkit.util.Vector; +import java.io.File; +import java.io.IOException; + @Getter @ToString public class Arena implements Cloneable { @@ -65,6 +69,8 @@ public class Arena implements Cloneable { @SerializedName("team2_spawn") @Setter private Location team2Spawn = new Location(null, 0.5, 100, -45.5, 0, 0); + + @Setter private transient File file; public Arena() { @@ -102,4 +108,12 @@ public Arena clone() { throw new AssertionError(); } } + + public void updateConfig() { + try { + Serializer.serialize(file, this); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } From 457f4dda43efd1869ad019aaadba9ebe2f25f701 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sun, 15 Jan 2023 14:31:20 +0100 Subject: [PATCH 10/13] Adding method to update the lobby file --- .../missilewars/configuration/Lobby.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java index 3fdb30ac..c378da5e 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java @@ -24,6 +24,7 @@ import de.butzlabben.missilewars.game.Arenas; import de.butzlabben.missilewars.game.enums.MapChooseProcedure; import de.butzlabben.missilewars.util.geometry.Area; +import de.butzlabben.missilewars.util.serialization.Serializer; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; @@ -33,6 +34,7 @@ import org.bukkit.World; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -91,4 +93,20 @@ public List getArenas() { .map(Optional::get) .collect(Collectors.toList()); } + + public void updateConfig() { + try { + Serializer.serialize(file, this); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public Location getAreaMinLocation() { + return new Location(getBukkitWorld(), area.getMinX(), area.getMinY(), area.getMinZ()); + } + + public Location getAreaMaxLocation() { + return new Location(getBukkitWorld(), area.getMaxX(), area.getMaxY(), area.getMaxZ()); + } } From 018652c7f46997851fe73d03aa0ee92ede3fb919 Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sun, 15 Jan 2023 14:33:21 +0100 Subject: [PATCH 11/13] Reworking commands --- .../butzlabben/missilewars/MissileWars.java | 11 +- .../missilewars/commands/MWCommands.java | 65 +++++---- .../missilewars/commands/SetupCommands.java | 126 +++++++----------- .../missilewars/commands/StatsCommands.java | 8 +- .../missilewars/commands/UserCommands.java | 1 + .../butzlabben/missilewars/game/Arenas.java | 1 + .../missilewars/game/GameManager.java | 10 +- 7 files changed, 111 insertions(+), 111 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java index 945e9a77..5eb366f6 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java @@ -58,6 +58,7 @@ public class MissileWars extends JavaPlugin { private static MissileWars instance; public final String version = getDescription().getVersion(); private SignRepository signRepository; + public PaperCommandManager commandManager; private boolean foundFAWE; @@ -163,12 +164,12 @@ private void registerCommands() { // Using the Paper Command Manager does not mean the plugin requires Paper. // It simply lets it take advantage of Paper specific features if available, // such as Asynchronous Tab Completions. - PaperCommandManager manager = new PaperCommandManager(this); + commandManager = new PaperCommandManager(this); - manager.registerCommand(new MWCommands()); - manager.registerCommand(new StatsCommands()); - manager.registerCommand(new UserCommands()); - manager.registerCommand(new SetupCommands()); + commandManager.registerCommand(new MWCommands()); + commandManager.registerCommand(new StatsCommands()); + commandManager.registerCommand(new UserCommands()); + commandManager.registerCommand(new SetupCommands()); } /** diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java index a7ba6f69..27b8b2d5 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java @@ -59,14 +59,14 @@ public void mwCommand(CommandSender sender) { sendHelpMessage(sender, "mw.listgames", "/mw listgames", "List the active games."); sendHelpMessage(sender, "mw.paste", "/mw paste ", "Pastes a missile."); - sendHelpMessage(sender, "mw.start", "/mw start", "Starts the game."); + sendHelpMessage(sender, "mw.start", "/mw start [lobby]", "Starts the game."); sendHelpMessage(sender, "mw.stop", "/mw stop [lobby]", "Stops the game."); - sendHelpMessage(sender, "mw.appendrestart", "/mw appendrestart", "Appends a restart after the next game ends."); + sendHelpMessage(sender, "mw.appendrestart", "/mw appendrestart [lobby]", "Appends a restart after the next game ends."); sendHelpMessage(sender, "mw.reload", "/mw reload", "Reload the plugin."); sendHelpMessage(sender, "mw.debug", "/mw debug", "Show debug info."); sendHelpMessage(sender, "mw.restartall", "/mw restartall", "Restart all games."); - sendHelpMessage(sender, "mw.setup", "/mw setup ", "Setup the game lobby and game area."); + sendHelpMessage(sender, "mw.setup", "/mw setup [lobby]", "Setup the MW Locations or the lobby/arena locations."); } @Subcommand("listgames|list|games") @@ -124,22 +124,32 @@ public void pasteCommand(CommandSender sender, String[] args) { } @Subcommand("start") - @CommandCompletion("@nothing") + @CommandCompletion("@games") @CommandPermission("mw.start") public void startCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; Player player = (Player) sender; - if (args.length > 0) { + if (args.length > 1) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - Game game = GameManager.getInstance().getGame(player.getLocation()); - if (game == null) { - player.sendMessage(Messages.getMessage("not_in_arena")); - return; + // Check optional game argument: + Game game; + if (args.length == 1) { + game = GameManager.getInstance().getGame(args[0]); + if (game == null) { + player.sendMessage(Messages.getPrefix() + "§cGame not found."); + return; + } + } else { + game = GameManager.getInstance().getGame(player.getLocation()); + if (game == null) { + player.sendMessage(Messages.getMessage("not_in_arena")); + return; + } } if (game.getState() != GameState.LOBBY) { @@ -171,7 +181,7 @@ public void startCommand(CommandSender sender, String[] args) { } @Subcommand("stop") - @CommandCompletion("@nothing") + @CommandCompletion("@games") @CommandPermission("mw.stop") public void stopCommand(CommandSender sender, String[] args) { @@ -182,7 +192,8 @@ public void stopCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - + + // Check optional game argument: Game game; if (args.length == 1) { game = GameManager.getInstance().getGame(args[0]); @@ -204,22 +215,32 @@ public void stopCommand(CommandSender sender, String[] args) { } @Subcommand("appendrestart") - @CommandCompletion("@nothing") + @CommandCompletion("@games") @CommandPermission("mw.appendrestart") public void appendrestartCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; Player player = (Player) sender; - if (args.length > 0) { + if (args.length > 1) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - Game game = GameManager.getInstance().getGame(player.getLocation()); - if (game == null) { - player.sendMessage(Messages.getMessage("not_in_arena")); - return; + // Check optional game argument: + Game game; + if (args.length == 1) { + game = GameManager.getInstance().getGame(args[0]); + if (game == null) { + player.sendMessage(Messages.getPrefix() + "§cGame not found."); + return; + } + } else { + game = GameManager.getInstance().getGame(player.getLocation()); + if (game == null) { + player.sendMessage(Messages.getMessage("not_in_arena")); + return; + } } GameManager.getInstance().getGames().values().forEach(Game::appendRestart); @@ -279,16 +300,10 @@ public void restartallCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - - Game game = GameManager.getInstance().getGame(player.getLocation()); - if (game == null) { - player.sendMessage(Messages.getMessage("not_in_arena")); - return; - } - + sender.sendMessage(Messages.getPrefix() + "§cWarning - Restarting all games. This may take a while"); GameManager.getInstance().restartAll(); - sender.sendMessage(Messages.getPrefix() + "Reloaded configs"); + sender.sendMessage(Messages.getPrefix() + "Restarted all games."); } static boolean senderIsPlayer(CommandSender sender) { diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java index 6df3a11d..8cc0155a 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java @@ -37,17 +37,17 @@ public class SetupCommands extends BaseCommand { @Default @CommandPermission("mw.setup") public void setupCommands(CommandSender sender, String[] args) { - sender.sendMessage(Messages.getPrefix() + "§fSetup usage: §7/mw stats "); + sender.sendMessage(Messages.getPrefix() + "§fSetup usage: §7/mw setup [lobby]"); } @Subcommand("main") - @Description("Main Setup") public class mainSetupCommands extends BaseCommand { @Subcommand("fallbackspawn") public class fallbackspawnSetup extends BaseCommand { @Subcommand("set") + @CommandCompletion("@nothing") public void set(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; @@ -56,6 +56,7 @@ public void set(CommandSender sender, String[] args) { } @Subcommand("teleport|tp") + @CommandCompletion("@nothing") public void teleport(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; @@ -67,126 +68,83 @@ public void teleport(CommandSender sender, String[] args) { } @Subcommand("lobby") - @Description("Setup the lobby") public class lobbySetupCommands extends BaseCommand { @Subcommand("spawnpoint") public class spawnpointSetup extends BaseCommand { - + @Subcommand("set") + @CommandCompletion("@games") public void set(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; game.getLobby().setSpawnPoint(player.getLocation()); + game.getLobby().updateConfig(); player.sendMessage(Messages.getPrefix() + "§fSet new 'spawnPoint' to " + player.getLocation() + "."); } @Subcommand("teleport|tp") + @CommandCompletion("@games") public void teleport(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; player.teleport(game.getLobby().getSpawnPoint()); player.sendMessage(Messages.getPrefix() + "§fTeleported to 'spawnPoint'."); } - + } @Subcommand("aftergamespawn") public class aftergamespawnSetup extends BaseCommand { @Subcommand("set") + @CommandCompletion("@games") public void set(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; game.getLobby().setAfterGameSpawn(player.getLocation()); + game.getLobby().updateConfig(); player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); } @Subcommand("teleport|tp") + @CommandCompletion("@games") public void teleport(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; player.teleport(game.getLobby().getAfterGameSpawn()); player.sendMessage(Messages.getPrefix() + "§fTeleported to 'afterGameSpawn'."); } } - - @Subcommand("area") - public class areaSetup extends BaseCommand { - - @Subcommand("set") - public class set extends BaseCommand { - - @Subcommand("pos1") - public void pos1(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; - - game.getLobby().setAfterGameSpawn(player.getLocation()); - player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); - } - - @Subcommand("pos2") - public void pos2(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; - - game.getLobby().setAfterGameSpawn(player.getLocation()); - player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); - } - - } - - @Subcommand("teleport|tp") - public class teleport extends BaseCommand { - - @Subcommand("pos1") - public void pos1(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; - - game.getLobby().setAfterGameSpawn(player.getLocation()); - player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); - } - - @Subcommand("pos2") - public void pos2(CommandSender sender, String[] args) { - if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; - - game.getLobby().setAfterGameSpawn(player.getLocation()); - player.sendMessage(Messages.getPrefix() + "§fSet new 'afterGameSpawn' to " + player.getLocation() + "."); - } - - } - } } - + @Subcommand("arena") - @Description("Setup the arena") public class arenaSetupCommands extends BaseCommand { @Subcommand("spectatorspawn") public class spectatorspawnSetup extends BaseCommand { @Subcommand("set") + @CommandCompletion("@games") public void set(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; game.getArena().setSpectatorSpawn(player.getLocation()); + game.getArena().updateConfig(); player.sendMessage(Messages.getPrefix() + "§fSet new 'spectatorSpawn' to " + player.getLocation() + "."); } @Subcommand("teleport|tp") + @CommandCompletion("@games") public void teleport(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; player.teleport(game.getArena().getSpectatorSpawn()); player.sendMessage(Messages.getPrefix() + "§fTeleported to 'spectatorSpawn'."); @@ -198,18 +156,21 @@ public void teleport(CommandSender sender, String[] args) { public class team1spawnSetup extends BaseCommand { @Subcommand("set") + @CommandCompletion("@games") public void set(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; game.getArena().setTeam1Spawn(player.getLocation()); + game.getArena().updateConfig(); player.sendMessage(Messages.getPrefix() + "§fSet new 'team1Spawn' to " + player.getLocation() + "."); } @Subcommand("teleport|tp") + @CommandCompletion("@games") public void teleport(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; player.teleport(game.getArena().getTeam1Spawn()); player.sendMessage(Messages.getPrefix() + "§fTeleported to 'team1Spawn'."); @@ -221,18 +182,21 @@ public void teleport(CommandSender sender, String[] args) { public class team2spawnSetup extends BaseCommand { @Subcommand("set") + @CommandCompletion("@games") public void set(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; game.getArena().setTeam2Spawn(player.getLocation()); + game.getArena().updateConfig(); player.sendMessage(Messages.getPrefix() + "§fSet new 'team2Spawn' to " + player.getLocation() + "."); } @Subcommand("teleport|tp") + @CommandCompletion("@games") public void teleport(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - if (!isValidGame()) return; + if (!isValidGame(args)) return; player.teleport(game.getArena().getTeam2Spawn()); player.sendMessage(Messages.getPrefix() + "§fTeleported to 'team2Spawn'."); @@ -256,20 +220,30 @@ private boolean senderIsPlayer(CommandSender sender) { sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); return false; } - + /** * This method checks if the player execute the command on a valid * game world (lobby or area). * * @return true, if it's a MissileWars game world */ - private boolean isValidGame() { - game = GameManager.getInstance().getGame(player.getLocation()); - - if (game != null) return true; - - player.sendMessage(Messages.getMessage("not_in_arena")); - return false; + private boolean isValidGame(String[] args) { + + // Check optional game argument: + if (args.length == 1) { + game = GameManager.getInstance().getGame(args[0]); + if (game == null) { + player.sendMessage(Messages.getPrefix() + "§cGame not found."); + return false; + } + } else { + game = GameManager.getInstance().getGame(player.getLocation()); + if (game == null) { + player.sendMessage(Messages.getMessage("not_in_arena")); + return false; + } + } + + return true; } - } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java index e2e2c64b..05ddea48 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/StatsCommands.java @@ -19,10 +19,7 @@ package de.butzlabben.missilewars.commands; import co.aikar.commands.BaseCommand; -import co.aikar.commands.annotation.CommandAlias; -import co.aikar.commands.annotation.CommandPermission; -import co.aikar.commands.annotation.Default; -import co.aikar.commands.annotation.Subcommand; +import co.aikar.commands.annotation.*; import de.butzlabben.missilewars.configuration.Config; import de.butzlabben.missilewars.configuration.Messages; import de.butzlabben.missilewars.game.stats.PlayerStats; @@ -107,6 +104,7 @@ public void onStats(CommandSender sender, String[] args) { } @Subcommand("recommendations") + @CommandCompletion("@nothing") @CommandPermission("mw.stats.recommendations") public void onRecommendations(CommandSender sender, String[] args) { @@ -145,6 +143,7 @@ public void onRecommendations(CommandSender sender, String[] args) { } @Subcommand("players") + @CommandCompletion("@nothing") @CommandPermission("mw.stats.players") public void onPlayers(CommandSender sender, String[] args) { @@ -161,6 +160,7 @@ public void onPlayers(CommandSender sender, String[] args) { } @Subcommand("list") + @CommandCompletion("@nothing") @CommandPermission("mw.stats.list") public void onList(CommandSender sender, String[] args) { diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java index d94550cc..dc64cfa9 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java @@ -42,6 +42,7 @@ public class UserCommands extends BaseCommand { @Subcommand("vote") + @CommandCompletion("@nothing") @CommandPermission("mw.vote") public void voteCommand(CommandSender sender, String[] args) { diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java index e6a9577c..d70512a8 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java @@ -66,6 +66,7 @@ public static void load() { if (!config.getName().endsWith(".yml") && !config.getName().endsWith(".yaml")) continue; try { Arena arena = Serializer.deserialize(config, Arena.class); + arena.setFile(config); if (getFromName(arena.getName()).isPresent()) { Logger.WARN.log("There are several arenas configured with the name \"" + arena.getName() + "\". Arenas must have a unique name"); continue; diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java index f6b72da0..85c4b3c3 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java @@ -29,7 +29,9 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; @Getter public class GameManager { @@ -150,7 +152,7 @@ public void restartGame(Lobby targetLobby, boolean forceStart) { lobby.setFile(targetLobby.getFile()); Logger.BOOTDONE.log("Reloaded lobby \"" + targetLobbyName + "\" (" + targetLobby.getFile().getName() + ")"); - games.put(targetLobbyName, new Game(lobby)); + addGame(targetLobbyName, new Game(lobby)); } catch (IOException exception) { Logger.ERROR.log("Could not load lobby of \"" + targetLobby.getFile().getName() + "\""); @@ -161,6 +163,12 @@ public void restartGame(Lobby targetLobby, boolean forceStart) { public Game getGame(String name) { return games.get(name); } + + public void addGame(String name, Game game) { + games.put(name, game); + List gameNames = new ArrayList<>(games.keySet()); + MissileWars.getInstance().commandManager.getCommandCompletions().registerCompletion("games", c -> gameNames); + } public int getGameAmount() { return games.size(); From dfc4f6c207ccd5ff98c905e0423e35281b94007b Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sun, 15 Jan 2023 14:44:50 +0100 Subject: [PATCH 12/13] Small typo --- .../butzlabben/missilewars/MissileWars.java | 6 +-- .../missilewars/commands/MWCommands.java | 44 +++++++++---------- .../missilewars/commands/SetupCommands.java | 18 ++++---- .../missilewars/commands/UserCommands.java | 10 ++--- .../missilewars/configuration/Lobby.java | 4 +- .../butzlabben/missilewars/game/Arenas.java | 2 +- .../de/butzlabben/missilewars/game/Game.java | 8 ++-- .../missilewars/game/GameManager.java | 26 +++++------ 8 files changed, 59 insertions(+), 59 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java index 5eb366f6..ed734702 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java @@ -61,7 +61,7 @@ public class MissileWars extends JavaPlugin { public PaperCommandManager commandManager; private boolean foundFAWE; - + private PlayerListener playerListener; private SignListener signListener; @@ -165,7 +165,7 @@ private void registerCommands() { // It simply lets it take advantage of Paper specific features if available, // such as Asynchronous Tab Completions. commandManager = new PaperCommandManager(this); - + commandManager.registerCommand(new MWCommands()); commandManager.registerCommand(new StatsCommands()); commandManager.registerCommand(new UserCommands()); @@ -187,7 +187,7 @@ public boolean foundFAWE() { private void deleteTempWorlds() { File[] dirs = Bukkit.getWorldContainer().listFiles(); if (dirs == null) return; - + for (File dir : dirs) { if (dir.getName().startsWith("mw-")) { try { diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java index 27b8b2d5..f54f779d 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java @@ -47,16 +47,16 @@ public class MWCommands extends BaseCommand { public void mwCommand(CommandSender sender) { sender.sendMessage(Messages.getPrefix() + "MissileWars v" + MissileWars.getInstance().version + " by Butzlabben"); - + sendHelpMessage(sender, "mw.vote", "/mw vote", "Vote for a arena."); sendHelpMessage(sender, "mw.change", "/mw change <1|2>", "Changes your team."); sendHelpMessage(sender, "mw.quit", "/mw quit", "Quit a game."); - + sendHelpMessage(sender, "mw.stats", "/mw stats [from] [arena]", "Shows stats."); sendHelpMessage(sender, "mw.stats.recommendations", "/mw stats recommendations [from] [arena]", "Shows recommendations."); sendHelpMessage(sender, "mw.stats.players", "/mw stats players [from] [arena]", "Shows player list."); sendHelpMessage(sender, "mw.stats.list", "/mw stats list [from] [arena]", "Lists history of games."); - + sendHelpMessage(sender, "mw.listgames", "/mw listgames", "List the active games."); sendHelpMessage(sender, "mw.paste", "/mw paste ", "Pastes a missile."); sendHelpMessage(sender, "mw.start", "/mw start [lobby]", "Starts the game."); @@ -73,22 +73,22 @@ public void mwCommand(CommandSender sender) { @CommandCompletion("@nothing") @CommandPermission("mw.listgames") public void listgamesCommand(CommandSender sender, String[] args) { - + sender.sendMessage(Messages.getPrefix() + "Current games:"); - + for (Game game : GameManager.getInstance().getGames().values()) { sender.sendMessage("§e " + game.getLobby().getName() + "§7 -- Name: »" + game.getLobby().getDisplayName() + "§7« | Status: " + game.getState()); sender.sendMessage("§8 - §f" + "Load with startup: §7" + game.getLobby().isAutoLoad()); sender.sendMessage("§8 - §f" + "Current Arena: §7" + game.getArena().getName() + "§7 -- Name: »" + game.getArena().getDisplayName() + "§7«"); sender.sendMessage("§8 - §f" + "Total players: §7" + game.getPlayers().size() + "x"); - sender.sendMessage("§8 - §f" + "Team 1: §7" + game.getTeam1().getColor() + game.getTeam1().getName() + sender.sendMessage("§8 - §f" + "Team 1: §7" + game.getTeam1().getColor() + game.getTeam1().getName() + " §7with " + game.getTeam1().getMembers().size() + " players"); sender.sendMessage("§8 - §f" + "Team 2: §7" + game.getTeam2().getColor() + game.getTeam2().getName() + " §7with " + game.getTeam2().getMembers().size() + " players"); } - + } - + @Subcommand("paste") @CommandCompletion("@nothing") @CommandPermission("mw.paste") @@ -96,12 +96,12 @@ public void pasteCommand(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; Player player = (Player) sender; - + if (args.length < 1) { player.sendMessage(Messages.getPrefix() + "§cMissile needed."); return; } - + if (args.length > 1) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; @@ -112,13 +112,13 @@ public void pasteCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getMessage("not_in_arena")); return; } - + Missile missile = game.getArena().getMissileConfiguration().getMissileFromName(args[0]); if (missile == null) { player.sendMessage(Messages.getPrefix() + "§cUnknown missile."); return; } - + MissileFacing missileFacing = MissileFacing.getFacingPlayer(player, game.getArena().getMissileConfiguration()); missile.paste(player, missileFacing, game); } @@ -151,12 +151,12 @@ public void startCommand(CommandSender sender, String[] args) { return; } } - + if (game.getState() != GameState.LOBBY) { player.sendMessage(Messages.getPrefix() + "§cGame already started"); return; } - + if (game.isReady()) game.startGame(); else { @@ -208,12 +208,12 @@ public void stopCommand(CommandSender sender, String[] args) { return; } } - + game.getTeam1().setGameResult(GameResult.DRAW); game.getTeam2().setGameResult(GameResult.DRAW); if (game.getState() == GameState.INGAME) game.stopGame(); } - + @Subcommand("appendrestart") @CommandCompletion("@games") @CommandPermission("mw.appendrestart") @@ -242,7 +242,7 @@ public void appendrestartCommand(CommandSender sender, String[] args) { return; } } - + GameManager.getInstance().getGames().values().forEach(Game::appendRestart); sender.sendMessage(Messages.getMessage("restart_after_game")); } @@ -259,7 +259,7 @@ public void reloadCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - + Config.load(); Messages.load(); Arenas.load(); @@ -278,7 +278,7 @@ public void debugCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - + int i = 0; Logger.NORMAL.log("Starting to print debug information for MissileWars v" + MissileWars.getInstance().version); for (Game game : GameManager.getInstance().getGames().values()) { @@ -300,19 +300,19 @@ public void restartallCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cToo many arguments."); return; } - + sender.sendMessage(Messages.getPrefix() + "§cWarning - Restarting all games. This may take a while"); GameManager.getInstance().restartAll(); sender.sendMessage(Messages.getPrefix() + "Restarted all games."); } - + static boolean senderIsPlayer(CommandSender sender) { if (sender instanceof Player) return true; sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); return false; } - + static void sendHelpMessage(CommandSender sender, String permission, String command, String description) { if (sender instanceof Player) { if (!sender.hasPermission(permission)) return; diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java index 8cc0155a..1bc3a2b3 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/SetupCommands.java @@ -50,7 +50,7 @@ public class fallbackspawnSetup extends BaseCommand { @CommandCompletion("@nothing") public void set(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - + Config.setFallbackSpawn(player.getLocation()); player.sendMessage(Messages.getPrefix() + "§fSet new 'fallbackSpawn' to " + player.getLocation() + "."); } @@ -59,14 +59,14 @@ public void set(CommandSender sender, String[] args) { @CommandCompletion("@nothing") public void teleport(CommandSender sender, String[] args) { if (!senderIsPlayer(sender)) return; - + player.teleport(Config.getFallbackSpawn()); player.sendMessage(Messages.getPrefix() + "§fTeleported to 'fallbackSpawn'."); } } } - + @Subcommand("lobby") public class lobbySetupCommands extends BaseCommand { @@ -122,7 +122,7 @@ public void teleport(CommandSender sender, String[] args) { } } - + @Subcommand("arena") public class arenaSetupCommands extends BaseCommand { @@ -204,7 +204,7 @@ public void teleport(CommandSender sender, String[] args) { } } - + /** * This method checks if the command sender is a valid ingame player. * @@ -216,11 +216,11 @@ private boolean senderIsPlayer(CommandSender sender) { player = (Player) sender; return true; } - + sender.sendMessage(Messages.getPrefix() + "§cYou are not a player"); return false; } - + /** * This method checks if the player execute the command on a valid * game world (lobby or area). @@ -228,7 +228,7 @@ private boolean senderIsPlayer(CommandSender sender) { * @return true, if it's a MissileWars game world */ private boolean isValidGame(String[] args) { - + // Check optional game argument: if (args.length == 1) { game = GameManager.getInstance().getGame(args[0]); @@ -243,7 +243,7 @@ private boolean isValidGame(String[] args) { return false; } } - + return true; } } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java index dc64cfa9..7ea70243 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java @@ -40,7 +40,7 @@ @CommandAlias("mw|missilewars") public class UserCommands extends BaseCommand { - + @Subcommand("vote") @CommandCompletion("@nothing") @CommandPermission("mw.vote") @@ -85,7 +85,7 @@ public void voteCommand(CommandSender sender, String[] args) { game.getVotes().put(arenaName, game.getVotes().get(arenaName) + 1); player.sendMessage(Messages.getMessage("vote.success").replace("%map%", arena.get().getDisplayName())); } - + @Subcommand("change") @CommandCompletion("@range:1-2") @CommandPermission("mw.change") @@ -109,7 +109,7 @@ public void changeCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§cThe game is not in the right state to change your team right now"); return; } - + try { MWPlayer mwPlayer = game.getPlayer(player); int teamNumber = Integer.parseInt(args[0]); @@ -130,7 +130,7 @@ public void changeCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getPrefix() + "§c/mw change <1|2>"); } } - + @Subcommand("quit|leave") @CommandCompletion("@nothing") @CommandPermission("mw.quit") @@ -149,7 +149,7 @@ public void quitCommand(CommandSender sender, String[] args) { player.sendMessage(Messages.getMessage("not_in_arena")); return; } - + MissileWars.getInstance().getPlayerListener().registerPlayerArenaLeaveEvent(player, game); game.teleportToFallbackSpawn(player); } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java index c378da5e..b5d05c9b 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/Lobby.java @@ -93,7 +93,7 @@ public List getArenas() { .map(Optional::get) .collect(Collectors.toList()); } - + public void updateConfig() { try { Serializer.serialize(file, this); @@ -101,7 +101,7 @@ public void updateConfig() { throw new RuntimeException(e); } } - + public Location getAreaMinLocation() { return new Location(getBukkitWorld(), area.getMinX(), area.getMinY(), area.getMinZ()); } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java index d70512a8..535b410d 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java @@ -59,7 +59,7 @@ public static void load() { Bukkit.getPluginManager().disablePlugin(MissileWars.getInstance()); return; } - files = new File[] {defaultArena}; + files = new File[]{defaultArena}; } for (File config : files) { diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java index d3780131..08089160 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Game.java @@ -242,7 +242,7 @@ private void stopTimer() { public void stopGame() { if (Config.isSetup()) return; - + Logger.DEBUG.log("Stopping"); for (BukkitTask bt : playerTasks.values()) { @@ -277,7 +277,7 @@ public void stopGame() { public void reset() { if (Config.isSetup()) return; - + if (restart) { Bukkit.getServer().spigot().restart(); return; @@ -291,11 +291,11 @@ public void appendRestart() { } public void disableGameOnServerStop() { - + for (MWPlayer mwPlayer : players.values()) { teleportToFallbackSpawn(mwPlayer.getPlayer()); } - + gameWorld.unload(); } diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java index 85c4b3c3..47b3790b 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java @@ -45,7 +45,7 @@ public void disableAll() { games.values().forEach(Game::disableGameOnServerStop); games.clear(); } - + public void restartAll() { games.values().forEach(game -> restartGame(game.getLobby(), false)); } @@ -61,7 +61,7 @@ public void loadGamesOnStartup() { } else { File file = new File(Config.getLobbiesFolder() + "/" + Config.getDefaultLobby()); if (file.exists()) { - lobbyFiles = new File[] {file}; + lobbyFiles = new File[]{file}; } } if (lobbyFiles == null) lobbyFiles = new File[0]; @@ -79,7 +79,7 @@ public void loadGamesOnStartup() { Bukkit.getPluginManager().disablePlugin(MissileWars.getInstance()); return; } - lobbyFiles = new File[] {file}; + lobbyFiles = new File[]{file}; } for (File lobbyFile : lobbyFiles) { @@ -98,23 +98,23 @@ public void loadGamesOnStartup() { */ private void debugStart(File lobbyFile) { Logger.BOOT.log("Try to loading lobby of \"" + lobbyFile.getName() + "\""); - + try { Lobby lobby = Serializer.deserialize(lobbyFile, Lobby.class); - + if (lobby == null) { Logger.ERROR.log("Could not load lobby of \"" + lobbyFile.getName() + "\""); return; } - + if (getGame(lobby.getName()) != null) { Logger.ERROR.log("A lobby with the same name was already loaded. Names of lobbies must be unique, this lobby will not be loaded"); return; } - + lobby.setFile(lobbyFile); restartGame(lobby, false); - + } catch (IOException exception) { Logger.ERROR.log("Could not load lobby of \"" + lobbyFile.getName() + "\""); exception.printStackTrace(); @@ -132,7 +132,7 @@ public void restartGame(Lobby targetLobby, boolean forceStart) { if (!targetLobby.isAutoLoad() && !forceStart) return; String targetLobbyName = targetLobby.getName(); - + // reset the old game Game game = getGame(targetLobbyName); if (game != null) { @@ -145,15 +145,15 @@ public void restartGame(Lobby targetLobby, boolean forceStart) { } Logger.DEBUG.log("Old Game disabled."); - + // read the game lobby configuration and build a new game and lobby from it try { Lobby lobby = Serializer.deserialize(targetLobby.getFile(), Lobby.class); lobby.setFile(targetLobby.getFile()); - + Logger.BOOTDONE.log("Reloaded lobby \"" + targetLobbyName + "\" (" + targetLobby.getFile().getName() + ")"); addGame(targetLobbyName, new Game(lobby)); - + } catch (IOException exception) { Logger.ERROR.log("Could not load lobby of \"" + targetLobby.getFile().getName() + "\""); exception.printStackTrace(); @@ -163,7 +163,7 @@ public void restartGame(Lobby targetLobby, boolean forceStart) { public Game getGame(String name) { return games.get(name); } - + public void addGame(String name, Game game) { games.put(name, game); List gameNames = new ArrayList<>(games.keySet()); From 710f50d68182c09add2f0a2039ba220f8719925f Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sun, 15 Jan 2023 17:11:39 +0100 Subject: [PATCH 13/13] Refactoring, re-adding config update of lobby after loading --- .../src/main/java/de/butzlabben/missilewars/game/Arenas.java | 3 +-- .../main/java/de/butzlabben/missilewars/game/GameManager.java | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java index 535b410d..b88732db 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java @@ -72,8 +72,7 @@ public static void load() { continue; } SetupUtil.checkMap(arena.getTemplateWorld()); - // Save for possible new values - Serializer.serialize(config, arena); + arena.updateConfig(); arenas.add(arena); } catch (IOException exception) { Logger.ERROR.log("Could not load config for arena " + config.getName()); diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java index 47b3790b..865d7cf6 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java @@ -150,6 +150,7 @@ public void restartGame(Lobby targetLobby, boolean forceStart) { try { Lobby lobby = Serializer.deserialize(targetLobby.getFile(), Lobby.class); lobby.setFile(targetLobby.getFile()); + lobby.updateConfig(); Logger.BOOTDONE.log("Reloaded lobby \"" + targetLobbyName + "\" (" + targetLobby.getFile().getName() + ")"); addGame(targetLobbyName, new Game(lobby));