Compare commits

...

6 commits

7 changed files with 44 additions and 74 deletions

View file

@ -43,6 +43,8 @@ public class Main {
private static boolean alreadyStarted = false; private static boolean alreadyStarted = false;
private static boolean stopping = false;
private static final List<Thread> alreadyAddedThreads = new ArrayList<>(); private static final List<Thread> alreadyAddedThreads = new ArrayList<>();
private static JDA jda = null; private static JDA jda = null;
@ -187,6 +189,10 @@ public class Main {
// most of these are stolen from HBot // most of these are stolen from HBot
public static void stop () { public static void stop () {
if (stopping) return;
stopping = true;
executor.shutdown(); executor.shutdown();
PersistentDataUtilities.stop(); PersistentDataUtilities.stop();
@ -209,17 +215,17 @@ public class Main {
final boolean discordEnabled = config.discord.enabled; final boolean discordEnabled = config.discord.enabled;
for (Bot bot : copiedList) { for (Bot bot : copiedList) {
if (discordEnabled) { try {
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port); if (discordEnabled) {
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port);
bot.discord.sendMessageInstantly("Stopping..", channelId); bot.discord.sendMessageInstantly("Stopping..", channelId);
} }
if (ircEnabled) { if (ircEnabled) bot.irc.quit("Stopping..");
bot.irc.quit("Stopping..");
}
bot.stop(); bot.stop();
} catch (Exception ignored) {}
} }
if (jda != null) jda.shutdown(); if (jda != null) jda.shutdown();

View file

@ -12,7 +12,6 @@ import me.chayapak1.chomens_bot.song.Loop;
import me.chayapak1.chomens_bot.song.Note; import me.chayapak1.chomens_bot.song.Note;
import me.chayapak1.chomens_bot.song.Song; import me.chayapak1.chomens_bot.song.Song;
import me.chayapak1.chomens_bot.util.*; import me.chayapak1.chomens_bot.util.*;
import me.chayapak1.chomens_bot.util.*;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.TranslatableComponent;
@ -36,8 +35,6 @@ import static me.chayapak1.chomens_bot.util.StringUtilities.isNotNullAndNotBlank
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",
@ -63,15 +60,11 @@ 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) throws CommandException { public Component execute(CommandContext context) throws CommandException {
ratelimit++; if (context.bot.music.locked) throw new CommandException(Component.text("Managing music is currently locked"));
if (ratelimit > 10) return null;
final String action = context.getString(false, true, true); final String action = context.getString(false, true, true);
@ -264,7 +257,7 @@ public class MusicCommand extends Command {
final CompletableFuture<Component> future = bot.core.runTracked( final CompletableFuture<Component> future = bot.core.runTracked(
"minecraft:data get entity " + "minecraft:data get entity " +
UUIDUtilities.selector(context.sender.profile.getId()) + UUIDUtilities.selector(context.sender.profile.getId()) +
" SelectedItem.tag.SongItemData.SongData" " SelectedItem.components.minecraft:custom_data.SongItemData.SongData"
); );
if (future == null) { if (future == null) {
@ -281,7 +274,7 @@ public class MusicCommand extends Command {
.key() .key()
.equals("arguments.nbtpath.nothing_found") .equals("arguments.nbtpath.nothing_found")
) { ) {
context.sendOutput(Component.text("Player has no SongItemData -> SongData NBT tag in the selected item").color(NamedTextColor.RED)); context.sendOutput(Component.text("Player has no SongItemData -> SongData NBT tag in their selected item's minecraft:custom_data").color(NamedTextColor.RED));
return output; return output;
} }

View file

@ -169,7 +169,7 @@ public class IRCPlugin extends ListenerAdapter {
} }
public void quit (String reason) { public void quit (String reason) {
bot.sendIRC().quitServer(reason); if (bot.isConnected()) bot.sendIRC().quitServer(reason);
} }
private void connected (Bot bot) { private void connected (Bot bot) {
@ -202,9 +202,7 @@ public class IRCPlugin extends ListenerAdapter {
bot.sendIRC().message(entry.getKey(), withIRCColors); bot.sendIRC().message(entry.getKey(), withIRCColors);
} }
} catch (Exception e) { } catch (Exception ignored) {}
e.printStackTrace();
}
} }
private void addMessageToQueue (Bot bot, String message) { private void addMessageToQueue (Bot bot, String message) {

View file

@ -56,6 +56,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
private int limit = 0; private int limit = 0;
public boolean locked = false; // this can be set through servereval
private final String bossbarName = "music"; private final String bossbarName = "music";
public BossBarColor bossBarColor; public BossBarColor bossBarColor;
@ -73,7 +75,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
public void loadSong (Path location, PlayerEntry sender) { public void loadSong (Path location, PlayerEntry sender) {
if (songQueue.size() > 100) return; if (songQueue.size() > 500) return;
loaderThread = new SongLoaderThread(location, bot, sender.profile.getName()); loaderThread = new SongLoaderThread(location, bot, sender.profile.getName());
@ -90,7 +92,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
public void loadSong (URL location, PlayerEntry sender) { public void loadSong (URL location, PlayerEntry sender) {
if (songQueue.size() > 100) return; if (songQueue.size() > 500) return;
limit++; limit++;
@ -114,7 +116,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
public void loadSong (byte[] data, PlayerEntry sender) { public void loadSong (byte[] data, PlayerEntry sender) {
if (songQueue.size() > 100) return; if (songQueue.size() > 500) return;
loaderThread = new SongLoaderThread(data, bot, sender.profile.getName()); loaderThread = new SongLoaderThread(data, bot, sender.profile.getName());
@ -194,13 +196,9 @@ public class MusicPlayerPlugin extends Bot.Listener {
if (songQueue.isEmpty()) { if (songQueue.isEmpty()) {
stopPlaying(); stopPlaying();
removeBossBar(); removeBossBar();
bot.chat.tellraw(
Component
.text("Finished playing every song in the queue")
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
);
return; return;
} }
if (currentSong.size() > 0) { if (currentSong.size() > 0) {
currentSong = songQueue.get(0); currentSong = songQueue.get(0);
currentSong.setTime(0); currentSong.setTime(0);

View file

@ -17,7 +17,7 @@ public class Song {
public long startTime = 0; // Start time in millis since unix epoch public long startTime = 0; // Start time in millis since unix epoch
public long length = 0; // Milliseconds in the song public long length = 0; // Milliseconds in the song
public long time = 0; // Time since start of song public long time = 0; // Time since start of song
public long loopPosition = 200; // Milliseconds into the song to start looping public long loopPosition = 0; // Milliseconds into the song to start looping
public final Map<Long, String> lyrics = new HashMap<>(); public final Map<Long, String> lyrics = new HashMap<>();
@ -80,7 +80,7 @@ public class Song {
*/ */
public void play () { public void play () {
if (paused) { if (paused) {
if (loopPosition != 200) bot.music.loop = Loop.CURRENT; if (loopPosition != 0) bot.music.loop = Loop.CURRENT;
paused = false; paused = false;
startTime = System.currentTimeMillis() - time; startTime = System.currentTimeMillis() - time;
} }

View file

@ -46,7 +46,7 @@ public class SongPlayerConverter implements Converter {
song.length = songLength; song.length = songLength;
// song.looping = loop > 0; // song.looping = loop > 0;
// song.loopCount = loopCount; // song.loopCount = loopCount;
song.loopPosition = loopPosition == 0 ? 200 : loopPosition; song.loopPosition = loopPosition;
long time = 0; long time = 0;
while (true) { while (true) {

View file

@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import me.chayapak1.chomens_bot.Main;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -12,10 +11,6 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class PersistentDataUtilities { public class PersistentDataUtilities {
public static final Path path = Path.of("persistent.json"); public static final Path path = Path.of("persistent.json");
@ -24,33 +19,10 @@ public class PersistentDataUtilities {
public static JsonObject jsonObject = new JsonObject(); public static JsonObject jsonObject = new JsonObject();
private static final Map<String, JsonElement> queue = new ConcurrentHashMap<>(); private static boolean stopping = false;
private static final ScheduledFuture<?> future;
static { static {
init(); init();
future = Main.executor.scheduleAtFixedRate(() -> {
try {
if (queue.isEmpty()) return;
final Map.Entry<String, JsonElement> entry = queue.entrySet().iterator().next(); // is this the best way to get the first item of the map?
final String property = entry.getKey();
final JsonElement value = entry.getValue();
Main.executorService.submit(() -> {
jsonObject.add(property, value);
write(jsonObject.toString());
queue.remove(property);
});
} catch (Exception e) {
e.printStackTrace();
}
}, 0, 100, TimeUnit.MILLISECONDS);
} }
private static void init () { private static void init () {
@ -66,19 +38,17 @@ public class PersistentDataUtilities {
jsonObject = gson.fromJson(reader, JsonObject.class); jsonObject = gson.fromJson(reader, JsonObject.class);
} }
writer = Files.newBufferedWriter(path, StandardOpenOption.TRUNCATE_EXISTING); writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static void write (String string) { private static void write (String string) {
try { if (stopping) return; // is this necessary?
writer.close();
// ? how do i clear the file contents without making a completely new writer? try {
// or is this the only way? writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
writer = Files.newBufferedWriter(path, StandardOpenOption.TRUNCATE_EXISTING);
writer.write(string); writer.write(string);
writer.flush(); writer.flush();
@ -86,27 +56,32 @@ public class PersistentDataUtilities {
} }
public static void stop () { public static void stop () {
future.cancel(false); stopping = true;
write(jsonObject.toString()); write(jsonObject.toString());
} }
public static void put (String property, JsonElement value) { public static void put (String property, JsonElement value) {
Main.executorService.submit(() -> queue.put(property, value)); jsonObject.add(property, value);
write(jsonObject.toString());
} }
public static void put (String property, String value) { public static void put (String property, String value) {
Main.executorService.submit(() -> queue.put(property, new JsonPrimitive(value))); jsonObject.add(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
public static void put (String property, boolean value) { public static void put (String property, boolean value) {
Main.executorService.submit(() -> queue.put(property, new JsonPrimitive(value))); jsonObject.add(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
public static void put (String property, int value) { public static void put (String property, int value) {
Main.executorService.submit(() -> queue.put(property, new JsonPrimitive(value))); jsonObject.add(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
public static void put (String property, char value) { public static void put (String property, char value) {
Main.executorService.submit(() -> queue.put(property, new JsonPrimitive(value))); jsonObject.add(property, new JsonPrimitive(value));
write(jsonObject.toString());
} }
} }