Skip to content

Commit 5017c71

Browse files
tastybentoCopilot
andauthored
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 1e478fb commit 5017c71

1 file changed

Lines changed: 37 additions & 27 deletions

File tree

src/main/java/world/bentobox/chat/listeners/ChatListener.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ public void execute(Listener listener, Event e) {
6565
onChat((AsyncPlayerChatEvent) e);
6666
}
6767

68-
public void onChat(final AsyncPlayerChatEvent e) {
69-
70-
Player p = e.getPlayer();
71-
World playerWorld = e.getPlayer().getWorld();
68+
private boolean handleChatSync(Player p, String message) {
69+
boolean handled = false;
70+
World playerWorld = p.getWorld();
7271

7372
// Determine the worlds to use for team chat
7473
List<World> teamChatWorlds = new ArrayList<>();
@@ -81,41 +80,52 @@ public void onChat(final AsyncPlayerChatEvent e) {
8180
if (teamChatWorlds.isEmpty()) {
8281
addon.getChatWorld().ifPresent(teamChatWorlds::add);
8382
}
84-
// If still empty, nothing to do
85-
if (teamChatWorlds.isEmpty()) {
86-
return;
87-
}
8883
}
8984

9085
// Process team chat for all matching worlds.
9186
// If multiple game modes cover the same extra world, chat goes to all matching teams.
92-
if (teamChatUsers.contains(p.getUniqueId())) {
87+
if (!teamChatWorlds.isEmpty() && teamChatUsers.contains(p.getUniqueId())) {
9388
for (World w : teamChatWorlds) {
9489
if (addon.getIslands().inTeam(w, p.getUniqueId())) {
95-
// Cancel the event
96-
e.setCancelled(true);
97-
if (e.isAsynchronous()) {
98-
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> teamChat(w, p, e.getMessage()));
99-
} else {
100-
teamChat(w, p, e.getMessage());
101-
}
90+
handled = true;
91+
teamChat(w, p, message);
10292
}
10393
}
10494
}
10595

10696
// Island chat - uses physical location, only meaningful if player is on an island
107-
addon.getIslands().getIslandAt(p.getLocation())
108-
.filter(islandChatters.keySet()::contains)
109-
.filter(i -> islandChatters.get(i).contains(p))
110-
.ifPresent(i -> {
111-
// Cancel the event
112-
e.setCancelled(true);
113-
if (e.isAsynchronous()) {
114-
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> islandChat(i, p, e.getMessage()));
115-
} else {
116-
islandChat(i, p, e.getMessage());
97+
Island island = addon.getIslands().getIslandAt(p.getLocation())
98+
.filter(islandChatters.keySet()::contains)
99+
.filter(i -> islandChatters.get(i).contains(p))
100+
.orElse(null);
101+
if (island != null) {
102+
handled = true;
103+
islandChat(island, p, message);
104+
}
105+
106+
return handled;
107+
}
108+
109+
public void onChat(final AsyncPlayerChatEvent e) {
110+
111+
Player p = e.getPlayer();
112+
String message = e.getMessage();
113+
114+
if (e.isAsynchronous()) {
115+
try {
116+
Boolean handled = Bukkit.getScheduler().callSyncMethod(addon.getPlugin(),
117+
() -> handleChatSync(p, message)).get();
118+
if (Boolean.TRUE.equals(handled)) {
119+
e.setCancelled(true);
120+
}
121+
} catch (InterruptedException ex) {
122+
Thread.currentThread().interrupt();
123+
} catch (java.util.concurrent.ExecutionException ex) {
124+
throw new RuntimeException("Failed to process async chat on the main thread", ex);
117125
}
118-
});
126+
} else if (handleChatSync(p, message)) {
127+
e.setCancelled(true);
128+
}
119129
}
120130

121131
// Removes player from TeamChat set if he left the island

0 commit comments

Comments
 (0)