diff --git a/.idea/workspace.xml b/.idea/workspace.xml index eecd1ab..7a699f2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,9 +4,7 @@ - - @@ -494,7 +490,9 @@ - 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 7967859..9dd999c 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -46,6 +46,7 @@ public class Bot { @Getter @Setter private LoggerPlugin logger; // in ConsolePlugin @Getter @Setter private DiscordPlugin discord; // same for this one too + @Getter private TickPlugin tick; @Getter private ChatPlugin chat; @Getter private PositionPlugin position; @Getter private SelfCarePlugin selfCare; @@ -89,6 +90,7 @@ public class Bot { } public void ready () { + this.tick = new TickPlugin(this); this.chat = new ChatPlugin(this); this.position = new PositionPlugin(this); this.selfCare = new SelfCarePlugin(this); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java index cba8b56..49de5ad 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java @@ -1,6 +1,5 @@ package land.chipmunk.chayapak.chomens_bot.plugins; -import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.BossBar; import lombok.Getter; @@ -9,14 +8,10 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; public class BossbarManagerPlugin extends Bot.Listener { private final Bot bot; - private ScheduledFuture tickTask; - private final Map bossBars = new HashMap<>(); @Getter @Setter private boolean enabled = true; @@ -28,25 +23,16 @@ public class BossbarManagerPlugin extends Bot.Listener { public BossbarManagerPlugin (Bot bot) { this.bot = bot; - bot.core().addListener(new CorePlugin.Listener() { + bot.tick().addListener(new TickPlugin.Listener() { @Override - public void ready() { - BossbarManagerPlugin.this.ready(); + public void onTick() { + tick(); } }); bot.addListener(this); } - public void ready () { - tickTask = bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS); - } - - @Override - public void disconnected(DisconnectedEvent event) { - tickTask.cancel(true); - } - public void tick () { if (!enabled) return; for (Map.Entry _bossBar : bossBars.entrySet()) { 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 3a7aee8..0e09226 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 @@ -22,7 +22,6 @@ import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.TimeUnit; // please ignore my shitcode public class DiscordPlugin { @@ -71,7 +70,12 @@ public class DiscordPlugin { if (channelAlreadyAddedListeners) return; - bot.executor().scheduleAtFixedRate(() -> onDiscordTick(channelId), 0, 50, TimeUnit.MILLISECONDS); + bot.tick().addListener(new TickPlugin.Listener() { + @Override + public void onTick() { + onDiscordTick(channelId); + } + }); bot.chat().addListener(new ChatPlugin.ChatListener() { @Override diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java index 5be534b..4dd1821 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java @@ -10,7 +10,6 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; public class FilterPlugin extends PlayersPlugin.PlayerListener { @@ -23,7 +22,12 @@ public class FilterPlugin extends PlayersPlugin.PlayerListener { bot.players().addListener(this); - bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS); + bot.tick().addListener(new TickPlugin.Listener() { + @Override + public void onTick() { + tick(); + } + }); } private FilteredPlayer getPlayer (String name) { 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 0c9d164..ea31596 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,7 +5,6 @@ import land.chipmunk.chayapak.chomens_bot.Bot; import lombok.Getter; import java.nio.charset.StandardCharsets; -import java.util.concurrent.TimeUnit; public class HashingPlugin { private final Bot bot; @@ -16,7 +15,12 @@ public class HashingPlugin { public HashingPlugin (Bot bot) { this.bot = bot; - bot.executor().scheduleAtFixedRate(this::update, 0, 1, TimeUnit.SECONDS); + bot.tick().addListener(new TickPlugin.Listener() { + @Override + public void onTick() { + update(); + } + }); } public void update () { 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 2d41910..d3e57d4 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 @@ -17,14 +17,10 @@ import java.net.URL; import java.nio.file.Path; import java.text.DecimalFormat; import java.util.LinkedList; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; public class MusicPlayerPlugin extends Bot.Listener { private final Bot bot; - private ScheduledFuture playTask; - public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]"; public static File SONG_DIR = new File("songs"); static { @@ -90,76 +86,77 @@ public class MusicPlayerPlugin extends Bot.Listener { } public void coreReady () { - 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; - - addBossBar(); - - 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; - - BossBar bossBar = bot.bossbar().get(bossbarName); - - if (bossBar == null) bossBar = addBossBar(); - - bossBar.players(SELECTOR); - bossBar.name(generateBossbar()); - bossBar.color(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW); - bossBar.visible(true); - bossBar.style(BossBarStyle.PROGRESS); - bossBar.value((int) Math.floor(currentSong.time * speed)); - bossBar.max(currentSong.length); - - if (currentSong.paused) return; - - handlePlaying(); - - if (currentSong.finished()) { - if (loop == Loop.CURRENT) { - currentSong.setTime(0); - return; + bot.tick().addListener(new TickPlugin.Listener() { + @Override + public void onTick() { + 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; } - bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); + if (currentSong == null) { + if (songQueue.size() == 0) return; - if (loop == Loop.ALL) { - skip(); - return; - } + addBossBar(); - songQueue.remove(); - - if (songQueue.size() == 0) { - stopPlaying(); - removeBossBar(); - 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 = songQueue.get(0); // songQueue.poll(); + bot.chat().tellraw(Component.translatable("Now playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); currentSong.play(); } - } - }; - playTask = bot.executor().scheduleAtFixedRate(task, 50, 50, TimeUnit.MILLISECONDS); + if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return; + else ticksUntilPausedBossbar = 20; + + BossBar bossBar = bot.bossbar().get(bossbarName); + + if (bossBar == null) bossBar = addBossBar(); + + bossBar.players(SELECTOR); + bossBar.name(generateBossbar()); + bossBar.color(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW); + bossBar.visible(true); + bossBar.style(BossBarStyle.PROGRESS); + bossBar.value((int) Math.floor(currentSong.time * speed)); + bossBar.max(currentSong.length); + + if (currentSong.paused) return; + + handlePlaying(); + + if (currentSong.finished()) { + if (loop == Loop.CURRENT) { + currentSong.setTime(0); + return; + } + + bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); + + if (loop == Loop.ALL) { + skip(); + return; + } + + songQueue.remove(); + + if (songQueue.size() == 0) { + stopPlaying(); + removeBossBar(); + 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 != null) currentSong.play(); } @@ -248,8 +245,6 @@ public class MusicPlayerPlugin extends Bot.Listener { @Override public void disconnected (DisconnectedEvent event) { - playTask.cancel(true); - if (currentSong != null) currentSong.pause(); // nice. } 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 50d2f18..2b19e89 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 @@ -13,7 +13,6 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import java.util.Arrays; -import java.util.concurrent.TimeUnit; public class TPSPlugin extends Bot.Listener { private final Bot bot; @@ -35,7 +34,12 @@ public class TPSPlugin extends Bot.Listener { bot.core().addListener(new CorePlugin.Listener() { @Override public void ready() { - bot.executor().scheduleAtFixedRate(() -> updateTPSBar(), 0, 50, TimeUnit.MILLISECONDS); + bot.tick().addListener(new TickPlugin.Listener() { + @Override + public void onTick() { + updateTPSBar(); + } + }); } } ); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TickPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TickPlugin.java new file mode 100644 index 0000000..0b7776e --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TickPlugin.java @@ -0,0 +1,48 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import com.github.steveice10.packetlib.event.session.ConnectedEvent; +import com.github.steveice10.packetlib.event.session.DisconnectedEvent; +import land.chipmunk.chayapak.chomens_bot.Bot; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +public class TickPlugin extends Bot.Listener { + private final Bot bot; + + private ScheduledFuture tickTask; + + private final List listeners = new ArrayList<>(); + + public TickPlugin (Bot bot) { + this.bot = bot; + + bot.addListener(this); + } + + @Override + public void connected(ConnectedEvent event) { + tickTask = bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS); + } + + private void tick () { + for (Listener listener : listeners) { + listener.onTick(); + } + } + + @Override + public void disconnected (DisconnectedEvent event) { + tickTask.cancel(true); + } + + public void addListener (Listener listener) { + listeners.add(listener); + } + + public static class Listener { + public void onTick () {} + } +}