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 stopping = false;
private static final List<Thread> alreadyAddedThreads = new ArrayList<>();
private static JDA jda = null;
@ -187,6 +189,10 @@ public class Main {
// most of these are stolen from HBot
public static void stop () {
if (stopping) return;
stopping = true;
executor.shutdown();
PersistentDataUtilities.stop();
@ -209,17 +215,17 @@ public class Main {
final boolean discordEnabled = config.discord.enabled;
for (Bot bot : copiedList) {
try {
if (discordEnabled) {
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port);
bot.discord.sendMessageInstantly("Stopping..", channelId);
}
if (ircEnabled) {
bot.irc.quit("Stopping..");
}
if (ircEnabled) bot.irc.quit("Stopping..");
bot.stop();
} catch (Exception ignored) {}
}
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.Song;
import me.chayapak1.chomens_bot.util.*;
import me.chayapak1.chomens_bot.util.*;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TranslatableComponent;
@ -36,8 +35,6 @@ import static me.chayapak1.chomens_bot.util.StringUtilities.isNotNullAndNotBlank
public class MusicCommand extends Command {
private Path root;
private int ratelimit = 0;
public MusicCommand () {
super(
"music",
@ -63,15 +60,11 @@ public class MusicCommand extends Command {
TrustLevel.PUBLIC,
false
);
Main.executor.scheduleAtFixedRate(() -> ratelimit = 0, 0, 5, TimeUnit.SECONDS);
}
@Override
public Component execute(CommandContext context) throws CommandException {
ratelimit++;
if (ratelimit > 10) return null;
if (context.bot.music.locked) throw new CommandException(Component.text("Managing music is currently locked"));
final String action = context.getString(false, true, true);
@ -264,7 +257,7 @@ public class MusicCommand extends Command {
final CompletableFuture<Component> future = bot.core.runTracked(
"minecraft:data get entity " +
UUIDUtilities.selector(context.sender.profile.getId()) +
" SelectedItem.tag.SongItemData.SongData"
" SelectedItem.components.minecraft:custom_data.SongItemData.SongData"
);
if (future == null) {
@ -281,7 +274,7 @@ public class MusicCommand extends Command {
.key()
.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;
}

View file

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

View file

@ -56,6 +56,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
private int limit = 0;
public boolean locked = false; // this can be set through servereval
private final String bossbarName = "music";
public BossBarColor bossBarColor;
@ -73,7 +75,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
}
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());
@ -90,7 +92,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
}
public void loadSong (URL location, PlayerEntry sender) {
if (songQueue.size() > 100) return;
if (songQueue.size() > 500) return;
limit++;
@ -114,7 +116,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
}
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());
@ -194,13 +196,9 @@ public class MusicPlayerPlugin extends Bot.Listener {
if (songQueue.isEmpty()) {
stopPlaying();
removeBossBar();
bot.chat.tellraw(
Component
.text("Finished playing every song in the queue")
.color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor))
);
return;
}
if (currentSong.size() > 0) {
currentSong = songQueue.get(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 length = 0; // Milliseconds in the 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<>();
@ -80,7 +80,7 @@ public class Song {
*/
public void play () {
if (paused) {
if (loopPosition != 200) bot.music.loop = Loop.CURRENT;
if (loopPosition != 0) bot.music.loop = Loop.CURRENT;
paused = false;
startTime = System.currentTimeMillis() - time;
}

View file

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

View file

@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import me.chayapak1.chomens_bot.Main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
@ -12,10 +11,6 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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 static final Path path = Path.of("persistent.json");
@ -24,33 +19,10 @@ public class PersistentDataUtilities {
public static JsonObject jsonObject = new JsonObject();
private static final Map<String, JsonElement> queue = new ConcurrentHashMap<>();
private static final ScheduledFuture<?> future;
private static boolean stopping = false;
static {
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 () {
@ -66,19 +38,17 @@ public class PersistentDataUtilities {
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) {
e.printStackTrace();
}
}
private static void write (String string) {
try {
writer.close();
if (stopping) return; // is this necessary?
// ? how do i clear the file contents without making a completely new writer?
// or is this the only way?
writer = Files.newBufferedWriter(path, StandardOpenOption.TRUNCATE_EXISTING);
try {
writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
writer.write(string);
writer.flush();
@ -86,27 +56,32 @@ public class PersistentDataUtilities {
}
public static void stop () {
future.cancel(false);
stopping = true;
write(jsonObject.toString());
}
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) {
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) {
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) {
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) {
Main.executorService.submit(() -> queue.put(property, new JsonPrimitive(value)));
jsonObject.add(property, new JsonPrimitive(value));
write(jsonObject.toString());
}
}