@Plugin(
id = "dragonsfixer",
name = "DragonsFixer",
version = "1.2",
description = "Антиспам плагин для Velocity",
authors = {"boboliii"}
)
public class DragonsFixer {
private final ProxyServer server;
private final Path dataDirectory;
private final Map<UUID, Long> cooldowns = new ConcurrentHashMap<>();
private final Map<UUID, Integer> warningCounts = new ConcurrentHashMap<>();
private final Map<UUID, List> commandTimestamps = new ConcurrentHashMap<>();
private int cooldownTime = 500;
private Component spamMessage;
private int maxWarnings;
private int kickAfterWarnings;
private Component kickMessage;
private final int maxCommandsPerSecond = 5;
@Inject
private Logger logger;
code
Assistant
@Inject
public DragonsFixer(ProxyServer server, @DataDirectory Path dataDirectory) {
this.server = server;
this.dataDirectory = dataDirectory;
this.spamMessage = Component.text("DragonsGrief » Не спамь!").color(NamedTextColor.RED);
this.maxWarnings = 15;
this.kickAfterWarnings = 20;
this.kickMessage = Component.text("DragonsGrief » Вы были кикнуты за спам командами").color(NamedTextColor.RED);
}
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
try {
this.initializeConfiguration();
} catch (Exception e) {
this.logger.error("Ошибка инициализации конфигурации", e);
}
}
private void initializeConfiguration() throws IOException {
Files.createDirectories(this.dataDirectory);
Path configPath = this.dataDirectory.resolve("config.yml");
if (Files.notExists(configPath, LinkOption.NOFOLLOW_LINKS)) {
try (InputStream inputStream = this.getClass().getResourceAsStream("/config.yml")) {
if (inputStream != null) {
Files.copy(inputStream, configPath, new CopyOption[0]);
} else {
this.createDefaultConfig(configPath);
}
}
}
this.loadConfiguration(configPath);
}
private void createDefaultConfig(Path configPath) throws IOException {
String defaultConfigContent = "cooldown: 500\nmax-warnings: 15\nkick-after-warnings: 20\nspam-message: \"§6DragonsGrief §7» §c§lНе спамь!\"\nkick-message: \"§cВы были кикнуты за спам командами\"\n";
Files.write(configPath, defaultConfigContent.getBytes(), new OpenOption[0]);
}
private void loadConfiguration(Path configPath) throws IOException {
try (InputStream input = Files.newInputStream(configPath)) {
Yaml yaml = new Yaml();
Map<String, Object> config = yaml.load(input);
this.cooldownTime = (Integer) config.getOrDefault("cooldown", 500);
this.maxWarnings = (Integer) config.getOrDefault("max-warnings", 15);
this.kickAfterWarnings = (Integer) config.getOrDefault("kick-after-warnings", 20);
this.spamMessage = LegacyComponentSerializer.legacyAmpersand().deserialize(
(String) config.getOrDefault("spam-message", "§6DragonsGrief §7» §c§lНе спамь!"));
this.kickMessage = LegacyComponentSerializer.legacyAmpersand().deserialize(
(String) config.getOrDefault("kick-message", "§cВы были кикнуты за спам командами"));
}
}
@Subscribe
public void onCommand(CommandExecuteEvent event) {
CommandSource source = event.getCommandSource();
if (source instanceof Player) {
Player player = (Player) source;
UUID playerId = player.getUniqueId();
// Получаем список временных меток команд игрока
List<Long> timestamps = this.commandTimestamps.computeIfAbsent(playerId, k -> new ArrayList<>());
long currentTime = System.currentTimeMillis();
// Убираем старые команды (старше 1 секунды)
timestamps.removeIf(timestamp -> currentTime - timestamp > 1000L);
// Добавляем текущую команду
timestamps.add(currentTime);
// Проверяем количество команд за последнюю секунду
if (timestamps.size() > maxCommandsPerSecond) {
this.addWarning(playerId, player);
event.setResult(CommandResult.denied());
player.sendMessage(this.spamMessage);
return;
}
// Проверка кулдауна на команды
if (this.isInCooldown(playerId)) {
event.setResult(CommandResult.denied());
player.sendMessage(this.spamMessage);
this.addWarning(playerId, player);
} else {
// Установка кулдауна
this.cooldowns.put(playerId, currentTime + cooldownTime);
}
}
}
private boolean isInCooldown(UUID playerId) {
return System.currentTimeMillis() < this.cooldowns.getOrDefault(playerId, 0L);
}
private void addWarning(UUID playerId, Player player) {
int warnings = this.warningCounts.merge(playerId, 1, Integer::sum);
if (warnings >= this.kickAfterWarnings) {
player.disconnect(this.kickMessage);
this.warningCounts.remove(playerId);
this.commandTimestamps.remove(playerId);
}
}
}
config.yml
cooldown: 500
max-warnings: 25
kick-after-warnings: 35
spam-message: "§6DragonsGrief §7» §c§lНе спамь!"
kick-message: "§cВы были кикнуты за спам командами"
Сделай так чтобы игроков просто так никогда не кикало, нужно чтобы был просто кд на команды, кд на ввод комманд 500 миллисекунд и сделай чтобы кикало игрока за спам командами только тогда, когда игрок вводил за секунду или полторы секунды столько команд, сколько указано в конфиге (kick-after-warnings: 35)
Ниже привожу модифицированный код, который отвечает требованиям вашего обновленного запроса: теперь игроков не будет кикать просто так, кик происходит только при превышении указанного в конфиге лимита команд за секунду/полторы, а также кулдаун на команды строго равен 500 миллисекундам.
java
package xyz.dragonsfixer;
import com.google.inject.Inject;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.command.CommandExecuteEvent;
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.slf4j.Logger;
import org.yaml.snakeyaml.Yaml;
@Plugin(
id = "dragonsfixer",
name = "DragonsFixer",
version = "1.2",
description = "Антиспам плагин для Velocity",
authors = {"boboliii"}
)
public class DragonsFixer {
private final ProxyServer server;
private final Path dataDirectory;
private final Map<UUID, Long> cooldowns = new ConcurrentHashMap<>();
private final Map<UUID, List<Long>> commandTimestamps = new ConcurrentHashMap<>();
private int cooldownTime = 500;
private Component spamMessage;
private int kickAfterWarnings;
private Component kickMessage;
@Inject
private Logger logger;
@Inject
public DragonsFixer(ProxyServer server, @DataDirectory Path dataDirectory) {
this.server = server;
this.dataDirectory = dataDirectory;
this.spamMessage = Component.text("DragonsGrief » Не спамь!").color(NamedTextColor.RED);
this.kickAfterWarnings = 35;
this.kickMessage = Component.text("DragonsGrief » Вы были кикнуты за спам командами").color(NamedTextColor.RED);
}
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
try {
this.initializeConfiguration();
} catch (Exception e) {
this.logger.error("Ошибка инициализации конфигурации", e);
}
}
private void initializeConfiguration() throws IOException {
Files.createDirectories(this.dataDirectory);
Path configPath = this.dataDirectory.resolve("config.yml");
if (Files.notExists(configPath, LinkOption.NOFOLLOW_LINKS)) {
try (InputStream inputStream = this.getClass().getResourceAsStream("/config.yml")) {
if (inputStream != null) {
Files.copy(inputStream, configPath, new CopyOption[0]);
} else {
this.createDefaultConfig(configPath);
}
}
}
this.loadConfiguration(configPath);
}
private void createDefaultConfig(Path configPath) throws IOException {
String defaultConfigContent = "cooldown: 500\nkick-after-warnings: 35\nspam-message: \"§6DragonsGrief §7» §c§lНе спамь!\"\nkick-message: \"§cВы были кикнуты за спам командами\"\n";
Files.write(configPath, defaultConfigContent.getBytes(), new OpenOption[0]);
}
private void loadConfiguration(Path configPath) throws IOException {
try (InputStream input = Files.newInputStream(configPath)) {
Yaml yaml = new Yaml();
Map<String, Object> config = yaml.load(input);
this.cooldownTime = (Integer) config.getOrDefault("cooldown", 500);
this.kickAfterWarnings = (Integer) config.getOrDefault("kick-after-warnings", 35);
this.spamMessage = LegacyComponentSerializer.legacyAmpersand().deserialize(
(String) config.getOrDefault("spam-message", "§6DragonsGrief §7» §c§lНе спамь!"));
this.kickMessage = LegacyComponentSerializer.legacyAmpersand().deserialize(
(String) config.getOrDefault("kick-message", "§cВы были кикнуты за спам командами"));
}
}
@Subscribe
public void onCommand(CommandExecuteEvent event) {
CommandSource source = event.getCommandSource();
if (source instanceof Player) {
Player player = (Player) source;
UUID playerId = player.getUniqueId();
// Проверка кулдауна
long currentTime = System.currentTimeMillis();
if (isInCooldown(playerId, currentTime)) {
event.setResult(CommandResult.denied());
player.sendMessage(this.spamMessage);
return;
}
// Устанавливаем новый кулдаун
this.cooldowns.put(playerId, currentTime + cooldownTime);
// Обрабатываем список команд за последнее время
List<Long> timestamps = this.commandTimestamps.computeIfAbsent(playerId, k -> new ArrayList<>());
timestamps.removeIf(timestamp -> currentTime - timestamp > 1500L); // Очищаем старые команды (старше 1.5 сек)
timestamps.add(currentTime);
// Проверяем количество команд за 1.5 секунды
if (timestamps.size() >= this.kickAfterWarnings) {
player.disconnect(this.kickMessage);
this.commandTimestamps.remove(playerId);
return;
}
}
}
private boolean isInCooldown(UUID playerId, long currentTime) {
return currentTime < this.cooldowns.getOrDefault(playerId, 0L);
}
}
Изменения в коде:
Кулдаун на команды:
Теперь кулдаун (cooldownTime) учитывается строго перед обработкой ввода команды. Он равен 500 миллисекундам, как указано в задаче.
Кик только за превышение лимита команд:
Параметр kick-after-warnings теперь означает количество допустимых команд за последние 1.5 секунды. Если игрок превышает это количество, он будет отключен.
Время проверки спама:
Временной интервал для проверки количества команд увеличен до 1.5 секунд (timestamps.removeIf(timestamp -> currentTime - timestamp > 1500L)).
Удалил логику предупреждений (warningCounts):
Она больше не нужна, так как кик зависит только от превышения лимита команд.
Обновленный config.yml:
yaml
cooldown: 500
kick-after-warnings: 35
spam-message: "§6DragonsGrief §7» §c§lНе спамь!"
kick-message: "§cВы были кикнуты за спам командами"
Теперь плагин более корректно работает с введенными командами и соответствует запрошенной логике.