From cd2fa73976e3c7b31f95c8aae4d6bc73b43355a1 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Tue, 28 Mar 2023 14:46:47 +0700 Subject: [PATCH] add logger & revert back to executor because yes not sure if executor will break again i set it to 100 instead of 69 now --- .../chipmunk/chayapak/chomens_bot/Bot.java | 15 +- .../chipmunk/chayapak/chomens_bot/Logger.java | 173 ++++++++++++++++++ .../chomens_bot/plugins/BruhifyPlugin.java | 38 ++-- .../chomens_bot/plugins/CorePlugin.java | 9 +- .../chomens_bot/plugins/DiscordPlugin.java | 13 +- .../chomens_bot/plugins/HashingPlugin.java | 11 +- .../chomens_bot/plugins/LoggerPlugin.java | 16 +- .../plugins/MusicPlayerPlugin.java | 125 ++++++------- .../chomens_bot/plugins/SelfCarePlugin.java | 40 ++-- .../chomens_bot/plugins/TPSPlugin.java | 11 +- 10 files changed, 292 insertions(+), 159 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/Logger.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index be08473..c681e33 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -13,8 +13,9 @@ import org.apache.commons.lang3.RandomStringUtils; import java.util.ArrayList; import java.util.List; -import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class Bot { private final ArrayList listeners = new ArrayList<>(); @@ -31,6 +32,8 @@ public class Bot { @Getter private Session session; + @Getter private ScheduledExecutorService executor = Executors.newScheduledThreadPool(100); + @Getter @Setter private ConsolePlugin console; @Getter @Setter private LoggerPlugin logger; // in ConsolePlugin @Getter private final ChatPlugin chat; @@ -140,13 +143,7 @@ public class Bot { if (reconnectDelay < 0) return; // to disable reconnecting - final Timer timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - reconnect(); - } - }, reconnectDelay); + executor.schedule(() -> reconnect(), reconnectDelay, TimeUnit.MILLISECONDS); } }); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Logger.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Logger.java new file mode 100644 index 0000000..b0c3ae7 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Logger.java @@ -0,0 +1,173 @@ +package land.chipmunk.chayapak.chomens_bot; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +// totallynotskidded™ from HBot +public class Logger { + public static File logDir = new File("logs"); + public static File logFile = new File(logDir, "log.txt"); + public static OutputStreamWriter logWriter; + + public static LocalDate currentLogDate; + public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("'['dd/MM/yyyy HH:mm:ss']' "); + public static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("'dd/MM/yyyy'"); + + public static String prevEntry = ""; + public static int duplicateCounter = 1; + + public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); + public static int spamLevel = 0; + public static long freezeTime = 0; + + static { + init(); + executor.scheduleAtFixedRate(() -> { + try { + tick(); + } catch (Exception e) { + e.printStackTrace(); + } + }, 0, 50, TimeUnit.MILLISECONDS); + } + + public static void init() { + if (!logDir.exists()) logDir.mkdir(); + + try { + if (!logFile.exists()) { + makeNewLogFile(); + } else if (!logIsCurrent(logFile)) { + compressLogFile(); + makeNewLogFile(); + } else { + openLogFile(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void tick() { + if (freezeTime <= System.currentTimeMillis() && spamLevel > 0) { + spamLevel--; + } + if (!currentLogDate.equals(LocalDate.now())) { + try { + compressLogFile(); + makeNewLogFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public static synchronized void makeNewLogFile() throws IOException { + currentLogDate = LocalDate.parse(LocalDate.now().format(dateFormatter)); + logWriter = new OutputStreamWriter(new FileOutputStream(logFile, false), StandardCharsets.UTF_8); + logWriter.write(currentLogDate.toString() + '\n'); + logWriter.flush(); + } + + public static synchronized void openLogFile() throws IOException { + currentLogDate = LocalDate.parse(getLogDate(logFile)); + logWriter = new OutputStreamWriter(new FileOutputStream(logFile, true), StandardCharsets.UTF_8); + } + + public static synchronized void compressLogFile() throws IOException { + if (Files.size(logFile.toPath()) > 100*1024*1024) { // Will not save because log file is too big + return; + } + + File gzipFile = new File(logDir, getLogDate(logFile) + ".txt.gz"); + FileInputStream in = new FileInputStream(logFile); + GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzipFile)); + + byte[] buffer = new byte[1024]; + int size; + while ((size = in.read(buffer)) > 0) { + out.write(buffer, 0, size); + } + in.close(); + out.finish(); + out.close(); + } + + public static synchronized BufferedReader getLogReader(File file) throws IOException { + GZIPInputStream in = new GZIPInputStream(new FileInputStream(file)); + return new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); + } + + public static synchronized String getLogDate(File file) throws IOException { + InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8); + BufferedReader reader = new BufferedReader(isr); + String date = reader.readLine(); + reader.close(); + return date; + } + + public static synchronized boolean logIsCurrent(File file) throws IOException { + LocalDate date = LocalDate.now(); + return getLogDate(file).equals(date.toString()); + } + + public static synchronized void log(String str) { + if (freezeTime > System.currentTimeMillis()) { + return; + } + + try { + if (spamLevel >= 100) { + spamLevel = 80; + freezeTime = System.currentTimeMillis() + 20000; + + if (duplicateCounter > 1) { + logWriter.write(String.format(" [%sx]\n", duplicateCounter)); + } else { + logWriter.write("\n"); + } + + logWriter.write("Spam detected, logs temporarily frozen"); + logWriter.flush(); + + duplicateCounter = 1; + prevEntry = ""; + + return; + } + + if (str.equalsIgnoreCase(prevEntry)) { + duplicateCounter++; + } else { + if (duplicateCounter > 1) { + logWriter.write(String.format(" [%sx]\n", duplicateCounter)); + } else { + logWriter.write("\n"); + } + + logWriter.write(getTimePrefix() + str.replaceAll("\\[(\\d+?)x](?=$|[\r\n])", "[/$1x]")); // the replaceAll will prevent conflicts with the duplicate counter + logWriter.flush(); + + duplicateCounter = 1; + prevEntry = str; + + spamLevel += 2; + } + } catch (IOException e) { + e.printStackTrace(); + } + } + public static String getTimePrefix() { + LocalDateTime dateTime = LocalDateTime.now(); + return dateTime.format(dateTimeFormatter); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BruhifyPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BruhifyPlugin.java index 73568fd..6d8cd3f 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BruhifyPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BruhifyPlugin.java @@ -8,8 +8,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; -import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.TimeUnit; public class BruhifyPlugin { @Getter @Setter private String bruhifyText = ""; @@ -17,29 +16,24 @@ public class BruhifyPlugin { private int startHue = 0; public BruhifyPlugin (Bot bot) { - final Timer timer = new Timer(); + bot.executor().scheduleAtFixedRate(() -> { + if (bruhifyText.equals("")) return; - timer.schedule(new TimerTask() { - @Override - public void run() { - if (bruhifyText.equals("")) return; + int hue = startHue; + String displayName = bruhifyText; + int increment = (int)(360.0 / Math.max(displayName.length(), 20)); - int hue = startHue; - String displayName = bruhifyText; - int increment = (int)(360.0 / Math.max(displayName.length(), 20)); + Component component = Component.empty(); - Component component = Component.empty(); - - for (char character : displayName.toCharArray()) { - String color = String.format("#%06x", ColorUtilities.hsvToRgb(hue, 100, 100)); - component = component.append(Component.text(character).color(TextColor.fromHexString(color))); - hue = (hue + increment) % 360; - } - - bot.core().run("minecraft:title @a actionbar " + GsonComponentSerializer.gson().serialize(component)); - - startHue = (startHue + increment) % 360; + for (char character : displayName.toCharArray()) { + String color = String.format("#%06x", ColorUtilities.hsvToRgb(hue, 100, 100)); + component = component.append(Component.text(character).color(TextColor.fromHexString(color))); + hue = (hue + increment) % 360; } - }, 50, 100); + + bot.core().run("minecraft:title @a actionbar " + GsonComponentSerializer.gson().serialize(component)); + + startHue = (startHue + increment) % 360; + }, 50, 100, TimeUnit.MILLISECONDS); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java index 61808bf..52729b6 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CorePlugin.java @@ -21,6 +21,7 @@ import lombok.Getter; import land.chipmunk.chayapak.chomens_bot.Bot; import java.util.*; +import java.util.concurrent.TimeUnit; public class CorePlugin extends PositionPlugin.PositionListener { private final Bot bot; @@ -118,13 +119,7 @@ public class CorePlugin extends PositionPlugin.PositionListener { if (!ready) { ready = true; - final Timer timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - refill(); - } - }, 1, bot.config().core().refillInterval()); + bot.executor().scheduleAtFixedRate(this::refill, 0, bot.config().core().refillInterval(), TimeUnit.MILLISECONDS); for (Listener listener : listeners) listener.ready(); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java index 815a102..d29850f 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java @@ -26,9 +26,8 @@ import org.jetbrains.annotations.NotNull; import javax.security.auth.login.LoginException; import java.util.HashMap; import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; public class DiscordPlugin { @Getter private JDA jda; @@ -171,15 +170,7 @@ public class DiscordPlugin { } }); - final TimerTask task = new TimerTask() { - @Override - public void run() { - onDiscordTick(channelId); - } - }; - - final Timer timer = new Timer(); - timer.schedule(task, 50, 50); + bot.executor().scheduleAtFixedRate(() -> onDiscordTick(channelId), 0, 50, TimeUnit.MILLISECONDS); } @Override diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java index bd090f5..0c9d164 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java @@ -5,8 +5,7 @@ import land.chipmunk.chayapak.chomens_bot.Bot; import lombok.Getter; import java.nio.charset.StandardCharsets; -import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.TimeUnit; public class HashingPlugin { private final Bot bot; @@ -17,13 +16,7 @@ public class HashingPlugin { public HashingPlugin (Bot bot) { this.bot = bot; - final Timer timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - update(); - } - }, 1, 1000); + bot.executor().scheduleAtFixedRate(this::update, 0, 1, TimeUnit.SECONDS); } public void update () { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java index 08ac4d7..255e6a6 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/LoggerPlugin.java @@ -4,6 +4,7 @@ import com.github.steveice10.packetlib.event.session.ConnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.SessionAdapter; import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.Logger; import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; import net.kyori.adventure.text.Component; @@ -29,11 +30,16 @@ public class LoggerPlugin extends ChatPlugin.ChatListener { } public void log (String message) { - bot.console().reader().printAbove( - String.format( - "[%s] %s", - bot.host() + ":" + bot.port(), - message + final String formattedMessage = String.format( + "[%s] %s", + bot.host() + ":" + bot.port(), + message + ); + bot.console().reader().printAbove(formattedMessage); + Logger.log( + formattedMessage.replaceAll( // use replaceAll for regexes, use replace for normal string + "\u001B\\[[;\\d]*[ -/]*[@-~]", + "" ) ); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java index 7847d66..def68a3 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java @@ -6,7 +6,6 @@ import land.chipmunk.chayapak.chomens_bot.song.*; import lombok.Getter; import lombok.Setter; import land.chipmunk.chayapak.chomens_bot.Bot; -import land.chipmunk.chayapak.chomens_bot.song.*; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; @@ -16,13 +15,13 @@ import java.net.URL; import java.nio.file.Path; import java.text.DecimalFormat; import java.util.LinkedList; -import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; public class MusicPlayerPlugin extends SessionAdapter { private final Bot bot; - private TimerTask playTask; + private ScheduledFuture playTask; public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]"; public static File SONG_DIR = new File("songs"); @@ -86,74 +85,70 @@ public class MusicPlayerPlugin extends SessionAdapter { } public void coreReady () { - playTask = new TimerTask() { - @Override - public void run() { - if (loaderThread != null && !loaderThread.isAlive()) { - if (loaderThread.exception != null) { - bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED)); - } else { - songQueue.add(loaderThread.song); - bot.chat().tellraw(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.GOLD))); - } - loaderThread = null; + final Runnable task = () -> { + if (loaderThread != null && !loaderThread.isAlive()) { + if (loaderThread.exception != null) { + bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED)); + } else { + songQueue.add(loaderThread.song); + bot.chat().tellraw(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.GOLD))); + } + loaderThread = null; + } + + if (currentSong == null) { + if (songQueue.size() == 0) return; + + currentSong = songQueue.get(0); // songQueue.poll(); + bot.chat().tellraw(Component.translatable("Now playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); + currentSong.play(); + } + + if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return; + else ticksUntilPausedBossbar = 20; + + bot.core().run("minecraft:bossbar add " + bossbarName + " \"\""); + bot.core().run("minecraft:bossbar set " + bossbarName + " players " + SELECTOR); + bot.core().run("minecraft:bossbar set " + bossbarName + " name " + GsonComponentSerializer.gson().serialize(generateBossbar())); + bot.core().run("minecraft:bossbar set " + bossbarName + " color " + (nightcore ? "purple" : "yellow")); + bot.core().run("minecraft:bossbar set " + bossbarName + " visible true"); + bot.core().run("minecraft:bossbar set " + bossbarName + " style progress"); + bot.core().run("minecraft:bossbar set " + bossbarName + " value " + (int) Math.floor(currentSong.time)); + bot.core().run("minecraft:bossbar set " + bossbarName + " max " + currentSong.length); + + if (currentSong.paused) return; + + handlePlaying(); + + if (currentSong.finished()) { + removeBossbar(); + bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); + + if (loop == Loop.CURRENT) { + currentSong.setTime(0); + return; + } + if (loop == Loop.ALL) { + skip(); + return; } - if (currentSong == null) { - if (songQueue.size() == 0) return; + songQueue.remove(); - currentSong = songQueue.get(0); // songQueue.poll(); - bot.chat().tellraw(Component.translatable("Now playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); + if (songQueue.size() == 0) { + stopPlaying(); + bot.chat().tellraw(Component.text("Finished playing every song in the queue")); + return; + } + if (currentSong.size() > 0) { + currentSong = songQueue.get(0); + currentSong.setTime(0); currentSong.play(); } - - if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return; - else ticksUntilPausedBossbar = 20; - - bot.core().run("minecraft:bossbar add " + bossbarName + " \"\""); - bot.core().run("minecraft:bossbar set " + bossbarName + " players " + SELECTOR); - bot.core().run("minecraft:bossbar set " + bossbarName + " name " + GsonComponentSerializer.gson().serialize(generateBossbar())); - bot.core().run("minecraft:bossbar set " + bossbarName + " color " + (nightcore ? "purple" : "yellow")); - bot.core().run("minecraft:bossbar set " + bossbarName + " visible true"); - bot.core().run("minecraft:bossbar set " + bossbarName + " style progress"); - bot.core().run("minecraft:bossbar set " + bossbarName + " value " + (int) Math.floor(currentSong.time)); - bot.core().run("minecraft:bossbar set " + bossbarName + " max " + currentSong.length); - - if (currentSong.paused) return; - - handlePlaying(); - - if (currentSong.finished()) { - removeBossbar(); - bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); - - if (loop == Loop.CURRENT) { - currentSong.setTime(0); - return; - } - if (loop == Loop.ALL) { - skip(); - return; - } - - songQueue.remove(); - - if (songQueue.size() == 0) { - stopPlaying(); - bot.chat().tellraw(Component.text("Finished playing every song in the queue")); - return; - } - if (currentSong.size() > 0) { - currentSong = songQueue.get(0); - currentSong.setTime(0); - currentSong.play(); - } - } } }; - final Timer timer = new Timer(); - timer.schedule(playTask, 1, 50); + playTask = bot.executor().schedule(task, 50, TimeUnit.MILLISECONDS); if (currentSong != null) currentSong.play(); } @@ -226,7 +221,7 @@ public class MusicPlayerPlugin extends SessionAdapter { @Override public void disconnected (DisconnectedEvent event) { - playTask.cancel(); + playTask.cancel(true); if (currentSong != null) currentSong.pause(); // nice. } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java index bc41e7e..ce8ad47 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/SelfCarePlugin.java @@ -16,19 +16,19 @@ import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.PacketProtocol; -import lombok.Getter; -import lombok.Setter; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Configuration; +import lombok.Getter; +import lombok.Setter; import net.kyori.adventure.text.Component; -import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; public class SelfCarePlugin extends SessionAdapter { private final Bot bot; - private TimerTask checkTask; + private ScheduledFuture checkTask; @Getter @Setter boolean visibility = false; @@ -108,25 +108,21 @@ public class SelfCarePlugin extends SessionAdapter { muted = false; prefix = false; - checkTask = new TimerTask() { - @Override - public void run() { - final Session session = bot.session(); - final PacketProtocol protocol = session.getPacketProtocol(); - if ( - !session.isConnected() || - ( - protocol instanceof MinecraftProtocol && - ((MinecraftProtocol) protocol).getState() != ProtocolState.GAME - ) - ) return; + final Runnable task = () -> { + final Session session = bot.session(); + final PacketProtocol protocol = session.getPacketProtocol(); + if ( + !session.isConnected() || + ( + protocol instanceof MinecraftProtocol && + ((MinecraftProtocol) protocol).getState() != ProtocolState.GAME + ) + ) return; - check(); - } + check(); }; - final Timer timer = new Timer(); - timer.schedule(checkTask, 1, bot.config().selfCare().checkInterval()); + checkTask = bot.executor().scheduleAtFixedRate(task, 0, bot.config().selfCare().checkInterval(), TimeUnit.MILLISECONDS); } public void packetReceived (ClientboundGameEventPacket packet) { @@ -156,6 +152,6 @@ public class SelfCarePlugin extends SessionAdapter { @Override public void disconnected (DisconnectedEvent event) { - checkTask.cancel(); + checkTask.cancel(true); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java index 79f93d6..f699dfc 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java @@ -12,8 +12,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import java.util.Arrays; -import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.TimeUnit; public class TPSPlugin extends SessionAdapter { private final Bot bot; @@ -32,13 +31,7 @@ public class TPSPlugin extends SessionAdapter { bot.addListener(this); - final Timer timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - updateTPSBar(); - } - }, 1, 50); + bot.executor().scheduleAtFixedRate(this::updateTPSBar, 0, 50, TimeUnit.MILLISECONDS); } public void on () {