Geyser/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java

40 lines
1.2 KiB
Java
Raw Normal View History

2019-08-02 04:16:17 +00:00
package org.geysermc.connector.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.geysermc.connector.GeyserConnector;
2019-08-06 01:59:54 +00:00
import java.io.*;
import java.util.function.Function;
2019-08-02 04:16:17 +00:00
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);
}
2019-08-06 01:59:54 +00:00
public static File fileOrCopiedFromResource(String name, Function<String, String> s) throws IOException {
2019-08-02 04:16:17 +00:00
File file = new File(name);
if (!file.exists()) {
FileOutputStream fos = new FileOutputStream(file);
2019-08-06 01:59:54 +00:00
InputStream input = GeyserConnector.class.getResourceAsStream("/" + name); // resources need leading "/" prefix
2019-08-02 04:16:17 +00:00
2019-08-06 01:59:54 +00:00
byte[] bytes = new byte[input.available()];
input.read(bytes);
for(char c : s.apply(new String(bytes)).toCharArray()) {
fos.write(c);
}
fos.flush();
input.close();
2019-08-02 04:16:17 +00:00
fos.close();
}
return file;
}
}