Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
fix: default channels
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Mar 29, 2024
1 parent 8771648 commit 095358c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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> channel = plugin.getChannels().getChannel(
plugin.getUserCache().getPlayerChannel(player.getUuid())
);
final Optional<Channel> channel = plugin.getUserCache().getPlayerChannel(player.getUuid())
.flatMap(channelId -> plugin.getChannels().getChannel(channelId));
if (channel.isEmpty()) {
plugin.getLocales().sendMessage(player, "error_no_channel");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> channel = plugin.getChannels().getChannel(
plugin.getUserCache().getPlayerChannel(player.getUuid())
);
final Optional<Channel> channel = plugin.getUserCache().getPlayerChannel(player.getUuid())
.flatMap(channelId -> plugin.getChannels().getChannel(channelId));
if (channel.isEmpty()) {
plugin.getLocales().sendMessage(player, "error_no_channel");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;

/**
* The HuskChat API
Expand All @@ -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<String> getPlayerChannel(@NotNull OnlineUser player) {
return plugin.getUserCache().getPlayerChannel(player.getUuid());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Optional;

@AllArgsConstructor
public abstract class PlayerListener {
Expand All @@ -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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class UserCache {
protected LinkedHashMap<UUID, SpyColor> socialSpies = new LinkedHashMap<>();

@NotNull
public String getPlayerChannel(@NotNull UUID uuid) {
return playerChannels.get(uuid);
public Optional<String> getPlayerChannel(@NotNull UUID uuid) {
return Optional.ofNullable(playerChannels.get(uuid));
}

public Optional<Set<UUID>> getLastMessengers(@NotNull UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public interface VelocityChatListener {

default boolean handlePlayerChat(PlayerChatEvent e) {
final VelocityUser player = VelocityUser.adapt(e.getPlayer(), plugin());
final Optional<Channel> channel = plugin().getChannels().getChannel(
plugin().getUserCache().getPlayerChannel(player.getUuid())
);
final Optional<Channel> channel = plugin().getUserCache().getPlayerChannel(player.getUuid())
.flatMap(channelId -> plugin().getChannels().getChannel(channelId));
if (channel.isEmpty()) {
plugin().getLocales().sendMessage(player, "error_no_channel");
return false;
Expand Down

0 comments on commit 095358c

Please sign in to comment.