1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-24 02:08:48 +00:00

feat: 添加保存文件替换模式

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-02-07 16:02:43 +08:00
parent f7d2f1a9f3
commit 6cdc5a41d3

View File

@ -6,6 +6,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -128,10 +129,22 @@ public class P {
* 目录 * 目录
*/ */
public static void saveFile(final String... dirs) { public static void saveFile(final String... dirs) {
saveFile(false, dirs);
}
/**
* 保存文件
*
* @param replace
* 是否替换
* @param dirs
* 目录
*/
public static void saveFile(boolean replace, final String... dirs) {
URL url = instance.getClass().getClassLoader().getResource("plugin.yml"); URL url = instance.getClass().getClassLoader().getResource("plugin.yml");
if (url == null) { return; } if (url == null) { return; }
String upath = url.getFile().substring(url.getFile().indexOf("/") + 1); String upath = url.getFile().substring(url.getFile().indexOf("/") + 1);
String jarPath = upath.substring(0, upath.indexOf('!')); String jarPath = URLDecoder.decode(upath.substring(0, upath.indexOf('!')));
if (!new File(jarPath).exists()) { if (!new File(jarPath).exists()) {
jarPath = "/" + jarPath; jarPath = "/" + jarPath;
} }
@ -139,9 +152,16 @@ public class P {
jar.stream().forEach(je -> { jar.stream().forEach(je -> {
if (!je.isDirectory()) { if (!je.isDirectory()) {
for (final String dir : dirs) { for (final String dir : dirs) {
if (je.getName().startsWith(dir) && !new File(getDataFolder(), je.getName()).exists()) { if (je.getName().startsWith(dir)) {
if (!replace) {
// 不替换 并且文件不存在
if (!new File(getDataFolder(), je.getName()).exists()) {
instance.saveResource(je.getName(), false); instance.saveResource(je.getName(), false);
} }
} else {
instance.saveResource(je.getName(), true);
}
}
} }
} }
}); });