Skip to content

Commit

Permalink
add method for ext saving default configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Sep 25, 2024
1 parent 8cfd7ff commit 870caab
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/main/java/think/rpgitems/RPGItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
import think.rpgitems.utils.nyaacore.NyaaCoreLoader;
import think.rpgitems.utils.prompt.PromptManager;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
Expand All @@ -51,6 +49,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static think.rpgitems.utils.cast.PluginUtils.stackTraceToString;

public final class RPGItems extends JavaPlugin implements PluginMessageListener {

@lombok.Getter
Expand Down Expand Up @@ -393,4 +393,35 @@ private void unregisterCommand(String name) {
command.setTabCompleter(null);
}
}

/**
* 保存资源到 RPGItems 文件夹
* @param ext 资源所在插件实例
* @param name 文件名
* @return 保存失败时返回 false,保存成功或文件已存在时返回 true
*/
public static boolean saveResource(JavaPlugin ext, String name) {
File file = new File(plugin.getDataFolder(), name);
if (!file.exists()) {
InputStream resource = ext.getResource(name);
return resource != null && save(file, resource);
}
return true;
}

private static boolean save(File file, InputStream resource) {
try (FileOutputStream os = new FileOutputStream(file)) {
try (resource) {
byte[] buffer = new byte[1024 * 10];
int len;
while ((len = resource.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
}
return true;
} catch (IOException e) {
plugin.getLogger().warning(stackTraceToString(e));
}
return false;
}
}

0 comments on commit 870caab

Please sign in to comment.