Skip to content

Commit 98a1d58

Browse files
committed
完善更新检查器
1 parent 90d4716 commit 98a1d58

File tree

7 files changed

+209
-89
lines changed

7 files changed

+209
-89
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,4 @@ Points使用 [sonarcloud.io](https://sonarcloud.io/project/overview?id=HowieHz_P
170170
13. 调用paperlib进行优化
171171
14. 兼容到spigot端
172172
15. 兼容到1.8
173+
16. 世界准许使用选项为列表,意思是可以指定哪些世界使用

src/main/java/com/hzzz/points/Points.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.hzzz.points.listeners.base_listener.NamedListener;
99
import com.hzzz.points.utils.base_utils_class.baseUtilsClass;
1010
import com.hzzz.points.utils.data_structure.CommandInfo;
11+
import com.hzzz.points.utils.github_update_checker.UpdateChecker;
12+
import com.hzzz.points.utils.data_structure.Tuple4;
1113
import org.bstats.bukkit.Metrics;
1214
import org.bukkit.Bukkit;
1315
import org.bukkit.command.CommandExecutor;
@@ -61,14 +63,18 @@ public static Points getInstance() {
6163
*/
6264
@Override
6365
public void onLoad() {
66+
// 检查更新
67+
updateChecker();
68+
6469
// 如果配置文件不存在, 保存默认的配置
6570
// config.yml
6671
saveDefaultConfig();
6772

6873
// 保存语言文件 加载文字 要在加载配置文件之后,因为要读取配置文件中language.file_name项
6974
saveLangConfig();
75+
reloadLangConfig();
7076

71-
// 插件正在加载 这个要在读取加载语言文件之后,不然输出的就是null
77+
// 插件正在加载
7278
logInfo(getMessage(PLUGIN_LOADING));
7379

7480
// 初始化数据库存放的文件夹
@@ -257,4 +263,31 @@ private void saveLangConfig() {
257263
saveResource(fileName, false);
258264
}
259265
}
266+
267+
private void updateChecker() {
268+
// TODO 向游戏内玩家发送(加上对应权限)
269+
// TODO 定期检查更新(配置文件也要加上)
270+
// TODO 向哪里检查更新,对应源url
271+
logInfo(getMessage(UPDATE_CHECKER_START));
272+
String current_version = this.getDescription().getVersion();
273+
runTaskAsynchronously(() -> {
274+
Tuple4<Boolean, Boolean, String, String> result = UpdateChecker.check(current_version);
275+
if (result._1) {
276+
if (result._2) {
277+
// 需要更新
278+
logInfo(getMessage(UPDATE_CHECKER_NEED_UPDATE)
279+
.replace("[current_version]", current_version)
280+
.replace("[latest_version]", result._3)
281+
.replace("[html_url]", result._4)
282+
);
283+
} else {
284+
// 是最新版
285+
logInfo(getMessage(UPDATE_CHECKER_IS_LATEST).replace("[current_version]", current_version));
286+
}
287+
} else {
288+
// 信息获取失败
289+
logInfo(getMessage(UPDATE_CHECKER_FAIL));
290+
}
291+
});
292+
}
260293
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.hzzz.points.utils.data_structure;
2+
3+
/**
4+
* <p>tuple4,从vavr中精简了一下</p>
5+
*
6+
* @author <a href="https://github.com/HowieHz/">HowieHz</a>
7+
* @version 0.2.4.1
8+
* @since 2022-12-25 15:33
9+
*/
10+
public class Tuple4<T1, T2, T3, T4> {
11+
public Tuple4(T1 t1, T2 t2, T3 t3, T4 t4) {
12+
this._1 = t1;
13+
this._2 = t2;
14+
this._3 = t3;
15+
this._4 = t4;
16+
}
17+
18+
/**
19+
* The 1st element of this tuple.
20+
*/
21+
public final T1 _1;
22+
23+
/**
24+
* The 2nd element of this tuple.
25+
*/
26+
public final T2 _2;
27+
28+
/**
29+
* The 3rd element of this tuple.
30+
*/
31+
public final T3 _3;
32+
33+
/**
34+
* The 4th element of this tuple.
35+
*/
36+
public final T4 _4;
37+
38+
/**
39+
* The 1st element of this tuple.
40+
*/
41+
public T1 _1() {
42+
return this._1;
43+
}
44+
45+
/**
46+
* The 2nd element of this tuple.
47+
*/
48+
public T2 _2() {
49+
return this._2;
50+
}
51+
52+
/**
53+
* The 3rd element of this tuple.
54+
*/
55+
public T3 _3() {
56+
return this._3;
57+
}
58+
59+
/**
60+
* The 4th element of this tuple.
61+
*/
62+
public T4 _4() {
63+
return this._4;
64+
}
65+
}

src/main/java/com/hzzz/points/utils/github_update_checker/UpdateChecker.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import com.alibaba.fastjson2.JSON;
44
import com.alibaba.fastjson2.JSONObject;
5+
import com.hzzz.points.utils.data_structure.Tuple4;
56

67
import java.io.BufferedReader;
78
import java.io.IOException;
89
import java.io.InputStreamReader;
910
import java.net.HttpURLConnection;
1011
import java.net.URL;
11-
import java.util.AbstractMap;
1212

1313
/**
1414
* <p>检查github上是否更新了</p>
@@ -29,17 +29,21 @@ private UpdateChecker() {
2929
* 检查版本是否是最新
3030
*
3131
* @param current_version 当前版本,如v1.0
32-
* @return 不是最新就返回true
32+
* @return (第一个值为true则为获取信息成功 , 第二个值为true为需要更新 , 最新版本版本号 , 最新版本的releases界面)
3333
*/
34-
static public AbstractMap.SimpleImmutableEntry<Boolean, Boolean> check(String current_version) {
35-
34+
static public Tuple4<Boolean, Boolean, String, String> check(String current_version) {
35+
//TODO 自己进行json解析
3636
String url = "https://api.github.com/repos/HowieHz/Points/releases/latest";
3737
JSONObject obj = JSON.parseObject(getJson(url));
38-
obj.getIntValue("");
39-
if (obj.getIntValue("response_code", 200) == 200) {
40-
return new AbstractMap.SimpleImmutableEntry<>(true, compare(current_version.substring(1), obj.getString("tag_name").substring(1)) < 0);
38+
String latest_version = obj.getString("tag_name");
39+
int response_code = obj.getIntValue("response_code", 200);
40+
if (response_code == 200) {
41+
return new Tuple4<>(true,
42+
compare(current_version.substring(1), latest_version.substring(1)) < 0,
43+
latest_version,
44+
obj.getString("html_url"));
4145
}
42-
return new AbstractMap.SimpleImmutableEntry<>(false, false);
46+
return new Tuple4<>(false, false, "", "");
4347
}
4448

4549
/**

0 commit comments

Comments
 (0)