fix some warnings and fix music exception

This commit is contained in:
Chayapak 2023-06-30 20:54:28 +07:00
parent dcf24f5d18
commit 268f554f4a
12 changed files with 55 additions and 70 deletions

View file

@ -42,7 +42,7 @@ public class CommandBlockCommand implements Command {
return TrustLevel.PUBLIC; return TrustLevel.PUBLIC;
} }
public Component execute(CommandContext context, String[] args, String[] fullArgs) throws ExecutionException, InterruptedException { public Component execute(CommandContext context, String[] args, String[] fullArgs) {
final Bot bot = context.bot(); final Bot bot = context.bot();
final CompletableFuture<CompoundTag> future = bot.core().runTracked(String.join(" ", args)); final CompletableFuture<CompoundTag> future = bot.core().runTracked(String.join(" ", args));

View file

@ -12,7 +12,7 @@ import java.util.UUID;
public class BotBossBar extends BossBar { public class BotBossBar extends BossBar {
public UUID uuid = UUID.randomUUID(); // the random uuid will be temporary public UUID uuid = UUID.randomUUID(); // the random uuid will be temporary
public Component secret = Component.text(Math.random() * 69420); public final Component secret = Component.text(Math.random() * 69420);
private final Bot bot; private final Bot bot;

View file

@ -2,7 +2,6 @@ package land.chipmunk.chayapak.chomens_bot.data;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.joda.time.DateTime;
@AllArgsConstructor @AllArgsConstructor
public class Mail { public class Mail {

View file

@ -25,7 +25,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
private final Bot bot; private final Bot bot;
public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]"; public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]";
public static File SONG_DIR = new File("songs"); public static final File SONG_DIR = new File("songs");
static { static {
if (!SONG_DIR.exists()) SONG_DIR.mkdir(); if (!SONG_DIR.exists()) SONG_DIR.mkdir();
} }
@ -52,7 +52,6 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
public void loadSong (Path location) { public void loadSong (Path location) {
try {
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot); final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot);
bot.chat().tellraw( bot.chat().tellraw(
@ -65,15 +64,9 @@ public class MusicPlayerPlugin extends Bot.Listener {
); );
bot.executorService().submit(runnable); bot.executorService().submit(runnable);
} catch (SongLoaderException e) {
e.printStackTrace();
bot.chat().tellraw(Component.translatable("Failed to load song: %s", e.message()).color(NamedTextColor.RED));
loaderThread = null;
}
} }
public void loadSong (URL location) { public void loadSong (URL location) {
try {
final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot); final SongLoaderRunnable runnable = new SongLoaderRunnable(location, bot);
bot.chat().tellraw( bot.chat().tellraw(
@ -86,10 +79,6 @@ public class MusicPlayerPlugin extends Bot.Listener {
); );
bot.executorService().submit(runnable); bot.executorService().submit(runnable);
} catch (SongLoaderException e) {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", e.message()).color(NamedTextColor.RED));
loaderThread = null;
}
} }
public void coreReady () { public void coreReady () {

View file

@ -33,7 +33,7 @@ public class VoiceChatPlugin extends Bot.Listener {
private boolean running = false; private boolean running = false;
@Getter private List<ClientGroup> groups = new ArrayList<>(); @Getter private final List<ClientGroup> groups = new ArrayList<>();
public VoiceChatPlugin(Bot bot) { public VoiceChatPlugin(Bot bot) {
this.bot = bot; this.bot = bot;

View file

@ -150,7 +150,7 @@ public class MidiConverter {
return null; return null;
} }
public static HashMap<Integer, Instrument[]> instrumentMap = new HashMap<>(); public static final HashMap<Integer, Instrument[]> instrumentMap = new HashMap<>();
static { static {
// Piano (HARP BASS BELL) // Piano (HARP BASS BELL)
instrumentMap.put(0, new Instrument[]{Instrument.HARP, Instrument.BASS, Instrument.BELL}); // Acoustic Grand Piano instrumentMap.put(0, new Instrument[]{Instrument.HARP, Instrument.BASS, Instrument.BELL}); // Acoustic Grand Piano
@ -303,7 +303,7 @@ public class MidiConverter {
instrumentMap.put(119, new Instrument[]{Instrument.IRON_XYLOPHONE, Instrument.BASS, Instrument.XYLOPHONE}); instrumentMap.put(119, new Instrument[]{Instrument.IRON_XYLOPHONE, Instrument.BASS, Instrument.XYLOPHONE});
} }
public static HashMap<Integer, Integer> percussionMap = new HashMap<>(); public static final HashMap<Integer, Integer> percussionMap = new HashMap<>();
static { static {
percussionMap.put(35, 10 + 25*Instrument.BASEDRUM.id); percussionMap.put(35, 10 + 25*Instrument.BASEDRUM.id);
percussionMap.put(36, 6 + 25*Instrument.BASEDRUM.id); percussionMap.put(36, 6 + 25*Instrument.BASEDRUM.id);

View file

@ -9,7 +9,7 @@ import java.util.ArrayList;
// Author: hhhzzzsss // Author: hhhzzzsss
public class NBSConverter { public class NBSConverter {
public static Instrument[] instrumentIndex = new Instrument[] { public static final Instrument[] instrumentIndex = new Instrument[] {
Instrument.HARP, Instrument.HARP,
Instrument.BASS, Instrument.BASS,
Instrument.BASEDRUM, Instrument.BASEDRUM,

View file

@ -7,8 +7,8 @@ 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 ArrayList<Note> notes = new ArrayList<>(); public final ArrayList<Note> notes = new ArrayList<>();
public Component name; public final Component 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
@ -16,12 +16,12 @@ public class 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 = 200; // Milliseconds into the song to start looping
public String songName; public final String songName;
public String songAuthor; public final String songAuthor;
public String songOriginalAuthor; public final String songOriginalAuthor;
public String songDescription; public final String songDescription;
public boolean nbs; public final boolean nbs;
// public int loopCount = 0; // Number of times to loop // public int loopCount = 0; // Number of times to loop
// public int currentLoop = 0; // Number of loops so far // public int currentLoop = 0; // Number of loops so far

View file

@ -1,7 +1,6 @@
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 land.chipmunk.chayapak.chomens_bot.plugins.MusicPlayerPlugin;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import land.chipmunk.chayapak.chomens_bot.util.DownloadUtilities; import land.chipmunk.chayapak.chomens_bot.util.DownloadUtilities;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -16,7 +15,7 @@ import java.nio.file.Paths;
// Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable // Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable
// because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public class SongLoaderRunnable implements Runnable { public class SongLoaderRunnable implements Runnable {
public String fileName; public final String fileName;
private File songPath; private File songPath;
private URL songUrl; private URL songUrl;
@ -27,7 +26,7 @@ public class SongLoaderRunnable implements Runnable {
private final boolean isUrl; private final boolean isUrl;
public SongLoaderRunnable(URL location, Bot bot) throws SongLoaderException { public SongLoaderRunnable(URL location, Bot bot) {
this.bot = bot; this.bot = bot;
isUrl = true; isUrl = true;
songUrl = location; songUrl = location;
@ -35,7 +34,7 @@ public class SongLoaderRunnable implements Runnable {
fileName = location.getFile(); fileName = location.getFile();
} }
public SongLoaderRunnable(Path location, Bot bot) throws SongLoaderException { public SongLoaderRunnable(Path location, Bot bot) {
this.bot = bot; this.bot = bot;
isUrl = false; isUrl = false;
songPath = location.toFile(); songPath = location.toFile();
@ -55,9 +54,9 @@ public class SongLoaderRunnable implements Runnable {
name = songPath.getName(); name = songPath.getName();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); exception = new SongLoaderException(Component.text(e.getMessage()));
showFailedMessage(); failed();
return; return;
} }
@ -79,7 +78,7 @@ public class SongLoaderRunnable implements Runnable {
if (song == null) { if (song == null) {
exception = new SongLoaderException(Component.translatable("Invalid format")); exception = new SongLoaderException(Component.translatable("Invalid format"));
showFailedMessage(); failed();
} else { } else {
bot.music().songQueue().add(song); bot.music().songQueue().add(song);
bot.chat().tellraw( bot.chat().tellraw(
@ -91,7 +90,9 @@ public class SongLoaderRunnable implements Runnable {
} }
} }
private void showFailedMessage () { private void failed() {
exception.printStackTrace();
bot.chat().tellraw(Component.translatable("Failed to load song: %s", exception.message()).color(NamedTextColor.RED)); bot.chat().tellraw(Component.translatable("Failed to load song: %s", exception.message()).color(NamedTextColor.RED));
bot.music().loaderThread(null);
} }
} }

View file

@ -11,7 +11,6 @@ import net.kyori.adventure.text.SelectorComponent;
import net.kyori.adventure.text.KeybindComponent; import net.kyori.adventure.text.KeybindComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
@ -176,23 +175,20 @@ public class ComponentUtilities {
} else if (ansi) { } else if (ansi) {
String ansiCode = ansiMap.get(code); String ansiCode = ansiMap.get(code);
if (ansiCode == null) { if (ansiCode == null) {
// will using string builders use less memory or does nothing? ansiCode = "\u001b[38;2;" +
final StringBuilder builder = new StringBuilder(); color.red() +
builder.append("\u001b[38;2;"); ";" +
builder.append(color.red()); color.green() +
builder.append(";"); ";" +
builder.append(color.green()); color.blue() +
builder.append(";"); "m";
builder.append(color.blue());
builder.append("m");
ansiCode = builder.toString();
} }
return ansiCode; return ansiCode;
} else return null; } else return null;
} }
@SuppressWarnings("UnnecessaryUnicodeEscape")
public static PartiallyStringifiedOutput stringifyPartially (TextComponent message, boolean motd, boolean ansi, String lastColor) { public static PartiallyStringifiedOutput stringifyPartially (TextComponent message, boolean motd, boolean ansi, String lastColor) {
if (motd || ansi) { if (motd || ansi) {
final String color = getColor(message.color(), motd, ansi); final String color = getColor(message.color(), motd, ansi);

View file

@ -14,8 +14,8 @@ import java.util.zip.GZIPOutputStream;
// totallynotskidded from HBot // totallynotskidded from HBot
public class FileLoggerUtilities { public class FileLoggerUtilities {
public static File logDir = new File("logs"); public static final File logDir = new File("logs");
public static File logFile = new File(logDir, "log.txt"); public static final File logFile = new File(logDir, "log.txt");
public static OutputStreamWriter logWriter; public static OutputStreamWriter logWriter;
public static LocalDate currentLogDate; public static LocalDate currentLogDate;
@ -24,7 +24,7 @@ public class FileLoggerUtilities {
public static String prevEntry = ""; public static String prevEntry = "";
public static int duplicateCounter = 1; public static int duplicateCounter = 1;
public static ScheduledExecutorService executor = Main.executor; public static final ScheduledExecutorService executor = Main.executor;
public static int spamLevel = 0; public static int spamLevel = 0;
public static long freezeTime = 0; public static long freezeTime = 0;

View file

@ -7,7 +7,7 @@ import com.google.gson.JsonObject;
import java.io.*; import java.io.*;
public class PersistentDataUtilities { public class PersistentDataUtilities {
public static File file = new File("persistent.json"); public static final File file = new File("persistent.json");
private static FileWriter writer; private static FileWriter writer;