diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java index ccc2d50..e52fae9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java @@ -15,6 +15,8 @@ import java.io.*; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -37,11 +39,12 @@ public class Main { private static Configuration config; public static void main(String[] args) throws IOException { - final File file = new File("config.yml"); + final Path configPath = Path.of("config.yml"); + final Constructor constructor = new Constructor(Configuration.class, new LoaderOptions()); final Yaml yaml = new Yaml(constructor); - if (!file.exists()) { + if (!Files.exists(configPath)) { // creates config file from default-config.yml InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default-config.yml"); @@ -56,14 +59,14 @@ public class Main { String defaultConfig = stringBuilder.toString(); // writes it - BufferedWriter configWriter = new BufferedWriter(new FileWriter(file)); + BufferedWriter configWriter = Files.newBufferedWriter(configPath); configWriter.write(defaultConfig); configWriter.close(); LoggerUtilities.info("config.yml file not found, so the default one was created"); } - InputStream opt = new FileInputStream(file); + InputStream opt = Files.newInputStream(configPath); BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); config = yaml.load(reader); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java index f57b9c1..9f68071 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/MusicCommand.java @@ -16,6 +16,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -54,7 +55,7 @@ false public Component execute(CommandContext context, String[] args, String[] fullArgs) { if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED); - root = Path.of(MusicPlayerPlugin.SONG_DIR.getPath()); + root = MusicPlayerPlugin.SONG_DIR; return switch (args[0]) { case "play", "playurl", "playnbs", "playnbsurl" -> play(context, args); case "stop" -> stop(context); @@ -202,8 +203,7 @@ false final List fullList = new ArrayList<>(); int i = 0; for (String filename : filenames) { - final String pathString = path.toString(); - final File file = new File(Paths.get(pathString, filename).toUri()); + final boolean isDirectory = Files.isDirectory(path); Path location; try { @@ -219,7 +219,7 @@ false ClickEvent.suggestCommand( prefix + name + - (file.isFile() ? " play " : " list ") + + (isDirectory ? " list " : " play ") + joinedPath ) ) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java index 2d6af86..23a646c 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java @@ -14,8 +14,9 @@ import land.chipmunk.chayapak.chomens_bot.util.MathUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import java.io.File; +import java.io.IOException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; import java.text.DecimalFormat; import java.util.ArrayList; @@ -26,9 +27,13 @@ public class MusicPlayerPlugin extends Bot.Listener { private final Bot bot; public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]"; - public static final File SONG_DIR = new File("songs"); + public static final Path SONG_DIR = Path.of("songs"); static { - if (!SONG_DIR.exists()) SONG_DIR.mkdir(); + try { + if (!Files.exists(SONG_DIR)) Files.createDirectory(SONG_DIR); + } catch (IOException e) { + e.printStackTrace(); + } } public Song currentSong; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PacketSnifferPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PacketSnifferPlugin.java index 779c3c9..9cc9f99 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PacketSnifferPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/PacketSnifferPlugin.java @@ -4,31 +4,33 @@ import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; -import java.io.FileOutputStream; +import java.io.BufferedWriter; import java.io.IOException; -import java.io.OutputStreamWriter; -import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; // normally unused in the main instance of the bot public class PacketSnifferPlugin extends Bot.Listener { public final boolean enabled = false; - private OutputStreamWriter writer; + private BufferedWriter writer; public PacketSnifferPlugin (Bot bot) { if (!enabled) return; + final String name = String.format( + "packets-%s-%s.log", + bot.options.host, + bot.options.port + ); + + final Path path = Path.of(name); + try { - writer = new OutputStreamWriter( - new FileOutputStream( - String.format( - "packets-%s-%s.log", - bot.options.host, - bot.options.port - ) - ), - StandardCharsets.UTF_8 - ); + if (!Files.exists(path)) Files.createFile(path); + + writer = Files.newBufferedWriter(path, StandardOpenOption.APPEND); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java index a1a1dfc..f9fa8b3 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/song/SongLoaderRunnable.java @@ -6,18 +6,19 @@ import land.chipmunk.chayapak.chomens_bot.util.DownloadUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import java.io.File; +import java.io.IOException; import java.net.URL; -import java.nio.file.Path; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.util.stream.Stream; // Author: _ChipMC_ or hhhzzzsss? also i modified it to use runnable // because thread = bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! public class SongLoaderRunnable implements Runnable { public final String fileName; - private File songPath; + private Path songPath; private URL songUrl; public SongLoaderException exception; public Song song; @@ -39,24 +40,26 @@ public class SongLoaderRunnable implements Runnable { public SongLoaderRunnable(Path location, Bot bot) { this.bot = bot; isUrl = false; - songPath = location.toFile(); + songPath = location; - isFolder = songPath.isDirectory(); + isFolder = Files.isDirectory(songPath); fileName = location.getFileName().toString(); } public void run () { if (isFolder && !isUrl) { - final File[] files = songPath.listFiles(); + try (Stream files = Files.list(songPath)) { + if (files != null) { + files.forEach((file) -> { + songPath = file; + processFile(); + }); - if (files != null) { - for (File file : files) { - songPath = file; - processFile(); + showAddedToQueue(); } - - showAddedToQueue(); + } catch (IOException e) { + e.printStackTrace(); } } else processFile(); } @@ -69,8 +72,8 @@ public class SongLoaderRunnable implements Runnable { bytes = DownloadUtilities.DownloadToByteArray(songUrl, 10*1024*1024); name = Paths.get(songUrl.toURI().getPath()).getFileName().toString(); } else { - bytes = Files.readAllBytes(songPath.toPath()); - name = songPath.getName(); + bytes = Files.readAllBytes(songPath); + name = fileName; } } catch (Exception e) { exception = new SongLoaderException(Component.text(e.getMessage()));