mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
Revamp redis handling
This commit is contained in:
1 parent
f861aa001a
commit
465bbdd5a4
7 files changed
+225
-91
No files matched your search
@@ -1,29 +1,27 @@
|
||||
package net.draycia.carbon.managers;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import io.lettuce.core.RedisURI;
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
|
||||
import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.storage.impl.CarbonChatUser;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.util.RedisListener;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RedisManager {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
private final RedisClient client;
|
||||
private final StatefulRedisPubSubConnection<String, String> connection;
|
||||
private final RedisPubSubCommands<String, String> sync;
|
||||
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final Type userType = new TypeToken<CarbonChatUser>() {}.getType();
|
||||
|
||||
public RedisManager(CarbonChat carbonChat) {
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
String host = carbonChat.getConfig().getString("redis.host");
|
||||
String password = carbonChat.getConfig().getString("redis.password");
|
||||
@@ -41,48 +39,106 @@ public class RedisManager {
|
||||
this.connection = client.connectPubSub();
|
||||
this.sync = connection.sync();
|
||||
|
||||
connection.addListener(new RedisPubSubListener<String, String>() {
|
||||
@Override
|
||||
public void message(String channel, String message) {
|
||||
if (channel.equalsIgnoreCase("refreshuser")) {
|
||||
carbonChat.getUserService().refreshUser(UUID.fromString(message));
|
||||
}
|
||||
connection.addListener((RedisListener)(channel, message) -> {
|
||||
UUID uuid = UUID.fromString(message.split(":", 2)[0]);
|
||||
|
||||
ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid);
|
||||
|
||||
if (user == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void message(String pattern, String channel, String message) { }
|
||||
String value = message.split(":", 2)[1];
|
||||
|
||||
@Override
|
||||
public void subscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void psubscribed(String pattern, long count) { }
|
||||
|
||||
@Override
|
||||
public void unsubscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
public void punsubscribed(String pattern, long count) { }
|
||||
switch (channel.toLowerCase()) {
|
||||
case "nickname":
|
||||
handleNicknameChange(user, value);
|
||||
break;
|
||||
case "selected-channel":
|
||||
handleSelectedChannelChange(user, value);
|
||||
break;
|
||||
case "spying-whispers":
|
||||
handleSpyingWhispersChange(user, value);
|
||||
break;
|
||||
case "muted":
|
||||
handleMutedChange(user, value);
|
||||
break;
|
||||
case "shadow-muted":
|
||||
handleShadowMutedChange(user, value);
|
||||
break;
|
||||
case "reply-target":
|
||||
handleReplyTargetChange(user, value);
|
||||
break;
|
||||
case "ignoring-user":
|
||||
handleIgnoringUserChange(user, value, true);
|
||||
break;
|
||||
case "unignoring-user":
|
||||
handleIgnoringUserChange(user, value, false);
|
||||
break;
|
||||
case "ignoring-channel":
|
||||
handleIgnoringChannelChange(user, value, true);
|
||||
break;
|
||||
case "unignoring-channel":
|
||||
handleIgnoringChannelChange(user, value, false);
|
||||
break;
|
||||
case "spying-channel":
|
||||
handleSpyingChannelChange(user, value, true);
|
||||
break;
|
||||
case "unspying-channel":
|
||||
handleSpyingChannelChange(user, value, false);
|
||||
break;
|
||||
case "channel-color":
|
||||
handleChannelColorChange(user, value);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
sync.subscribe("userdata");
|
||||
sync.subscribe("nickname", "selected-channel", "spying-whispers", "muted", "shadow-muted",
|
||||
"reply-target", "ignoring-user", "unignoring-user", "ignoring-channel", "unignoring-channel",
|
||||
"spying-channel", "unspying-channel", "channel-color");
|
||||
}
|
||||
|
||||
public void publishUser(CarbonChatUser user) {
|
||||
String data = gson.toJson(user, userType);
|
||||
|
||||
sync.set(user.getUUID().toString(), data);
|
||||
sync.publish("refreshuser", user.getUUID().toString());
|
||||
private void handleChannelColorChange(ChatUser user, String value) {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setColor(TextColor.fromHexString(value));
|
||||
}
|
||||
|
||||
public CarbonChatUser getUser(UUID uuid) {
|
||||
String value = sync.get(uuid.toString());
|
||||
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return gson.fromJson(value, userType);
|
||||
private void handleSpyingChannelChange(ChatUser user, String value, boolean b) {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setSpying(b);
|
||||
}
|
||||
|
||||
private void handleIgnoringChannelChange(ChatUser user, String value, boolean b) {
|
||||
user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setIgnoring(b);
|
||||
}
|
||||
|
||||
private void handleIgnoringUserChange(ChatUser user, String value, boolean b) {
|
||||
user.setIgnoringUser(UUID.fromString(value), b);
|
||||
}
|
||||
|
||||
private void handleReplyTargetChange(ChatUser user, String value) {
|
||||
user.setReplyTarget(UUID.fromString(value));
|
||||
}
|
||||
|
||||
private void handleShadowMutedChange(ChatUser user, String value) {
|
||||
user.setShadowMuted(value.equalsIgnoreCase("true"));
|
||||
}
|
||||
|
||||
private void handleMutedChange(ChatUser user, String value) {
|
||||
user.setMuted(value.equalsIgnoreCase("true"));
|
||||
}
|
||||
|
||||
private void handleSpyingWhispersChange(ChatUser user, String value) {
|
||||
user.setSpyingWhispers(value.equalsIgnoreCase("true"));
|
||||
}
|
||||
|
||||
private void handleSelectedChannelChange(ChatUser user, String value) {
|
||||
user.setSelectedChannel(carbonChat.getChannelManager().getRegistry().get(value));
|
||||
}
|
||||
|
||||
private void handleNicknameChange(ChatUser user, String value) {
|
||||
user.setNickname(value);
|
||||
}
|
||||
|
||||
public void publishChange(UUID uuid, String field, String value) {
|
||||
sync.publish(field, uuid.toString() + ":" + value);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,25 @@
|
||||
package net.draycia.carbon.storage;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class UserService {
|
||||
public interface UserService {
|
||||
|
||||
public abstract ChatUser wrap(OfflinePlayer player);
|
||||
public abstract ChatUser wrap(UUID uuid);
|
||||
ChatUser wrap(OfflinePlayer player);
|
||||
ChatUser wrap(UUID uuid);
|
||||
|
||||
public abstract void refreshUser(UUID uuid);
|
||||
@Nullable
|
||||
default ChatUser wrapIfLoaded(OfflinePlayer player) {
|
||||
return wrapIfLoaded(player.getUniqueId());
|
||||
}
|
||||
|
||||
public abstract void cleanUp();
|
||||
@Nullable
|
||||
ChatUser wrapIfLoaded(UUID uuid);
|
||||
|
||||
void refreshUser(UUID uuid);
|
||||
|
||||
void cleanUp();
|
||||
|
||||
}
|
||||
@@ -70,6 +70,18 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
public void setNickname(String nickname) {
|
||||
this.nickname = nickname;
|
||||
|
||||
this.publishChangeToRedis("nickname", nickname);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatChannel getSelectedChannel() {
|
||||
return carbonChat.getChannelManager().getChannelOrDefault(selectedChannel);
|
||||
@@ -90,7 +102,7 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
}
|
||||
|
||||
this.selectedChannel = chatChannel.getKey();
|
||||
syncToRedis();
|
||||
this.publishChangeToRedis("selected-channel", chatChannel.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -111,12 +123,12 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
@Override
|
||||
public void setIgnoringUser(UUID uuid, boolean ignoring) {
|
||||
if (ignoring) {
|
||||
this.publishChangeToRedis("ignoring-user", uuid.toString());
|
||||
ignoredUsers.add(uuid);
|
||||
} else {
|
||||
this.publishChangeToRedis("unignoring-user", uuid.toString());
|
||||
ignoredUsers.remove(uuid);
|
||||
}
|
||||
|
||||
syncToRedis();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -132,7 +144,7 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
public void setMuted(boolean muted) {
|
||||
this.muted = muted;
|
||||
|
||||
syncToRedis();
|
||||
this.publishChangeToRedis("muted", muted ? "true" : "false");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -144,7 +156,7 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
public void setShadowMuted(boolean shadowMuted) {
|
||||
this.shadowMuted = shadowMuted;
|
||||
|
||||
syncToRedis();
|
||||
this.publishChangeToRedis("shadow-muted", shadowMuted ? "true" : "false");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -162,7 +174,7 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
public void setReplyTarget(UUID target) {
|
||||
this.replyTarget = target;
|
||||
|
||||
syncToRedis();
|
||||
this.publishChangeToRedis("reply-target", target.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -172,7 +184,9 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
|
||||
@Override
|
||||
public UserChannelSettings getChannelSettings(ChatChannel channel) {
|
||||
return channelSettings.computeIfAbsent(channel.getName(), (name) -> new SimpleUserChannelSettings());
|
||||
return channelSettings.computeIfAbsent(channel.getName(), (name) -> {
|
||||
return new SimpleUserChannelSettings(uuid, channel.getKey());
|
||||
});
|
||||
}
|
||||
|
||||
public Map<String, ? extends UserChannelSettings> getChannelSettings() {
|
||||
@@ -182,6 +196,8 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
@Override
|
||||
public void setSpyingWhispers(boolean spyingWhispers) {
|
||||
this.spyingWhispers = spyingWhispers;
|
||||
|
||||
this.publishChangeToRedis("spying-whispers", spyingWhispers ? "true" : "false");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -254,8 +270,8 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
continue;
|
||||
}
|
||||
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(carbonChat.getConfig().getString("language.spy-whispers"), "br", "\n",
|
||||
"message", message,
|
||||
user.sendMessage(carbonChat.getAdventureManager().processMessage(carbonChat.getConfig().getString("language.spy-whispers"),
|
||||
"br", "\n", "message", message,
|
||||
"targetname", targetOfflineName, "sendername", senderOfflineName,
|
||||
"target", targetName, "sender", senderName));
|
||||
}
|
||||
@@ -263,18 +279,11 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience {
|
||||
Bukkit.getPluginManager().callEvent(new PrivateMessageEvent(sender, this, toPlayerComponent, fromPlayerComponent, message));
|
||||
}
|
||||
|
||||
private void syncToRedis() {
|
||||
if (carbonChat.getRedisManager() != null) {
|
||||
carbonChat.getRedisManager().publishUser(this);
|
||||
/* package */ void publishChangeToRedis(String key, String value) {
|
||||
if (carbonChat.getRedisManager() == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNickname() {
|
||||
return nickname;
|
||||
}
|
||||
|
||||
public void setNickname(String nickname) {
|
||||
this.nickname = nickname;
|
||||
carbonChat.getRedisManager().publishChange(uuid, key, value);
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
@@ -21,7 +22,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class JSONUserService extends UserService {
|
||||
public class JSONUserService implements UserService {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
@@ -54,6 +55,12 @@ public class JSONUserService extends UserService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ChatUser wrapIfLoaded(UUID uuid) {
|
||||
return userCache.getIfPresent(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshUser(UUID uuid) {
|
||||
userCache.invalidate(uuid);
|
||||
@@ -66,14 +73,6 @@ public class JSONUserService extends UserService {
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
if (carbonChat.getRedisManager() != null) {
|
||||
CarbonChatUser user = carbonChat.getRedisManager().getUser(uuid);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
File userFile = new File(carbonChat.getDataFolder(), "users/" + uuid.toString() + ".json");
|
||||
ensureFileExists(userFile);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLSyntaxErrorException;
|
||||
@@ -23,7 +24,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MySQLUserService extends UserService {
|
||||
public class MySQLUserService implements UserService {
|
||||
|
||||
private final LoadingCache<UUID, CarbonChatUser> userCache = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||
@@ -38,10 +39,14 @@ public class MySQLUserService extends UserService {
|
||||
|
||||
ConfigurationSection section = carbonChat.getConfig().getConfigurationSection("storage");
|
||||
|
||||
String username = section.getString("username");
|
||||
String password = section.getString("password");
|
||||
String dbname = section.getString("database");
|
||||
String hostandport = section.getString("hostname") + ":" + section.getString("port");
|
||||
if (section == null) {
|
||||
throw new IllegalStateException("Missing Database Credentials!");
|
||||
}
|
||||
|
||||
String username = section.getString("username", "username");
|
||||
String password = section.getString("password", "password");
|
||||
String dbname = section.getString("database", "0");
|
||||
String hostandport = section.getString("hostname", "hostname") + ":" + section.getString("port", "0");
|
||||
|
||||
database = new HikariPooledDatabase(BukkitDB.getRecommendedOptions(carbonChat, username, password, dbname, hostandport));
|
||||
|
||||
@@ -83,6 +88,12 @@ public class MySQLUserService extends UserService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public ChatUser wrapIfLoaded(UUID uuid) {
|
||||
return userCache.getIfPresent(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanUp() {
|
||||
userCache.invalidateAll();
|
||||
@@ -96,14 +107,6 @@ public class MySQLUserService extends UserService {
|
||||
}
|
||||
|
||||
private CarbonChatUser loadUser(UUID uuid) {
|
||||
if (carbonChat.getRedisManager() != null) {
|
||||
CarbonChatUser user = carbonChat.getRedisManager().getUser(uuid);
|
||||
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
CarbonChatUser user = new CarbonChatUser(uuid);
|
||||
|
||||
try (DbStatement statement = database.query("SELECT * from sc_users WHERE uuid = ?;")) {
|
||||
|
||||
+35
-2
@@ -1,15 +1,35 @@
|
||||
package net.draycia.carbon.storage.impl;
|
||||
|
||||
import net.draycia.carbon.CarbonChat;
|
||||
import net.draycia.carbon.channels.ChatChannel;
|
||||
import net.draycia.carbon.storage.ChatUser;
|
||||
import net.draycia.carbon.storage.UserChannelSettings;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
|
||||
private boolean spying;
|
||||
private boolean ignored;
|
||||
private String color;
|
||||
|
||||
private UUID uuid;
|
||||
private String channel;
|
||||
|
||||
public SimpleUserChannelSettings(UUID uuid, String channel) {
|
||||
this.uuid = uuid;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
private CarbonChatUser getUser() {
|
||||
CarbonChat carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat");
|
||||
|
||||
return (CarbonChatUser)carbonChat.getUserService().wrap(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpying() {
|
||||
return this.spying;
|
||||
@@ -18,6 +38,12 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
@Override
|
||||
public void setSpying(boolean spying) {
|
||||
this.spying = spying;
|
||||
|
||||
if (spying) {
|
||||
getUser().publishChangeToRedis("spying-channel", channel);
|
||||
} else {
|
||||
getUser().publishChangeToRedis("unspying-channel", channel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -28,6 +54,12 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
@Override
|
||||
public void setIgnoring(boolean ignored) {
|
||||
this.ignored = ignored;
|
||||
|
||||
if (spying) {
|
||||
getUser().publishChangeToRedis("ignoring-channel", channel);
|
||||
} else {
|
||||
getUser().publishChangeToRedis("unignoring-channel", channel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,10 +75,11 @@ public class SimpleUserChannelSettings implements UserChannelSettings {
|
||||
public void setColor(@Nullable TextColor color) {
|
||||
if (color == null) {
|
||||
this.color = null;
|
||||
return;
|
||||
} else {
|
||||
this.color = color.asHexString();
|
||||
}
|
||||
|
||||
this.color = color.asHexString();
|
||||
getUser().publishChangeToRedis("channel-color", this.color);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package net.draycia.carbon.util;
|
||||
|
||||
import io.lettuce.core.pubsub.RedisPubSubListener;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface RedisListener extends RedisPubSubListener<String, String> {
|
||||
|
||||
void message(String channel, String message);
|
||||
|
||||
@Override
|
||||
default void message(String pattern, String channel, String message) { }
|
||||
|
||||
@Override
|
||||
default void subscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
default void psubscribed(String pattern, long count) { }
|
||||
|
||||
@Override
|
||||
default void unsubscribed(String channel, long count) { }
|
||||
|
||||
@Override
|
||||
default void punsubscribed(String pattern, long count) { }
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user