mirror of
https://github.com/plexusorg/Plex.git
synced 2025-06-28 22:46:40 +00:00
add admin chat redis support
This commit is contained in:
@ -1,9 +1,14 @@
|
||||
package dev.plex.util;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.PlexBase;
|
||||
import dev.plex.listener.impl.ChatListener;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.rank.enums.Rank;
|
||||
import dev.plex.storage.StorageType;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Month;
|
||||
@ -11,7 +16,9 @@ import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
@ -21,6 +28,7 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommandYamlParser;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@ -180,7 +188,7 @@ public class PlexUtils implements PlexBase
|
||||
{
|
||||
try
|
||||
{
|
||||
return ((TextComponent)component).content();
|
||||
return ((TextComponent) component).content();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@ -232,6 +240,36 @@ public class PlexUtils implements PlexBase
|
||||
});
|
||||
}
|
||||
|
||||
public static List<UUID> adminChat(String senderName, String message, UUID... ignore)
|
||||
{
|
||||
List<UUID> sent = Lists.newArrayList();
|
||||
for (Player player : Bukkit.getOnlinePlayers())
|
||||
{
|
||||
if (Arrays.stream(ignore).anyMatch(uuid -> player.getUniqueId().equals(uuid)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
||||
{
|
||||
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
|
||||
if (plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN) && plexPlayer.isAdminActive())
|
||||
{
|
||||
player.sendMessage(messageComponent("adminChatFormat", senderName, message).replaceText(ChatListener.URL_REPLACEMENT_CONFIG));
|
||||
sent.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
||||
{
|
||||
if (player.hasPermission("plex.adminchat"))
|
||||
{
|
||||
player.sendMessage(PlexUtils.messageComponent("adminChatFormat", senderName, message).replaceText(ChatListener.URL_REPLACEMENT_CONFIG));
|
||||
sent.add(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
public static String cleanString(String input)
|
||||
{
|
||||
return CharMatcher.ascii().retainFrom(input);
|
||||
|
72
server/src/main/java/dev/plex/util/redis/MessageUtil.java
Normal file
72
server/src/main/java/dev/plex/util/redis/MessageUtil.java
Normal file
@ -0,0 +1,72 @@
|
||||
package dev.plex.util.redis;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.util.PlexLog;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import dev.plex.util.minimessage.SafeMiniMessage;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPubSub;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static dev.plex.util.PlexUtils.messageComponent;
|
||||
|
||||
public class MessageUtil {
|
||||
private static final Gson GSON = new Gson();
|
||||
private static JedisPubSub SUBSCRIBER;
|
||||
|
||||
public static void subscribe() {
|
||||
PlexLog.debug("Subscribing");
|
||||
SUBSCRIBER = new JedisPubSub() {
|
||||
@Override
|
||||
public void onMessage(String channel, String message) {
|
||||
try {
|
||||
JSONObject object = new JSONObject(message);
|
||||
if (channel.equalsIgnoreCase("staffchat")) {
|
||||
UUID[] ignore = GSON.fromJson(object.getString("ignore"), new TypeToken<UUID[]>(){}.getType());
|
||||
String sender = object.getString("sender").isEmpty() ? "CONSOLE": object.getString("sender");
|
||||
PlexUtils.adminChat(sender, object.getString("message"), ignore);
|
||||
String[] server = object.getString("server").split(":");
|
||||
if (!Bukkit.getServer().getIp().equalsIgnoreCase(server[0]) || Bukkit.getServer().getPort() != Integer.parseInt(server[1])) {
|
||||
Plex.get().getServer().getConsoleSender().sendMessage(messageComponent("adminChatFormat", sender, object.getString("message")));
|
||||
}
|
||||
}
|
||||
} catch (JSONException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(String channel, int subscribedChannels)
|
||||
{
|
||||
PlexLog.debug("Subscribed to {0}", channel);
|
||||
}
|
||||
};
|
||||
// SUBSCRIBER.subscribe("staffchat", "chat");
|
||||
Plex.get().getRedisConnection().runAsync(jedis -> {
|
||||
jedis.subscribe(SUBSCRIBER, "staffchat", "chat");
|
||||
});
|
||||
}
|
||||
|
||||
public static void sendStaffChat(CommandSender sender, Component message, UUID... ignore) {
|
||||
if (!Plex.get().getRedisConnection().isEnabled() || Plex.get().getRedisConnection().getJedis() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String miniMessage = SafeMiniMessage.mmSerialize(message);
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("sender", sender instanceof Player player ? player.getName() : "");
|
||||
object.put("message", miniMessage);
|
||||
object.put("ignore", GSON.toJson(ignore));
|
||||
object.put("server", String.format("%s:%s", Bukkit.getServer().getIp(), Bukkit.getServer().getPort()));
|
||||
Plex.get().getRedisConnection().getJedis().publish("staffchat", object.toString());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user