最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Incorrect operation of radius check for local messages (Plugin MC, Spigot 1.15.2, Java 8, Gradle) - Stack Overflow

programmeradmin1浏览0评论

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...

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论