fix and lazy fix #69420

This commit is contained in:
Chayapak 2023-08-03 16:46:48 +07:00
parent 95d193d11c
commit 662e016842
5 changed files with 52 additions and 20 deletions

View file

@ -219,6 +219,13 @@ public class Bot {
public void disconnected(DisconnectedEvent disconnectedEvent) { public void disconnected(DisconnectedEvent disconnectedEvent) {
loggedIn = false; loggedIn = false;
final Throwable cause = disconnectedEvent.getCause();
if (cause != null) {
// lazy fix (#69420)
if (cause instanceof OutOfMemoryError) System.exit(1);
}
int reconnectDelay = options.reconnectDelay; int reconnectDelay = options.reconnectDelay;
final String stringMessage = ComponentUtilities.stringify(disconnectedEvent.getReason()); final String stringMessage = ComponentUtilities.stringify(disconnectedEvent.getReason());

View file

@ -1,6 +1,7 @@
package land.chipmunk.chayapak.chomens_bot.commands; package land.chipmunk.chayapak.chomens_bot.commands;
import land.chipmunk.chayapak.chomens_bot.Bot; 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.Command;
import land.chipmunk.chayapak.chomens_bot.command.CommandContext; import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; import land.chipmunk.chayapak.chomens_bot.command.TrustLevel;
@ -21,10 +22,13 @@ import java.nio.file.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
public class MusicCommand extends Command { public class MusicCommand extends Command {
private Path root; private Path root;
private int ratelimit = 0;
public MusicCommand () { public MusicCommand () {
super( super(
"music", "music",
@ -48,12 +52,18 @@ public class MusicCommand extends Command {
TrustLevel.PUBLIC, TrustLevel.PUBLIC,
false false
); );
Main.executor.scheduleAtFixedRate(() -> ratelimit = 0, 0, 5, TimeUnit.SECONDS);
} }
@Override @Override
public Component execute(CommandContext context, String[] args, String[] fullArgs) { public Component execute(CommandContext context, String[] args, String[] fullArgs) {
if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED); if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED);
ratelimit++;
if (ratelimit > 15) return null;
root = MusicPlayerPlugin.SONG_DIR; root = MusicPlayerPlugin.SONG_DIR;
return switch (args[0]) { return switch (args[0]) {
case "play", "playurl", "playnbs", "playnbsurl" -> play(context, args); 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); if (!path.normalize().startsWith(root.toString())) return Component.text("no").color(NamedTextColor.RED);
// ignore my ohio code for autocomplete // 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("")) { if (_path.contains(separator) && !_path.equals("")) {
final String[] pathSplitted = _path.split(separator); final String[] pathSplitted = _path.split(separator);
@ -160,7 +170,7 @@ public class MusicCommand extends Command {
context.sendOutput( context.sendOutput(
Component.empty() Component.empty()
.append(Component.text("Now looping ")) .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)) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
); );
} }
@ -264,7 +274,7 @@ public class MusicCommand extends Command {
context.sendOutput( context.sendOutput(
Component.empty() Component.empty()
.append(Component.text("Skipping ")) .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)) .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
); );
@ -280,7 +290,7 @@ public class MusicCommand extends Command {
return Component.empty() return Component.empty()
.append(Component.text("Now playing ")) .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)); .color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
} }
@ -292,7 +302,7 @@ public class MusicCommand extends Command {
int i = 0; int i = 0;
for (Song song : queue) { for (Song song : queue) {
queueWithNames.add( 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); if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED);
// ig very code yup // 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 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 songOriginalAuthor = currentSong.songOriginalAuthor == null || currentSong.songOriginalAuthor.equals("") ? "N/A" : currentSong.songOriginalAuthor;
final String songDescription = currentSong.songDescription == null || currentSong.songDescription.equals("") ? "N/A" : currentSong.songDescription; final String songDescription = currentSong.songDescription == null || currentSong.songDescription.equals("") ? "N/A" : currentSong.songDescription;
@ -390,7 +400,7 @@ public class MusicCommand extends Command {
Author: %s Author: %s
Original author: %s Original author: %s
Description: %s""", Description: %s""",
title.color(NamedTextColor.AQUA), Component.text(title).color(NamedTextColor.AQUA),
Component.text(songAuthor).color(NamedTextColor.AQUA), Component.text(songAuthor).color(NamedTextColor.AQUA),
Component.text(songOriginalAuthor).color(NamedTextColor.AQUA), Component.text(songOriginalAuthor).color(NamedTextColor.AQUA),
Component.text(songDescription).color(NamedTextColor.AQUA) Component.text(songDescription).color(NamedTextColor.AQUA)

View file

@ -21,6 +21,7 @@ import java.nio.file.Path;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
// Author: _ChipMC_ & chayapak <3 // Author: _ChipMC_ & chayapak <3
public class MusicPlayerPlugin extends Bot.Listener { public class MusicPlayerPlugin extends Bot.Listener {
@ -36,6 +37,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
} }
public int loadings = 0;
public Song currentSong; public Song currentSong;
public final List<Song> songQueue = new ArrayList<>(); public final List<Song> songQueue = new ArrayList<>();
public SongLoaderRunnable loaderThread; public SongLoaderRunnable loaderThread;
@ -47,6 +50,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
private int ticksUntilPausedBossbar = 20; private int ticksUntilPausedBossbar = 20;
private int notesPerSecond = 0;
private final String bossbarName = "music"; private final String bossbarName = "music";
public MusicPlayerPlugin (Bot bot) { public MusicPlayerPlugin (Bot bot) {
@ -55,6 +60,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
bot.core.addListener(new CorePlugin.Listener() { bot.core.addListener(new CorePlugin.Listener() {
public void ready () { coreReady(); } public void ready () { coreReady(); }
}); });
bot.executor.scheduleAtFixedRate(() -> notesPerSecond = 0, 0, 1, TimeUnit.SECONDS);
} }
public void loadSong (Path location) { public void loadSong (Path location) {
@ -73,6 +79,13 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
public void loadSong (URL location) { 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); final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot);
bot.chat.tellraw( bot.chat.tellraw(
@ -101,7 +114,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
bot.chat.tellraw( bot.chat.tellraw(
Component.translatable( Component.translatable(
"Now playing %s", "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)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
); );
currentSong.play(); currentSong.play();
@ -134,7 +147,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
bot.chat.tellraw( bot.chat.tellraw(
Component.translatable( Component.translatable(
"Finished playing %s", "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)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
); );
@ -208,7 +221,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
public Component generateBossbar () { public Component generateBossbar () {
Component component = Component.empty() 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.text(" | ").color(NamedTextColor.DARK_GRAY))
.append( .append(
Component Component
@ -262,6 +275,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
public void stopPlaying () { public void stopPlaying () {
removeBossBar(); removeBossBar();
currentSong = null; currentSong = null;
notesPerSecond = 0;
} }
@Override @Override
@ -274,6 +288,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
while (currentSong.reachedNextNote()) { while (currentSong.reachedNextNote()) {
final Note note = currentSong.getNextNote(); final Note note = currentSong.getNextNote();
if (notesPerSecond > 1000) continue;
float key = note.pitch; float key = note.pitch;
// totally didn't look at the minecraft code and found the note block pitch thingy so i totallydidnotskidded it // 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) MathUtilities.clamp(floatingPitch, 0, 2)
); );
notesPerSecond++;
} }
} }
} }

View file

@ -1,14 +1,14 @@
package land.chipmunk.chayapak.chomens_bot.song; package land.chipmunk.chayapak.chomens_bot.song;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import net.kyori.adventure.text.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
// Author: hhhzzzsss & _ChipMC_ but i changed most of the stuff // Author: hhhzzzsss & _ChipMC_ but i changed most of the stuff
public class Song { public class Song {
public final ArrayList<Note> notes = new ArrayList<>(); public final ArrayList<Note> notes = new ArrayList<>();
public final Component name; public final String name;
public int position = 0; // Current note index public int position = 0; // Current note index
public boolean paused = true; public boolean paused = true;
public long startTime = 0; // Start time in millis since unix epoch public long startTime = 0; // Start time in millis since unix epoch
@ -28,9 +28,9 @@ public class Song {
private Bot bot; private Bot bot;
public Song (Component name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) { public Song (String name, Bot bot, String songName, String songAuthor, String songOriginalAuthor, String songDescription, boolean nbs) {
this.bot = bot;
this.name = name; this.name = name;
this.bot = bot;
this.songName = songName; this.songName = songName;
this.songAuthor = songAuthor; this.songAuthor = songAuthor;
this.songOriginalAuthor = songOriginalAuthor; this.songOriginalAuthor = songOriginalAuthor;
@ -38,11 +38,6 @@ public class Song {
this.nbs = nbs; 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) { public Note get (int i) {
return notes.get(i); return notes.get(i);
} }

View file

@ -71,6 +71,8 @@ public class SongLoaderRunnable implements Runnable {
if (isUrl) { if (isUrl) {
bytes = DownloadUtilities.DownloadToByteArray(songUrl, 10*1024*1024); bytes = DownloadUtilities.DownloadToByteArray(songUrl, 10*1024*1024);
name = Paths.get(songUrl.toURI().getPath()).getFileName().toString(); name = Paths.get(songUrl.toURI().getPath()).getFileName().toString();
bot.music.loadings--;
} else { } else {
bytes = Files.readAllBytes(songPath); bytes = Files.readAllBytes(songPath);
name = !isFolder ? fileName : songPath.getFileName().toString(); name = !isFolder ? fileName : songPath.getFileName().toString();
@ -119,7 +121,7 @@ public class SongLoaderRunnable implements Runnable {
bot.chat.tellraw( bot.chat.tellraw(
Component.translatable( Component.translatable(
"Added %s to the song queue", "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)) ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
); );
} }