refactor: attempt to make the bot more thread-safe (specifically the core)

This commit is contained in:
Chayapak 2025-03-27 18:21:16 +07:00
parent 1e6421a17c
commit 517e685c8d
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
4 changed files with 31 additions and 36 deletions
build-number.txt
src/main/java/me/chayapak1/chomens_bot/plugins

View file

@ -1 +1 @@
2245 2248

View file

@ -53,7 +53,7 @@ public class ChatPlugin extends Bot.Listener {
public final List<Component> chatTypes = new ArrayList<>(); public final List<Component> chatTypes = new ArrayList<>();
private final ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>(); private final Queue<String> queue = new ConcurrentLinkedQueue<>();
public final int queueDelay; public final int queueDelay;

View file

@ -27,14 +27,12 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.inventory.S
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket; import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket; import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class CorePlugin public class CorePlugin
extends Bot.Listener extends Bot.Listener
@ -46,7 +44,7 @@ public class CorePlugin
private final List<Listener> listeners = new ArrayList<>(); private final List<Listener> listeners = new ArrayList<>();
public boolean ready = false; public volatile boolean ready = false;
private ScheduledFuture<?> refillTask; private ScheduledFuture<?> refillTask;
@ -56,12 +54,12 @@ public class CorePlugin
public Vector3i from; public Vector3i from;
public Vector3i to; public Vector3i to;
public Vector3i block = null; public volatile Vector3i block = null;
public final ConcurrentLinkedQueue<String> placeBlockQueue = new ConcurrentLinkedQueue<>(); public final Queue<String> placeBlockQueue = new ConcurrentLinkedQueue<>();
private int commandsPerTick = 0; private final AtomicInteger commandsPerTick = new AtomicInteger(0);
private int commandsPerSecond = 0; private final AtomicInteger commandsPerSecond = new AtomicInteger(0);
private boolean shouldRefill = false; private boolean shouldRefill = false;
@ -83,7 +81,7 @@ public class CorePlugin
if (hasRateLimit() && hasReset()) { if (hasRateLimit() && hasReset()) {
bot.executor.scheduleAtFixedRate( bot.executor.scheduleAtFixedRate(
() -> commandsPerSecond = 0, () -> commandsPerSecond.set(0),
0, 0,
bot.options.coreRateLimit.reset, bot.options.coreRateLimit.reset,
TimeUnit.MILLISECONDS TimeUnit.MILLISECONDS
@ -97,22 +95,18 @@ public class CorePlugin
@Override @Override
public void onTick () { public void onTick () {
if (commandsPerTick > 0) commandsPerTick--; if (commandsPerTick.get() > 0) commandsPerTick.decrementAndGet();
try { if (placeBlockQueue.size() > 300) {
if (placeBlockQueue.size() > 300) { placeBlockQueue.clear();
placeBlockQueue.clear(); return;
return;
}
final String command = placeBlockQueue.poll();
if (command == null) return;
forceRunPlaceBlock(command);
} catch (Exception e) {
bot.logger.error(e);
} }
final String command = placeBlockQueue.poll();
if (command == null) return;
forceRunPlaceBlock(command);
} }
@Override @Override
@ -136,7 +130,7 @@ public class CorePlugin
} }
public boolean isRateLimited () { public boolean isRateLimited () {
return commandsPerSecond > bot.options.coreRateLimit.limit; return commandsPerSecond.get() > bot.options.coreRateLimit.limit;
} }
private void forceRun (String command) { private void forceRun (String command) {
@ -144,7 +138,7 @@ public class CorePlugin
// to still run the command, even if forced. // to still run the command, even if forced.
if (!ready || command.length() > 32767) return; if (!ready || command.length() > 32767) return;
commandsPerTick++; commandsPerTick.incrementAndGet();
if (!bot.serverFeatures.hasNamespaces) command = StringUtilities.removeNamespace(command); if (!bot.serverFeatures.hasNamespaces) command = StringUtilities.removeNamespace(command);
@ -193,7 +187,7 @@ public class CorePlugin
forceRun(command); forceRun(command);
if (hasRateLimit()) commandsPerSecond++; if (hasRateLimit()) commandsPerSecond.incrementAndGet();
} else if (command.length() < 256) { } else if (command.length() < 256) {
bot.chat.send("/" + command); bot.chat.send("/" + command);
} }
@ -324,9 +318,9 @@ public class CorePlugin
int y = -64; int y = -64;
int z = toSize.getZ(); int z = toSize.getZ();
while (commandsPerTick > (16 * 16)) { while (commandsPerTick.get() > 16 * 16) {
y++; y++;
commandsPerTick -= 16 * 16; commandsPerTick.getAndAdd(-(16 * 16));
} }
toSize = Vector3i.from(x, y, z); toSize = Vector3i.from(x, y, z);
@ -388,7 +382,7 @@ public class CorePlugin
position.getZ() >= from.getZ() && position.getZ() <= to.getZ(); position.getZ() >= from.getZ() && position.getZ() <= to.getZ();
} }
private void incrementBlock () { private synchronized void incrementBlock () {
int x = block.getX() + 1; int x = block.getX() + 1;
int y = block.getY(); int y = block.getY();
int z = block.getZ(); int z = block.getZ();
@ -451,7 +445,7 @@ public class CorePlugin
reset(); reset();
} }
public void recalculateRelativePositions() { public void recalculateRelativePositions () {
final int botChunkPosX = (int) Math.floor(bot.position.position.getX() / 16); final int botChunkPosX = (int) Math.floor(bot.position.position.getX() / 16);
final int botChunkPosZ = (int) Math.floor(bot.position.position.getZ() / 16); final int botChunkPosZ = (int) Math.floor(bot.position.position.getZ() / 16);

View file

@ -23,11 +23,12 @@ import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.Collections;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
// Author: _ChipMC_ & chayapak <3 // Author: _ChipMC_ & hhhzzzsss
public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listener { public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listener {
public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic,tag=!custompitch]"; public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic,tag=!custompitch]";
public static final String CUSTOM_PITCH_SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic,tag=custompitch]"; public static final String CUSTOM_PITCH_SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic,tag=custompitch]";
@ -45,7 +46,7 @@ public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listen
} }
public Song currentSong; public Song currentSong;
public final List<Song> songQueue = new ArrayList<>(); public final List<Song> songQueue = Collections.synchronizedList(new LinkedList<>());
public SongLoaderThread loaderThread = null; public SongLoaderThread loaderThread = null;
public Loop loop = Loop.OFF; public Loop loop = Loop.OFF;