From 871970f541aa71b0a30739b4d7988e7cf71cdc52 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 9 May 2020 16:37:37 +0200 Subject: [PATCH] feat: add tab autocomplete for varo commands --- .../main/java/de/cliffbreak/varo/Varo.java | 2 + .../cliffbreak/varo/commands/VaroCommand.java | 22 +++++++- .../varo/commands/VaroTabCompleter.java | 55 +++++++++++++++++++ .../varo/listeners/PlayerDeathListener.java | 2 +- .../cliffbreak/varo/managers/NPCManager.java | 2 +- .../de/cliffbreak/varo/uitls/BanUtils.java | 17 ++++-- 6 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 varo/src/main/java/de/cliffbreak/varo/commands/VaroTabCompleter.java diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index 60b9ad5..bf528f9 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -9,6 +9,7 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.configuration.file.YamlConfiguration; import de.cliffbreak.varo.commands.VaroCommand; +import de.cliffbreak.varo.commands.VaroTabCompleter; import de.cliffbreak.varo.listeners.BannedActionListener; import de.cliffbreak.varo.listeners.BannedItemListener; import de.cliffbreak.varo.listeners.ChatListener; @@ -64,6 +65,7 @@ public class Varo extends JavaPlugin { this.getServer().getPluginManager().registerEvents(new PlayerInteractNPCListener(this), this); this.getCommand("varo").setExecutor(new VaroCommand(this)); + this.getCommand("varo").setTabCompleter(new VaroTabCompleter(this)); this.varoUtils.init(); } diff --git a/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java b/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java index 8812b57..1262959 100644 --- a/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java +++ b/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java @@ -36,7 +36,6 @@ public class VaroCommand implements CommandExecutor { return true; } if (args.length == 2 && args[0].equals("unban")) { - try { final String result = WebUtils.GET("https://api.mojang.com/users/profiles/minecraft/" + args[1]); if (result.isEmpty()) { @@ -55,7 +54,25 @@ public class VaroCommand implements CommandExecutor { "§c§lFehler:§r§c beim Verbinden mit der Mojang-API ist ein Fehler aufgetreten. Eventuell sind die Mojang Server down."); e.printStackTrace(); } - + } else if (args.length == 2 && args[0].equals("ban")) { + try { + final String result = WebUtils.GET("https://api.mojang.com/users/profiles/minecraft/" + args[1]); + if (result.isEmpty()) { + sender.sendMessage("§c§lFehler: §r§cDer Spieler wurde nicht gefunden."); + return true; + } + final JSONObject uuidObject = (JSONObject) JSONValue.parseWithException(result); + if (this.plugin.getPlayerCache().getName(uuidObject.get("id").toString()) == null) { + sender.sendMessage("§c§lFehler: §r§cDer Spieler war noch nie auf diesem Server."); + return true; + } + this.plugin.getBanUtils().addBan(uuidObject.get("id").toString()); + sender.sendMessage("§a§lErfolg: §r§aDer Spieler ist nun gebannt."); + } catch (IOException | ParseException e) { + sender.sendMessage( + "§c§lFehler:§r§c beim Verbinden mit der Mojang-API ist ein Fehler aufgetreten. Eventuell sind die Mojang Server down."); + e.printStackTrace(); + } } else if (args.length == 1 && args[0].equals("start")) { sender.sendMessage("§aStarting..."); for (final Player p : Bukkit.getServer().getOnlinePlayers()) { @@ -104,6 +121,7 @@ public class VaroCommand implements CommandExecutor { } else { sender.sendMessage("\n§6========== §b§lCliffbreak.de VARO §r§6============\n "); sender.sendMessage("§a/varo start: §rStarte das Varo Projekt!"); + sender.sendMessage("§a/varo ban [Spieler]: §rSpieler bannen"); sender.sendMessage("§a/varo unban [Spieler]: §rSpieler wieder auf den Server lassen"); sender.sendMessage("§a/varo portal [enable/disable]: §rAktiviere/Deaktiviere die Portal-Erstellung"); sender.sendMessage("§a/varo teamcolors: §rZeige verfügbare Team-Farben an"); diff --git a/varo/src/main/java/de/cliffbreak/varo/commands/VaroTabCompleter.java b/varo/src/main/java/de/cliffbreak/varo/commands/VaroTabCompleter.java new file mode 100644 index 0000000..2ff3bf0 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/commands/VaroTabCompleter.java @@ -0,0 +1,55 @@ +package de.cliffbreak.varo.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; + +import de.cliffbreak.varo.Varo; + +public class VaroTabCompleter implements TabCompleter { + + private final Varo plugin; + + public VaroTabCompleter(final Varo plugin) { + this.plugin = plugin; + } + + @Override + public List onTabComplete(final CommandSender sender, final Command command, final String alias, + final String[] args) { + final List list = new ArrayList(); + if (sender.hasPermission("varo.admin")) { + if (args.length == 1) { + list.add("unban"); + list.add("ban"); + list.add("start"); + list.add("teamcolors"); + list.add("portal"); + list.add("test"); + list.add("testremove"); + } else if (args.length == 2 && args[0].equals("unban")) { + final ArrayList bans = this.plugin.getBanUtils().getBans(); + for (String uuid : bans) { + list.add(this.plugin.getPlayerCache().getName(uuid)); + } + } else if (args.length == 2 && args[0].equals("ban")) { + return null; // Return null to show default player autocomplete + } else if (args.length == 2 && args[0].equals("portal")) { + list.add("enable"); + list.add("disable"); + } + final List removeQueue = new ArrayList(); + for (String s : list) { + if (!s.startsWith(args[args.length - 1])) { + removeQueue.add(s); + } + } + list.removeAll(removeQueue); + } + return list; + } + +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java index b4708bd..06c12d5 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java @@ -26,7 +26,7 @@ public class PlayerDeathListener implements Listener { }.runTaskLater(this.plugin, 20); e.getEntity().setHealth(20d); // Reset health to prevent death dialog after rejoin (if unbanned) e.getEntity().setGameMode(GameMode.SPECTATOR); - this.plugin.getBanUtils().addBann(e.getEntity().getUniqueId()); + this.plugin.getBanUtils().addBan(e.getEntity().getUniqueId()); } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java index cecc3b9..4b8771b 100644 --- a/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java +++ b/varo/src/main/java/de/cliffbreak/varo/managers/NPCManager.java @@ -201,7 +201,7 @@ public class NPCManager { } }.runTask(this.plugin); // Run Task in sync to be thread safe - this.plugin.getBanUtils().addBann(npc.getUniqueID()); + this.plugin.getBanUtils().addBan(npc.getUniqueID()); Bukkit.broadcastMessage("TODO: Player(" + "??" + ") killed AFK Player (" + npc.getName() + ")"); new BukkitRunnable() { diff --git a/varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java b/varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java index efd1506..760a602 100644 --- a/varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java +++ b/varo/src/main/java/de/cliffbreak/varo/uitls/BanUtils.java @@ -23,12 +23,21 @@ public class BanUtils { return bans.contains(compressedUuid); } - public void addBann(final UUID uuid) { + public ArrayList getBans() { @SuppressWarnings("unchecked") final ArrayList bans = (ArrayList) this.plugin.getConfig().get("Varo.Bans"); - final String compressedUUID = uuid.toString().replace("-", ""); - if (!bans.contains(compressedUUID)) { - bans.add(compressedUUID); + return bans; + } + + public void addBan(final UUID uuid) { + addBan(uuid.toString().replace("-", "")); + } + + public void addBan(final String compressedUuid) { + @SuppressWarnings("unchecked") + final ArrayList bans = (ArrayList) this.plugin.getConfig().get("Varo.Bans"); + if (!bans.contains(compressedUuid)) { + bans.add(compressedUuid); this.plugin.getConfig().set("Varo.Bans", bans); this.plugin.saveConfig(); }