fix EVERY SINGLE music problem

idk i fixed it i guess which is nice ?
This commit is contained in:
ChomeNS 2023-03-21 10:12:19 +07:00
parent b340175aab
commit b75c4c29ab
4 changed files with 186 additions and 64 deletions

View file

@ -88,6 +88,7 @@ public class Bot {
for (SessionListener listener : listeners) {
listener.packetError(packetErrorEvent);
}
packetErrorEvent.setSuppress(false); // ? idk what this does but whatever
}
@Override

View file

@ -4,6 +4,7 @@ import me.chayapak1.chomensbot_mabe.Bot;
import me.chayapak1.chomensbot_mabe.command.Command;
import me.chayapak1.chomensbot_mabe.command.CommandContext;
import me.chayapak1.chomensbot_mabe.plugins.MusicPlayerPlugin;
import me.chayapak1.chomensbot_mabe.song.Song;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.event.ClickEvent;
@ -16,6 +17,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class MusicCommand implements Command {
@ -36,6 +38,9 @@ public class MusicCommand implements Command {
usages.add("skip");
usages.add("nowplaying");
usages.add("queue");
usages.add("goto");
usages.add("pause");
usages.add("resume");
return usages;
}
@ -67,6 +72,19 @@ public class MusicCommand implements Command {
case "skip" -> {
return skip(context);
}
case "nowplaying" -> {
return nowplaying(context);
}
case "queue" -> queue(context);
case "goto" -> {
return goTo(context, args);
}
case "pause", "resume" -> {
return pause(context);
}
default -> {
return Component.text("Invalid argument").color(NamedTextColor.RED);
}
}
return Component.text("success");
@ -102,15 +120,32 @@ public class MusicCommand implements Command {
int loop;
switch (args[1]) {
case "off" -> loop = 0;
case "current" -> loop = 1;
case "all" -> loop = 2;
case "off" -> {
loop = 0;
context.sendOutput(
Component.empty()
.append(Component.text("Looping is now "))
.append(Component.text("disabled").color(NamedTextColor.RED))
);
}
case "current" -> {
loop = 1;
context.sendOutput(
Component.empty()
.append(Component.text("Now looping "))
.append(bot.music().currentSong().name.color(NamedTextColor.GOLD))
);
}
case "all" -> {
loop = 2;
context.sendOutput(Component.text("Now looping every song"));
}
default -> {
return Component.text("Invalid argument");
return Component.text("Invalid argument").color(NamedTextColor.RED);
}
}
bot.music().currentSong().looping = loop;
bot.music().loop(loop);
return Component.text("success");
}
@ -167,7 +202,65 @@ public class MusicCommand implements Command {
.append(music.currentSong().name.color(NamedTextColor.GOLD))
);
music.stopPlaying();
music.skip();
return Component.text("success");
}
public Component nowplaying (CommandContext context) {
final Bot bot = context.bot();
final Song song = bot.music().currentSong();
if (song == null) return Component.text("No song is currently playing").color(NamedTextColor.RED);
context.sendOutput(
Component.empty()
.append(Component.text("Now playing "))
.append(song.name.color(NamedTextColor.GOLD))
);
return Component.text("success");
}
public void queue (CommandContext context) {
final Bot bot = context.bot();
final LinkedList<Song> queue = bot.music().songQueue();
final List<Component> queueWithNames = new ArrayList<>();
for (Song song : queue) queueWithNames.add(song.name);
context.sendOutput(
Component.empty()
.append(Component.text("Queue: ").color(NamedTextColor.GREEN))
.append(Component.join(JoinConfiguration.separator(Component.space()), queueWithNames))
);
}
// lazy fix for java using "goto" as keyword real
public Component goTo (CommandContext context, String[] args) {
final Bot bot = context.bot();
final Song currentSong = bot.music().currentSong();
final long milliseconds = Long.getLong(args[0]);
if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED);
if (milliseconds < 0 || milliseconds > currentSong.length) return Component.text("Invalid timestamp").color(NamedTextColor.RED);
currentSong.setTime(milliseconds);
return Component.text("success");
}
public Component pause (CommandContext context) {
final Bot bot = context.bot();
final Song currentSong = bot.music().currentSong();
if (currentSong == null) return Component.text("No song is currently playing").color(NamedTextColor.RED);
if (currentSong.paused) {
currentSong.play();
context.sendOutput(Component.text("Resumed the current song"));
} else {
currentSong.pause();
context.sendOutput(Component.text("Paused the current song"));
}
return Component.text("success");
}

View file

@ -36,7 +36,8 @@ public class MusicPlayerPlugin extends SessionAdapter {
@Getter @Setter private Song currentSong;
@Getter @Setter private LinkedList<Song> songQueue = new LinkedList<>();
@Getter @Setter private SongLoaderThread loaderThread;
private int ticksUntilPausedActionbar = 20;
@Getter @Setter private int loop = 0;
private int ticksUntilPausedBossbar = 20;
private final String bossbarName = "chomens_bot:music"; // maybe make this in the config?
public MusicPlayerPlugin (Bot bot) {
@ -97,13 +98,13 @@ public class MusicPlayerPlugin extends SessionAdapter {
if (currentSong == null) {
if (songQueue.size() == 0) return;
currentSong = songQueue.poll();
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 && ticksUntilPausedActionbar-- < 0) return;
else ticksUntilPausedActionbar = 20;
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);
@ -120,7 +121,28 @@ public class MusicPlayerPlugin extends SessionAdapter {
if (currentSong.finished()) {
removeBossbar();
bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
currentSong = null;
if (loop == 1) {
currentSong.setTime(0);
return;
}
if (loop == 2) {
skip();
return;
}
songQueue.remove();
if (songQueue.size() == 0) {
stopPlaying();
bot.chat().tellraw(Component.text("Finished playing every sone in the queue"));
return;
}
if (currentSong.size() > 0) {
currentSong = songQueue.get(0);
currentSong.setTime(0);
currentSong.play();
}
}
};
@ -129,6 +151,19 @@ public class MusicPlayerPlugin extends SessionAdapter {
if (currentSong != null) currentSong.play();
}
public void skip () {
if (loop == 2) {
songQueue.add(songQueue.remove()); // bot.music.queue.push(bot.music.queue.shift()) in js
} else {
songQueue.remove();
stopPlaying();
}
if (songQueue.size() == 0) return;
currentSong = songQueue.get(0);
currentSong.setTime(0);
currentSong.play();
}
public void removeBossbar () {
bot.core().run("minecraft:bossbar remove " + bossbarName);
}
@ -144,14 +179,13 @@ public class MusicPlayerPlugin extends SessionAdapter {
if (currentSong.paused) {
return component
.append(Component.text(" | ", NamedTextColor.DARK_GRAY))
.append(Component.text("Paused", NamedTextColor.GREEN));
.append(Component.text("Paused", NamedTextColor.LIGHT_PURPLE));
}
if (currentSong.looping > 0) {
final int looping = currentSong().looping;
if (loop > 0) {
return component
.append(Component.translatable(" | ", NamedTextColor.DARK_GRAY))
.append(Component.translatable("Looping " + ((looping == 1) ? "current" : "all"), NamedTextColor.DARK_GREEN));
.append(Component.translatable("Looping " + ((loop == 1) ? "current" : "all"), NamedTextColor.LIGHT_PURPLE));
}
return component;

View file

@ -14,13 +14,12 @@ public class Song {
public long startTime = 0; // Start time in millis since unix epoch
public long length = 0; // Milliseconds in the song
public long time = 0; // Time since start of song
public long loopPosition = 0; // Milliseconds into the song to start looping
public int loopCount = 0; // Number of times to loop
public int currentLoop = 0; // Number of loops so far
public int queueIndex = 0;
public long loopPosition = 200; // Milliseconds into the song to start looping
// public int loopCount = 0; // Number of times to loop
// public int currentLoop = 0; // Number of loops so far
private Bot bot;
public Song (Component name, Bot bot) {
this.bot = bot;
this.name = name;
@ -30,11 +29,11 @@ public class Song {
this(Component.text(name), bot);
this.bot = bot;
}
public Note get (int i) {
return notes.get(i);
}
public void add (Note e) {
notes.add(e);
}
@ -84,8 +83,7 @@ public class Song {
if (position < notes.size()) {
return notes.get(position).time <= time;
} else {
if (time > length && shouldLoop()) {
loop();
if (finished() && bot.music().loop() > 0) {
if (position < notes.size()) {
return notes.get(position).time <= time;
} else {
@ -99,65 +97,61 @@ public class Song {
public Note getNextNote () {
if (position >= notes.size()) {
if (shouldLoop()) {
loop();
} else {
return null;
}
if (bot.music().loop() == 0) return null;
}
return notes.get(position++);
}
public boolean finished () {
return time > length && !shouldLoop();
return time > length;
}
private void loop () {
// private void loop () {
// if (looping == 2) {
// System.out.println("before adding any single shit length " + bot.music().songQueue().size());
// System.out.println("looping is 2 position " + position);
// final Song toAdd = bot.music().songQueue().remove();
// System.out.println("TO ADD " + toAdd.name);
// bot.music().songQueue().add(toAdd);
// try {
// for (Song song : bot.music().songQueue()) {
// System.out.println("song in list here " + song.name);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// System.out.println("BOT SONG QUEUEUE SIZE IS " + bot.music().songQueue().size());
// }
//
// System.out.println("now doing the position and the shits to reset the shit TIME");
//
// position = 0;
// startTime += length - loopPosition;
// time -= length - loopPosition;
// while (position < notes.size() && notes.get(position).time < loopPosition) {
// position++;
// }
// currentLoop++;
// if (looping == 1) {
position = 0;
startTime += length - loopPosition;
time -= length - loopPosition;
while (position < notes.size() && notes.get(position).time < loopPosition) {
position++;
}
// System.out.println("looping is 1 we did all shits here and position is " + position);
// } else if (looping == 2) {
// position = 0;
// setTime(0);
// System.out.println("looping is 2 position " + position);
// queueIndex = (queueIndex + 1) % bot.music().songQueue().size();
// System.out.println("queue INDEX IS " + queueIndex);
// System.out.println("BOT SONG QUEUEUE SIZE IS " + bot.music().songQueue().size());
// }
// System.out.println(currentLoop);
currentLoop++;
}
//// currentLoop++;
// }
private boolean shouldLoop () {
// if (looping) {
// private boolean shouldLoop () {
//// if (looping) {
//// if (loopCount == 0) {
//// return true;
//// } else {
//// return currentLoop < loopCount;
//// }
//// } else {
//// return false;
//// }
// if (looping == 1) {
// if (loopCount == 0) {
// return true;
// } else {
// return currentLoop < loopCount;
// }
// } else {
// return false;
// }
if (looping == 1) {
if (loopCount == 0) {
return true;
} else {
return currentLoop < loopCount;
}
} else return looping == 2;
}
// } else return looping == 2;
// }
public int size () {
return notes.size();