don't use File AT ALL

This commit is contained in:
Chayapak 2023-07-23 09:47:30 +07:00
parent 000c53a079
commit d653403b0c
5 changed files with 52 additions and 39 deletions

View file

@ -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);

View file

@ -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<Component> 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
)
)

View file

@ -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;

View file

@ -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;
try {
writer = new OutputStreamWriter(
new FileOutputStream(
String.format(
final String name = String.format(
"packets-%s-%s.log",
bot.options.host,
bot.options.port
)
),
StandardCharsets.UTF_8
);
final Path path = Path.of(name);
try {
if (!Files.exists(path)) Files.createFile(path);
writer = Files.newBufferedWriter(path, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}

View file

@ -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,25 +40,27 @@ 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<Path> files = Files.list(songPath)) {
if (files != null) {
for (File file : files) {
files.forEach((file) -> {
songPath = file;
processFile();
}
});
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()));