Dateien nach "src/main/java/de/nexuslobby/modules/suppressor" hochladen
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
package de.nexuslobby.modules.suppressor;
|
||||
|
||||
import de.nexuslobby.api.Module;
|
||||
import de.nexuslobby.NexusLobby;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
|
||||
public class GlobalChatSuppressor implements Module, PluginMessageListener, Listener {
|
||||
|
||||
private static final String CHANNEL_CONTROL = "global:control";
|
||||
private static final String CHANNEL_CHAT = "global:chat";
|
||||
private final NexusLobby plugin;
|
||||
|
||||
public GlobalChatSuppressor(NexusLobby plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "GlobalChatSuppressor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, CHANNEL_CONTROL, this);
|
||||
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, CHANNEL_CHAT, this);
|
||||
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, CHANNEL_CONTROL);
|
||||
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
|
||||
plugin.getLogger().info("GlobalChatSuppressor aktiviert (Chat Relay included)!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
plugin.getServer().getMessenger().unregisterIncomingPluginChannel(plugin, CHANNEL_CONTROL);
|
||||
plugin.getServer().getMessenger().unregisterIncomingPluginChannel(plugin, CHANNEL_CHAT);
|
||||
plugin.getServer().getMessenger().unregisterOutgoingPluginChannel(plugin, CHANNEL_CONTROL);
|
||||
|
||||
plugin.getLogger().info("GlobalChatSuppressor deaktiviert!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
|
||||
if (!channel.equals(CHANNEL_CONTROL) && !channel.equals(CHANNEL_CHAT)) return;
|
||||
|
||||
try {
|
||||
if (channel.equals(CHANNEL_CHAT)) {
|
||||
String data = new String(message, StandardCharsets.UTF_8);
|
||||
BaseComponent[] components = ComponentSerializer.parse(data);
|
||||
for (Player all : Bukkit.getOnlinePlayers()) {
|
||||
all.spigot().sendMessage(components);
|
||||
}
|
||||
} else if (channel.equals(CHANNEL_CONTROL)) {
|
||||
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) {
|
||||
String subChannel = in.readUTF();
|
||||
if ("suppress".equalsIgnoreCase(subChannel)) {
|
||||
String uuidStr = in.readUTF();
|
||||
UUID playerUUID = UUID.fromString(uuidStr);
|
||||
|
||||
SuppressManager.suppress(playerUUID);
|
||||
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SuppressManager.unsuppress(playerUUID);
|
||||
}
|
||||
}.runTaskLater(plugin, 40L);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
UUID uuid = event.getPlayer().getUniqueId();
|
||||
if (SuppressManager.isSuppressed(uuid)) {
|
||||
event.setJoinMessage(null);
|
||||
SuppressManager.unsuppress(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
UUID uuid = event.getPlayer().getUniqueId();
|
||||
if (SuppressManager.isSuppressed(uuid)) {
|
||||
event.setQuitMessage(null);
|
||||
SuppressManager.unsuppress(uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package de.nexuslobby.modules.suppressor;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SuppressManager {
|
||||
|
||||
private static final Set<UUID> suppressedPlayers = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
public static void suppress(UUID playerUUID) {
|
||||
suppressedPlayers.add(playerUUID);
|
||||
}
|
||||
|
||||
public static void unsuppress(UUID playerUUID) {
|
||||
suppressedPlayers.remove(playerUUID);
|
||||
}
|
||||
|
||||
public static boolean isSuppressed(UUID playerUUID) {
|
||||
return suppressedPlayers.contains(playerUUID);
|
||||
}
|
||||
|
||||
public static Set<UUID> getSuppressedPlayers() {
|
||||
return suppressedPlayers;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user