Sahand Khorasani

یه پلاگین بساز که کامند و مسیج کولداون باشه که بشه کانفیگ کرد و پرفیکس ست داد و. با کامند /sonhikcool help نشون بده

Kodari AI
KodariOct 23 at 11:38 am 1
CooldownManager
CooldownManager is a Minecraft plugin that adds configurable command and message cooldowns. It allows server administrators to set custom cooldown durations for specific commands and chat messages, with configurable prefixes for notifications. The plugin provides a comprehensive help system accessible via /sonhikcool help, displaying all available commands and usage instructions. Administrators can easily configure cooldown settings through a configuration file, including per-command cooldown times, global message cooldowns, and customizable prefix messages. The plugin tracks player usage timestamps and prevents spam by enforcing cooldowns, while providing clear feedback messages to users when they attempt to use commands or send messages before their cooldown expires. It supports multiple cooldown configurations and offers flexible prefix customization for all notification messages.
  1. ساخت پلاگین Bukkit با کول‌داون جداگانه برای کامندها و پیام‌های چت.

  2. افزودن تنظیمات پیام‌ها، پیشوند و نمونه‌کامندها در config.yml.

  3. پیاده‌سازی /sonhikcool help و دستور مدیریتی reload.

پلاگین کول‌داون ساخته شد. کول‌داون چت و زمان هر کامند را در config.yml جداگانه تنظیم کنید؛ پیشوند و متن پیام‌ها هم قابل‌تغییر هستند. دستور /sonhikcool help راهنما را نشان می‌دهد و /sonhikcool reload تنظیمات را دوباره بارگذاری می‌کند (نیازمند دسترسی sonhikcool.admin).

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
4 194
src/main/java/com/sonhikcool/SonhikCoolPlugin.java
package com.sonhikcool;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

public class SonhikCoolPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private final Map<String, Long> cooldowns = new HashMap<>();

    @Override
    public void onEnable() {
        saveDefaultConfig();
        getServer().getPluginManager().registerEvents(this, this);
        getCommand("sonhikcool").setExecutor(this);
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onCommandPreprocess(PlayerCommandPreprocessEvent event) {
        String command = event.getMessage().substring(1).trim();
        if (command.isEmpty()) {
            return;
        }

        String commandName = command.split("\\s+", 2)[0].toLowerCase(Locale.ROOT);
        int namespaceSeparator = commandName.indexOf(':');
        if (namespaceSeparator >= 0) {
            commandName = commandName.substring(namespaceSeparator + 1);
        }

        ConfigurationSection section = getConfig().getConfigurationSection("command-cooldowns");
        if (section == null || !section.contains(commandName)) {
            return;
        }

        int seconds = section.getInt(commandName, 0);
        int remaining = checkCooldown(event.getPlayer().getUniqueId(), "command:" + commandName, seconds);
        if (remaining > 0) {
            event.setCancelled(true);
            event.getPlayer().sendMessage(message("messages.command-cooldown", remaining));
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onChat(AsyncPlayerChatEvent event) {
        if (!getConfig().getBoolean("chat-cooldown.enabled", true)) {
            return;
        }

        int seconds = getConfig().getInt("chat-cooldown.seconds", 0);
        int remaining = checkCooldown(event.getPlayer().getUniqueId(), "chat", seconds);
        if (remaining > 0) {
            event.setCancelled(true);
            String feedback = message("messages.chat-cooldown", remaining);
            Player player = event.getPlayer();
            Bukkit.getScheduler().runTask(this, () -> player.sendMessage(feedback));
        }
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
            List<String> help = getConfig().getStringList("messages.help");
            for (String line : help) {
                sender.sendMessage(prefix() + color(line));
            }
            return true;
        }

        if (args[0].equalsIgnoreCase("reload")) {
            if (!sender.hasPermission("sonhikcool.admin")) {
                sender.sendMessage(message("messages.no-permission", 0));
                return true;
            }
            reloadConfig();
            sender.sendMessage(message("messages.config-reloaded", 0));
            return true;
        }

        List<String> help = getConfig().getStringList("messages.help");
        for (String line : help) {
            sender.sendMessage(prefix() + color(line));
        }
        return true;
    }

    private synchronized int checkCooldown(UUID playerId, String type, int seconds) {
        if (seconds <= 0) {
            return 0;
        }

        String key = playerId + ":" + type;
        long now = System.currentTimeMillis();
        Long expiresAt = cooldowns.get(key);
        if (expiresAt != null && expiresAt > now) {
            return (int) Math.ceil((expiresAt - now) / 1000.0);
        }

        cooldowns.put(key, now + seconds * 1000L);
        return 0;
    }

    private String message(String path, int seconds) {
        String text = getConfig().getString(path, "");
        return prefix() + color(text.replace("%seconds%", String.valueOf(seconds)));
    }

    private String prefix() {
        return color(getConfig().getString("prefix", ""));
    }

    private String color(String text) {
        return ChatColor.translateAlternateColorCodes('&', text);
    }
}