From 095358cda69329bb3554fad3686ce8f5d23999b9 Mon Sep 17 00:00:00 2001 From: William Date: Fri, 29 Mar 2024 15:44:21 +0000 Subject: [PATCH] fix: default channels --- .../huskchat/listener/BukkitListener.java | 5 ++--- .../huskchat/listener/BungeeListener.java | 5 ++--- .../net/william278/huskchat/api/HuskChatAPI.java | 6 ++++-- .../huskchat/listener/PlayerListener.java | 13 ++++++++++--- .../net/william278/huskchat/user/UserCache.java | 4 ++-- .../huskchat/listener/VelocityChatListener.java | 5 ++--- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/bukkit/src/main/java/net/william278/huskchat/listener/BukkitListener.java b/bukkit/src/main/java/net/william278/huskchat/listener/BukkitListener.java index c7f6930..ac8d0b7 100644 --- a/bukkit/src/main/java/net/william278/huskchat/listener/BukkitListener.java +++ b/bukkit/src/main/java/net/william278/huskchat/listener/BukkitListener.java @@ -43,9 +43,8 @@ public BukkitListener(@NotNull HuskChat plugin) { public void onPlayerChat(AsyncPlayerChatEvent e) { // Verify they are in a channel final BukkitUser player = BukkitUser.adapt(e.getPlayer(), plugin); - final Optional channel = plugin.getChannels().getChannel( - plugin.getUserCache().getPlayerChannel(player.getUuid()) - ); + final Optional channel = plugin.getUserCache().getPlayerChannel(player.getUuid()) + .flatMap(channelId -> plugin.getChannels().getChannel(channelId)); if (channel.isEmpty()) { plugin.getLocales().sendMessage(player, "error_no_channel"); return; diff --git a/bungee/src/main/java/net/william278/huskchat/listener/BungeeListener.java b/bungee/src/main/java/net/william278/huskchat/listener/BungeeListener.java index 98677c4..0a218d5 100644 --- a/bungee/src/main/java/net/william278/huskchat/listener/BungeeListener.java +++ b/bungee/src/main/java/net/william278/huskchat/listener/BungeeListener.java @@ -49,9 +49,8 @@ public void onPlayerChat(ChatEvent e) { // Verify they are in a channel final BungeeUser player = BungeeUser.adapt((ProxiedPlayer) e.getSender(), plugin); - final Optional channel = plugin.getChannels().getChannel( - plugin.getUserCache().getPlayerChannel(player.getUuid()) - ); + final Optional channel = plugin.getUserCache().getPlayerChannel(player.getUuid()) + .flatMap(channelId -> plugin.getChannels().getChannel(channelId)); if (channel.isEmpty()) { plugin.getLocales().sendMessage(player, "error_no_channel"); return; diff --git a/common/src/main/java/net/william278/huskchat/api/HuskChatAPI.java b/common/src/main/java/net/william278/huskchat/api/HuskChatAPI.java index 447c53c..96432de 100644 --- a/common/src/main/java/net/william278/huskchat/api/HuskChatAPI.java +++ b/common/src/main/java/net/william278/huskchat/api/HuskChatAPI.java @@ -28,6 +28,7 @@ import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.Optional; /** * The HuskChat API @@ -49,13 +50,14 @@ public static HuskChatAPI getInstance() { } /** - * Returns the player's current channel + * Returns the player's current channel if they are in one * * @param player The player to get the channel for + * @return The player's current channel, optionally, if they are in one * @since 3.0 */ @NotNull - public String getPlayerChannel(@NotNull OnlineUser player) { + public Optional getPlayerChannel(@NotNull OnlineUser player) { return plugin.getUserCache().getPlayerChannel(player.getUuid()); } diff --git a/common/src/main/java/net/william278/huskchat/listener/PlayerListener.java b/common/src/main/java/net/william278/huskchat/listener/PlayerListener.java index 7c192fb..53c4eb1 100644 --- a/common/src/main/java/net/william278/huskchat/listener/PlayerListener.java +++ b/common/src/main/java/net/william278/huskchat/listener/PlayerListener.java @@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Map; +import java.util.Optional; @AllArgsConstructor public abstract class PlayerListener { @@ -41,10 +42,16 @@ public final void handlePlayerSwitchServer(@NotNull OnlineUser player, @NotNull return; } - // Switch channels to the default one if their current channel is now restricted - final String currentChannel = plugin.getUserCache().getPlayerChannel(player.getUuid()); + // Switch channels to the default one if they don't have one + final Optional currentChannel = plugin.getUserCache().getPlayerChannel(player.getUuid()); + if (currentChannel.isEmpty()) { + plugin.editUserCache(c -> c.switchPlayerChannel(player, plugin.getChannels().getDefaultChannel(), plugin)); + return; + } + + // Switch the player's channel away if their current channel is now restricted plugin.getChannels().getChannels().stream() - .filter(channel -> channel.getId().equalsIgnoreCase(currentChannel)) + .filter(channel -> channel.getId().equalsIgnoreCase(currentChannel.get())) .findFirst().flatMap(channel -> channel.getRestrictedServers().stream() .filter(restrictedServer -> restrictedServer.equalsIgnoreCase(newServer)).findFirst()) .ifPresent(restricted -> plugin.editUserCache(c -> c diff --git a/common/src/main/java/net/william278/huskchat/user/UserCache.java b/common/src/main/java/net/william278/huskchat/user/UserCache.java index 2f8367f..d9f05d8 100644 --- a/common/src/main/java/net/william278/huskchat/user/UserCache.java +++ b/common/src/main/java/net/william278/huskchat/user/UserCache.java @@ -43,8 +43,8 @@ public class UserCache { protected LinkedHashMap socialSpies = new LinkedHashMap<>(); @NotNull - public String getPlayerChannel(@NotNull UUID uuid) { - return playerChannels.get(uuid); + public Optional getPlayerChannel(@NotNull UUID uuid) { + return Optional.ofNullable(playerChannels.get(uuid)); } public Optional> getLastMessengers(@NotNull UUID uuid) { diff --git a/velocity/src/main/java/net/william278/huskchat/listener/VelocityChatListener.java b/velocity/src/main/java/net/william278/huskchat/listener/VelocityChatListener.java index 58d12f9..f56dca0 100644 --- a/velocity/src/main/java/net/william278/huskchat/listener/VelocityChatListener.java +++ b/velocity/src/main/java/net/william278/huskchat/listener/VelocityChatListener.java @@ -32,9 +32,8 @@ public interface VelocityChatListener { default boolean handlePlayerChat(PlayerChatEvent e) { final VelocityUser player = VelocityUser.adapt(e.getPlayer(), plugin()); - final Optional channel = plugin().getChannels().getChannel( - plugin().getUserCache().getPlayerChannel(player.getUuid()) - ); + final Optional channel = plugin().getUserCache().getPlayerChannel(player.getUuid()) + .flatMap(channelId -> plugin().getChannels().getChannel(channelId)); if (channel.isEmpty()) { plugin().getLocales().sendMessage(player, "error_no_channel"); return false;