Kaynağa Gözat

Reworking commands

RedstoneFuture 2 yıl önce
ebeveyn
işleme
018652c7f4

+ 6 - 5
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 @@ public class MissileWars extends JavaPlugin {
         // 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());
     }
 
     /**

+ 40 - 25
missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommands.java

@@ -59,14 +59,14 @@ public class MWCommands extends BaseCommand {
         
         sendHelpMessage(sender, "mw.listgames", "/mw listgames", "List the active games.");
         sendHelpMessage(sender, "mw.paste", "/mw paste <missile>", "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 <main|lobby|area>", "Setup the game lobby and game area.");
+        sendHelpMessage(sender, "mw.setup", "/mw setup <main|lobby|arena> <value> <set|teleport> [lobby]", "Setup the MW Locations or the lobby/arena locations.");
     }
     
     @Subcommand("listgames|list|games")
@@ -124,22 +124,32 @@ public class MWCommands extends BaseCommand {
     }
 
     @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 class MWCommands extends BaseCommand {
     }
 
     @Subcommand("stop")
-    @CommandCompletion("@nothing")
+    @CommandCompletion("@games")
     @CommandPermission("mw.stop")
     public void stopCommand(CommandSender sender, String[] args) {
 
@@ -182,7 +192,8 @@ public class MWCommands extends BaseCommand {
             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 class MWCommands extends BaseCommand {
     }
     
     @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 class MWCommands extends BaseCommand {
             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) {

+ 50 - 76
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 <main|lobby|area>");
+        sender.sendMessage(Messages.getPrefix() + "§fSetup usage: §7/mw setup <main|lobby|arena> <value> <set|teleport> [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 class SetupCommands extends BaseCommand {
             }
 
             @Subcommand("teleport|tp")
+            @CommandCompletion("@nothing")
             public void teleport(CommandSender sender, String[] args) {
                 if (!senderIsPlayer(sender)) return;
                 
@@ -67,126 +68,83 @@ public class SetupCommands extends BaseCommand {
     }
     
     @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 class SetupCommands extends BaseCommand {
         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 class SetupCommands extends BaseCommand {
         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 @@ public class SetupCommands extends BaseCommand {
         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;
     }
-    
 }

+ 4 - 4
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 class StatsCommands extends BaseCommand {
     }
 
     @Subcommand("recommendations")
+    @CommandCompletion("@nothing")
     @CommandPermission("mw.stats.recommendations")
     public void onRecommendations(CommandSender sender, String[] args) {
 
@@ -145,6 +143,7 @@ public class StatsCommands extends BaseCommand {
     }
 
     @Subcommand("players")
+    @CommandCompletion("@nothing")
     @CommandPermission("mw.stats.players")
     public void onPlayers(CommandSender sender, String[] args) {
 
@@ -161,6 +160,7 @@ public class StatsCommands extends BaseCommand {
     }
 
     @Subcommand("list")
+    @CommandCompletion("@nothing")
     @CommandPermission("mw.stats.list")
     public void onList(CommandSender sender, String[] args) {
 

+ 1 - 0
missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/UserCommands.java

@@ -42,6 +42,7 @@ import java.util.Optional;
 public class UserCommands extends BaseCommand {
     
     @Subcommand("vote")
+    @CommandCompletion("@nothing")
     @CommandPermission("mw.vote")
     public void voteCommand(CommandSender sender, String[] args) {
 

+ 1 - 0
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/Arenas.java

@@ -66,6 +66,7 @@ public class Arenas {
             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;

+ 9 - 1
missilewars-plugin/src/main/java/de/butzlabben/missilewars/game/GameManager.java

@@ -29,7 +29,9 @@ import org.bukkit.Location;
 
 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 class GameManager {
             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 class GameManager {
     public Game getGame(String name) {
         return games.get(name);
     }
+    
+    public void addGame(String name, Game game) {
+        games.put(name, game);
+        List<String> gameNames = new ArrayList<>(games.keySet());
+        MissileWars.getInstance().commandManager.getCommandCompletions().registerCompletion("games", c -> gameNames);
+    }
 
     public int getGameAmount() {
         return games.size();