Skip to content

Commit

Permalink
加强ConfigUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
0XPYEX0 committed Jan 20, 2024
1 parent 3fc011f commit 94263dc
Show file tree
Hide file tree
Showing 4 changed files with 5,837 additions and 5,828 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package me.xpyex.plugin.xplib.bukkit.api;

public interface TryFunction<T, R> {
public R apply(T t) throws Throwable;
R apply(T t) throws Throwable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.HashMap;
import me.xpyex.plugin.xplib.bukkit.util.RootUtil;
import me.xpyex.plugin.xplib.bukkit.util.files.FileUtil;
import me.xpyex.plugin.xplib.bukkit.util.value.ValueUtil;
import org.bukkit.plugin.Plugin;

public class ConfigUtil extends RootUtil {
Expand All @@ -31,7 +30,7 @@ public static <T> T getConfig(Plugin plugin, Class<T> type) {

@SuppressWarnings("unchecked")
public static <T> T getConfig(Plugin plugin, String path, Class<T> type) {
String key = plugin.getName() + "/" + path;
String key = plugin.getName() + "/" + path + "(" + type.getSimpleName() + ")";
if (!CONFIGS.containsKey(key)) {
try {
CONFIGS.put(key, GsonUtil.parseJson(FileUtil.readFile(new File(plugin.getDataFolder(), path + ".json")), type));
Expand Down Expand Up @@ -63,17 +62,27 @@ public static void reload(Plugin plugin) {
}

public static void saveConfig(Plugin plugin, String path, Object obj, boolean replaced) {
saveConfig(plugin, path, obj, replaced, true);
//
}

public static void saveConfig(Plugin plugin, String path, Object obj, boolean replaced, boolean cache) {
File file = new File(plugin.getDataFolder(), path + ".json");
if (!replaced && file.exists()) return;
try {
FileUtil.writeFile(file, GsonUtil.parseStr(obj));
CONFIGS.put(plugin.getName() + "/" + path, obj);
if (cache) CONFIGS.put(plugin.getName() + "/" + path + "(" + obj.getClass().getSimpleName() + ")", obj);
} catch (Throwable e) {
e.printStackTrace();
}
}

public static void saveConfig(Plugin plugin, String path, boolean replaced) {
ValueUtil.ifPresent(CONFIGS.get(plugin.getName() + "/" + path), config -> saveConfig(plugin, path, config, replaced));
for (String key : CONFIGS.keySet()) {
if (key.startsWith(plugin.getName() + "/" + path + "(")) {
saveConfig(plugin, path, getConfig(plugin, path), true);
return;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ public <T, R> XPTask thenUsingLastResultWithResult(TaskType type, Class<T> lastR
return this;
}

public enum TaskType {
SYNC,
ASYNC
}

@Override
public XPTask clone() {
try {
Expand All @@ -159,4 +154,9 @@ public XPTask clone() {
return cloned;
}
}

public enum TaskType {
SYNC,
ASYNC
}
}
Loading

0 comments on commit 94263dc

Please sign in to comment.