From 662e016842a2996aed5b8c148f9c7edb33075fac Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 3 Aug 2023 16:46:48 +0700 Subject: [PATCH] fix and lazy fix #69420 --- .../chipmunk/chayapak/chomens_bot/Bot.java | 7 ++++++ .../chomens_bot/commands/MusicCommand.java | 24 +++++++++++++------ .../plugins/MusicPlayerPlugin.java | 24 ++++++++++++++++--- .../chayapak/chomens_bot/song/Song.java | 13 ++++------ .../chomens_bot/song/SongLoaderRunnable.java | 4 +++- 5 files changed, 52 insertions(+), 20 deletions(-) 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 fccf791..2799af2 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -219,6 +219,13 @@ public class Bot { public void disconnected(DisconnectedEvent disconnectedEvent) { loggedIn = false; + final Throwable cause = disconnectedEvent.getCause(); + + if (cause != null) { + // lazy fix (#69420) + if (cause instanceof OutOfMemoryError) System.exit(1); + } + int reconnectDelay = options.reconnectDelay; final String stringMessage = ComponentUtilities.stringify(disconnectedEvent.getReason()); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java index bdfb72b..7ecd7a6 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java @@ -1,6 +1,7 @@ package land.chipmunk.chayapak.chomens_bot.commands; import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.Main; import land.chipmunk.chayapak.chomens_bot.command.Command; import land.chipmunk.chayapak.chomens_bot.command.CommandContext; import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; @@ -21,10 +22,13 @@ import java.nio.file.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; public class MusicCommand extends Command { private Path root; + private int ratelimit = 0; + public MusicCommand () { super( "music", @@ -48,12 +52,18 @@ public class MusicCommand extends Command { TrustLevel.PUBLIC, false ); + + Main.executor.scheduleAtFixedRate(() -> ratelimit = 0, 0, 5, TimeUnit.SECONDS); } @Override public Component execute(CommandContext context, String[] args, String[] fullArgs) { if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED); + ratelimit++; + + if (ratelimit > 15) return null; + root = MusicPlayerPlugin.SONG_DIR; return switch (args[0]) { case "play", "playurl", "playnbs", "playnbsurl" -> play(context, args); @@ -87,7 +97,7 @@ public class MusicCommand extends Command { if (!path.normalize().startsWith(root.toString())) return Component.text("no").color(NamedTextColor.RED); // ignore my ohio code for autocomplete - final String separator = File.separator; + final String separator = File.separator; // how do i do this with the new Files? if (_path.contains(separator) && !_path.equals("")) { final String[] pathSplitted = _path.split(separator); @@ -160,7 +170,7 @@ public class MusicCommand extends Command { context.sendOutput( Component.empty() .append(Component.text("Now looping ")) - .append(bot.music.currentSong.name.color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) + .append(Component.text(bot.music.currentSong.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) ); } @@ -264,7 +274,7 @@ public class MusicCommand extends Command { context.sendOutput( Component.empty() .append(Component.text("Skipping ")) - .append(music.currentSong.name.color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) + .append(Component.text(music.currentSong.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) ); @@ -280,7 +290,7 @@ public class MusicCommand extends Command { return Component.empty() .append(Component.text("Now playing ")) - .append(song.name.color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) + .append(Component.text(song.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); } @@ -292,7 +302,7 @@ public class MusicCommand extends Command { int i = 0; for (Song song : queue) { queueWithNames.add( - song.name.color((i++ & 1) == 0 ? ColorUtilities.getColorByString(bot.config.colorPalette.primary) : ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) + Component.text(song.name).color((i++ & 1) == 0 ? ColorUtilities.getColorByString(bot.config.colorPalette.primary) : ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) ); } @@ -379,7 +389,7 @@ public class MusicCommand extends Command { if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED); // ig very code yup - final Component title = currentSong.name; + final String title = currentSong.name; final String songAuthor = currentSong.songAuthor == null || currentSong.songAuthor.equals("") ? "N/A" : currentSong.songAuthor; final String songOriginalAuthor = currentSong.songOriginalAuthor == null || currentSong.songOriginalAuthor.equals("") ? "N/A" : currentSong.songOriginalAuthor; final String songDescription = currentSong.songDescription == null || currentSong.songDescription.equals("") ? "N/A" : currentSong.songDescription; @@ -390,7 +400,7 @@ public class MusicCommand extends Command { Author: %s Original author: %s Description: %s""", - title.color(NamedTextColor.AQUA), + Component.text(title).color(NamedTextColor.AQUA), Component.text(songAuthor).color(NamedTextColor.AQUA), Component.text(songOriginalAuthor).color(NamedTextColor.AQUA), Component.text(songDescription).color(NamedTextColor.AQUA) 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 fc88169..95d5e04 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 @@ -21,6 +21,7 @@ import java.nio.file.Path; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; // Author: _ChipMC_ & chayapak <3 public class MusicPlayerPlugin extends Bot.Listener { @@ -36,6 +37,8 @@ public class MusicPlayerPlugin extends Bot.Listener { } } + public int loadings = 0; + public Song currentSong; public final List songQueue = new ArrayList<>(); public SongLoaderRunnable loaderThread; @@ -47,6 +50,8 @@ public class MusicPlayerPlugin extends Bot.Listener { private int ticksUntilPausedBossbar = 20; + private int notesPerSecond = 0; + private final String bossbarName = "music"; public MusicPlayerPlugin (Bot bot) { @@ -55,6 +60,7 @@ public class MusicPlayerPlugin extends Bot.Listener { bot.core.addListener(new CorePlugin.Listener() { public void ready () { coreReady(); } }); + bot.executor.scheduleAtFixedRate(() -> notesPerSecond = 0, 0, 1, TimeUnit.SECONDS); } public void loadSong (Path location) { @@ -73,6 +79,13 @@ public class MusicPlayerPlugin extends Bot.Listener { } public void loadSong (URL location) { + if (loadings > 2) { + bot.chat.tellraw(Component.text("Too many songs loading at once").color(NamedTextColor.RED)); + return; + } + + loadings++; + final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot); bot.chat.tellraw( @@ -101,7 +114,7 @@ public class MusicPlayerPlugin extends Bot.Listener { bot.chat.tellraw( Component.translatable( "Now playing %s", - Component.empty().append(currentSong.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) + Component.empty().append(Component.text(currentSong.name)).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) ); currentSong.play(); @@ -134,7 +147,7 @@ public class MusicPlayerPlugin extends Bot.Listener { bot.chat.tellraw( Component.translatable( "Finished playing %s", - Component.empty().append(currentSong.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) + Component.empty().append(Component.text(currentSong.name)).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) ); @@ -208,7 +221,7 @@ public class MusicPlayerPlugin extends Bot.Listener { public Component generateBossbar () { Component component = Component.empty() - .append(Component.empty().append(currentSong.name).color(pitch > 0 ? NamedTextColor.LIGHT_PURPLE : NamedTextColor.GREEN)) + .append(Component.empty().append(Component.text(currentSong.name)).color(pitch > 0 ? NamedTextColor.LIGHT_PURPLE : NamedTextColor.GREEN)) .append(Component.text(" | ").color(NamedTextColor.DARK_GRAY)) .append( Component @@ -262,6 +275,7 @@ public class MusicPlayerPlugin extends Bot.Listener { public void stopPlaying () { removeBossBar(); currentSong = null; + notesPerSecond = 0; } @Override @@ -274,6 +288,8 @@ public class MusicPlayerPlugin extends Bot.Listener { while (currentSong.reachedNextNote()) { final Note note = currentSong.getNextNote(); + if (notesPerSecond > 1000) continue; + float key = note.pitch; // totally didn't look at the minecraft code and found the note block pitch thingy so i totallydidnotskidded™ it @@ -299,6 +315,8 @@ public class MusicPlayerPlugin extends Bot.Listener { " " + MathUtilities.clamp(floatingPitch, 0, 2) ); + + notesPerSecond++; } } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java index 440d25d..93cf486 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/Song.java @@ -1,14 +1,14 @@ package land.chipmunk.chayapak.chomens_bot.song; import land.chipmunk.chayapak.chomens_bot.Bot; -import net.kyori.adventure.text.Component; + import java.util.ArrayList; import java.util.Collections; // Author: hhhzzzsss & _ChipMC_ but i changed most of the stuff public class Song { public final ArrayList notes = new ArrayList<>(); - public final Component name; + public final String name; public int position = 0; // Current note index public boolean paused = true; public long startTime = 0; // Start time in millis since unix epoch @@ -28,9 +28,9 @@ public class Song { private Bot bot; - public Song (Component name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) { - this.bot = bot; + public Song (String name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) { this.name = name; + this.bot = bot; this.songName = songName; this.songAuthor = songAuthor; this.songOriginalAuthor = songOriginalAuthor; @@ -38,11 +38,6 @@ public class Song { this.nbs = nbs; } - public Song (String name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) { - this(Component.text(name), bot, songName, songAuthor, songOriginalAuthor, songDescription, nbs); - this.bot = bot; - } - public Note get (int i) { return notes.get(i); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java index cad3048..0ed0101 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java @@ -71,6 +71,8 @@ public class SongLoaderRunnable implements Runnable { if (isUrl) { bytes = DownloadUtilities.DownloadToByteArray(songUrl, 10*1024*1024); name = Paths.get(songUrl.toURI().getPath()).getFileName().toString(); + + bot.music.loadings--; } else { bytes = Files.readAllBytes(songPath); name = !isFolder ? fileName : songPath.getFileName().toString(); @@ -119,7 +121,7 @@ public class SongLoaderRunnable implements Runnable { bot.chat.tellraw( Component.translatable( "Added %s to the song queue", - Component.empty().append(song.name).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) + Component.empty().append(Component.text(song.name)).color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)) ); }