Skip to content

Commit 337eb7d

Browse files
authored
GH-278 Move updater to eternalcode-commons library (#287)
* Move updater to eternalcode-commons library * Use gson 2.11.0 for WorldGuard and update gradle.properties to latest standards
1 parent b02231b commit 337eb7d

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ object Versions {
1616
const val ADVENTURE_API = "4.24.0"
1717

1818
const val LITE_COMMANDS = "3.10.5"
19-
const val OKAERI_CONFIGS_YAML_BUKKIT = "5.0.6"
2019
const val OKAERI_CONFIGS_SERDES_COMMONS = "5.0.13"
2120
const val OKAERI_CONFIGS_SERDES_BUKKIT = "5.0.13"
2221

23-
const val GIT_CHECK = "1.0.0"
24-
2522
const val CAFFEINE = "3.2.2"
2623

2724
const val B_STATS_BUKKIT = "3.1.0"

eternalcombat-plugin/build.gradle.kts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ plugins {
99
id("xyz.jpenilla.run-paper")
1010
}
1111

12+
configurations.all {
13+
resolutionStrategy {
14+
eachDependency {
15+
if (requested.group == "com.google.code.gson" && requested.name == "gson") {
16+
useVersion("2.11.0")
17+
because("WorldGuard requires strictly gson 2.11.0")
18+
}
19+
}
20+
}
21+
}
22+
1223
dependencies {
1324
implementation(project(":eternalcombat-api"))
1425

@@ -29,9 +40,6 @@ dependencies {
2940
implementation("eu.okaeri:okaeri-configs-serdes-commons:${Versions.OKAERI_CONFIGS_SERDES_COMMONS}")
3041
implementation("eu.okaeri:okaeri-configs-serdes-bukkit:${Versions.OKAERI_CONFIGS_SERDES_BUKKIT}")
3142

32-
// GitCheck
33-
implementation("com.eternalcode:gitcheck:${Versions.GIT_CHECK}")
34-
3543
// bstats
3644
implementation("org.bstats:bstats-bukkit:${Versions.B_STATS_BUKKIT}")
3745

@@ -42,6 +50,7 @@ dependencies {
4250
implementation("com.eternalcode:eternalcode-commons-bukkit:${Versions.ETERNALCODE_COMMONS}")
4351
implementation("com.eternalcode:eternalcode-commons-shared:${Versions.ETERNALCODE_COMMONS}")
4452
implementation("com.eternalcode:eternalcode-commons-folia:${Versions.ETERNALCODE_COMMONS}")
53+
implementation("com.eternalcode:eternalcode-commons-updater:${Versions.ETERNALCODE_COMMONS}")
4554

4655
// worldguard
4756
compileOnly("com.sk89q.worldguard:worldguard-bukkit:${Versions.WORLD_GUARD_BUKKIT}")
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.eternalcode.combat.updater;
22

33
import com.eternalcode.combat.config.implementation.PluginConfig;
4+
import com.eternalcode.commons.concurrent.FutureHandler;
45
import net.kyori.adventure.audience.Audience;
56
import net.kyori.adventure.platform.AudienceProvider;
67
import net.kyori.adventure.text.minimessage.MiniMessage;
@@ -9,12 +10,9 @@
910
import org.bukkit.event.Listener;
1011
import org.bukkit.event.player.PlayerJoinEvent;
1112

12-
import java.util.concurrent.CompletableFuture;
13-
1413
public class UpdaterNotificationController implements Listener {
1514

1615
private static final String NEW_VERSION_AVAILABLE = "<b><gradient:#8a1212:#fc6b03>EternalCombat:</gradient></b> <color:#fce303>New version of EternalCombat is available, please update!";
17-
private static final String RECEIVE_UPDATES_PERMISSION = "eternalcombat.receiveupdates";
1816

1917
private final UpdaterService updaterService;
2018
private final PluginConfig pluginConfig;
@@ -33,23 +31,21 @@ void onJoin(PlayerJoinEvent event) {
3331
Player player = event.getPlayer();
3432
Audience audience = this.audienceProvider.player(player.getUniqueId());
3533

36-
if (!player.hasPermission(RECEIVE_UPDATES_PERMISSION) || !this.pluginConfig.settings.notifyAboutUpdates) {
34+
if (!shouldNotify(player)) {
3735
return;
3836
}
3937

40-
CompletableFuture<Boolean> upToDate = this.updaterService.isUpToDate();
41-
42-
upToDate.whenComplete((isUpToDate, throwable) -> {
43-
if (throwable != null) {
44-
throwable.printStackTrace();
45-
46-
return;
47-
}
38+
this.updaterService.checkForUpdate()
39+
.thenAccept(result -> {
40+
if (result.isUpdateAvailable()) {
41+
audience.sendMessage(this.miniMessage.deserialize(NEW_VERSION_AVAILABLE));
42+
}
43+
})
44+
.exceptionally(FutureHandler::handleException);
45+
}
4846

49-
if (!isUpToDate) {
50-
audience.sendMessage(this.miniMessage.deserialize(NEW_VERSION_AVAILABLE));
51-
}
52-
});
47+
private boolean shouldNotify(Player player) {
48+
return player.hasPermission("eternalcombat.receiveupdates") && this.pluginConfig.settings.notifyAboutUpdates;
5349
}
5450

5551
}
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
package com.eternalcode.combat.updater;
22

3-
import com.eternalcode.gitcheck.GitCheck;
4-
import com.eternalcode.gitcheck.GitCheckResult;
5-
import com.eternalcode.gitcheck.git.GitRepository;
6-
import com.eternalcode.gitcheck.git.GitTag;
7-
import dev.rollczi.litecommands.shared.Lazy;
3+
import com.eternalcode.commons.updater.UpdateResult;
4+
import com.eternalcode.commons.updater.Version;
5+
import com.eternalcode.commons.updater.impl.ModrinthUpdateChecker;
6+
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
7+
import com.github.benmanes.caffeine.cache.Caffeine;
88
import org.bukkit.plugin.PluginDescriptionFile;
99

10+
import java.time.Duration;
1011
import java.util.concurrent.CompletableFuture;
1112

1213
public class UpdaterService {
1314

14-
private static final GitRepository GIT_REPOSITORY = GitRepository.of("EternalCodeTeam", "EternalCombat");
15+
private static final String MODRINTH_PROJECT_ID = "EternalCombat";
16+
private static final String CACHE_KEY = "modrinth-update";
1517

16-
private final GitCheck gitCheck = new GitCheck();
17-
private final Lazy<GitCheckResult> gitCheckResult;
18+
private final AsyncLoadingCache<String, UpdateResult> updateCache;
1819

1920
public UpdaterService(PluginDescriptionFile descriptionFile) {
20-
this.gitCheckResult = new Lazy<>(() -> {
21-
String version = descriptionFile.getVersion();
21+
Version currentVersion = new Version(descriptionFile.getVersion());
22+
ModrinthUpdateChecker updateChecker = new ModrinthUpdateChecker();
2223

23-
return this.gitCheck.checkRelease(GIT_REPOSITORY, GitTag.of("v" + version));
24-
});
24+
this.updateCache = Caffeine.newBuilder()
25+
.expireAfterWrite(Duration.ofHours(1))
26+
.buildAsync(key -> updateChecker.check(MODRINTH_PROJECT_ID, currentVersion));
2527
}
2628

27-
public CompletableFuture<Boolean> isUpToDate() {
28-
return CompletableFuture.supplyAsync(() -> {
29-
GitCheckResult result = this.gitCheckResult.get();
30-
31-
return result.isUpToDate();
32-
});
29+
CompletableFuture<UpdateResult> checkForUpdate() {
30+
return this.updateCache.get(CACHE_KEY);
3331
}
3432

35-
}
33+
}

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
org.gradle.configuration-cache=true
2-
org.gradle.caching=true
31
org.gradle.parallel=true
4-
org.gradle.vfs.watch=false
2+
org.gradle.caching=true
3+
org.gradle.configuration-cache=true
4+
org.gradle.configuration-cache.parallel=true

0 commit comments

Comments
 (0)