fix: denis check and use AtomicInteger on the thingy

This commit is contained in:
ChomeNS
2025-05-04 18:56:30 +07:00
parent 3bf8b78e28
commit 3db1002af1
4 changed files with 24 additions and 11 deletions

View File

@@ -1 +1 @@
3076
3078

View File

@@ -2,12 +2,14 @@ package me.chayapak1.chomens_bot.commands;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.command.contexts.ConsoleCommandContext;
import me.chayapak1.chomens_bot.data.chat.ChatPacketType;
import me.chayapak1.chomens_bot.data.listener.Listener;
import me.chayapak1.chomens_bot.plugins.MusicPlayerPlugin;
import me.chayapak1.chomens_bot.song.Instrument;
import me.chayapak1.chomens_bot.song.Loop;
@@ -31,12 +33,16 @@ import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static me.chayapak1.chomens_bot.util.StringUtilities.isNotNullAndNotBlank;
public class MusicCommand extends Command {
public class MusicCommand extends Command implements Listener {
private static final Path ROOT = MusicPlayerPlugin.SONG_DIR;
private static final AtomicInteger commandsPerSecond = new AtomicInteger();
public MusicCommand () {
super(
"music",
@@ -65,10 +71,16 @@ public class MusicCommand extends Command {
false,
new ChatPacketType[] { ChatPacketType.DISGUISED }
);
Main.EXECUTOR.scheduleAtFixedRate(() -> commandsPerSecond.set(0), 0, 1, TimeUnit.SECONDS);
}
@Override
public Component execute (final CommandContext context) throws CommandException {
// denis check
if (commandsPerSecond.get() > 3) return null;
else commandsPerSecond.getAndIncrement();
if (context.bot.music.locked && !(context instanceof ConsoleCommandContext))
throw new CommandException(Component.text("Managing music is currently locked"));

View File

@@ -26,9 +26,10 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class UrbanCommand extends Command {
public int requestsPerSecond = 0;
public final AtomicInteger requestsPerSecond = new AtomicInteger();
public UrbanCommand () {
super(
@@ -41,11 +42,11 @@ public class UrbanCommand extends Command {
new ChatPacketType[]{ ChatPacketType.DISGUISED }
);
Main.EXECUTOR.scheduleAtFixedRate(() -> requestsPerSecond = 0, 0, 1, TimeUnit.SECONDS);
Main.EXECUTOR.scheduleAtFixedRate(() -> requestsPerSecond.set(0), 0, 1, TimeUnit.SECONDS);
}
public Component execute (final CommandContext context) throws CommandException {
if (requestsPerSecond > 3) throw new CommandException(Component.text("Too many requests"));
if (requestsPerSecond.get() > 3) throw new CommandException(Component.text("Too many requests"));
final Bot bot = context.bot;
@@ -186,7 +187,7 @@ public class UrbanCommand extends Command {
}
});
requestsPerSecond++;
requestsPerSecond.getAndIncrement();
return null;
}

View File

@@ -19,6 +19,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class CommandHandlerPlugin implements Listener {
public static final List<Command> COMMANDS = ObjectList.of(
@@ -83,7 +84,7 @@ public class CommandHandlerPlugin implements Listener {
private final Bot bot;
private int commandPerSecond = 0;
private static final AtomicInteger commandsPerSecond = new AtomicInteger();
public CommandHandlerPlugin (final Bot bot) {
this.bot = bot;
@@ -93,7 +94,7 @@ public class CommandHandlerPlugin implements Listener {
@Override
public void onLocalSecondTick () {
commandPerSecond = 0;
commandsPerSecond.set(0);
}
// BETTER QUALITY than the js version
@@ -120,9 +121,8 @@ public class CommandHandlerPlugin implements Listener {
final boolean bypass = context instanceof ConsoleCommandContext || context instanceof ChomeNSModCommandContext;
if (commandPerSecond > 100) return;
commandPerSecond++;
if (commandsPerSecond.get() > 100) return;
else commandsPerSecond.getAndIncrement();
final String[] splitInput = input.trim().split("\\s+");