From f01793068922e9682220b057d0429f15ddf3ca5c Mon Sep 17 00:00:00 2001 From: RedstoneFuture Date: Sun, 5 Feb 2023 19:43:55 +0100 Subject: [PATCH] File management: using of child pathname instead of string combination --- .../butzlabben/missilewars/MissileWars.java | 9 ++- .../commands/MWCommandCompletions.java | 58 +++++++++++++++++++ .../missilewars/commands/MWCommands.java | 2 +- .../arena/MissileConfiguration.java | 8 +++ .../missilewars/game/GameManager.java | 4 -- 5 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommandCompletions.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 ed734702..c87afa91 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java @@ -19,10 +19,7 @@ package de.butzlabben.missilewars; 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.commands.*; import de.butzlabben.missilewars.configuration.Config; import de.butzlabben.missilewars.configuration.Messages; import de.butzlabben.missilewars.game.Arenas; @@ -166,12 +163,14 @@ private void registerCommands() { // such as Asynchronous Tab Completions. commandManager = new PaperCommandManager(this); + new MWCommandCompletions(commandManager); + commandManager.registerCommand(new MWCommands()); commandManager.registerCommand(new StatsCommands()); commandManager.registerCommand(new UserCommands()); commandManager.registerCommand(new SetupCommands()); } - + /** * This method checks if FAWE (FastAsyncWorldEdit) is installed. * diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommandCompletions.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommandCompletions.java new file mode 100644 index 00000000..d81dd064 --- /dev/null +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/commands/MWCommandCompletions.java @@ -0,0 +1,58 @@ +/* + * 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.BukkitCommandCompletionContext; +import co.aikar.commands.CommandCompletions; +import co.aikar.commands.PaperCommandManager; +import de.butzlabben.missilewars.game.Game; +import de.butzlabben.missilewars.game.GameManager; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class MWCommandCompletions { + + private final CommandCompletions commandCompletions; + + public MWCommandCompletions(PaperCommandManager commandManager) { + this.commandCompletions = commandManager.getCommandCompletions(); + + registerGamesResult(); + registerMissilesResult(); + } + + private void registerGamesResult() { + commandCompletions.registerCompletion("games", c -> GameManager.getInstance().getGames().keySet()); + } + + private void registerMissilesResult() { + commandCompletions.registerCompletion("missiles", c -> { + CommandSender sender = c.getSender(); + + if (!(sender instanceof Player)) return null; + Player player = (Player) sender; + + Game game = GameManager.getInstance().getGame(player.getLocation()); + if (game == null) return null; + + return game.getArena().getMissileConfiguration().getMissileNames(); + }); + } + +} 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 f54f779d..cc86a78e 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 @@ -90,7 +90,7 @@ public void listgamesCommand(CommandSender sender, String[] args) { } @Subcommand("paste") - @CommandCompletion("@nothing") + @CommandCompletion("@missiles") @CommandPermission("mw.paste") public void pasteCommand(CommandSender sender, String[] args) { diff --git a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/MissileConfiguration.java b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/MissileConfiguration.java index 1ab5f640..6e819ab1 100644 --- a/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/MissileConfiguration.java +++ b/missilewars-plugin/src/main/java/de/butzlabben/missilewars/configuration/arena/MissileConfiguration.java @@ -89,4 +89,12 @@ public void check() { } missiles.removeAll(toRemove); } + + public List getMissileNames() { + List missileNames = new ArrayList<>(); + for (Missile missile : missiles) { + missileNames.add(missile.getSchematicName(true)); + } + return missileNames; + } } 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 865d7cf6..c1d3a8eb 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,9 +29,7 @@ import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; @Getter public class GameManager { @@ -167,8 +165,6 @@ public Game getGame(String 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() {