Skip to content

Commit

Permalink
Add method to check if player has chat enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Sep 26, 2024
1 parent 406f922 commit bcfe093
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/main/java/world/bentobox/chat/listeners/ChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public class ChatListener implements Listener, EventExecutor {
private static final String MESSAGE = "[message]";
private final Chat addon;
private final Set<UUID> teamChatUsers;
private final Map<Island, Set<Player>> islands;
private final Map<Island, Set<Player>> islandChatters;
// List of which users are spying or not on team and island chat
private final Set<UUID> spies;
private final Set<UUID> islandSpies;

public ChatListener(Chat addon) {
this.teamChatUsers = new HashSet<>();
this.islands = new HashMap<>();
this.islandChatters = new HashMap<>();
this.addon = addon;
// Initialize spies
spies = new HashSet<>();
Expand Down Expand Up @@ -83,8 +83,8 @@ public void onChat(final AsyncPlayerChatEvent e) {
}
}
addon.getIslands().getIslandAt(p.getLocation())
.filter(islands.keySet()::contains)
.filter(i -> islands.get(i).contains(p))
.filter(islandChatters.keySet()::contains)
.filter(i -> islandChatters.get(i).contains(p))
.ifPresent(i -> {
// Cancel the event
e.setCancelled(true);
Expand All @@ -99,17 +99,13 @@ public void onChat(final AsyncPlayerChatEvent e) {
// Removes player from TeamChat set if he left the island
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onLeave(TeamLeaveEvent e) {

if (teamChatUsers.contains(e.getPlayerUUID()))
teamChatUsers.remove(e.getPlayerUUID());
teamChatUsers.remove(e.getPlayerUUID());
}

// Removes player from TeamChat set if he was kicked from the island
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onKick(TeamKickEvent e) {

if (teamChatUsers.contains(e.getPlayerUUID()))
teamChatUsers.remove(e.getPlayerUUID());
teamChatUsers.remove(e.getPlayerUUID());
}

public void islandChat(Island i, Player player, String message) {
Expand Down Expand Up @@ -157,6 +153,20 @@ public boolean isTeamChat(UUID playerUUID) {
return this.teamChatUsers.contains(playerUUID);
}

/**
* Whether the player has chat on or not. Note that with multiple islands the response is true
* if *any* chat is enabled
* @param playerUUID - player's UUID
* @return true if chat is active on any island
*/
public boolean isChat(UUID playerUUID) {
Player p = Bukkit.getPlayer(playerUUID);
if (p == null) {
return false;
}
return this.islandChatters.values().stream().anyMatch(playerSet -> playerSet.contains(p));
}

/**
* Toggles team chat spy. Spy must also have the spy permission to see chats
* @param playerUUID - the player's UUID
Expand Down Expand Up @@ -206,20 +216,17 @@ public boolean togglePlayerTeamChat(UUID playerUUID) {
/**
* Toggle island chat state
* @param island - island
* @param player - player
* @return true if island chat is now on, otherwise false
*/
public boolean toggleIslandChat(Island island, Player player) {
if (islands.containsKey(island)) {
if (islands.get(island).contains(player)) {
islands.get(island).remove(player);
return false;
}
islands.get(island).add(player);
return true;
}
else {
islands.put(island, new HashSet<>());
islands.get(island).add(player);
var chatters = islandChatters.computeIfAbsent(island, k -> new HashSet<>());

if (chatters.contains(player)) {
chatters.remove(player);
return false;
} else {
chatters.add(player);
return true;
}
}
Expand Down

0 comments on commit bcfe093

Please sign in to comment.