forked from ChomeNS/chomens-bot-java
don't use File AT ALL
This commit is contained in:
parent
000c53a079
commit
d653403b0c
5 changed files with 52 additions and 39 deletions
|
@ -15,6 +15,8 @@ import java.io.*;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
@ -37,11 +39,12 @@ public class Main {
|
||||||
private static Configuration config;
|
private static Configuration config;
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
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 Constructor constructor = new Constructor(Configuration.class, new LoaderOptions());
|
||||||
final Yaml yaml = new Yaml(constructor);
|
final Yaml yaml = new Yaml(constructor);
|
||||||
|
|
||||||
if (!file.exists()) {
|
if (!Files.exists(configPath)) {
|
||||||
// creates config file from default-config.yml
|
// creates config file from default-config.yml
|
||||||
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default-config.yml");
|
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default-config.yml");
|
||||||
|
|
||||||
|
@ -56,14 +59,14 @@ public class Main {
|
||||||
String defaultConfig = stringBuilder.toString();
|
String defaultConfig = stringBuilder.toString();
|
||||||
|
|
||||||
// writes it
|
// writes it
|
||||||
BufferedWriter configWriter = new BufferedWriter(new FileWriter(file));
|
BufferedWriter configWriter = Files.newBufferedWriter(configPath);
|
||||||
configWriter.write(defaultConfig);
|
configWriter.write(defaultConfig);
|
||||||
configWriter.close();
|
configWriter.close();
|
||||||
|
|
||||||
LoggerUtilities.info("config.yml file not found, so the default one was created");
|
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));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
|
||||||
|
|
||||||
config = yaml.load(reader);
|
config = yaml.load(reader);
|
||||||
|
|
|
@ -16,6 +16,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -54,7 +55,7 @@ false
|
||||||
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
|
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
|
||||||
if (args.length < 1) return Component.text("Not enough arguments").color(NamedTextColor.RED);
|
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]) {
|
return switch (args[0]) {
|
||||||
case "play", "playurl", "playnbs", "playnbsurl" -> play(context, args);
|
case "play", "playurl", "playnbs", "playnbsurl" -> play(context, args);
|
||||||
case "stop" -> stop(context);
|
case "stop" -> stop(context);
|
||||||
|
@ -202,8 +203,7 @@ false
|
||||||
final List<Component> fullList = new ArrayList<>();
|
final List<Component> fullList = new ArrayList<>();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (String filename : filenames) {
|
for (String filename : filenames) {
|
||||||
final String pathString = path.toString();
|
final boolean isDirectory = Files.isDirectory(path);
|
||||||
final File file = new File(Paths.get(pathString, filename).toUri());
|
|
||||||
|
|
||||||
Path location;
|
Path location;
|
||||||
try {
|
try {
|
||||||
|
@ -219,7 +219,7 @@ false
|
||||||
ClickEvent.suggestCommand(
|
ClickEvent.suggestCommand(
|
||||||
prefix +
|
prefix +
|
||||||
name +
|
name +
|
||||||
(file.isFile() ? " play " : " list ") +
|
(isDirectory ? " list " : " play ") +
|
||||||
joinedPath
|
joinedPath
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,8 +14,9 @@ import land.chipmunk.chayapak.chomens_bot.util.MathUtilities;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -26,9 +27,13 @@ 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 final File SONG_DIR = new File("songs");
|
public static final Path SONG_DIR = Path.of("songs");
|
||||||
static {
|
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;
|
public Song currentSong;
|
||||||
|
|
|
@ -4,31 +4,33 @@ import com.github.steveice10.packetlib.Session;
|
||||||
import com.github.steveice10.packetlib.packet.Packet;
|
import com.github.steveice10.packetlib.packet.Packet;
|
||||||
import land.chipmunk.chayapak.chomens_bot.Bot;
|
import land.chipmunk.chayapak.chomens_bot.Bot;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
import java.nio.file.Files;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
|
||||||
// normally unused in the main instance of the bot
|
// normally unused in the main instance of the bot
|
||||||
public class PacketSnifferPlugin extends Bot.Listener {
|
public class PacketSnifferPlugin extends Bot.Listener {
|
||||||
public final boolean enabled = false;
|
public final boolean enabled = false;
|
||||||
|
|
||||||
private OutputStreamWriter writer;
|
private BufferedWriter writer;
|
||||||
|
|
||||||
public PacketSnifferPlugin (Bot bot) {
|
public PacketSnifferPlugin (Bot bot) {
|
||||||
if (!enabled) return;
|
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 {
|
try {
|
||||||
writer = new OutputStreamWriter(
|
if (!Files.exists(path)) Files.createFile(path);
|
||||||
new FileOutputStream(
|
|
||||||
String.format(
|
writer = Files.newBufferedWriter(path, StandardOpenOption.APPEND);
|
||||||
"packets-%s-%s.log",
|
|
||||||
bot.options.host,
|
|
||||||
bot.options.port
|
|
||||||
)
|
|
||||||
),
|
|
||||||
StandardCharsets.UTF_8
|
|
||||||
);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,18 +6,19 @@ import land.chipmunk.chayapak.chomens_bot.util.DownloadUtilities;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
// 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 final String fileName;
|
public final String fileName;
|
||||||
|
|
||||||
private File songPath;
|
private Path songPath;
|
||||||
private URL songUrl;
|
private URL songUrl;
|
||||||
public SongLoaderException exception;
|
public SongLoaderException exception;
|
||||||
public Song song;
|
public Song song;
|
||||||
|
@ -39,24 +40,26 @@ public class SongLoaderRunnable implements Runnable {
|
||||||
public SongLoaderRunnable(Path location, Bot bot) {
|
public SongLoaderRunnable(Path location, Bot bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
isUrl = false;
|
isUrl = false;
|
||||||
songPath = location.toFile();
|
songPath = location;
|
||||||
|
|
||||||
isFolder = songPath.isDirectory();
|
isFolder = Files.isDirectory(songPath);
|
||||||
|
|
||||||
fileName = location.getFileName().toString();
|
fileName = location.getFileName().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run () {
|
public void run () {
|
||||||
if (isFolder && !isUrl) {
|
if (isFolder && !isUrl) {
|
||||||
final File[] files = songPath.listFiles();
|
try (Stream<Path> files = Files.list(songPath)) {
|
||||||
|
if (files != null) {
|
||||||
|
files.forEach((file) -> {
|
||||||
|
songPath = file;
|
||||||
|
processFile();
|
||||||
|
});
|
||||||
|
|
||||||
if (files != null) {
|
showAddedToQueue();
|
||||||
for (File file : files) {
|
|
||||||
songPath = file;
|
|
||||||
processFile();
|
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
showAddedToQueue();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
} else processFile();
|
} else processFile();
|
||||||
}
|
}
|
||||||
|
@ -69,8 +72,8 @@ public class SongLoaderRunnable implements Runnable {
|
||||||
bytes = DownloadUtilities.DownloadToByteArray(songUrl, 10*1024*1024);
|
bytes = DownloadUtilities.DownloadToByteArray(songUrl, 10*1024*1024);
|
||||||
name = Paths.get(songUrl.toURI().getPath()).getFileName().toString();
|
name = Paths.get(songUrl.toURI().getPath()).getFileName().toString();
|
||||||
} else {
|
} else {
|
||||||
bytes = Files.readAllBytes(songPath.toPath());
|
bytes = Files.readAllBytes(songPath);
|
||||||
name = songPath.getName();
|
name = fileName;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
exception = new SongLoaderException(Component.text(e.getMessage()));
|
exception = new SongLoaderException(Component.text(e.getMessage()));
|
||||||
|
|
Loading…
Reference in a new issue