{"title":"import com.google.gson.Gson; \nimport com.google.gson.GsonBuilder; \nimport com.google.gson.JsonObject; \nimport java.io.File; \nimport java.io.IOException; \nimport java.nio.file.Files; \nimport me.zhenxin.zmusic.ZMusic; \nimport me.zhenxin.zmusic.config.Config; \nimport me.zhenxin.zmusic.config.LoadConfig; \nimport me.zhenxin.zmusic.utils.NetUtils; \nimport me.zhenxin.zmusic.utils.OtherUtils; \n\npublic class LoadConfig { \n public void load() { \n JsonObject configJson; \n File oldConfig = new File(ZMusic.dataFolder.getPath(), "config.yml"); \n if (oldConfig.exists()) { \n File reToOld = new File(ZMusic.dataFolder.getPath(), "config.yml.old"); \n if (!oldConfig.renameTo(reToOld)) { \n reToOld.delete(); \n oldConfig.renameTo(reToOld); \n } \n } \n File config = new File(ZMusic.dataFolder.getPath(), "config.json"); \n if (!config.exists()) { \n ZMusic.log.sendErrorMessage("); \n saveDefaultConfig(); \n } \n String json = OtherUtils.readFileToString(config); \n try { \n configJson = (JsonObject)(new Gson()).fromJson(json, JsonObject.class); \n } catch (Exception e) { \n ZMusic.log.sendErrorMessage("); \n File configErrBak = new File(ZMusic.dataFolder.getPath(), System.currentTimeMillis() + "_error-config.json"); \n config.renameTo(configErrBak); \n config.delete(); \n saveDefaultConfig(); \n load(); \n return; \n } \n Config.version = configJson.get("version").getAsInt(); \n if (Config.version != Config.latestVersion) { \n ZMusic.log.sendNormalMessage("-- "); \n config = new File(ZMusic.dataFolder.getPath(), "config.json"); \n File configBak = new File(ZMusic.dataFolder.getPath(), "config.json.v" + Config.version + ".bak"); \n ZMusic.log.sendNormalMessage("-- "); \n config.renameTo(configBak); \n ZMusic.log.sendNormalMessage("-- "); \n saveDefaultConfig(); \n ZMusic.log.sendNormalMessage("-- "); \n load(); \n return; \n } \n init(configJson); \n } \n \n private void init(JsonObject config) { \n Config.version = config.get("version").getAsInt(); \n Config.debug = config.get("debug").getAsBoolean(); \n Config.update = config.get("update").getAsBoolean(); \n Config.prefix = config.get("prefix").getAsString().replaceAll("&", "); \n JsonObject api = config.get("api").getAsJsonObject(); \n String neteaseApiRoot = api.get("netease").getAsString(); \n if (!neteaseApiRoot.endsWith("/")) \n neteaseApiRoot = neteaseApiRoot + "/"; \n Config.neteaseApiRoot = neteaseApiRoot; \n JsonObject account = config.get("account").getAsJsonObject(); \n JsonObject netease = account.get("netease").getAsJsonObject(); \n Config.neteaseFollow = netease.get("follow").getAsBoolean(); \n JsonObject bilibili = account.get("bilibili").getAsJsonObject(); \n Config.bilibiliQQ = bilibili.get("qq").getAsString(); \n Config.bilibiliKey = bilibili.get("key").getAsString(); \n if (!Config.bilibiliKey.equalsIgnoreCase("none")) \n ZMusic.runTask.runAsync(() -> { \n Gson gson = (new GsonBuilder()).create(); \n String jsonText = NetUtils.getNetString("https://api.zhenxin.xyz/minecraft/plugins/ZMusic/bilibili/checkVIP.php", null, "qq=" + Config.bilibiliQQ + "&key=" + Config.bilibiliKey); \n JsonObject json = (JsonObject)gson.fromJson(jsonText, JsonObject.class); \n ZMusic.bilibiliIsVIP = json.get("isVIP").getAsBoolean(); \n }); \n JsonObject music = config.get("music").getAsJsonObject(); \n Config.money = music.get("money").getAsInt(); \n Config.cooldown = music.get("cooldown").getAsInt(); \n JsonObject lyric = config.get("lyric").getAsJsonObject(); \n Config.lyricEnable = lyric.get("enable").getAsBoolean(); \n Config.showLyricTr = lyric.get("showLyricTr").getAsBoolean(); \n Config.lyricColor = lyric.get("color").getAsString().replaceAll("&", "); \n if (Config.realSupportBossBar) \n Config.supportBossBar = lyric.get("bossBar").getAsBoolean(); \n if (Config.realSupportActionBar) \n Config.supportActionBar = lyric.get("actionBar").getAsBoolean(); \n if (Config.realSupportTitle) \n Config.supportTitle = lyric.get("subTitle").getAsBoolean(); \n Config.supportChat = lyric.get("chatMessage").getAsBoolean(); \n if (Config.realSupportHud) { \n JsonObject hud = lyric.get("hud").getAsJsonObject(); \n Config.supportHud = hud.get("enable").getAsBoolean(); \n Config.hudInfoX = hud.get("infoX").getAsInt(); \n Config.hudInfoY = hud.get("infoY").getAsInt(); \n Config.hudLyricX = hud.get("lyricX").getAsInt(); \n Config.hudLyricY = hud.get("lyricY").getAsInt(); \n } \n } \n \n private void saveDefaultConfig() { \n File config = new File(ZMusic.dataFolder.getPath(), "config.json"); \n try { \n Files.copy(getClass().getResourceAsStream("/config.json"), config.toPath(), new java.nio.file.CopyOption[0]); \n } catch (IOException e) { \n e.printStackTrace(); \n } \n } \n \n public void reload(Object sender) { \n load(); \n ZMusic.message.sendNormalMessage(", sender); \n } \n}

ZMusic 配置文件加载与重载 - Java 代码示例

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

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