diff --git a/src/main/java/com/ilummc/tlib/ExampleMain.java b/src/main/java/com/ilummc/tlib/ExampleMain.java index 324599f..b6426a6 100644 --- a/src/main/java/com/ilummc/tlib/ExampleMain.java +++ b/src/main/java/com/ilummc/tlib/ExampleMain.java @@ -13,6 +13,8 @@ import java.util.concurrent.TimeUnit; @TConfig(name = "cfg.yml", charset = "GBK") public class ExampleMain extends JavaPlugin { + private Property update = Property.of(false); + public static void main(String[] args) { MemoryMXBean bean = ManagementFactory.getMemoryMXBean(); System.out.println(bean.getHeapMemoryUsage().toString()); @@ -34,8 +36,6 @@ public class ExampleMain extends JavaPlugin { } } - private Property update = Property.of(false); - @Override public void onEnable() { update.addListener(((oldVal, newVal) -> { diff --git a/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java b/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java index 954d423..cec1fbf 100644 --- a/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java +++ b/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java @@ -779,39 +779,12 @@ public class TabooLibMainCommand extends BaseMainCommand { @Override public void onCommand(CommandSender sender, Command command, String label, String[] args) { - if (!UpdateTask.isHaveUpdate()) { - TLocale.sendTo(sender, "COMMANDS.TABOOLIB.UPDATEPLUGIN.UPDATE-NOT-FOUND"); - return; - } + UpdateTask.updatePlugin(true); + } - File pluginFile = PluginUtils.getPluginFile(Main.getInst()); - if (pluginFile == null) { - TLocale.sendTo(sender, "COMMANDS.TABOOLIB.UPDATEPLUGIN.FILE-NOT-FOUND"); - return; - } - - new BukkitRunnable() { - - @Override - public void run() { - if (PlayerUtils.getOnlinePlayers().size() > 0) { - TLocale.sendTo(sender, "COMMANDS.TABOOLIB.UPDATEPLUGIN.PLAYER-ONLINE"); - return; - } - - String url = Strings.replaceWithOrder("https://github.com/Bkm016/TabooLib/releases/download/{0}/TabooLib-{0}.jar", UpdateTask.getNewVersion()); - TLocale.sendTo(sender, "COMMANDS.TABOOLIB.UPDATEPLUGIN.UPDATE-START", url); - - File file = new File("plugins/update"); - if (file.exists()) { - FileUtils.download(url, new File(file, pluginFile.getName())); - TLocale.sendTo(sender, "COMMANDS.TABOOLIB.UPDATEPLUGIN.UPDATE-SUCCESS"); - } else { - FileUtils.download(url, pluginFile); - Bukkit.shutdown(); - } - } - }.runTaskAsynchronously(Main.getInst()); + @Override + public CommandType getType() { + return CommandType.CONSOLE; } }; } diff --git a/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java b/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java index cb8aa1c..c53525f 100644 --- a/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java +++ b/src/main/java/me/skymc/taboolib/common/configuration/TConfiguration.java @@ -82,6 +82,7 @@ public class TConfiguration extends YamlConfiguration { public void reload() { try { load(file); + runListener(); } catch (IOException | InvalidConfigurationException e) { TLogger.getGlobalLogger().warn("Cannot load configuration from stream: " + e.toString()); } diff --git a/src/main/java/me/skymc/taboolib/common/pathfinder/internal/InternalPathfinderExecutor.java b/src/main/java/me/skymc/taboolib/common/pathfinder/internal/InternalPathfinderExecutor.java index 68330f9..8f0b5e3 100644 --- a/src/main/java/me/skymc/taboolib/common/pathfinder/internal/InternalPathfinderExecutor.java +++ b/src/main/java/me/skymc/taboolib/common/pathfinder/internal/InternalPathfinderExecutor.java @@ -6,14 +6,12 @@ import me.skymc.taboolib.common.pathfinder.SimpleAiSelector; import me.skymc.taboolib.nms.NMSUtils; import net.minecraft.server.v1_8_R3.*; import org.bukkit.Location; -import org.bukkit.attribute.Attribute; import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import java.lang.reflect.Field; -import java.util.Set; +import java.util.Collection; /** * 该类仅用作生成 ASM 代码,无任何意义 @@ -117,7 +115,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor { @Override public void clearGoalAi(LivingEntity entity) { try { - ((Set) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).goalSelector)).clear(); + ((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).goalSelector)).clear(); } catch (Exception e) { e.printStackTrace(); } @@ -126,7 +124,7 @@ public class InternalPathfinderExecutor extends PathfinderExecutor { @Override public void clearTargetAi(LivingEntity entity) { try { - ((Set) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).targetSelector)).clear(); + ((Collection) pathfinderGoalSelectorSet.get(((EntityInsentient) getEntityInsentient(entity)).targetSelector)).clear(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java index 144b39f..861223c 100644 --- a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java +++ b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java @@ -1,13 +1,15 @@ package me.skymc.taboolib.fileutils; import ch.njol.util.Closeable; +import com.ilummc.eagletdl.EagletTask; +import com.ilummc.eagletdl.ProgressEvent; +import com.ilummc.tlib.resources.TLocale; import com.ilummc.tlib.util.IO; import me.skymc.taboolib.Main; import org.apache.commons.io.IOUtils; import org.bukkit.plugin.Plugin; import java.io.*; -import java.net.HttpURLConnection; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; @@ -405,35 +407,38 @@ public class FileUtils { /** * 下载文件 * - * @param downloadURL 下载地址 - * @param file 保存位置 + * @param url 下载地址 + * @param file 下载位置 + * @param async 是否异步 */ - public static void download(String downloadURL, File file) { - HttpURLConnection conn = null; - InputStream inputStream = null; - FileOutputStream fos = null; - try { - URL url = new URL(downloadURL); - conn = (HttpURLConnection) url.openConnection(); - conn.setConnectTimeout(5 * 1000); - conn.setRequestProperty("User-Agent", "Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)"); - - inputStream = conn.getInputStream(); - byte[] data = read(inputStream); - - fos = new FileOutputStream(createNewFile(file)); - fos.write(data); - } catch (Exception ignored) { - } finally { - IOUtils.close(conn); - IOUtils.closeQuietly(fos); - IOUtils.closeQuietly(inputStream); + public static void download(String url, File file, boolean async) { + EagletTask eagletTask = new EagletTask() + .url(url) + .file(file) + .setThreads(8) + .setOnError(event -> { + }) + .setOnConnected(event -> TLocale.Logger.info("UTIL.DOWNLOAD-CONNECTED", file.getName(), ProgressEvent.format(event.getContentLength()))) + .setOnProgress(event -> TLocale.Logger.info("UTIL.DOWNLOAD-PROGRESS", event.getSpeedFormatted(), event.getPercentageFormatted())) + .setOnComplete(event -> { + if (event.isSuccess()) { + TLocale.Logger.info("UTIL.DOWNLOAD-SUCCESS", file.getName()); + } else { + TLocale.Logger.error("UTIL.DOWNLOAD-FAILED", file.getName()); + } + }).start(); + if (!async) { + eagletTask.waitUntil(); } } + public static void download(String url, File file) { + download(url, file, false); + } + @Deprecated - public static void download(String downloadURL, String filename, File saveDir) { - download(downloadURL, new File(saveDir, filename)); + public static void download(String url, String filename, File saveDir) { + download(url, new File(saveDir, filename)); } @Deprecated diff --git a/src/main/java/me/skymc/taboolib/update/UpdateTask.java b/src/main/java/me/skymc/taboolib/update/UpdateTask.java index 0bb475f..2c3b61c 100644 --- a/src/main/java/me/skymc/taboolib/update/UpdateTask.java +++ b/src/main/java/me/skymc/taboolib/update/UpdateTask.java @@ -4,12 +4,17 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.ilummc.tlib.resources.TLocale; +import com.ilummc.tlib.util.Strings; import me.skymc.taboolib.Main; import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.fileutils.FileUtils; +import me.skymc.taboolib.player.PlayerUtils; +import me.skymc.taboolib.plugin.PluginUtils; import org.bukkit.Bukkit; import org.bukkit.scheduler.BukkitRunnable; +import java.io.File; + /** * @author sky * @since 2018年2月23日 下午10:39:14 @@ -19,14 +24,6 @@ public class UpdateTask { private static boolean haveUpdate = false; private static double newVersion = 0; - public static boolean isHaveUpdate() { - return haveUpdate; - } - - public static double getNewVersion() { - return newVersion; - } - public UpdateTask() { new BukkitRunnable() { @@ -35,13 +32,11 @@ public class UpdateTask { if (!Main.getInst().getConfig().getBoolean("UPDATE-CHECK")) { return; } - String value = FileUtils.getStringFromURL("https://api.github.com/repos/Bkm016/TabooLib/tags", null); if (value == null) { TLocale.Logger.error("UPDATETASK.VERSION-FAIL"); return; } - JsonElement json = new JsonParser().parse(value); if (json.isJsonArray()) { JsonObject latestObject = json.getAsJsonArray().get(0).getAsJsonObject(); @@ -51,18 +46,39 @@ public class UpdateTask { } else { haveUpdate = true; TLocale.Logger.info("UPDATETASK.VERSION-OUTDATED", String.valueOf(TabooLib.getPluginVersion()), String.valueOf(newVersion)); - - new BukkitRunnable() { - @Override - public void run() { - if (Main.getInst().getConfig().getBoolean("UPDATE-DOWNLOAD", false)) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "taboolib updatePlugin"); - } - } - }.runTask(Main.getInst()); + Bukkit.getScheduler().runTask(TabooLib.instance(), () -> updatePlugin(true)); } } } }.runTaskTimerAsynchronously(Main.getInst(), 100, 20 * 60 * 60 * 6); } + + public static boolean isHaveUpdate() { + return haveUpdate; + } + + public static double getNewVersion() { + return newVersion; + } + + public static void updatePlugin(boolean shutdown) { + if (!UpdateTask.isHaveUpdate()) { + TLocale.Logger.info("COMMANDS.TABOOLIB.UPDATEPLUGIN.UPDATE-NOT-FOUND"); + return; + } + if (PlayerUtils.getOnlinePlayers().size() > 0) { + TLocale.Logger.info("COMMANDS.TABOOLIB.UPDATEPLUGIN.PLAYER-ONLINE"); + return; + } + File pluginFile = PluginUtils.getPluginFile(Main.getInst()); + if (pluginFile == null) { + TLocale.Logger.info("COMMANDS.TABOOLIB.UPDATEPLUGIN.FILE-NOT-FOUND"); + return; + } + FileUtils.download(Strings.replaceWithOrder("https://github.com/Bkm016/TabooLib/releases/download/{0}/TabooLib-{0}.jar", UpdateTask.getNewVersion()), pluginFile); + TLocale.Logger.info("COMMANDS.TABOOLIB.UPDATEPLUGIN.UPDATE-SUCCESS"); + if (shutdown) { + Bukkit.shutdown(); + } + } } diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 6fc3c0b..8fc0a19 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -159,8 +159,8 @@ COMMANDS: ONLY-STORAGE-SQL: '&8[&3&lTabooLib&8] &4只有启用数据库储存时才能这么做' INTERNAL: TYPE-ERROR: '&8[&3&lTabooLib&8] &7指令 &f{0} &7只能由 &f{1} &7执行' - TYPE-PLAYER: 玩家 - TYPE-CONSOLE: 控制台 + TYPE-PLAYER: '玩家' + TYPE-CONSOLE: '控制台' ERROR-USAGE: - '&8[&3&lTabooLib&8] &7指令 &f{0} &7参数不足' - '&8[&3&lTabooLib&8] &7正确用法:' @@ -237,10 +237,9 @@ COMMANDS: DESCRIPTION: '&4更新插件 &8(谨防非正规途径的插件获取方式)' UPDATE-NOT-FOUND: '&8[&3&lTabooLib&8] &7插件已是最新版, 无需更新!' UPDATE-NOT-SUPPORT: '&8[&3&lTabooLib&8] &4您的服务器不支持在线更新!' + UPDATE-SUCCESS: '&8[&3&lTabooLib&8] &7最新版下载完成, 服务器即将重启!' FILE-NOT-FOUND: '&8[&3&lTabooLib&8] &4尚未寻找到插件文件' PLAYER-ONLINE: '&8[&3&lTabooLib&8] &4服务器有玩家在线无法更新插件.' - UPDATE-START: '&8[&3&lTabooLib&8] &7开始下载:&f {0}' - UPDATE-SUCCESS: '&8[&3&lTabooLib&8] &7最新版下载完成, 服务器即将重启!' PLAYERTAG: DESCRIPTION: DISPLAY: '设置玩家展示名称' @@ -575,4 +574,10 @@ COMMUNICATION: CLIENT-JOINED: '§8[§3§lTabooLibClient§8] &7服务器 &f{0} &7加入本地通讯网络.' CLIENT-QUITED: '§8[§3§lTabooLibClient§8] &7服务器 &f{0} &7退出本地通讯网络.' PACKET-MESSAGE: '§8[§3§lTabooLibClient§8] &7服务器 &f{0} &7发送信息: &f{1}' - PACKET-COMMAND: '§8[§3§lTabooLibClient§8] &7服务器 &f{0} &7运行命令: &f{1}' \ No newline at end of file + PACKET-COMMAND: '§8[§3§lTabooLibClient§8] &7服务器 &f{0} &7运行命令: &f{1}' + +UTIL: + DOWNLOAD-CONNECTED: '开始下载文件 {0} 大小 {1}' + DOWNLOAD-PROGRESS: ' 下载速度 {0} 进度 {1}' + DOWNLOAD-SUCCESS: '下载 {0} 完成!' + DOWNLOAD-FAILED: '下载 {0} 失败!' \ No newline at end of file