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