Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
功能增加
Browse files Browse the repository at this point in the history
  • Loading branch information
slinar committed Nov 21, 2018
1 parent de056fb commit f74568d
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions src/me/commandBlock/ConfigFile.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package me.commandBlock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.codec.digest.DigestUtils;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

Expand All @@ -12,6 +18,9 @@ public class ConfigFile {
private static CommandBlock ins = CommandBlock.getIns();
private static File file;
private static FileConfiguration config;
private static String md5;
private static int oldInterval = 0;
public static ScheduledFuture<?> futureUpdateConfig;
private static Runnable task = new Runnable() {
public void run() {
updateConfig();
Expand All @@ -22,23 +31,40 @@ public static void loadConfig() {
if (!ins.getDataFolder().exists()) {
ins.getDataFolder().mkdirs();
}
file = new File(CommandBlock.getIns().getDataFolder(), "config.yml");
file = new File(ins.getDataFolder(), "config.yml");
if (!file.exists()) {
ins.getLogger().info("配置文件不存在,载入默认配置文件!");
ins.saveDefaultConfig();
}
config = YamlConfiguration.loadConfiguration(file);
md5 = getConfigMd5();
oldInterval = getInterval();
updateConfigTask();
ins.getLogger().info("配置文件已载入!");
}

public static void updateConfig() {
config = YamlConfiguration.loadConfiguration(file);
private static void updateConfig() {
String var = getConfigMd5();
if (!var.equals(md5)) {
config = YamlConfiguration.loadConfiguration(file);
if (getInterval() != oldInterval) {
oldInterval = getInterval();
updateConfigTask();
}
md5 = var;
ins.getLogger().info("配置文件已更改!");
}
}

public static Runnable getTask() {
return task;
/**异步自动更新配置文件*/
private static void updateConfigTask() {
if(!(futureUpdateConfig == null)) {
futureUpdateConfig.cancel(true);
ins.getLogger().info("重新设置自动更新配置文件的间隔为:" + oldInterval + "秒!");
}
futureUpdateConfig = CommandBlock.ses.scheduleAtFixedRate(task, oldInterval, oldInterval, TimeUnit.SECONDS);
}

public static List<String> getCommandList() {
return config.getStringList("list");
}
Expand All @@ -47,4 +73,24 @@ public static List<String> getPlayerWhiteList() {
return config.getStringList("playerWhiteList");
}

public static int getInterval() {
int interval = config.getInt("configUpdateInterval");
if (interval < 1) interval = 1;
if (interval > 60) interval = 60;
return interval;
}

private static String getConfigMd5() {
String var = "";
try {
var = DigestUtils.md5Hex(new FileInputStream(file));
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return var;
}
}

0 comments on commit f74568d

Please sign in to comment.