Skip to content

Commit

Permalink
Tab completion for bungee
Browse files Browse the repository at this point in the history
also added permission checks on tab completion
  • Loading branch information
DevLeoko committed Jul 25, 2020
1 parent 85d58d1 commit ff6a1cf
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 51 deletions.
7 changes: 6 additions & 1 deletion src/main/java/me/leoko/advancedban/bukkit/BukkitMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/me/leoko/advancedban/utils/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")){
Expand Down Expand Up @@ -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")){
Expand Down Expand Up @@ -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("<ID>", "ban", "mute");
}else {
Expand Down Expand Up @@ -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, "<Name>", "<Page>");
else if(args.length == 2 && !args[0].matches("\\d+"))
Expand All @@ -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, "<Name>", "<Page>");
if(Universal.get().getMethods().hasPerms(user, "ab.notes.other"))
return list(CleanTabCompleter.PLAYER_PLACEHOLDER, "<Name>", "<Page>");
else
return list("<Page>");
else if(args.length == 2 && !args[0].matches("\\d+"))
return list("<Page>");
else
Expand Down Expand Up @@ -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, "<Name>", "<Page>");
if(Universal.get().getMethods().hasPerms(user, "ab.notes.other"))
return list(CleanTabCompleter.PLAYER_PLACEHOLDER, "<Name>", "<Page>");
else
return list("<Page>");
else if(args.length == 2 && !args[0].matches("\\d+"))
return list("<Page>");
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public CleanTabCompleter(MutableTabCompleter rawTabCompleter) {
}

@Override
public ArrayList<String> onTabComplete(String[] args) {
ArrayList<String> suggestions = rawTabCompleter.onTabComplete(args);
public ArrayList<String> onTabComplete(Object user, String[] args) {
ArrayList<String> suggestions = rawTabCompleter.onTabComplete(user, args);

if(!suggestions.isEmpty() && suggestions.get(0).equals(PLAYER_PLACEHOLDER)) {
suggestions.remove(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public interface MutableTabCompleter extends TabCompleter {
@Override
ArrayList<String> onTabComplete(String[] args);
ArrayList<String> onTabComplete(Object user, String[] args);

static <T> ArrayList<T> list(T... elements){
return new ArrayList<T>(Arrays.asList(elements));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PunishmentTabCompleter(boolean temporary) {
}

@Override
public List<String> onTabComplete(String[] args) {
public List<String> onTabComplete(Object user, String[] args) {
final MethodInterface methodInterface = Universal.get().getMethods();

List<String> suggestions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

@FunctionalInterface
public interface TabCompleter {
List<String> onTabComplete(String[] args);
List<String> onTabComplete(Object user, String[] args);
}

0 comments on commit ff6a1cf

Please sign in to comment.