Skip to content

Commit

Permalink
[v1.3.4] 版本更新
Browse files Browse the repository at this point in the history
- [A] 添加版本更新检查。
  • Loading branch information
CarmJos committed Jan 21, 2022
1 parent afd62a4 commit a427db3
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<groupId>cc.carm.plugin</groupId>
<artifactId>ultradepository</artifactId>
<packaging>jar</packaging>
<version>1.3.3</version>
<version>1.3.4</version>

<name>UltraDepository</name>
<description>超级仓库插件,支持设定不同物品的存储仓库。</description>
Expand Down Expand Up @@ -116,6 +116,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>cc.carm.lib</groupId>
<artifactId>githubreleases4j</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
Expand Down Expand Up @@ -252,10 +259,18 @@
<pattern>org.bstats</pattern>
<shadedPattern>cc.carm.plugin.ultradepository.lib.bstats</shadedPattern>
</relocation>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>cc.carm.plugin.ultradepository.lib.json</shadedPattern>
</relocation>
<relocation>
<pattern>cc.carm.lib.easyplugin</pattern>
<shadedPattern>cc.carm.plugin.ultradepository.lib.easyplugin</shadedPattern>
</relocation>
<relocation>
<pattern>cc.carm.lib.githubreleases4j</pattern>
<shadedPattern>cc.carm.plugin.ultradepository.lib.github</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cc.carm.plugin.ultradepository.command.DepositoryCommand;
import cc.carm.plugin.ultradepository.configuration.PluginConfig;
import cc.carm.plugin.ultradepository.hooker.PAPIExpansion;
import cc.carm.plugin.ultradepository.hooker.UpdateChecker;
import cc.carm.plugin.ultradepository.listener.CollectListener;
import cc.carm.plugin.ultradepository.listener.UserListener;
import cc.carm.plugin.ultradepository.manager.ConfigManager;
Expand Down Expand Up @@ -109,6 +110,13 @@ protected boolean initialize() {
}));
}

if (PluginConfig.CHECK_UPDATE.get()) {
log("开始检查更新...");
UpdateChecker.checkUpdate(this);
} else {
log("已禁用检查更新,跳过。");
}

getUserManager().loadPlayersData();

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class PluginConfig {
"metrics", Boolean.class, true
);

public static final ConfigValue<Boolean> CHECK_UPDATE = new ConfigValue<>(
"check-update", Boolean.class, true
);

public static final ConfigStringCast<StorageMethod> STORAGE_METHOD = new ConfigStringCast<>(
"storage.method", StorageMethod::read, StorageMethod.YAML
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cc.carm.plugin.ultradepository.hooker;

import cc.carm.lib.githubreleases4j.GithubRelease;
import cc.carm.lib.githubreleases4j.GithubReleases4J;
import cc.carm.plugin.ultradepository.UltraDepository;

import java.util.List;

public class UpdateChecker {

public static void checkUpdate(UltraDepository plugin) {
plugin.getScheduler().runAsync(() -> {

try {

List<GithubRelease> releases = GithubReleases4J.listReleases("CarmJos", "UltraDepository");
if (releases.isEmpty()) throw new NullPointerException(); // 无法获取更新

String currentVersion = plugin.getDescription().getVersion();
int i = 0;

for (GithubRelease release : releases) {
if (release.getTagName().equalsIgnoreCase(currentVersion)) {
break;
}
i++;
}

if (i > 0) {
GithubRelease latestRelease = releases.get(0);
plugin.log("检查更新完成,当前已落后 " + i + " 个版本,最新版本为 &6&l" + latestRelease.getTagName() + " &r。");
plugin.log("最新版本下载地址&e " + latestRelease.getHTMLUrl());
} else {
plugin.log("检查更新完成,当前已是最新版本。");
}

} catch (Exception exception) {
plugin.error("检查更新失败,请您定期查看插件是否更新,避免安全问题。");
plugin.error("插件下载地址&e https://github.com/CarmJos/UltraDepository/releases");
}

});
}


}
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ debug: false
# 当然,您也可以选择在这里关闭,或在plugins/bStats下的配置文件中关闭。
metrics: true

# 检查更新设定
# 该选项用于插件判断是否要检查更新,若您不希望插件检查更新并提示您,可以选择关闭。
# 检查更新为异步操作,绝不会影响性能与使用体验。
check-update: true

# 存储相关配置
# 注意:存储配置不会通过重载指令生效,如有修改请重新启动服务器。
storage:
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/ReleasesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import cc.carm.lib.githubreleases4j.GithubRelease;
import cc.carm.lib.githubreleases4j.GithubReleases4J;
import org.junit.Test;

import java.util.List;

public class ReleasesTest {

@Test
public void onTest() {

List<GithubRelease> releases = GithubReleases4J.listReleases("CarmJos", "UltraDepository");

for (GithubRelease release : releases) {
System.out.println("#" + release.getID() + " (:" + release.getTagName() + ")" + " " + release.getName());
System.out.println("- " + release.getHTMLUrl());
}

}
}

0 comments on commit a427db3

Please sign in to comment.