From ff6a1cf9db98402705781c03b86d577421d8477a Mon Sep 17 00:00:00 2001 From: LeokoGar Date: Sat, 25 Jul 2020 13:25:36 +0200 Subject: [PATCH] Tab completion for bungee also added permission checks on tab completion --- .../advancedban/bukkit/BukkitMethods.java | 7 ++- .../bungee/listener/ChatListenerBungee.java | 45 ++++--------------- .../me/leoko/advancedban/utils/Command.java | 22 +++++---- .../tabcompletion/BasicTabCompleter.java | 2 +- .../tabcompletion/CleanTabCompleter.java | 4 +- .../tabcompletion/MutableTabCompleter.java | 2 +- .../tabcompletion/PunishmentTabCompleter.java | 2 +- .../utils/tabcompletion/TabCompleter.java | 2 +- 8 files changed, 35 insertions(+), 51 deletions(-) diff --git a/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java b/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java index 9a0e2f87..81603f3f 100644 --- a/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java +++ b/src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java @@ -27,6 +27,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.List; import java.util.UUID; import java.util.function.BiFunction; @@ -151,7 +152,11 @@ public void setCommandExecutor(String cmd, TabCompleter tabCompleter) { if (command != null) { command.setExecutor(CommandReceiver.get()); if (tabCompleter != null) - command.setTabCompleter((commandSender, c, s, args) -> tabCompleter.onTabComplete(args)); + command.setTabCompleter((commandSender, c, s, args) -> { + if (command.getPermission() != null && !hasPerms(commandSender, command.getPermission())) + return Collections.emptyList(); + return tabCompleter.onTabComplete(commandSender, args); + }); } else { System.out.println("AdvancedBan >> Failed to register command " + cmd); } diff --git a/src/main/java/me/leoko/advancedban/bungee/listener/ChatListenerBungee.java b/src/main/java/me/leoko/advancedban/bungee/listener/ChatListenerBungee.java index 61c443e7..fbff687d 100644 --- a/src/main/java/me/leoko/advancedban/bungee/listener/ChatListenerBungee.java +++ b/src/main/java/me/leoko/advancedban/bungee/listener/ChatListenerBungee.java @@ -1,8 +1,7 @@ package me.leoko.advancedban.bungee.listener; import me.leoko.advancedban.Universal; -import me.leoko.advancedban.bungee.BungeeMain; -import me.leoko.advancedban.utils.PunishmentType; +import me.leoko.advancedban.utils.Command; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.event.ChatEvent; import net.md_5.bungee.api.event.TabCompleteEvent; @@ -32,41 +31,15 @@ public void onChat(ChatEvent event) { @EventHandler public void onTabComplete(TabCompleteEvent event) { - String cursor = event.getCursor().toLowerCase(); - if (!(cursor.startsWith("/ban ") || cursor.startsWith("/ban-ip ") || cursor.startsWith("/banip ") - || cursor.startsWith("/check ") || cursor.startsWith("/history ") || cursor.startsWith("/ipban ") - || cursor.startsWith("/kick ") || cursor.startsWith("/mute ") || cursor.startsWith("/tempban ") - || cursor.startsWith("/tempipban ") || cursor.startsWith("/tempmute ") || cursor.startsWith("/tempwarn ") - || cursor.startsWith("/tipban ") || cursor.startsWith("/unmute ") || cursor.startsWith("/unwarn ") - || cursor.startsWith("/warn ")) || cursor.startsWith("/unnote ") || cursor.startsWith("/note ")) { - return; - } - if (event.getSender() instanceof ProxiedPlayer) { // Check if the player has permission for tab complete - ProxiedPlayer pp = (ProxiedPlayer) event.getSender(); - boolean deny = false; - if (!Universal.get().hasPerms(pp, "ab.all")) { - deny = true; - } - if (deny) { // If was denied above, try checking for specific punishments. - for (PunishmentType pt : PunishmentType.values()) { - if (pp.hasPermission(pt.getPerms())) { // The player has permission for some punishment, so allow. - deny = false; - break; - } - } - } - if (deny) { // The event was denied, so cancell it and return. - event.setCancelled(true); - return; - } - } + final String commandName = event.getCursor().split(" ")[0]; + if (commandName.length() > 1 && event.getCursor().length() > commandName.length()) { + final Command command = Command.getByName(commandName.substring(1)); - String[] split = cursor.split(" "); - String partialPlayerName = split[split.length - 1]; - - for (ProxiedPlayer p : BungeeMain.get().getProxy().getPlayers()) { - if (p.getName().toLowerCase().startsWith(partialPlayerName)) { - event.getSuggestions().add(p.getName()); + if (command != null && event.getSender() instanceof ProxiedPlayer) { + if (command.getPermission() == null || Universal.get().getMethods().hasPerms(event.getSender(), command.getPermission())) { + final String[] args = event.getCursor().substring(commandName.length() + 1).split(" ", -1); + event.getSuggestions().addAll(command.getTabCompleter().onTabComplete(event.getSender(), args)); + } } } } diff --git a/src/main/java/me/leoko/advancedban/utils/Command.java b/src/main/java/me/leoko/advancedban/utils/Command.java index 3ddebc4e..7899ac25 100644 --- a/src/main/java/me/leoko/advancedban/utils/Command.java +++ b/src/main/java/me/leoko/advancedban/utils/Command.java @@ -122,7 +122,7 @@ public enum Command { UN_WARN("ab." + PunishmentType.WARNING.getName() + ".undo", "[0-9]+|(?i:clear \\S+)", - new CleanTabCompleter(args -> { + new CleanTabCompleter((user, args) -> { if(args.length == 1) { return list("[ID]", "clear"); }else if(args.length == 2 && args[0].equalsIgnoreCase("clear")){ @@ -161,7 +161,7 @@ public enum Command { "unwarn"), UN_NOTE("ab." + PunishmentType.NOTE.getName() + ".undo", "[0-9]+|(?i:clear \\S+)", - new CleanTabCompleter(args -> { + new CleanTabCompleter((user, args) -> { if(args.length == 1) { return list("[ID]", "clear"); }else if(args.length == 2 && args[0].equalsIgnoreCase("clear")){ @@ -208,7 +208,7 @@ public enum Command { CHANGE_REASON("ab.changeReason", "([0-9]+|(?i)(ban|mute) \\S+) .+", - new CleanTabCompleter(args -> { + new CleanTabCompleter((user, args) -> { if(args.length <= 1) { return list("", "ban", "mute"); }else { @@ -272,7 +272,7 @@ public enum Command { HISTORY("ab.history", "\\S+( [1-9][0-9]*)?|\\S+|", - new CleanTabCompleter(args -> { + new CleanTabCompleter((user, args) -> { if(args.length == 1) return list(CleanTabCompleter.PLAYER_PLACEHOLDER, "", ""); else if(args.length == 2 && !args[0].matches("\\d+")) @@ -288,9 +288,12 @@ else if(args.length == 2 && !args[0].matches("\\d+")) WARNS(null, "\\S+( [1-9][0-9]*)?|\\S+|", - new CleanTabCompleter(args -> { + new CleanTabCompleter((user, args) -> { if(args.length == 1) - return list(CleanTabCompleter.PLAYER_PLACEHOLDER, "", ""); + if(Universal.get().getMethods().hasPerms(user, "ab.notes.other")) + return list(CleanTabCompleter.PLAYER_PLACEHOLDER, "", ""); + else + return list(""); else if(args.length == 2 && !args[0].matches("\\d+")) return list(""); else @@ -323,9 +326,12 @@ else if(args.length == 2 && !args[0].matches("\\d+")) "warns"), NOTES(null, "\\S+( [1-9][0-9]*)?|\\S+|", - new CleanTabCompleter(args -> { + new CleanTabCompleter((user, args) -> { if(args.length == 1) - return list(CleanTabCompleter.PLAYER_PLACEHOLDER, "", ""); + if(Universal.get().getMethods().hasPerms(user, "ab.notes.other")) + return list(CleanTabCompleter.PLAYER_PLACEHOLDER, "", ""); + else + return list(""); else if(args.length == 2 && !args[0].matches("\\d+")) return list(""); else diff --git a/src/main/java/me/leoko/advancedban/utils/tabcompletion/BasicTabCompleter.java b/src/main/java/me/leoko/advancedban/utils/tabcompletion/BasicTabCompleter.java index 3c844a17..1195fdf3 100644 --- a/src/main/java/me/leoko/advancedban/utils/tabcompletion/BasicTabCompleter.java +++ b/src/main/java/me/leoko/advancedban/utils/tabcompletion/BasicTabCompleter.java @@ -2,7 +2,7 @@ public class BasicTabCompleter extends CleanTabCompleter { public BasicTabCompleter(String... firstLayerArguments) { - super(args -> { + super((user, args) -> { if(args.length == 1){ return MutableTabCompleter.list(firstLayerArguments); } else { diff --git a/src/main/java/me/leoko/advancedban/utils/tabcompletion/CleanTabCompleter.java b/src/main/java/me/leoko/advancedban/utils/tabcompletion/CleanTabCompleter.java index d2a026ad..6cc4c131 100644 --- a/src/main/java/me/leoko/advancedban/utils/tabcompletion/CleanTabCompleter.java +++ b/src/main/java/me/leoko/advancedban/utils/tabcompletion/CleanTabCompleter.java @@ -13,8 +13,8 @@ public CleanTabCompleter(MutableTabCompleter rawTabCompleter) { } @Override - public ArrayList onTabComplete(String[] args) { - ArrayList suggestions = rawTabCompleter.onTabComplete(args); + public ArrayList onTabComplete(Object user, String[] args) { + ArrayList suggestions = rawTabCompleter.onTabComplete(user, args); if(!suggestions.isEmpty() && suggestions.get(0).equals(PLAYER_PLACEHOLDER)) { suggestions.remove(0); diff --git a/src/main/java/me/leoko/advancedban/utils/tabcompletion/MutableTabCompleter.java b/src/main/java/me/leoko/advancedban/utils/tabcompletion/MutableTabCompleter.java index 097bce16..e0a7db42 100644 --- a/src/main/java/me/leoko/advancedban/utils/tabcompletion/MutableTabCompleter.java +++ b/src/main/java/me/leoko/advancedban/utils/tabcompletion/MutableTabCompleter.java @@ -5,7 +5,7 @@ public interface MutableTabCompleter extends TabCompleter { @Override - ArrayList onTabComplete(String[] args); + ArrayList onTabComplete(Object user, String[] args); static ArrayList list(T... elements){ return new ArrayList(Arrays.asList(elements)); diff --git a/src/main/java/me/leoko/advancedban/utils/tabcompletion/PunishmentTabCompleter.java b/src/main/java/me/leoko/advancedban/utils/tabcompletion/PunishmentTabCompleter.java index d7caf695..811df85c 100644 --- a/src/main/java/me/leoko/advancedban/utils/tabcompletion/PunishmentTabCompleter.java +++ b/src/main/java/me/leoko/advancedban/utils/tabcompletion/PunishmentTabCompleter.java @@ -17,7 +17,7 @@ public PunishmentTabCompleter(boolean temporary) { } @Override - public List onTabComplete(String[] args) { + public List onTabComplete(Object user, String[] args) { final MethodInterface methodInterface = Universal.get().getMethods(); List suggestions = new ArrayList<>(); diff --git a/src/main/java/me/leoko/advancedban/utils/tabcompletion/TabCompleter.java b/src/main/java/me/leoko/advancedban/utils/tabcompletion/TabCompleter.java index a7b62bbd..756748ad 100644 --- a/src/main/java/me/leoko/advancedban/utils/tabcompletion/TabCompleter.java +++ b/src/main/java/me/leoko/advancedban/utils/tabcompletion/TabCompleter.java @@ -4,5 +4,5 @@ @FunctionalInterface public interface TabCompleter { - List onTabComplete(String[] args); + List onTabComplete(Object user, String[] args); }