From 168778026a091c42891cef2b2eddb4ea1d509c1f Mon Sep 17 00:00:00 2001 From: Blue Kelp Date: Thu, 1 Aug 2019 21:16:17 -0700 Subject: [PATCH] refactor file utils for reuse --- .../geysermc/connector/GeyserConnector.java | 20 +++-------- .../geysermc/connector/utils/FileUtils.java | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/utils/FileUtils.java diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 137a6238f..639f53709 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -25,8 +25,6 @@ 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.BedrockServer; 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.GeyserPluginManager; import org.geysermc.connector.thread.PingPassthroughThread; +import org.geysermc.connector.utils.FileUtils; import org.geysermc.connector.utils.Toolbox; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.net.InetSocketAddress; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -112,19 +112,9 @@ public class GeyserConnector implements Connector { logger.info("******************************************"); try { - File configFile = new File("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(); - } + File configFile = FileUtils.fileOrCopiedFromResource("config.yml"); - ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); - config = objectMapper.readValue(configFile, GeyserConfiguration.class); + config = FileUtils.loadConfig(configFile, GeyserConfiguration.class); } catch (IOException ex) { logger.severe("Failed to create config.yml! Make sure it's up to date and writable!"); shutdown(); diff --git a/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java new file mode 100644 index 000000000..435bd603a --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java @@ -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 loadConfig(File src, Class 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; + } + + +}