I was writing my plugin and during one of the changes, my radius stopped working, that is, all messages are sent both local and global, but if player_1 writes messages in the local chat, then player_2 a couple of blocks away from him will not see it (In the logs - the message is sent) and the biggest snag is that I can't see where exactly the error is, because I simply forgot due to a long break.... (I tried to rewrite the radius method, but still nothing)
That's why I would like to get help here.
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
Player player = event.getPlayer();
String message = event.getMessage();
if (message.startsWith("!")) {
handleGlobalChat(message.substring(1), player);
} else {
handleLocalChat(message, player);
}
event.setCancelled(true);
}
private void handleLocalChat(String message, Player player) {
UUID uuid = player.getUniqueId();
long currentTime = System.currentTimeMillis();
if (player.hasPermission("testchat.chat.local.bypass")) {
logger.logMessage(player.getName(), message, "LOCAL");
sendLocalChat(message, player);
return;
}
if (lastMessageTimes.containsKey(uuid)) {
long lastMessageTime = lastMessageTimes.get(uuid);
long elapsedTime = currentTime - lastMessageTime;
if (elapsedTime / 1000 < chatCooldown) {
player.sendMessage(ChatColor.RED + "Пожалуйста, подождите " + (chatCooldown - (int)(elapsedTime / 1000)) + " сек. перед отправкой сообщения");
return;
}
}
lastMessageTimes.put(uuid, currentTime);
logger.logMessage(player.getName(), message, "LOCAL");
sendLocalChat(message, player);
}
private void handleGlobalChat(String message, Player player) {
UUID uuid = player.getUniqueId();
long currentTime = System.currentTimeMillis();
if (player.hasPermission("testchat.chat.global.bypass")) {
logger.logMessage(player.getName(), message, "GLOBAL");
sendGlobalChat(message, player);
return;
}
if (lastMessageTimes.containsKey(uuid)) {
long lastMessageTime = lastMessageTimes.get(uuid);
long elapsedTime = currentTime - lastMessageTime;
if (elapsedTime / 1000 < chatCooldown) {
player.sendMessage(ChatColor.RED + "Пожалуйста, подождите " + (chatCooldown - (int)(elapsedTime / 1000)) + " сек. перед отправкой сообщения");
return;
}
}
lastMessageTimes.put(uuid, currentTime);
logger.logMessage(player.getName(), message, "GLOBAL");
sendGlobalChat(message, player);
}
private void sendLocalChat(String message, Player player) {
Location playerLocation = player.getLocation();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
if (isInRange(playerLocation, onlinePlayer.getLocation())) {
onlinePlayer.sendMessage(formatLocalChat(message, player));
}
}
}
private void sendGlobalChat(String message, Player player) {
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
onlinePlayer.sendMessage(formatGlobalChat(message, player));
}
}
private String formatLocalChat(String message, Player player) {
String formattedMessage = localChatFormat
.replace("{player}", player.getName())
.replace("{message}", message);
return ChatColor.translateAlternateColorCodes('&', setPlaceholders(formattedMessage, player));
}
private String formatGlobalChat(String message, Player player) {
String formattedMessage = globalChatFormat
.replace("{player}", player.getName())
.replace("{message}", message);
return ChatColor.translateAlternateColorCodes('&', setPlaceholders(formattedMessage, player));
}
private boolean isInRange(Location location1, Location location2) {
return location1.distance(location2) <= localChatRadius;
}
Tried to remove the method for checking the distance between players - it worked, but I don't need it.
Tried to rewrite the isInRange method itself, but it didn't work...