From aa913c36ec715edf76ddf87054d81540d569bbe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Mon, 1 Oct 2018 12:59:48 +0800 Subject: [PATCH] =?UTF-8?q?+=20=E5=8D=8A=E6=88=90=E5=93=81=20tcloud?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../skymc/taboolib/cloud/TCloudCommand.java | 151 ++++++++++++++++++ .../me/skymc/taboolib/cloud/TCloudLoader.java | 115 +++++++++++++ .../taboolib/cloud/expansion/Expansion.java | 97 +++++++++++ .../cloud/expansion/ExpansionType.java | 10 ++ .../commands/internal/plugin/TLibLocale.java | 29 ++++ .../me/skymc/taboolib/update/UpdateTask.java | 2 +- src/main/resources/lang/zh_CN.yml | 48 +++++- 8 files changed, 451 insertions(+), 3 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/cloud/TCloudCommand.java create mode 100644 src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java create mode 100644 src/main/java/me/skymc/taboolib/cloud/expansion/Expansion.java create mode 100644 src/main/java/me/skymc/taboolib/cloud/expansion/ExpansionType.java create mode 100644 src/main/java/me/skymc/taboolib/commands/internal/plugin/TLibLocale.java diff --git a/pom.xml b/pom.xml index f9b19a9..ad2417d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.skymc TabooLib - 4.325 + 4.4 UTF-8 diff --git a/src/main/java/me/skymc/taboolib/cloud/TCloudCommand.java b/src/main/java/me/skymc/taboolib/cloud/TCloudCommand.java new file mode 100644 index 0000000..695d65b --- /dev/null +++ b/src/main/java/me/skymc/taboolib/cloud/TCloudCommand.java @@ -0,0 +1,151 @@ +package me.skymc.taboolib.cloud; + +import com.ilummc.tlib.resources.TLocale; +import me.skymc.taboolib.TabooLib; +import me.skymc.taboolib.cloud.expansion.Expansion; +import me.skymc.taboolib.commands.internal.BaseMainCommand; +import me.skymc.taboolib.commands.internal.BaseSubCommand; +import me.skymc.taboolib.commands.internal.TCommand; +import me.skymc.taboolib.commands.internal.plugin.TLibLocale; +import me.skymc.taboolib.commands.internal.type.CommandArgument; +import me.skymc.taboolib.commands.internal.type.CommandRegister; +import me.skymc.taboolib.fileutils.FileUtils; +import me.skymc.taboolib.plugin.PluginUtils; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.io.File; +import java.util.Arrays; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2018-09-30 19:17 + */ +@TCommand( + name = "taboolibcloud", + permission = "taboolib.admin", + aliases = "tcloud" +) +public class TCloudCommand extends BaseMainCommand { + + @CommandRegister + BaseSubCommand refresh = new BaseSubCommand() { + @Override + public String getLabel() { + return "refresh"; + } + + @Override + public String getDescription() { + return TLibLocale.description("TCLOUD", "REFRESH"); + } + + @Override + public CommandArgument[] getArguments() { + return new CommandArgument[0]; + } + + @Override + public void onCommand(CommandSender sender, Command command, String label, String[] args) { + TLocale.sendTo(sender, "COMMANDS.TCLOUD.REFRESH.SUCCESS"); + TCloudLoader.refresh(); + } + }; + @CommandRegister(priority = 1) + BaseSubCommand status = new BaseSubCommand() { + @Override + public String getLabel() { + return "status"; + } + + @Override + public String getDescription() { + return TLibLocale.description("TCLOUD", "STATUS"); + } + + @Override + public CommandArgument[] getArguments() { + return new CommandArgument[0]; + } + + @Override + public void onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!TCloudLoader.isConnected()) { + TLocale.sendTo(sender, "COMMANDS.TCLOUD.STATUS.CONNECT-FAILED"); + } else { + TLocale.sendTo(sender, "COMMANDS.TCLOUD.STATUS.STATUS", String.valueOf(TCloudLoader.getExpansionInternal().size() + TCloudLoader.getExpansionPlugins().size()), String.valueOf(TCloudLoader.getExpansionInternal().size()), String.valueOf(TCloudLoader.getExpansionPlugins().size())); + } + } + }; + @CommandRegister(priority = 2) + BaseSubCommand info = new BaseSubCommand() { + @Override + public String getLabel() { + return "info"; + } + + @Override + public String getDescription() { + return TLibLocale.description("TCLOUD", "INFO"); + } + + @Override + public CommandArgument[] getArguments() { + return TLibLocale.arguments("TCLOUD", "INFO", 1); + } + + @Override + public void onCommand(CommandSender sender, Command command, String label, String[] args) { + Expansion expansion = TCloudLoader.getExpansion(args[0]); + if (expansion == null) { + TLocale.sendTo(sender, "COMMANDS.TCLOUD.INFO.EXPANSION-NOT-FOUND", args[0]); + } else { + String builder = Arrays.stream(expansion.getAuthor()).map(author -> author + ", ").collect(Collectors.joining()); + TLocale.sendTo(sender, "COMMANDS.TCLOUD.INFO.EXPANSION-INFO", expansion.getName(), builder.substring(0, builder.length() - 2), expansion.getVersion(), expansion.getDescription(), expansion.getLastUpdate(), expansion.getLastUpdateNote()); + TLocale.sendTo(sender, "COMMANDS.TCLOUD.INFO.EXPANSION-INFO-DETAIL.0"); + Arrays.stream(expansion.getDetail()).forEach(detail -> TLocale.sendTo(sender, "COMMANDS.TCLOUD.INFO.EXPANSION-INFO-DETAIL.1", detail)); + } + } + }; + @CommandRegister(priority = 3) + BaseSubCommand download = new BaseSubCommand() { + @Override + public String getLabel() { + return "download"; + } + + @Override + public String getDescription() { + return TLibLocale.description("TCLOUD", "DOWNLOAD"); + } + + @Override + public CommandArgument[] getArguments() { + return TLibLocale.arguments("TCLOUD", "DOWNLOAD", 1); + } + + @Override + public void onCommand(CommandSender sender, Command command, String label, String[] args) { + Expansion expansion = TCloudLoader.getExpansion(args[0]); + if (expansion == null) { + TLocale.sendTo(sender, "COMMANDS.TCLOUD.DOWNLOAD.EXPANSION-NOT-FOUND", args[0]); + } else if (TCloudLoader.isExpansionExists(expansion)) { + TLocale.sendTo(sender, "COMMANDS.TCLOUD.DOWNLOAD.EXPANSION-EXISTS", args[0]); + } else { + Bukkit.getScheduler().runTaskAsynchronously(TabooLib.instance(), () -> { + TLocale.sendTo(sender, "COMMANDS.TCLOUD.DOWNLOAD.DOWNLOAD-START", args[0], expansion.getFile().getPath(), expansion.getLink()); + FileUtils.download(expansion.getLink(), expansion.getFile()); + TLocale.sendTo(sender, "COMMANDS.TCLOUD.DOWNLOAD.DOWNLOAD-SUCCESS", args[0]); + Bukkit.getScheduler().runTask(TabooLib.instance(), () -> PluginUtils.load(expansion.getName())); + }); + } + } + }; + + @Override + public String getCommandTitle() { + return TLocale.asString("COMMANDS.TCLOUD.COMMAND-TITLE"); + } +} diff --git a/src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java b/src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java new file mode 100644 index 0000000..1aa1671 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/cloud/TCloudLoader.java @@ -0,0 +1,115 @@ +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 +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 expansionPlugins = Maps.newHashMap(); + private static Map expansionInternal = Maps.newHashMap(); + private static File expansionInternalFolder; + + public void onEnable() { + 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) { + TLocale.Logger.error("TCLOUD.LIST-CONNECT-FAILED"); + return; + } + TLocale.Logger.info("TCLOUD.LIST-CONNECT-SUCCESS", String.valueOf(System.currentTimeMillis() - time)); + time = System.currentTimeMillis(); + try { + latestJsonObject = new JsonParser().parse(latestJsonOrigin).getAsJsonObject(); + } catch (Exception e) { + TLocale.Logger.info("TCLOUD.LIST-PARSE-FAILED", e.getMessage()); + return; + } + TLocale.Logger.info("TCLOUD.LIST-PARSE-SUCCESS", String.valueOf(System.currentTimeMillis() - time)); + time = System.currentTimeMillis(); + if (latestJsonObject.has("plugins")) { + for (Map.Entry pluginEntry : latestJsonObject.getAsJsonObject("plugins").entrySet()) { + try { + expansionPlugins.put(pluginEntry.getKey(), Expansion.unSerialize(ExpansionType.PLUGIN, pluginEntry.getKey(), pluginEntry.getValue().getAsJsonObject())); + } catch (Exception e) { + TLocale.Logger.info("TCLOUD.LIST-LOAD-FAILED", pluginEntry.getKey(), e.getMessage()); + } + } + } + if (latestJsonObject.has("internal")) { + for (Map.Entry pluginEntry : latestJsonObject.getAsJsonObject("internal").entrySet()) { + try { + expansionInternal.put(pluginEntry.getKey(), Expansion.unSerialize(ExpansionType.INTERNAL, pluginEntry.getKey(), pluginEntry.getValue().getAsJsonObject())); + } catch (Exception e) { + TLocale.Logger.info("TCLOUD.LIST-LOAD-FAILED", pluginEntry.getKey(), e.getMessage()); + } + } + } + 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 getExpansionPlugins() { + return expansionPlugins; + } + + public static Map getExpansionInternal() { + return expansionInternal; + } + + public static File getExpansionInternalFolder() { + return expansionInternalFolder; + } + + public static Expansion getExpansion(String name) { + return expansionPlugins.getOrDefault(name, expansionInternal.get(name)); + } + + public static boolean isExpansionExists(Expansion expansion) { + return expansion.getType() == ExpansionType.PLUGIN && PluginUtils.isPluginExists(expansion.getName()); + } +} diff --git a/src/main/java/me/skymc/taboolib/cloud/expansion/Expansion.java b/src/main/java/me/skymc/taboolib/cloud/expansion/Expansion.java new file mode 100644 index 0000000..ff99b76 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/cloud/expansion/Expansion.java @@ -0,0 +1,97 @@ +package me.skymc.taboolib.cloud.expansion; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.ilummc.eagletdl.EagletTask; +import com.ilummc.eagletdl.ProgressEvent; +import com.ilummc.tlib.resources.TLocale; +import me.skymc.taboolib.cloud.TCloudLoader; +import me.skymc.taboolib.fileutils.FileUtils; +import me.skymc.taboolib.string.ArrayUtils; + +import java.io.File; +import java.util.stream.IntStream; + +/** + * @Author sky + * @Since 2018-09-30 16:39 + */ +public class Expansion { + + private final String name; + private final String[] author; + private final String description; + private final String[] detail; + private final String version; + private final String lastUpdate; + private final String lastUpdateNote; + private final String link; + private final ExpansionType type; + + public Expansion(String name, String[] author, String description, String[] detail, String version, String lastUpdate, String lastUpdateNote, String link, ExpansionType type) { + this.name = name; + this.author = author; + this.description = description; + this.detail = detail; + this.version = version; + this.lastUpdate = lastUpdate; + this.lastUpdateNote = lastUpdateNote; + this.link = link; + this.type = type; + } + + public static Expansion unSerialize(ExpansionType type, String name, JsonObject object) { + String[] author = object.get("author").isJsonArray() ? toArray(object.get("author").getAsJsonArray()) : ArrayUtils.asArray(object.get("author").getAsString()); + String description = object.get("description").getAsString(); + String[] detail = object.get("detail").isJsonArray() ? toArray(object.get("detail").getAsJsonArray()) : ArrayUtils.asArray(object.get("detail").getAsString()); + String version = object.get("version").getAsString(); + String lastUpdate = object.get("last_update").getAsString(); + String lastUpdateNote = object.get("last_update_note").getAsString(); + String link = object.get("link").getAsString(); + return new Expansion(name, author, description, detail, version, lastUpdate, lastUpdateNote, link, type); + } + + public static String[] toArray(JsonArray json) { + return IntStream.range(0, json.size()).mapToObj(i -> json.get(i).getAsString()).toArray(String[]::new); + } + + public String getName() { + return name; + } + + public String[] getAuthor() { + return author; + } + + public String getDescription() { + return description; + } + + public String[] getDetail() { + return detail; + } + + public String getVersion() { + return version; + } + + public String getLastUpdate() { + return lastUpdate; + } + + public String getLastUpdateNote() { + return lastUpdateNote; + } + + public String getLink() { + return link; + } + + public ExpansionType getType() { + return type; + } + + public File getFile() { + return type == ExpansionType.INTERNAL ? new File(TCloudLoader.getExpansionInternalFolder(), "[TCLOUD] " + name + ".jar") : new File("plugins/[TCLOUD] " + name + ".jar"); + } +} diff --git a/src/main/java/me/skymc/taboolib/cloud/expansion/ExpansionType.java b/src/main/java/me/skymc/taboolib/cloud/expansion/ExpansionType.java new file mode 100644 index 0000000..a59b388 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/cloud/expansion/ExpansionType.java @@ -0,0 +1,10 @@ +package me.skymc.taboolib.cloud.expansion; + +/** + * @Author sky + * @Since 2018-09-30 16:39 + */ +public enum ExpansionType { + + INTERNAL, PLUGIN +} \ No newline at end of file diff --git a/src/main/java/me/skymc/taboolib/commands/internal/plugin/TLibLocale.java b/src/main/java/me/skymc/taboolib/commands/internal/plugin/TLibLocale.java new file mode 100644 index 0000000..426e663 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/commands/internal/plugin/TLibLocale.java @@ -0,0 +1,29 @@ +package me.skymc.taboolib.commands.internal.plugin; + +import com.ilummc.tlib.resources.TLocale; +import me.skymc.taboolib.commands.internal.type.CommandArgument; + +import java.util.stream.IntStream; + +/** + * @Author sky + * @Since 2018-09-30 19:26 + */ +public class TLibLocale { + + public static String title(String name) { + return TLocale.asString("COMMANDS." + name + ".COMMAND-TITLE"); + } + + public static String description(String name, String label) { + return TLocale.asString("COMMANDS." + name + "." + label + ".DESCRIPTION"); + } + + public static CommandArgument[] arguments(String name, String label, int size) { + return IntStream.range(0, size).mapToObj(i -> new CommandArgument(TLocale.asString("COMMANDS." + name + "." + label + ".ARGUMENTS." + i))).toArray(CommandArgument[]::new); + } + + public static CommandArgument argument(String name, String label, int index) { + return new CommandArgument(TLocale.asString("COMMANDS." + name + "." + label + ".ARGUMENTS." + index)); + } +} diff --git a/src/main/java/me/skymc/taboolib/update/UpdateTask.java b/src/main/java/me/skymc/taboolib/update/UpdateTask.java index 0536cef..14e5bb4 100644 --- a/src/main/java/me/skymc/taboolib/update/UpdateTask.java +++ b/src/main/java/me/skymc/taboolib/update/UpdateTask.java @@ -49,7 +49,7 @@ public class UpdateTask { } public static boolean isHaveUpdate() { - return newVersion > 0; + return newVersion > TabooLib.getPluginVersion(); } public static double getNewVersion() { diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 8fc0a19..e085359 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -560,6 +560,44 @@ COMMANDS: 0: '目录' 1: '内容' SUCCESS: '&8[&3&lTabooLib&8] &7写入完成.' + TCLOUD: + COMMAND-TITLE: '&e&l----- &6&lTabooLibCloud Commands &e&l-----' + REFRESH: + DESCRIPTION: '刷新扩展列表' + SUCCESS: '&8[&3&lTabooLib&8] &7请求已发送. &8(详细信息请在控制台查看)' + STATUS: + DESCRIPTION: '查看扩展统计' + STATUS: + - '&8[&3&lTabooLib&8] &7当前总共 &f{0} &7项扩展被 &fTCLOUD &7录入, 其中包含:' + - '&8[&3&lTabooLib&8] &7内置扩展 &f{1} &7项' + - '&8[&3&lTabooLib&8] &7插件扩展 &f{2} &7项' + CONNECT-FAILED: '&8[&3&lTabooLib&8] &c尚未获取扩展列表, 输入 &4/tcloud refresh &c刷新.' + INFO: + DESCRIPTION: '查看扩展信息' + ARGUMENTS: + 0: '名称' + EXPANSION-NOT-FOUND: '&8[&3&lTabooLib&8] &c扩展 &4{0} &c不存在, 输入 &4/tcloud refresh &c刷新.' + EXPANSION-INFO: + - '&8[&3&lTabooLib&8] &7扩展 &f{0} &7信息:' + - '&8[&3&lTabooLib&8] &7作者 &f{1}' + - '&8[&3&lTabooLib&8] &7版本 &f{2}' + - '&8[&3&lTabooLib&8] &7描述 &f{3}' + - '&8[&3&lTabooLib&8] &7最后更新时间 &f{4}' + - '&8[&3&lTabooLib&8] &7最后更新内容 &f{5}' + EXPANSION-INFO-DETAIL: + 0: '&8[&3&lTabooLib&8] &7详细描述' + 1: '&8[&3&lTabooLib&8] &7- &f{0}' + DOWNLOAD: + DESCRIPTION: '下载扩展' + ARGUMENTS: + 0: '名称' + EXPANSION-NOT-FOUND: '&8[&3&lTabooLib&8] &c扩展 &4{0} &c不存在, 输入 &4/tcloud refresh &c刷新.' + EXPANSION-EXISTS: '&8[&3&lTabooLib&8] &c扩展 &4{0} &c已存在, 输入 &4/tcloud update &c更新.' + DOWNLOAD-START: + - '&8[&3&lTabooLib&8] &7扩展 &f{0} &7开始下载:' + - '&8[&3&lTabooLib&8] &7目标 &f{1}' + - '&8[&3&lTabooLib&8] &7地址 &f{2}' + DOWNLOAD-SUCCESS: '&8[&3&lTabooLib&8] &7扩展 &f{0} &7下载完成. &8(详细信息请在控制台查看)' DATABASE: CONNECTION-ESTABLISHED: '成功连接到 {0} 数据库,连接池大小 {1}' @@ -580,4 +618,12 @@ UTIL: DOWNLOAD-CONNECTED: '开始下载文件 {0} 大小 {1}' DOWNLOAD-PROGRESS: ' 下载速度 {0} 进度 {1}' DOWNLOAD-SUCCESS: '下载 {0} 完成!' - DOWNLOAD-FAILED: '下载 {0} 失败!' \ No newline at end of file + DOWNLOAD-FAILED: '下载 {0} 失败!' + +TCLOUD: + LIST-CONNECT-SUCCESS: '获取 &fTCLOUD &7扩展列表完成! 耗时 &f{0} &7毫秒.' + LIST-CONNECT-FAILED: '获取 &4TCLOUD &c扩展列表失败!' + LIST-PARSE-SUCCESS: '读取 &fTCLOUD &7扩展列表完成! 耗时 &f{0} &7毫秒' + LIST-PARSE-FAILED: '读取 &4TCLOUD &c扩展列表失败: &4{0}' + LIST-LOAD-SUCCESS: '载入 &fTCLOUD &7扩展列表完成! 耗时 &f{0} &7毫秒' + LIST-LOAD-FAILED: '载入 &4{0} &c扩展数据失败: &4{1}' \ No newline at end of file