From 6c531c601d7262980f04c3b59af7f0e4327e99c8 Mon Sep 17 00:00:00 2001 From: Deko Date: Wed, 10 Jul 2024 16:58:19 +0200 Subject: [PATCH 1/3] Remove unused TabExecutor --- .../it/frafol/cleanping/bukkit/CleanPing.java | 28 +------------------ 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/src/main/java/it/frafol/cleanping/bukkit/CleanPing.java b/src/main/java/it/frafol/cleanping/bukkit/CleanPing.java index 3f74e1b..9cfecf1 100644 --- a/src/main/java/it/frafol/cleanping/bukkit/CleanPing.java +++ b/src/main/java/it/frafol/cleanping/bukkit/CleanPing.java @@ -13,12 +13,8 @@ import net.byteflux.libby.BukkitLibraryManager; import net.byteflux.libby.Library; import net.byteflux.libby.relocation.Relocation; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import org.jetbrains.annotations.NotNull; import org.simpleyaml.configuration.file.YamlFile; import java.io.File; @@ -30,11 +26,9 @@ import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Collections; -import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; -public class CleanPing extends JavaPlugin implements TabExecutor { +public class CleanPing extends JavaPlugin { private TextFile configTextFile; private TextFile messagesTextFile; @@ -125,9 +119,7 @@ public void onEnable() { getLogger().info("Loading commands for legacy versions..."); Objects.requireNonNull(getCommand("ping")).setExecutor(new it.frafol.cleanping.bukkit.commands.legacy.PingCommand(this)); - Objects.requireNonNull(getCommand("ping")).setTabCompleter(this); Objects.requireNonNull(getCommand("cleanping")).setExecutor(new it.frafol.cleanping.bukkit.commands.legacy.PingCommand(this)); - Objects.requireNonNull(getCommand("cleanping")).setTabCompleter(this); Objects.requireNonNull(getCommand("pingreload")).setExecutor(new it.frafol.cleanping.bukkit.commands.legacy.ReloadCommand(this)); Objects.requireNonNull(getCommand("cleanpingreload")).setExecutor(new it.frafol.cleanping.bukkit.commands.legacy.ReloadCommand(this)); @@ -135,9 +127,7 @@ public void onEnable() { getLogger().info("Loading commands..."); Objects.requireNonNull(getCommand("ping")).setExecutor(new PingCommand(this)); - Objects.requireNonNull(getCommand("ping")).setTabCompleter(this); Objects.requireNonNull(getCommand("cleanping")).setExecutor(new PingCommand(this)); - Objects.requireNonNull(getCommand("cleanping")).setTabCompleter(this); Objects.requireNonNull(getCommand("pingreload")).setExecutor(new ReloadCommand(this)); Objects.requireNonNull(getCommand("cleanpingreload")).setExecutor(new ReloadCommand(this)); @@ -264,20 +254,4 @@ public void onDisable() { getLogger().info("Plugin successfully disabled!"); } - - @Override - public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) { - - if (args.length != 1) { - return null; - } - - String partialName = args[0].toLowerCase(); - - return getServer().getOnlinePlayers().stream() - .map(Player::getName) - .filter(name -> name.toLowerCase().startsWith(partialName)) - .collect(Collectors.toList()); - - } } \ No newline at end of file From 45f6ef578e4c601d4c9d4c58e7b33cd0586477f6 Mon Sep 17 00:00:00 2001 From: Deko Date: Wed, 10 Jul 2024 17:37:29 +0200 Subject: [PATCH 2/3] Filter matching names in TabComplete --- .../bukkit/commands/utils/TabComplete.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java b/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java index e6fe9e8..a4d1d42 100644 --- a/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java +++ b/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java @@ -1,29 +1,25 @@ package it.frafol.cleanping.bukkit.commands.utils; -import it.frafol.cleanping.bukkit.CleanPing; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; public class TabComplete implements TabCompleter { public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) { - - final List list = new ArrayList<>(); - - if (command.getName().equalsIgnoreCase("ping") - || command.getName().equalsIgnoreCase("cleanping")) { - - for (Player players : CleanPing.getInstance().getServer().getOnlinePlayers()) { - list.add(players.getName()); - } - + if (args.length != 1) { + return Collections.emptyList(); } - return list; + + return sender.getServer().getOnlinePlayers().stream() + .filter(player -> player.getName().toLowerCase().startsWith(args[0].toLowerCase())) + .map(Player::getName) + .collect(Collectors.toList()); } } From b2facd48568736b297245176b6c06dc1413b4fe2 Mon Sep 17 00:00:00 2001 From: Deko Date: Wed, 10 Jul 2024 17:38:04 +0200 Subject: [PATCH 3/3] Ignore vanished players in TabComplete --- .../bukkit/commands/utils/TabComplete.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java b/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java index a4d1d42..49df385 100644 --- a/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java +++ b/src/main/java/it/frafol/cleanping/bukkit/commands/utils/TabComplete.java @@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; +import org.bukkit.metadata.MetadataValue; import org.jetbrains.annotations.NotNull; import java.util.Collections; @@ -18,8 +19,18 @@ public List onTabComplete(@NotNull CommandSender sender, @NotNull Comman } return sender.getServer().getOnlinePlayers().stream() - .filter(player -> player.getName().toLowerCase().startsWith(args[0].toLowerCase())) + .filter(player -> + !isVanished(player) && player.getName().toLowerCase().startsWith(args[0].toLowerCase())) .map(Player::getName) .collect(Collectors.toList()); } + + private boolean isVanished(Player player) { + for (MetadataValue meta : player.getMetadata("vanished")) { + if (meta.asBoolean()) { + return true; + } + } + return false; + } }