From 8a20fb7edb65184599ea480dc399b3d156e8e2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Wed, 23 May 2018 22:12:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=E8=87=B3?= =?UTF-8?q?=204.05=20=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=A4=B1=E6=95=88=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=EF=BC=9ATLocale=20=E8=BD=BD=E5=85=A5=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=B5=84=E6=BA=90=E6=B5=AA=E8=B4=B9=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=8B=A5?= =?UTF-8?q?=E6=9C=89=20"taboolib.update.notify"=20=E6=9D=83=E9=99=90?= =?UTF-8?q?=E7=9A=84=E7=8E=A9=E5=AE=B6=E8=BF=9B=E6=9C=8D=E5=B0=86=E4=BC=9A?= =?UTF-8?q?=E6=94=B6=E5=88=B0=E6=9B=B4=E6=96=B0=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../ilummc/tlib/resources/TLocaleLoader.java | 2 +- src/main/java/me/skymc/taboolib/Main.java | 3 +- .../skymc/taboolib/fileutils/FileUtils.java | 8 ++++- .../listener/ListenerPlayerJoinAndQuit.java | 26 ++++++++++++++++ .../taboolib/listener/ListenerPlayerQuit.java | 15 ---------- .../me/skymc/taboolib/update/UpdateTask.java | 30 +++++++++++++++---- src/main/resources/lang/zh_CN.yml | 3 +- 8 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/listener/ListenerPlayerJoinAndQuit.java delete mode 100644 src/main/java/me/skymc/taboolib/listener/ListenerPlayerQuit.java diff --git a/pom.xml b/pom.xml index 07bb135..4aaeaa4 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.skymc TabooLib - 4.03 + 4.05 UTF-8 diff --git a/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java b/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java index c9f6bcb..2aef0c0 100644 --- a/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java +++ b/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java @@ -141,7 +141,7 @@ public class TLocaleLoader { } private static YamlConfiguration getLocaleAtStream(Plugin plugin, File localeFile) { - InputStream localeInputSteam = FileUtils.getResource("lang/" + localeFile.getName()); + InputStream localeInputSteam = FileUtils.getResource(plugin, "lang/" + localeFile.getName()); try { String yamlText = new String(IO.readFully(localeInputSteam), Charset.forName("utf-8")); YamlConfiguration yaml = new YamlConfiguration(); diff --git a/src/main/java/me/skymc/taboolib/Main.java b/src/main/java/me/skymc/taboolib/Main.java index a181285..731b886 100644 --- a/src/main/java/me/skymc/taboolib/Main.java +++ b/src/main/java/me/skymc/taboolib/Main.java @@ -7,7 +7,6 @@ import com.ilummc.tlib.resources.TLocale; import me.skymc.taboolib.anvil.AnvilContainerAPI; import me.skymc.taboolib.bstats.Metrics; import me.skymc.taboolib.commands.TabooLibMainCommand; -import me.skymc.taboolib.commands.internal.BaseMainCommand; import me.skymc.taboolib.commands.internal.TBaseCommand; import me.skymc.taboolib.commands.language.Language2Command; import me.skymc.taboolib.commands.locale.TabooLibLocaleCommand; @@ -334,7 +333,7 @@ public class Main extends JavaPlugin implements Listener { getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(new ListenerPlayerCommand(), this); getServer().getPluginManager().registerEvents(new ListenerPlayerJump(), this); - getServer().getPluginManager().registerEvents(new ListenerPlayerQuit(), this); + getServer().getPluginManager().registerEvents(new ListenerPlayerJoinAndQuit(), this); getServer().getPluginManager().registerEvents(new ChatCatcher(), this); getServer().getPluginManager().registerEvents(new DataUtils(), this); getServer().getPluginManager().registerEvents(new AnvilContainerAPI(), this); diff --git a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java index a3040e4..a0afd4d 100644 --- a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java +++ b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java @@ -2,8 +2,10 @@ package me.skymc.taboolib.fileutils; import ch.njol.util.Closeable; import com.ilummc.tlib.util.IO; +import javafx.print.PageLayout; import me.skymc.taboolib.Main; import org.apache.commons.io.IOUtils; +import org.bukkit.plugin.Plugin; import java.io.*; import java.net.HttpURLConnection; @@ -48,8 +50,12 @@ public class FileUtils { } public static InputStream getResource(String filename) { + return getResource(Main.getInst(), filename); + } + + public static InputStream getResource(Plugin plugin, String filename) { try { - URL url = Main.class.getClassLoader().getResource(filename); + URL url = plugin.getClass().getClassLoader().getResource(filename); if (url == null) { return null; } else { diff --git a/src/main/java/me/skymc/taboolib/listener/ListenerPlayerJoinAndQuit.java b/src/main/java/me/skymc/taboolib/listener/ListenerPlayerJoinAndQuit.java new file mode 100644 index 0000000..3f262df --- /dev/null +++ b/src/main/java/me/skymc/taboolib/listener/ListenerPlayerJoinAndQuit.java @@ -0,0 +1,26 @@ +package me.skymc.taboolib.listener; + +import com.ilummc.tlib.resources.TLocale; +import me.skymc.taboolib.TabooLib; +import me.skymc.taboolib.playerdata.DataUtils; +import me.skymc.taboolib.update.UpdateTask; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +public class ListenerPlayerJoinAndQuit implements Listener { + + @EventHandler + public void onJoin(PlayerJoinEvent e) { + if (UpdateTask.isHaveUpdate() && e.getPlayer().hasPermission("taboolib.update.notify")) { + TLocale.Logger.sendTo(e.getPlayer(), "UPDATETASK.VERSION-OUTDATED", String.valueOf(TabooLib.getPluginVersion()), String.valueOf(UpdateTask.getNewVersion())); + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onQuit(PlayerQuitEvent e) { + DataUtils.saveOnline(e.getPlayer().getName()); + } +} diff --git a/src/main/java/me/skymc/taboolib/listener/ListenerPlayerQuit.java b/src/main/java/me/skymc/taboolib/listener/ListenerPlayerQuit.java deleted file mode 100644 index 066e77c..0000000 --- a/src/main/java/me/skymc/taboolib/listener/ListenerPlayerQuit.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.skymc.taboolib.listener; - -import me.skymc.taboolib.playerdata.DataUtils; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerQuitEvent; - -public class ListenerPlayerQuit implements Listener{ - - @EventHandler (priority = EventPriority.MONITOR) - public void quit(PlayerQuitEvent e) { - DataUtils.saveOnline(e.getPlayer().getName()); - } -} diff --git a/src/main/java/me/skymc/taboolib/update/UpdateTask.java b/src/main/java/me/skymc/taboolib/update/UpdateTask.java index 7deb383..ee4f992 100644 --- a/src/main/java/me/skymc/taboolib/update/UpdateTask.java +++ b/src/main/java/me/skymc/taboolib/update/UpdateTask.java @@ -1,5 +1,6 @@ package me.skymc.taboolib.update; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.ilummc.tlib.resources.TLocale; @@ -14,10 +15,19 @@ import org.bukkit.scheduler.BukkitRunnable; */ public class UpdateTask { - private static final String API = "https://internal.github.com/repos/Bkm016/TabooLib/releases/latest"; + private static boolean haveUpdate = false; + private static double newVersion = 0; + + public static boolean isHaveUpdate() { + return haveUpdate; + } + + public static double getNewVersion() { + return newVersion; + } /** - * 检测更新 + * 旧地址:https://internal.github.com/repos/Bkm016/TabooLib/releases/latest */ public UpdateTask() { new BukkitRunnable() { @@ -27,13 +37,21 @@ public class UpdateTask { if (!Main.getInst().getConfig().getBoolean("UPDATE-CHECK")) { return; } - String value = FileUtils.getStringFromURL(API, "{}"); - JsonObject json = new JsonParser().parse(value).getAsJsonObject(); - if (json.entrySet().size() > 0) { - double newVersion = Double.parseDouble(json.get("tag_name").getAsString()); + + 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(); + newVersion = latestObject.get("name").getAsDouble(); if (TabooLib.getPluginVersion() >= newVersion) { TLocale.Logger.info("UPDATETASK.VERSION-LATEST"); } else { + haveUpdate = true; TLocale.Logger.info("UPDATETASK.VERSION-OUTDATED", String.valueOf(TabooLib.getPluginVersion()), String.valueOf(newVersion)); } } diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 6555707..3e8ddbf 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -112,7 +112,8 @@ ANVIL-CONTAINER: - '&7随后点击右侧输出物品' UPDATETASK: - VERSION-LATEST: '插件已是最新版, 无需更新!' + VERSION-FAIL: '&4更新记录获取失败, 请检查网络连接!' + VERSION-LATEST: '&7插件已是最新版, 无需更新!' VERSION-OUTDATED: - '&8####################################################' - '&7 检测到有新的版本更新!'