import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import me.zhenxin.zmusic.ZMusic;
import me.zhenxin.zmusic.config.Config;
import me.zhenxin.zmusic.utils.NetUtils;
import me.zhenxin.zmusic.utils.OtherUtils;

public class LoadConfig {
  public void load() {
    JsonObject configJson;
    File oldConfig = new File(ZMusic.dataFolder.getPath(), "config.yml");
    if (oldConfig.exists()) {
      File reToOld = new File(ZMusic.dataFolder.getPath(), "config.yml.old");
      if (!oldConfig.renameTo(reToOld)) {
        reToOld.delete();
        oldConfig.renameTo(reToOld);
      }
    }
    File config = new File(ZMusic.dataFolder.getPath(), "config.json");
    if (!config.exists()) {
      ZMusic.log.sendErrorMessage("");
      saveDefaultConfig();
    }
    String json = OtherUtils.readFileToString(config);
    try {
      configJson = (JsonObject)(new Gson()).fromJson(json, JsonObject.class);
    } catch (Exception e) {
      ZMusic.log.sendErrorMessage("");
      File configErrBak = new File(ZMusic.dataFolder.getPath(), System.currentTimeMillis() + "_error-config.json");
      config.renameTo(configErrBak);
      config.delete();
      saveDefaultConfig();
      load();
      return;
    }
    Config.version = configJson.get("version").getAsInt();
    if (Config.version != Config.latestVersion) {
      ZMusic.log.sendNormalMessage("-- ");
      config = new File(ZMusic.dataFolder.getPath(), "config.json");
      File configBak = new File(ZMusic.dataFolder.getPath(), "config.json.v" + Config.version + ".bak");
      ZMusic.log.sendNormalMessage("-- ");
      config.renameTo(configBak);
      ZMusic.log.sendNormalMessage("-- ");
      saveDefaultConfig();
      ZMusic.log.sendNormalMessage("-- ");
      load();
      return;
    }
    init(configJson);
  }

  private void init(JsonObject config) {
    Config.version = config.get("version").getAsInt();
    Config.debug = config.get("debug").getAsBoolean();
    Config.update = config.get("update").getAsBoolean();
    Config.prefix = config.get("prefix").getAsString().replaceAll("&", "");
    JsonObject api = config.get("api").getAsJsonObject();
    String neteaseApiRoot = api.get("netease").getAsString();
    if (!neteaseApiRoot.endsWith("/"))
      neteaseApiRoot = neteaseApiRoot + "/";
    Config.neteaseApiRoot = neteaseApiRoot;
    JsonObject account = config.get("account").getAsJsonObject();
    JsonObject netease = account.get("netease").getAsJsonObject();
    Config.neteaseFollow = netease.get("follow").getAsBoolean();
    JsonObject bilibili = account.get("bilibili").getAsJsonObject();
    Config.bilibiliQQ = bilibili.get("qq").getAsString();
    Config.bilibiliKey = bilibili.get("key").getAsString();
    if (!Config.bilibiliKey.equalsIgnoreCase("none"))
      ZMusic.runTask.runAsync(() -> {
            Gson gson = (new GsonBuilder()).create();
            String jsonText = NetUtils.getNetString("https://api.zhenxin.xyz/minecraft/plugins/ZMusic/bilibili/checkVIP.php", null, "qq=" + Config.bilibiliQQ + "&key=" + Config.bilibiliKey);
            JsonObject json = (JsonObject)gson.fromJson(jsonText, JsonObject.class);
            ZMusic.bilibiliIsVIP = json.get("isVIP").getAsBoolean();
          });
    JsonObject music = config.get("music").getAsJsonObject();
    Config.money = music.get("money").getAsInt();
    Config.cooldown = music.get("cooldown").getAsInt();
    JsonObject lyric = config.get("lyric").getAsJsonObject();
    Config.lyricEnable = lyric.get("enable").getAsBoolean();
    Config.showLyricTr = lyric.get("showLyricTr").getAsBoolean();
    Config.lyricColor = lyric.get("color").getAsString().replaceAll("&", "");
    if (Config.realSupportBossBar)
      Config.supportBossBar = lyric.get("bossBar").getAsBoolean();
    if (Config.realSupportActionBar)
      Config.supportActionBar = lyric.get("actionBar").getAsBoolean();
    if (Config.realSupportTitle)
      Config.supportTitle = lyric.get("subTitle").getAsBoolean();
    Config.supportChat = lyric.get("chatMessage").getAsBoolean();
    if (Config.realSupportHud) {
      JsonObject hud = lyric.get("hud").getAsJsonObject();
      Config.supportHud = hud.get("enable").getAsBoolean();
      Config.hudInfoX = hud.get("infoX").getAsInt();
      Config.hudInfoY = hud.get("infoY").getAsInt();
      Config.hudLyricX = hud.get("lyricX").getAsInt();
      Config.hudLyricY = hud.get("lyricY").getAsInt();
    }
  }

  private void saveDefaultConfig() {
    File config = new File(ZMusic.dataFolder.getPath(), "config.json");
    try {
      Files.copy(getClass().getResourceAsStream("/config.json"), config.toPath(), new java.nio.file.CopyOption[0]);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public void reload(Object sender) {
    load();
    ZMusic.message.sendNormalMessage("", sender);
  }
}
ZMusic 配置文件加载:加载、保存和重载

原文地址: https://www.cveoy.top/t/topic/qAoa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录