mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-28 05:54:16 +02:00
Add vanish command and permissions
This commit is contained in:
@@ -5,18 +5,23 @@ import de.shiewk.smoderation.event.CustomInventoryEvents;
|
||||
import de.shiewk.smoderation.event.EnderchestSeeEvents;
|
||||
import de.shiewk.smoderation.event.InvSeeEvents;
|
||||
import de.shiewk.smoderation.listener.PunishmentListener;
|
||||
import de.shiewk.smoderation.listener.VanishListener;
|
||||
import de.shiewk.smoderation.storage.PunishmentContainer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static org.bukkit.Bukkit.getPluginManager;
|
||||
|
||||
public final class SModeration extends JavaPlugin {
|
||||
@@ -30,7 +35,7 @@ public final class SModeration extends JavaPlugin {
|
||||
public static final TextColor SECONDARY_COLOR = TextColor.color(52, 143, 255);
|
||||
public static final TextColor INACTIVE_COLOR = NamedTextColor.GRAY;
|
||||
public static final TextColor FAIL_COLOR = NamedTextColor.RED;
|
||||
public static final TextComponent CHAT_PREFIX = Component.text("SM \u00BB ").color(PRIMARY_COLOR);
|
||||
public static final TextComponent CHAT_PREFIX = text("SM \u00BB ").color(PRIMARY_COLOR);
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
@@ -45,6 +50,7 @@ public final class SModeration extends JavaPlugin {
|
||||
getPluginManager().registerEvents(new CustomInventoryEvents(), this);
|
||||
getPluginManager().registerEvents(new InvSeeEvents(), this);
|
||||
getPluginManager().registerEvents(new EnderchestSeeEvents(), this);
|
||||
getPluginManager().registerEvents(new VanishListener(), this);
|
||||
|
||||
registerCommand("mute", new MuteCommand());
|
||||
registerCommand("ban", new BanCommand());
|
||||
@@ -55,6 +61,7 @@ public final class SModeration extends JavaPlugin {
|
||||
registerCommand("unban", new UnbanCommand());
|
||||
registerCommand("invsee", new InvseeCommand());
|
||||
registerCommand("enderchestsee", new EnderchestSeeCommand());
|
||||
registerCommand("vanish", new VanishCommand());
|
||||
|
||||
container.load(SAVE_FILE);
|
||||
}
|
||||
@@ -72,5 +79,59 @@ public final class SModeration extends JavaPlugin {
|
||||
@Override
|
||||
public void onDisable() {
|
||||
SModeration.container.save(SModeration.SAVE_FILE);
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
// in case players are still vanished when the server shuts down
|
||||
if (isVanished(player)){
|
||||
toggleVanish(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final ObjectArrayList<Player> vanishedPlayers = new ObjectArrayList<>();
|
||||
|
||||
public static void toggleVanish(Player player){
|
||||
final boolean newStatus = !isVanished(player);
|
||||
if (newStatus){
|
||||
vanishedPlayers.add(player);
|
||||
for (CommandSender sender : container.collectBroadcastTargets()) {
|
||||
sender.sendMessage(CHAT_PREFIX.append(
|
||||
player.displayName()
|
||||
.colorIfAbsent(SECONDARY_COLOR)
|
||||
).append(
|
||||
text()
|
||||
.content(" vanished.")
|
||||
.color(PRIMARY_COLOR)
|
||||
));
|
||||
}
|
||||
player.sendMessage(CHAT_PREFIX.append(text("You are now vanished.").color(PRIMARY_COLOR)));
|
||||
player.setVisibleByDefault(false);
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (onlinePlayer.hasPermission("smod.vanish.see")){
|
||||
onlinePlayer.showEntity(PLUGIN, player);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vanishedPlayers.remove(player);
|
||||
for (CommandSender sender : container.collectBroadcastTargets()) {
|
||||
sender.sendMessage(CHAT_PREFIX.append(
|
||||
player.displayName()
|
||||
.colorIfAbsent(SECONDARY_COLOR)
|
||||
).append(
|
||||
text()
|
||||
.content(" re-appeared.")
|
||||
.color(PRIMARY_COLOR)
|
||||
));
|
||||
}
|
||||
player.sendMessage(CHAT_PREFIX.append(text("You are no longer vanished.").color(PRIMARY_COLOR)));
|
||||
player.setVisibleByDefault(true);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isVanished(Player player){
|
||||
return vanishedPlayers.contains(player);
|
||||
}
|
||||
|
||||
public static ObjectArrayList<Player> getVanishedPlayers() {
|
||||
return vanishedPlayers.clone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package de.shiewk.smoderation.command;
|
||||
|
||||
import de.shiewk.smoderation.SModeration;
|
||||
import de.shiewk.smoderation.util.PlayerUtil;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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){
|
||||
SModeration.toggleVanish(player);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package de.shiewk.smoderation.listener;
|
||||
|
||||
import de.shiewk.smoderation.SModeration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class VanishListener implements Listener {
|
||||
|
||||
@EventHandler public void onPlayerQuit(PlayerQuitEvent event){
|
||||
final Player player = event.getPlayer();
|
||||
if (SModeration.isVanished(player)){
|
||||
SModeration.toggleVanish(player);
|
||||
}
|
||||
for (Player vanishedPlayer : SModeration.getVanishedPlayers()) {
|
||||
// to clean up visibility status
|
||||
player.hideEntity(SModeration.PLUGIN, vanishedPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST) public void onPlayerJoin(PlayerJoinEvent event){
|
||||
final Player player = event.getPlayer();
|
||||
if (player.hasPermission("smod.vanish.see")){
|
||||
for (Player vanishedPlayer : SModeration.getVanishedPlayers()) {
|
||||
// to show visible vanished players
|
||||
player.showEntity(SModeration.PLUGIN, vanishedPlayer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,7 +9,10 @@ import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public abstract class PlayerUtil {
|
||||
private PlayerUtil(){}
|
||||
@@ -55,4 +58,17 @@ public abstract class PlayerUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(){
|
||||
return listPlayerNames(pl -> true);
|
||||
}
|
||||
|
||||
public static List<String> listPlayerNames(final Predicate<Player> predicate) {
|
||||
final ArrayList<String> names = new ArrayList<>();
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
if (predicate.test(onlinePlayer)){
|
||||
names.add(onlinePlayer.getName());
|
||||
}
|
||||
}
|
||||
return List.copyOf(names);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,12 @@ commands:
|
||||
- ecs
|
||||
permission: smod.enderchestsee
|
||||
description: Views the ender chest of another player.
|
||||
vanish:
|
||||
usage: "§cUsage: /vanish <player>"
|
||||
aliases:
|
||||
- smvanish
|
||||
permission: smod.vanish
|
||||
description: Toggles vanish mode which prevents other players from seeing you're online
|
||||
permissions:
|
||||
smod.mute:
|
||||
default: op
|
||||
@@ -126,3 +132,9 @@ permissions:
|
||||
smod.preventban:
|
||||
default: op
|
||||
description: Prevents the player from being muted (if online)
|
||||
smod.vanish:
|
||||
default: op
|
||||
description: Allows the player to use /vanish
|
||||
smod.vanish.see:
|
||||
default: op
|
||||
description: Allows the player to see vanished players
|
||||
Reference in New Issue
Block a user