132 lines
4.9 KiB
Java
132 lines
4.9 KiB
Java
package me.skymc.taboolib.cloud;
|
|
|
|
import com.google.common.collect.Maps;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import com.ilummc.tlib.resources.TLocale;
|
|
import me.skymc.taboolib.TabooLib;
|
|
import me.skymc.taboolib.cloud.expansion.Expansion;
|
|
import me.skymc.taboolib.cloud.expansion.ExpansionType;
|
|
import me.skymc.taboolib.common.function.TFunction;
|
|
import me.skymc.taboolib.fileutils.FileUtils;
|
|
import me.skymc.taboolib.plugin.PluginUtils;
|
|
import org.bukkit.Bukkit;
|
|
|
|
import java.io.File;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @Author sky
|
|
* @Since 2018-09-30 17:30
|
|
*/
|
|
@TFunction(enable = "init")
|
|
public class TCloudLoader {
|
|
|
|
private static String url = "https://gitee.com/bkm016/TabooLibCloud/raw/master/cloud.json";
|
|
private static String latestJsonOrigin;
|
|
private static JsonObject latestJsonObject;
|
|
private static Map<String, Expansion> expansionPlugins = Maps.newTreeMap();
|
|
private static Map<String, Expansion> expansionInternal = Maps.newTreeMap();
|
|
private static File expansionInternalFolder;
|
|
|
|
static void init() {
|
|
createFolder();
|
|
refresh();
|
|
}
|
|
|
|
public static void createFolder() {
|
|
if (!(expansionInternalFolder = new File(TabooLib.instance().getDataFolder(), "TCloud")).exists()) {
|
|
expansionInternalFolder.mkdirs();
|
|
}
|
|
}
|
|
|
|
public static void refresh() {
|
|
Bukkit.getScheduler().runTaskAsynchronously(TabooLib.instance(), () -> {
|
|
long time = System.currentTimeMillis();
|
|
latestJsonOrigin = FileUtils.getStringFromURL(url, 1024);
|
|
if (latestJsonOrigin == null) {
|
|
if (!TabooLib.isSilent()) {
|
|
TLocale.Logger.error("TCLOUD.LIST-CONNECT-FAILED");
|
|
}
|
|
return;
|
|
}
|
|
try {
|
|
latestJsonObject = new JsonParser().parse(latestJsonOrigin).getAsJsonObject();
|
|
} catch (Exception e) {
|
|
if (!TabooLib.isSilent()) {
|
|
TLocale.Logger.info("TCLOUD.LIST-PARSE-FAILED", e.getMessage());
|
|
}
|
|
return;
|
|
}
|
|
if (latestJsonObject.has("plugins")) {
|
|
for (Map.Entry<String, JsonElement> pluginEntry : latestJsonObject.getAsJsonObject("plugins").entrySet()) {
|
|
try {
|
|
expansionPlugins.put(pluginEntry.getKey(), Expansion.unSerialize(ExpansionType.PLUGIN, pluginEntry.getKey(), pluginEntry.getValue().getAsJsonObject()));
|
|
} catch (Exception e) {
|
|
if (!TabooLib.isSilent()) {
|
|
TLocale.Logger.info("TCLOUD.LIST-LOAD-FAILED", pluginEntry.getKey(), e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (latestJsonObject.has("internal")) {
|
|
for (Map.Entry<String, JsonElement> pluginEntry : latestJsonObject.getAsJsonObject("internal").entrySet()) {
|
|
try {
|
|
expansionInternal.put(pluginEntry.getKey(), Expansion.unSerialize(ExpansionType.INTERNAL, pluginEntry.getKey(), pluginEntry.getValue().getAsJsonObject()));
|
|
} catch (Exception e) {
|
|
if (!TabooLib.isSilent()) {
|
|
TLocale.Logger.info("TCLOUD.LIST-LOAD-FAILED", pluginEntry.getKey(), e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!TabooLib.isSilent()) {
|
|
TLocale.Logger.info("TCLOUD.LIST-LOAD-SUCCESS", String.valueOf(System.currentTimeMillis() - time));
|
|
}
|
|
});
|
|
}
|
|
|
|
public static boolean isConnected() {
|
|
return latestJsonOrigin != null;
|
|
}
|
|
|
|
public static String getLatestJsonOrigin() {
|
|
return latestJsonOrigin;
|
|
}
|
|
|
|
public static JsonObject getLatestJsonObject() {
|
|
return latestJsonObject;
|
|
}
|
|
|
|
public static Map<String, Expansion> getExpansionPlugins() {
|
|
return expansionPlugins;
|
|
}
|
|
|
|
public static Map<String, Expansion> getExpansionInternal() {
|
|
return expansionInternal;
|
|
}
|
|
|
|
public static File getExpansionInternalFolder() {
|
|
return expansionInternalFolder;
|
|
}
|
|
|
|
public static boolean isExpansionExists(Expansion expansion) {
|
|
return expansion.getType() == ExpansionType.PLUGIN && PluginUtils.isPluginExists(expansion.getName());
|
|
}
|
|
|
|
public static Expansion getExpansion(String name) {
|
|
for (Map.Entry<String, Expansion> stringExpansionEntry : expansionPlugins.entrySet()) {
|
|
if (stringExpansionEntry.getKey().equalsIgnoreCase(name)) {
|
|
return stringExpansionEntry.getValue();
|
|
}
|
|
}
|
|
for (Map.Entry<String, Expansion> stringExpansionEntry : expansionInternal.entrySet()) {
|
|
if (stringExpansionEntry.getKey().equalsIgnoreCase(name)) {
|
|
return stringExpansionEntry.getValue();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|