refactor file utils for reuse

This commit is contained in:
Blue Kelp 2019-08-01 21:16:17 -07:00
parent 6f5c9a535e
commit 168778026a
2 changed files with 40 additions and 15 deletions

View file

@ -25,8 +25,6 @@
package org.geysermc.connector; package org.geysermc.connector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.nukkitx.protocol.bedrock.BedrockPacketCodec; import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
import com.nukkitx.protocol.bedrock.BedrockServer; import com.nukkitx.protocol.bedrock.BedrockServer;
import com.nukkitx.protocol.bedrock.v361.Bedrock_v361; import com.nukkitx.protocol.bedrock.v361.Bedrock_v361;
@ -47,9 +45,11 @@ import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.plugin.GeyserPluginLoader; import org.geysermc.connector.plugin.GeyserPluginLoader;
import org.geysermc.connector.plugin.GeyserPluginManager; import org.geysermc.connector.plugin.GeyserPluginManager;
import org.geysermc.connector.thread.PingPassthroughThread; import org.geysermc.connector.thread.PingPassthroughThread;
import org.geysermc.connector.utils.FileUtils;
import org.geysermc.connector.utils.Toolbox; import org.geysermc.connector.utils.Toolbox;
import java.io.*; import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -112,19 +112,9 @@ public class GeyserConnector implements Connector {
logger.info("******************************************"); logger.info("******************************************");
try { try {
File configFile = new File("config.yml"); File configFile = FileUtils.fileOrCopiedFromResource("config.yml");
if (!configFile.exists()) {
FileOutputStream fos = new FileOutputStream(configFile);
InputStream is = GeyserConnector.class.getResourceAsStream("/config.yml");
int data;
while ((data = is.read()) != -1)
fos.write(data);
is.close();
fos.close();
}
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); config = FileUtils.loadConfig(configFile, GeyserConfiguration.class);
config = objectMapper.readValue(configFile, GeyserConfiguration.class);
} catch (IOException ex) { } catch (IOException ex) {
logger.severe("Failed to create config.yml! Make sure it's up to date and writable!"); logger.severe("Failed to create config.yml! Make sure it's up to date and writable!");
shutdown(); shutdown();

View file

@ -0,0 +1,35 @@
package org.geysermc.connector.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.geysermc.connector.GeyserConnector;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileUtils {
public static <T> T loadConfig(File src, Class<T> valueType) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
return objectMapper.readValue(src, valueType);
}
public static File fileOrCopiedFromResource(String name) throws IOException {
File file = new File(name);
if (!file.exists()) {
FileOutputStream fos = new FileOutputStream(file);
InputStream is = GeyserConnector.class.getResourceAsStream("/" + name); // resources need leading "/" prefix
int data;
while ((data = is.read()) != -1)
fos.write(data);
is.close();
fos.close();
}
return file;
}
}