queue and some improvements LoL
This commit is contained in:
parent
196fe8f2e2
commit
34f9989d11
5 changed files with 34 additions and 20 deletions
|
@ -50,7 +50,7 @@ public class UrbanCommand extends Command {
|
|||
|
||||
final JsonArray list = jsonObject.getAsJsonArray("list");
|
||||
|
||||
if (list.size() == 0) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED));
|
||||
if (list.isEmpty()) context.sendOutput(Component.text("No results found").color(NamedTextColor.RED));
|
||||
|
||||
for (JsonElement element : list) {
|
||||
final JsonObject definitionObject = element.getAsJsonObject();
|
||||
|
|
|
@ -216,7 +216,7 @@ public class ChatPlugin extends Bot.Listener {
|
|||
}
|
||||
|
||||
private void sendChatTick () {
|
||||
if (queue.size() == 0) return;
|
||||
if (queue.isEmpty()) return;
|
||||
|
||||
final String message = queue.get(0);
|
||||
|
||||
|
|
|
@ -146,7 +146,7 @@ public class DiscordPlugin {
|
|||
|
||||
Component embedsComponent = Component.empty();
|
||||
if (messageEvent.getEmbeds().size() > 0) {
|
||||
if (messageEvent.getAttachments().size() == 0) embedsComponent = embedsComponent.append(Component.space());
|
||||
if (messageEvent.getAttachments().isEmpty()) embedsComponent = embedsComponent.append(Component.space());
|
||||
for (MessageEmbed embed : messageEvent.getEmbeds()) {
|
||||
final Component hoverEvent = Component.translatable(
|
||||
"""
|
||||
|
|
|
@ -85,7 +85,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
|||
public void onTick() {
|
||||
try {
|
||||
if (currentSong == null) {
|
||||
if (songQueue.size() == 0) return; // this line
|
||||
if (songQueue.isEmpty()) return; // this line
|
||||
|
||||
addBossBar();
|
||||
|
||||
|
@ -136,7 +136,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
|||
|
||||
songQueue.remove(0);
|
||||
|
||||
if (songQueue.size() == 0) {
|
||||
if (songQueue.isEmpty()) {
|
||||
stopPlaying();
|
||||
removeBossBar();
|
||||
bot.chat.tellraw(
|
||||
|
@ -168,7 +168,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
|
|||
songQueue.remove(0);
|
||||
stopPlaying();
|
||||
}
|
||||
if (songQueue.size() == 0) return;
|
||||
if (songQueue.isEmpty()) return;
|
||||
currentSong = songQueue.get(0);
|
||||
currentSong.setTime(0);
|
||||
currentSong.play();
|
||||
|
|
|
@ -3,9 +3,13 @@ package land.chipmunk.chayapak.chomens_bot.util;
|
|||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import land.chipmunk.chayapak.chomens_bot.Main;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class PersistentDataUtilities {
|
||||
public static final File file = new File("persistent.json");
|
||||
|
@ -14,7 +18,7 @@ public class PersistentDataUtilities {
|
|||
|
||||
public static JsonObject jsonObject = new JsonObject();
|
||||
|
||||
private static boolean isWriting = false;
|
||||
private static final List<String> queue = new ArrayList<>();
|
||||
|
||||
static {
|
||||
init();
|
||||
|
@ -35,27 +39,37 @@ public class PersistentDataUtilities {
|
|||
}
|
||||
|
||||
writer = new FileWriter(file, false);
|
||||
|
||||
Main.executor.scheduleAtFixedRate(() -> {
|
||||
if (queue.isEmpty()) return;
|
||||
|
||||
if (queue.size() > 50) queue.clear();
|
||||
|
||||
write(queue.get(0));
|
||||
|
||||
queue.remove(0);
|
||||
}, 0, 50, TimeUnit.MILLISECONDS);
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
while (true) {
|
||||
if (queue.isEmpty()) break;
|
||||
}
|
||||
}));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void write () {
|
||||
private static void write (String object) {
|
||||
try {
|
||||
if (isWriting) return;
|
||||
|
||||
isWriting = true;
|
||||
|
||||
writer.close();
|
||||
|
||||
// ? how do i clear the file contents without making a completely new FileWriter
|
||||
// or is this the only way?
|
||||
writer = new FileWriter(file, false);
|
||||
|
||||
writer.write(jsonObject.toString());
|
||||
writer.write(object);
|
||||
writer.flush();
|
||||
|
||||
isWriting = false;
|
||||
} catch (IOException | ConcurrentModificationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -65,34 +79,34 @@ public class PersistentDataUtilities {
|
|||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.add(property, value);
|
||||
|
||||
write();
|
||||
queue.add(jsonObject.toString());
|
||||
}
|
||||
|
||||
public static void put (String property, String value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
write();
|
||||
queue.add(jsonObject.toString());
|
||||
}
|
||||
|
||||
public static void put (String property, boolean value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
write();
|
||||
queue.add(jsonObject.toString());
|
||||
}
|
||||
|
||||
public static void put (String property, int value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
write();
|
||||
queue.add(jsonObject.toString());
|
||||
}
|
||||
|
||||
public static void put (String property, char value) {
|
||||
if (jsonObject.has(property)) jsonObject.remove(property);
|
||||
jsonObject.addProperty(property, value);
|
||||
|
||||
write();
|
||||
queue.add(jsonObject.toString());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue