From 5c6d05eb33002eee365624fb0d7d2a94674de7ea Mon Sep 17 00:00:00 2001 From: Draycia Date: Fri, 7 Aug 2020 03:58:43 -0700 Subject: [PATCH] Initial steps in fixing redis and nicknames. --- .../draycia/carbon/managers/RedisManager.java | 32 +++++-- .../net/draycia/carbon/storage/ChatUser.java | 55 +++++++++-- .../carbon/storage/UserChannelSettings.java | 15 ++- .../carbon/storage/impl/CarbonChatUser.java | 92 +++++++++++-------- .../impl/SimpleUserChannelSettings.java | 30 +++--- 5 files changed, 150 insertions(+), 74 deletions(-) diff --git a/CarbonChat/src/main/java/net/draycia/carbon/managers/RedisManager.java b/CarbonChat/src/main/java/net/draycia/carbon/managers/RedisManager.java index a0a706c0..b38b7c38 100644 --- a/CarbonChat/src/main/java/net/draycia/carbon/managers/RedisManager.java +++ b/CarbonChat/src/main/java/net/draycia/carbon/managers/RedisManager.java @@ -49,6 +49,7 @@ public class RedisManager { subscribeConnection.addListener((RedisListener)(channel, message) -> { UUID uuid = UUID.fromString(message.split(":", 2)[0]); + System.out.println("receiving message, uuid: " + uuid); ChatUser user = carbonChat.getUserService().wrapIfLoaded(uuid); @@ -58,6 +59,8 @@ public class RedisManager { String value = message.split(":", 2)[1]; + System.out.println("user not null, value: " + value); + switch (channel.toLowerCase()) { case "nickname": handleNicknameChange(user, value); @@ -107,46 +110,55 @@ public class RedisManager { } private void handleChannelColorChange(ChatUser user, String value) { - user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setColor(TextColor.fromHexString(value)); + user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setColor(TextColor.fromHexString(value), true); } private void handleSpyingChannelChange(ChatUser user, String value, boolean b) { - user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setSpying(b); + user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setSpying(b, true); } private void handleIgnoringChannelChange(ChatUser user, String value, boolean b) { - user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setIgnoring(b); + user.getChannelSettings(carbonChat.getChannelManager().getRegistry().get(value)).setIgnoring(b, true); } private void handleIgnoringUserChange(ChatUser user, String value, boolean b) { - user.setIgnoringUser(UUID.fromString(value), b); + user.setIgnoringUser(UUID.fromString(value), b, true); } private void handleReplyTargetChange(ChatUser user, String value) { - user.setReplyTarget(UUID.fromString(value)); + user.setReplyTarget(UUID.fromString(value), true); } private void handleShadowMutedChange(ChatUser user, String value) { - user.setShadowMuted(value.equalsIgnoreCase("true")); + user.setShadowMuted(value.equalsIgnoreCase("true"), true); } private void handleMutedChange(ChatUser user, String value) { - user.setMuted(value.equalsIgnoreCase("true")); + user.setMuted(value.equalsIgnoreCase("true"), true); } private void handleSpyingWhispersChange(ChatUser user, String value) { - user.setSpyingWhispers(value.equalsIgnoreCase("true")); + user.setSpyingWhispers(value.equalsIgnoreCase("true"), true); } private void handleSelectedChannelChange(ChatUser user, String value) { - user.setSelectedChannel(carbonChat.getChannelManager().getRegistry().get(value)); + user.setSelectedChannel(carbonChat.getChannelManager().getRegistry().get(value), true); } private void handleNicknameChange(ChatUser user, String value) { - user.setNickname(value); + System.out.println("nickname set!"); + user.setNickname(value, true); + + if (user.isOnline()) { + String message = carbonChat.getConfig().getString("language.nickname-set"); + + user.sendMessage(carbonChat.getAdventureManager().processMessageWithPapi(user.asPlayer(), + message, "nickname", value)); + } } public void publishChange(UUID uuid, String field, String value) { + System.out.println("publishing change " + field); publishSync.publish(field, uuid.toString() + ":" + value); } } diff --git a/CarbonChat/src/main/java/net/draycia/carbon/storage/ChatUser.java b/CarbonChat/src/main/java/net/draycia/carbon/storage/ChatUser.java index 270c0b04..60e5d13e 100644 --- a/CarbonChat/src/main/java/net/draycia/carbon/storage/ChatUser.java +++ b/CarbonChat/src/main/java/net/draycia/carbon/storage/ChatUser.java @@ -17,32 +17,67 @@ public interface ChatUser extends Audience { boolean isOnline(); String getNickname(); - void setNickname(String nickname); + void setNickname(String nickname, boolean fromRemote); + default void setNickname(String nickname) { + this.setNickname(nickname, false); + } ChatChannel getSelectedChannel(); - void setSelectedChannel(ChatChannel channel); + void setSelectedChannel(ChatChannel channel, boolean fromRemote); + default void setSelectedChannel(ChatChannel channel) { + this.setSelectedChannel(channel, false); + } void clearSelectedChannel(); UserChannelSettings getChannelSettings(ChatChannel channel); - void setSpyingWhispers(boolean spyingWhispers); boolean isSpyingWhispers(); + void setSpyingWhispers(boolean spyingWhispers, boolean fromRemote); + default void setSpyingWhispers(boolean spyingWhispers) { + this.setSpyingWhispers(spyingWhispers, false); + } - void setMuted(boolean muted); boolean isMuted(); + void setMuted(boolean muted, boolean fromRemote); + default void setMuted(boolean muted) { + this.setMuted(muted, false); + } - void setShadowMuted(boolean shadowMuted); boolean isShadowMuted(); + void setShadowMuted(boolean shadowMuted, boolean fromRemote); + default void setShadowMuted(boolean shadowMuted) { + this.setShadowMuted(shadowMuted, false); + } @Nullable UUID getReplyTarget(); - void setReplyTarget(@Nullable UUID target); - void setReplyTarget(@Nullable ChatUser user); + void setReplyTarget(@Nullable UUID target, boolean fromRemote); + default void setReplyTarget(@Nullable UUID target) { + this.setReplyTarget(target, false); + } + + default void setReplyTarget(@Nullable ChatUser user, boolean fromRemote) { + this.setReplyTarget(user.getUUID(), fromRemote); + } + default void setReplyTarget(@Nullable ChatUser user) { + this.setReplyTarget(user.getUUID(), false); + } boolean isIgnoringUser(UUID uuid); - boolean isIgnoringUser(ChatUser user); - void setIgnoringUser(UUID uuid, boolean ignoring); - void setIgnoringUser(ChatUser user, boolean ignoring); + void setIgnoringUser(UUID uuid, boolean ignoring, boolean fromRemote); + default void setIgnoringUser(UUID uuid, boolean ignoring) { + this.setIgnoringUser(uuid, ignoring, false); + } + + default boolean isIgnoringUser(ChatUser user) { + return this.isIgnoringUser(user.getUUID()); + } + default void setIgnoringUser(ChatUser user, boolean ignoring, boolean fromRemote) { + this.setIgnoringUser(user.getUUID(), ignoring, fromRemote); + } + default void setIgnoringUser(ChatUser user, boolean ignoring) { + this.setIgnoringUser(user.getUUID(), ignoring, false); + } void sendMessage(ChatUser sender, String message); diff --git a/CarbonChat/src/main/java/net/draycia/carbon/storage/UserChannelSettings.java b/CarbonChat/src/main/java/net/draycia/carbon/storage/UserChannelSettings.java index fc83af0a..9077b9d5 100644 --- a/CarbonChat/src/main/java/net/draycia/carbon/storage/UserChannelSettings.java +++ b/CarbonChat/src/main/java/net/draycia/carbon/storage/UserChannelSettings.java @@ -6,12 +6,21 @@ import org.jetbrains.annotations.Nullable; public interface UserChannelSettings { boolean isSpying(); - void setSpying(boolean spying); + void setSpying(boolean spying, boolean fromRemote); + default void setSpying(boolean spying) { + this.setSpying(spying, false); + } boolean isIgnored(); - void setIgnoring(boolean ignored); + void setIgnoring(boolean ignored, boolean fromRemote); + default void setIgnoring(boolean ignored) { + this.setIgnoring(ignored, false); + } @Nullable TextColor getColor(); - void setColor(@Nullable TextColor color); + void setColor(@Nullable TextColor color, boolean fromRemote); + default void setColor(@Nullable TextColor color) { + this.setColor(color, false); + } } diff --git a/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/CarbonChatUser.java b/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/CarbonChatUser.java index b4a5f0a7..9a658580 100644 --- a/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/CarbonChatUser.java +++ b/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/CarbonChatUser.java @@ -37,11 +37,11 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { private transient UUID replyTarget = null; public CarbonChatUser() { - this.carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat"); + this.carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat"); } public CarbonChatUser(UUID uuid) { - this.carbonChat = (CarbonChat)Bukkit.getPluginManager().getPlugin("CarbonChat"); + this.carbonChat = (CarbonChat) Bukkit.getPluginManager().getPlugin("CarbonChat"); this.uuid = uuid; } @@ -75,17 +75,26 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { return nickname; } - public void setNickname(String nickname) { - this.nickname = nickname; + @Override + public void setNickname(String nickname, boolean fromRemote) { + Component component = carbonChat.getAdventureManager().processMessage(nickname); + String legacyNickname = CarbonChat.LEGACY.serialize(component); + + System.out.println(legacyNickname); + + this.nickname = legacyNickname; if (isOnline()) { - Component component = carbonChat.getAdventureManager().processMessage(nickname); - String legacyNickname = CarbonChat.LEGACY.serialize(component); - - this.asPlayer().setDisplayName(legacyNickname); + if (nickname != null) { + this.asPlayer().setDisplayName(legacyNickname); + } else { + this.asPlayer().setDisplayName(null); + } } - this.publishChangeToRedis("nickname", nickname); + if (!fromRemote) { + this.publishChangeToRedis("nickname", legacyNickname); + } } @Override @@ -94,7 +103,7 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { } @Override - public void setSelectedChannel(ChatChannel chatChannel) { + public void setSelectedChannel(ChatChannel chatChannel, boolean fromRemote) { String failureMessage = carbonChat.getConfig().getString("language.channel-switch-failure"); ChannelSwitchEvent event = new ChannelSwitchEvent(chatChannel, this, failureMessage); @@ -108,7 +117,10 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { } this.selectedChannel = chatChannel.getKey(); - this.publishChangeToRedis("selected-channel", chatChannel.getKey()); + + if (!fromRemote) { + this.publishChangeToRedis("selected-channel", chatChannel.getKey()); + } } @Override @@ -122,35 +134,33 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { } @Override - public boolean isIgnoringUser(ChatUser user) { - return ignoredUsers.contains(user.getUUID()); - } - - @Override - public void setIgnoringUser(UUID uuid, boolean ignoring) { + public void setIgnoringUser(UUID uuid, boolean ignoring, boolean fromRemote) { if (ignoring) { - this.publishChangeToRedis("ignoring-user", uuid.toString()); + if (!fromRemote) { + this.publishChangeToRedis("ignoring-user", uuid.toString()); + } + ignoredUsers.add(uuid); } else { - this.publishChangeToRedis("unignoring-user", uuid.toString()); + if (!fromRemote) { + this.publishChangeToRedis("unignoring-user", uuid.toString()); + } + ignoredUsers.remove(uuid); } } - @Override - public void setIgnoringUser(ChatUser user, boolean ignoring) { - setIgnoringUser(user.getUUID(), ignoring); - } - public List getIgnoredUsers() { return ignoredUsers; } @Override - public void setMuted(boolean muted) { + public void setMuted(boolean muted, boolean fromRemote) { this.muted = muted; - this.publishChangeToRedis("muted", muted ? "true" : "false"); + if (!fromRemote) { + this.publishChangeToRedis("muted", muted ? "true" : "false"); + } } @Override @@ -159,10 +169,12 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { } @Override - public void setShadowMuted(boolean shadowMuted) { + public void setShadowMuted(boolean shadowMuted, boolean fromRemote) { this.shadowMuted = shadowMuted; - this.publishChangeToRedis("shadow-muted", shadowMuted ? "true" : "false"); + if (!fromRemote) { + this.publishChangeToRedis("shadow-muted", shadowMuted ? "true" : "false"); + } } @Override @@ -177,15 +189,12 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { } @Override - public void setReplyTarget(UUID target) { + public void setReplyTarget(UUID target, boolean fromRemote) { this.replyTarget = target; - this.publishChangeToRedis("reply-target", target.toString()); - } - - @Override - public void setReplyTarget(ChatUser user) { - setReplyTarget(user.getUUID()); + if (!fromRemote) { + this.publishChangeToRedis("reply-target", target.toString()); + } } @Override @@ -200,10 +209,12 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { } @Override - public void setSpyingWhispers(boolean spyingWhispers) { + public void setSpyingWhispers(boolean spyingWhispers, boolean fromRemote) { this.spyingWhispers = spyingWhispers; - this.publishChangeToRedis("spying-whispers", spyingWhispers ? "true" : "false"); + if (!fromRemote) { + this.publishChangeToRedis("spying-whispers", spyingWhispers ? "true" : "false"); + } } @Override @@ -233,12 +244,12 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { targetName = this.asPlayer().getDisplayName(); } - Component toPlayerComponent = carbonChat.getAdventureManager().processMessage(toPlayerFormat, "br", "\n", + Component toPlayerComponent = carbonChat.getAdventureManager().processMessage(toPlayerFormat, "br", "\n", "message", message, "targetname", targetOfflineName, "sendername", senderOfflineName, "target", targetName, "sender", senderName); - Component fromPlayerComponent = carbonChat.getAdventureManager().processMessage(fromPlayerFormat, "br", "\n", + Component fromPlayerComponent = carbonChat.getAdventureManager().processMessage(fromPlayerFormat, "br", "\n", "message", message, "targetname", targetOfflineName, "sendername", senderOfflineName, "target", targetName, "sender", senderName); @@ -290,10 +301,13 @@ public class CarbonChatUser implements ChatUser, ForwardingAudience { } /* package */ void publishChangeToRedis(String key, String value) { + System.out.println("checking if null"); if (carbonChat.getRedisManager() == null) { return; } + System.out.println("not null"); + carbonChat.getRedisManager().publishChange(uuid, key, value); } } diff --git a/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/SimpleUserChannelSettings.java b/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/SimpleUserChannelSettings.java index 5d8bdde9..2649de0f 100644 --- a/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/SimpleUserChannelSettings.java +++ b/CarbonChat/src/main/java/net/draycia/carbon/storage/impl/SimpleUserChannelSettings.java @@ -36,13 +36,15 @@ public class SimpleUserChannelSettings implements UserChannelSettings { } @Override - public void setSpying(boolean spying) { + public void setSpying(boolean spying, boolean fromRemote) { this.spying = spying; - if (spying) { - getUser().publishChangeToRedis("spying-channel", channel); - } else { - getUser().publishChangeToRedis("unspying-channel", channel); + if (fromRemote) { + if (spying) { + getUser().publishChangeToRedis("spying-channel", channel); + } else { + getUser().publishChangeToRedis("unspying-channel", channel); + } } } @@ -52,13 +54,15 @@ public class SimpleUserChannelSettings implements UserChannelSettings { } @Override - public void setIgnoring(boolean ignored) { + public void setIgnoring(boolean ignored, boolean fromRemote) { this.ignored = ignored; - if (spying) { - getUser().publishChangeToRedis("ignoring-channel", channel); - } else { - getUser().publishChangeToRedis("unignoring-channel", channel); + if (fromRemote) { + if (spying) { + getUser().publishChangeToRedis("ignoring-channel", channel); + } else { + getUser().publishChangeToRedis("unignoring-channel", channel); + } } } @@ -72,14 +76,16 @@ public class SimpleUserChannelSettings implements UserChannelSettings { } @Override - public void setColor(@Nullable TextColor color) { + public void setColor(@Nullable TextColor color, boolean fromRemote) { if (color == null) { this.color = null; } else { this.color = color.asHexString(); } - getUser().publishChangeToRedis("channel-color", this.color); + if (fromRemote) { + getUser().publishChangeToRedis("channel-color", this.color); + } } }