Skip to content

Commit

Permalink
Merge pull request #7 from akshualy/fix/bukkit-tab-complete
Browse files Browse the repository at this point in the history
Fix & Improve Bukkit Tab Completion
frafol authored Jul 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents fdab9a8 + b2facd4 commit 5411ff0
Showing 2 changed files with 18 additions and 37 deletions.
28 changes: 1 addition & 27 deletions src/main/java/it/frafol/cleanping/bukkit/CleanPing.java
Original file line number Diff line number Diff line change
@@ -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,19 +119,15 @@ 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));

} else {

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<String> 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());

}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
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.bukkit.metadata.MetadataValue;
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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
if (args.length != 1) {
return Collections.emptyList();
}

final List<String> list = new ArrayList<>();

if (command.getName().equalsIgnoreCase("ping")
|| command.getName().equalsIgnoreCase("cleanping")) {
return sender.getServer().getOnlinePlayers().stream()
.filter(player ->
!isVanished(player) && player.getName().toLowerCase().startsWith(args[0].toLowerCase()))
.map(Player::getName)
.collect(Collectors.toList());
}

for (Player players : CleanPing.getInstance().getServer().getOnlinePlayers()) {
list.add(players.getName());
private boolean isVanished(Player player) {
for (MetadataValue meta : player.getMetadata("vanished")) {
if (meta.asBoolean()) {
return true;
}

}
return list;
return false;
}
}

0 comments on commit 5411ff0

Please sign in to comment.