Skip to content

Commit

Permalink
update to 1.4.1
Browse files Browse the repository at this point in the history
  - 重写检查更新,现在会在控制台输出当前版本到最新版本的更新内容并且OP每日第一次进游戏时会收到更新提醒.
  - 权限 servermonitor.use 更改为 servermonitor.admin
  • Loading branch information
myunco committed Sep 25, 2024
1 parent 802669e commit 4328116
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 60 deletions.
5 changes: 4 additions & 1 deletion src/net/myunco/servermonitor/ServerMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.myunco.servermonitor.listener.PluginEventListener;
import net.myunco.servermonitor.metrics.Metrics;
import net.myunco.servermonitor.update.UpdateChecker;
import net.myunco.servermonitor.update.UpdateNotification;
import net.myunco.servermonitor.util.Log;
import net.myunco.servermonitor.util.Util;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -35,7 +36,9 @@ public void onEnable() {
command.setTabCompleter((TabCompleter) command.getExecutor());
}
getServer().getPluginManager().registerEvents(new PluginEventListener(this), this);

if (Config.checkUpdate) {
getServer().getPluginManager().registerEvents(new UpdateNotification(), this);
}
new Metrics(this, 12934);
logMessage(Language.enableMessage);
}
Expand Down
104 changes: 87 additions & 17 deletions src/net/myunco/servermonitor/update/CheckResult.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,89 @@
package net.myunco.servermonitor.update;

import net.myunco.servermonitor.ServerMonitor;
import net.myunco.servermonitor.config.Language;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class CheckResult {
public static String currentVersion = ServerMonitor.getPlugin().getDescription().getVersion();
public static String[] current = currentVersion.replace('-', '.').split("\\.");
private static final ServerMonitor plugin = ServerMonitor.getPlugin();
private CheckResult.ResultType resultType;
private final String currentVersion;
private String downloadLink;
private final StringBuilder updateInfo = new StringBuilder();
private boolean majorUpdate;
private boolean newVersion;
private String latestVersion;
private String errorMessage;

public enum ResultType {
SUCCESS, FAILURE
}

private final ResultType resultType;
private final String latestVersion;
private final boolean majorUpdate;
private final boolean newVersion;
private final int responseCode;

public CheckResult(int responseCode, ResultType type) {
this(null, responseCode, type);
public CheckResult(String url, String currentVersion) {
this.currentVersion = currentVersion;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
int code = conn.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line = reader.readLine();
if (line == null || !line.startsWith("ULT ")) { // 第一行必须以ULT 开头 作为标识判断
resultType = CheckResult.ResultType.FAILURE;
errorMessage = "Error Code: 1";
} else {
downloadLink = line.substring(4); // ULT 后面跟下载地址
line = reader.readLine();
if (line == null || !line.startsWith("LV ")) { // 第二行必须以LV 开头
resultType = CheckResult.ResultType.FAILURE;
errorMessage = "Error Code: 2";
reader.close();
conn.disconnect();
return;
}
latestVersion = line.substring(3); // LV 后面跟最新版本
compareVersion();
if (newVersion) { // 如果有新版本 获取更新信息
while ((line = reader.readLine()) != null) {
if (line.startsWith("v")) {
line = line.substring(1);
if (line.split(" ")[0].equals(currentVersion)) { //TODO
break;
} else if (!majorUpdate && line.endsWith(" 重要更新")) {
majorUpdate = true;
}
if (line.endsWith("SNAPSHOT") || line.endsWith("BETA")) {
continue;
}
} else if (line.startsWith("//")) {
continue;
}
updateInfo.append(line).append('\n');
}
}
resultType = CheckResult.ResultType.SUCCESS;
}
reader.close();
conn.disconnect();
} else {
resultType = CheckResult.ResultType.FAILURE;
errorMessage = "HTTP " + code;
}
} catch (IOException e) {
plugin.logMessage(Language.updateCheckException);
e.printStackTrace();
}
}

public CheckResult(String latestVersion, int responseCode, ResultType type) {
this.latestVersion = latestVersion;
this.responseCode = responseCode;
this.resultType = type;
private void compareVersion() {
if (latestVersion != null && !currentVersion.equals(latestVersion)) {
String[] latest = latestVersion.replace('-', '.').split("\\.");
String[] current = currentVersion.replace('-', '.').split("\\.");
if (Integer.parseInt(latest[0]) > Integer.parseInt(current[0])) {
newVersion = true;
majorUpdate = true;
Expand All @@ -40,7 +98,7 @@ public CheckResult(String latestVersion, int responseCode, ResultType type) {
newVersion = true;
majorUpdate = false;
return;
} else if (Integer.parseInt(latest[2]) == Integer.parseInt(current[2]) && current.length > 3) {
} else if (latest[2].equals(current[2]) && current.length > 3) { //主版本次版本修正号相同 但当前版本不是最终正式版 例如1.0.0-SNAPSHOT
newVersion = true;
majorUpdate = false;
return;
Expand All @@ -56,10 +114,22 @@ public ResultType getResultType() {
return resultType;
}

public String getUpdateInfo() {
return updateInfo.toString();
}

public String getErrorMessage() {
return errorMessage;
}

public String getLatestVersion() {
return latestVersion;
}

public String getCurrentVersion() {
return currentVersion;
}

public boolean hasNewVersion() {
return newVersion;
}
Expand All @@ -68,7 +138,7 @@ public boolean hasMajorUpdate() {
return majorUpdate;
}

public int getResponseCode() {
return responseCode;
public String getDownloadLink() {
return downloadLink;
}
}
57 changes: 18 additions & 39 deletions src/net/myunco/servermonitor/update/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,40 @@
import net.myunco.servermonitor.ServerMonitor;
import net.myunco.servermonitor.config.Language;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Timer;
import java.util.TimerTask;

public class UpdateChecker {
private static final ServerMonitor plugin = ServerMonitor.getPlugin();
private static Timer timer;
private static String downloadLink;
static boolean isUpdateAvailable;
static String newVersion;
static String downloadLink;

public static void start() {
plugin.getServer().getScheduler().runTask(plugin, () -> { // 直接使用Timer不能等到开服完成后再检查更新
// 什么?你问我为什么要用Timer?我只能告诉你,我忘了我为什么用Timer了。 ———— 2023/12/2
// 等等,我好像又想起来了,是因为计时准确性。 ———— 2023/12/2
plugin.getServer().getScheduler().runTask(plugin, () -> {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
CheckResult result = checkVersionUpdate("https://sinacloud.net/myunco/E776DD23/version.txt");
if (result.getResultType() == CheckResult.ResultType.SUCCESS) {
if (result.hasNewVersion()) {
String str = Language.replaceArgs(Language.updateFoundNewVersion, CheckResult.currentVersion, result.getLatestVersion());
plugin.logMessage(result.hasMajorUpdate() ? Language.updateMajorUpdate + str : str);
// plugin.logMessage(Language.updateDownloadLink + "https://www.mcbbs.net/thread-995756-1-1.html");
plugin.logMessage(Language.updateDownloadLink + downloadLink);
}
CheckResult result = new CheckResult("https://myunco.sinacloud.net/E776DD23/ServerMonitor.txt", plugin.getDescription().getVersion());
if (result.getResultType() == CheckResult.ResultType.SUCCESS) {
if (result.hasNewVersion()) {
isUpdateAvailable = true;
String str = Language.replaceArgs(Language.updateFoundNewVersion, result.getCurrentVersion(), result.getLatestVersion());
newVersion = result.hasMajorUpdate() ? Language.updateMajorUpdate + str : str;
downloadLink = Language.updateDownloadLink + result.getDownloadLink();
plugin.logMessage(newVersion);
plugin.logMessage(downloadLink);
plugin.logMessage(result.getUpdateInfo());
} else {
plugin.logMessage(Language.updateCheckFailure + result.getResponseCode());
isUpdateAvailable = false;
}
} catch (IOException e) {
plugin.logMessage(Language.updateCheckException);
e.printStackTrace();
} else {
plugin.logMessage(Language.updateCheckFailure + result.getErrorMessage());
}
}
}, 10000, 12 * 60 * 60 * 1000); // 开服完成10秒后检查一次,以后每12小时检查一次
}, 5000, 12 * 60 * 60 * 1000); // 开服完成5秒后检查一次,以后每12小时检查一次
});
}

Expand All @@ -52,19 +46,4 @@ public static void stop() {
}
}

public static CheckResult checkVersionUpdate(String url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
int code = conn.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String latestVersion = reader.readLine();
downloadLink = reader.readLine();
reader.close();
conn.disconnect();
return new CheckResult(latestVersion, code, CheckResult.ResultType.SUCCESS);
} else {
return new CheckResult(code, CheckResult.ResultType.FAILURE);
}
}

}
37 changes: 37 additions & 0 deletions src/net/myunco/servermonitor/update/UpdateNotification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.myunco.servermonitor.update;

import net.myunco.servermonitor.ServerMonitor;
import net.myunco.servermonitor.config.Language;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.UUID;

public class UpdateNotification implements Listener {
private int day = LocalDate.now().getDayOfMonth();
private final ArrayList<UUID> notifiedPlayers = new ArrayList<>();

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent event) {
if (UpdateChecker.isUpdateAvailable && event.getPlayer().hasPermission("servermonitor.admin")) {
if (day != LocalDate.now().getDayOfMonth()) {
day = LocalDate.now().getDayOfMonth();
if (!notifiedPlayers.isEmpty()) {
notifiedPlayers.clear();
}
}
if (!notifiedPlayers.contains(event.getPlayer().getUniqueId())) {
notifiedPlayers.add(event.getPlayer().getUniqueId());
ServerMonitor.getPlugin().getServer().getScheduler().runTaskLaterAsynchronously(ServerMonitor.getPlugin(), () -> {
event.getPlayer().sendMessage(Language.messagePrefix + UpdateChecker.newVersion);
event.getPlayer().sendMessage(Language.messagePrefix + UpdateChecker.downloadLink);
}, 60);
}
}
}

}
6 changes: 3 additions & 3 deletions src/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ServerMonitor
main: net.myunco.servermonitor.ServerMonitor
version: 1.4.1-SNAPSHOT
version: 1.4.1
api-version: 1.13
author: myunco
website: https://github.com/myunco/ServerMonitor
Expand All @@ -11,9 +11,9 @@ commands:
description: This is main command of the plugin.
usage: §e/ServerMonitor §3<§ahelp§c|§areload§c|§aversion§3>
aliases: sm
permission: servermonitor.use
permission: servermonitor.admin
permission-message: "§cYou don't have permission!"
permissions:
servermonitor.use:
servermonitor.admin:
description: Has permission to access the all ServerMonitor command
default: op

0 comments on commit 4328116

Please sign in to comment.