mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-28 05:54:16 +02:00
Added /vanish list command and list vanished players on join
This commit is contained in:
@@ -3,6 +3,9 @@ package de.shiewk.smoderation.command;
|
||||
import de.shiewk.smoderation.SModeration;
|
||||
import de.shiewk.smoderation.util.PlayerUtil;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectListIterator;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -20,14 +23,25 @@ public class VanishCommand implements TabExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
Player player = null;
|
||||
if (args.length > 0){
|
||||
player = PlayerUtil.findOnlinePlayer(args[0]);
|
||||
} else if (sender instanceof Player){
|
||||
player = (Player) sender;
|
||||
}
|
||||
if (player != null){
|
||||
toggleVanish(player);
|
||||
if (args.length == 0 || args[0].equalsIgnoreCase("toggle")){
|
||||
Player player = null;
|
||||
if (args.length > 1){
|
||||
player = PlayerUtil.findOnlinePlayer(args[1]);
|
||||
} else if (sender instanceof Player){
|
||||
player = (Player) sender;
|
||||
}
|
||||
if (player != null){
|
||||
toggleVanish(player);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if (args[0].equalsIgnoreCase("list")) {
|
||||
if (sender.hasPermission("smod.vanish.see")){
|
||||
listVanishedPlayersTo(sender);
|
||||
} else {
|
||||
sender.sendMessage(text().color(NamedTextColor.RED).content("You do not have permission to list all vanished players."));
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@@ -37,7 +51,10 @@ public class VanishCommand implements TabExecutor {
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 2){
|
||||
return PlayerUtil.listPlayerNames();
|
||||
return List.of("list", "toggle");
|
||||
}
|
||||
if (args.length < 3 && args[0].equalsIgnoreCase("toggle")){
|
||||
return PlayerUtil.listPlayerNames(args[1]);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
@@ -89,4 +106,28 @@ public class VanishCommand implements TabExecutor {
|
||||
public static ObjectArrayList<Player> getVanishedPlayers() {
|
||||
return vanishedPlayers.clone();
|
||||
}
|
||||
|
||||
public static void listVanishedPlayersTo(CommandSender receiver){
|
||||
if (vanishedPlayers.isEmpty()){
|
||||
receiver.sendMessage(CHAT_PREFIX.append(
|
||||
text().content("No players are currently vanished.").color(PRIMARY_COLOR)
|
||||
));
|
||||
} else {
|
||||
Component vanishList = CHAT_PREFIX.append(
|
||||
text().content("The following players are currently vanished: ").color(PRIMARY_COLOR)
|
||||
);
|
||||
for (ObjectListIterator<Player> iterator = vanishedPlayers.iterator(); iterator.hasNext(); ) {
|
||||
Player vanishedPlayer = iterator.next();
|
||||
vanishList = vanishList.append(
|
||||
vanishedPlayer.displayName().colorIfAbsent(SECONDARY_COLOR)
|
||||
);
|
||||
if (iterator.hasNext()){
|
||||
vanishList = vanishList.append(
|
||||
text().content(", ").color(PRIMARY_COLOR)
|
||||
);
|
||||
}
|
||||
}
|
||||
receiver.sendMessage(vanishList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,12 +25,14 @@ public class VanishListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR) public void onPlayerJoin(PlayerJoinEvent event){
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(SModeration.PLUGIN, () -> {
|
||||
final Player player = event.getPlayer();
|
||||
final Player player = event.getPlayer().getPlayer();
|
||||
assert player != null;
|
||||
if (player.hasPermission("smod.vanish.see")){
|
||||
for (Player vanishedPlayer : VanishCommand.getVanishedPlayers()) {
|
||||
// to show visible vanished players
|
||||
player.showEntity(SModeration.PLUGIN, vanishedPlayer);
|
||||
}
|
||||
VanishCommand.listVanishedPlayersTo(player);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -71,4 +72,8 @@ public abstract class PlayerUtil {
|
||||
}
|
||||
return List.copyOf(names);
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(String search) {
|
||||
return StringUtil.copyPartialMatches(search, listPlayerNames(), new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ commands:
|
||||
permission: smod.enderchestsee
|
||||
description: Views the ender chest of another player.
|
||||
vanish:
|
||||
usage: "§cUsage: /vanish <player>"
|
||||
usage: "§cUsage: /vanish list or /vanish toggle <player>"
|
||||
aliases:
|
||||
- smvanish
|
||||
permission: smod.vanish
|
||||
|
||||
Reference in New Issue
Block a user