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);
}
}