Skip to content

Commit a64bfa4

Browse files
committed
From now on, updates are checked when the administrator joins the server.
1 parent 4770e75 commit a64bfa4

5 files changed

Lines changed: 87 additions & 49 deletions

File tree

src/main/java/com/github/imdmk/automessage/AutoMessage.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.github.imdmk.automessage.notification.task.AutoNotificationTask;
2424
import com.github.imdmk.automessage.scheduler.TaskScheduler;
2525
import com.github.imdmk.automessage.scheduler.TaskSchedulerImpl;
26+
import com.github.imdmk.automessage.update.UpdateListener;
2627
import com.github.imdmk.automessage.update.UpdateService;
2728
import com.github.imdmk.automessage.util.DurationUtil;
2829
import com.google.common.base.Stopwatch;
@@ -45,6 +46,7 @@
4546
import java.time.Duration;
4647
import java.util.concurrent.TimeUnit;
4748
import java.util.logging.Logger;
49+
import java.util.stream.Stream;
4850

4951
public class AutoMessage {
5052

@@ -54,6 +56,8 @@ public class AutoMessage {
5456

5557
private final BossBarAudienceManager bossBarAudienceManager;
5658

59+
private final UpdateService updateService;
60+
5761
private final BukkitAudiences bukkitAudiences;
5862
private final NotificationSender notificationSender;
5963

@@ -75,6 +79,9 @@ public AutoMessage(Plugin plugin) {
7579
/* Managers */
7680
this.bossBarAudienceManager = new BossBarAudienceManager();
7781

82+
/* Services */
83+
this.updateService = new UpdateService(plugin.getDescription());
84+
7885
/* Adventure */
7986
this.bukkitAudiences = BukkitAudiences.create(plugin);
8087
this.notificationSender = new NotificationSender(this.bukkitAudiences, this.bossBarAudienceManager);
@@ -85,17 +92,16 @@ public AutoMessage(Plugin plugin) {
8592
this.taskScheduler.runTimerAsync(new AutoNotificationTask(this.pluginConfiguration.notificationConfiguration, this.notificationSender), 10L, DurationUtil.toTicks(this.pluginConfiguration.notificationConfiguration.autoMessagesDelay));
8693
this.taskScheduler.runTimerAsync(new BossBarAudienceTask(this.bossBarAudienceManager), 0L, DurationUtil.toTicks(Duration.ofSeconds(1)));
8794

95+
/* Listeners */
96+
Stream.of(
97+
new UpdateListener(this.pluginConfiguration, this.notificationSender, this.updateService, this.taskScheduler)
98+
).forEach(listener -> this.server.getPluginManager().registerEvents(listener, plugin));
99+
88100
/* Commands */
89101
if (this.pluginConfiguration.commandConfiguration.autoMessageEnabled) {
90102
this.liteCommands = this.registerLiteCommands();
91103
}
92104

93-
/* Update service */
94-
if (this.pluginConfiguration.checkForUpdate) {
95-
UpdateService updateService = new UpdateService(plugin.getDescription(), logger);
96-
this.taskScheduler.runLaterAsync(updateService::check, DurationUtil.toTicks(Duration.ofSeconds(3)));
97-
}
98-
99105
/* Metrics */
100106
this.metrics = new Metrics((JavaPlugin) plugin, 19487);
101107

src/main/java/com/github/imdmk/automessage/configuration/PluginConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class PluginConfiguration extends OkaeriConfig {
99

10-
@Comment("# Check if you are using the latest version when enabling the plugin?")
10+
@Comment("# Check if you are using the latest version when joining the server?")
1111
public boolean checkForUpdate = true;
1212

1313
@Comment("# Command configuration")
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.imdmk.automessage.update;
2+
3+
import com.eternalcode.gitcheck.GitCheckResult;
4+
import com.eternalcode.gitcheck.git.GitException;
5+
import com.eternalcode.gitcheck.git.GitRelease;
6+
import com.github.imdmk.automessage.configuration.PluginConfiguration;
7+
import com.github.imdmk.automessage.notification.Notification;
8+
import com.github.imdmk.automessage.notification.NotificationSender;
9+
import com.github.imdmk.automessage.notification.implementation.ChatNotification;
10+
import com.github.imdmk.automessage.scheduler.TaskScheduler;
11+
import org.bukkit.entity.Player;
12+
import org.bukkit.event.EventHandler;
13+
import org.bukkit.event.Listener;
14+
import org.bukkit.event.player.PlayerJoinEvent;
15+
16+
public class UpdateListener implements Listener {
17+
18+
private static final String DOUBLE_JUMP_PREFIX = "<dark_gray>[<red>DoubleJump<dark_gray>]";
19+
private static final String UPDATE_AVAILABLE = DOUBLE_JUMP_PREFIX + " <yellow>A new version is available: %s\n<yellow>Download it here: %s";
20+
private static final String UPDATE_EXCEPTION = DOUBLE_JUMP_PREFIX + "<red>An error occurred while checking for update: %s";
21+
22+
private final PluginConfiguration pluginConfiguration;
23+
private final NotificationSender notificationSender;
24+
private final UpdateService updateService;
25+
private final TaskScheduler taskScheduler;
26+
27+
public UpdateListener(PluginConfiguration pluginConfiguration, NotificationSender notificationSender, UpdateService updateService, TaskScheduler taskScheduler) {
28+
this.pluginConfiguration = pluginConfiguration;
29+
this.notificationSender = notificationSender;
30+
this.updateService = updateService;
31+
this.taskScheduler = taskScheduler;
32+
}
33+
34+
@EventHandler
35+
public void onPlayerJoin(PlayerJoinEvent event) {
36+
if (!this.pluginConfiguration.checkForUpdate) {
37+
return;
38+
}
39+
40+
Player player = event.getPlayer();
41+
42+
if (!player.isOp()) {
43+
return;
44+
}
45+
46+
this.taskScheduler.runAsync(() -> this.checkForUpdate(player));
47+
}
48+
49+
private void checkForUpdate(Player player) {
50+
try {
51+
GitCheckResult gitCheckResult = this.updateService.check();
52+
if (gitCheckResult.isUpToDate()) {
53+
return;
54+
}
55+
56+
GitRelease latestRelease = gitCheckResult.getLatestRelease();
57+
58+
String message = UPDATE_AVAILABLE.formatted(latestRelease.getTag(), latestRelease.getPageUrl());
59+
Notification updateAvailableNotification = new ChatNotification(message);
60+
61+
this.notificationSender.sendNotification(player, updateAvailableNotification);
62+
}
63+
catch (GitException gitException) {
64+
String message = UPDATE_EXCEPTION.formatted(gitException.getMessage());
65+
Notification updateExceptionNotification = new ChatNotification(message);
66+
67+
this.notificationSender.sendNotification(player, updateExceptionNotification);
68+
}
69+
}
70+
}

src/main/java/com/github/imdmk/automessage/update/UpdateService.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,25 @@
22

33
import com.eternalcode.gitcheck.GitCheck;
44
import com.eternalcode.gitcheck.GitCheckResult;
5-
import com.eternalcode.gitcheck.git.GitException;
6-
import com.eternalcode.gitcheck.git.GitRelease;
75
import com.eternalcode.gitcheck.git.GitRepository;
86
import com.eternalcode.gitcheck.git.GitTag;
9-
import com.github.imdmk.automessage.util.AnsiColor;
107
import org.bukkit.plugin.PluginDescriptionFile;
118

12-
import java.util.logging.Level;
13-
import java.util.logging.Logger;
14-
159
public class UpdateService {
1610

1711
private static final GitRepository GIT_REPOSITORY = GitRepository.of("imDMK", "AutoMessage");
1812

1913
private final GitCheck gitCheck;
2014
private final PluginDescriptionFile pluginDescriptionFile;
21-
private final Logger logger;
2215

23-
public UpdateService(PluginDescriptionFile pluginDescriptionFile, Logger logger) {
16+
public UpdateService(PluginDescriptionFile pluginDescriptionFile) {
2417
this.gitCheck = new GitCheck();
2518
this.pluginDescriptionFile = pluginDescriptionFile;
26-
this.logger = logger;
2719
}
2820

29-
public void check() {
30-
this.logger.info("Checking for update...");
31-
32-
try {
33-
GitTag gitTag = GitTag.of("v" + this.pluginDescriptionFile.getVersion());
34-
GitCheckResult checkResult = this.gitCheck.checkRelease(GIT_REPOSITORY, gitTag);
35-
36-
if (checkResult.isUpToDate()) {
37-
this.logger.info(AnsiColor.GREEN + "You are using latest version - Thank you :)" + AnsiColor.RESET);
38-
}
39-
else {
40-
GitRelease latestRelease = checkResult.getLatestRelease();
21+
public GitCheckResult check() {
22+
GitTag gitTag = GitTag.of("v" + this.pluginDescriptionFile.getVersion());
4123

42-
this.logger.info(AnsiColor.YELLOW + "A new version is available: " + latestRelease.getTag() + AnsiColor.RESET);
43-
this.logger.info(AnsiColor.YELLOW + "Download it here: " + latestRelease.getPageUrl() + AnsiColor.RESET);
44-
}
45-
}
46-
catch (GitException gitException) {
47-
this.logger.log(Level.SEVERE, AnsiColor.RED + "An error occurred while checking for update: " + gitException.getMessage() + AnsiColor.RESET);
48-
}
24+
return this.gitCheck.checkRelease(GIT_REPOSITORY, gitTag);
4925
}
5026
}

src/main/java/com/github/imdmk/automessage/util/AnsiColor.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)