Skip to content

Commit 5619b1c

Browse files
committed
Added plugin updater to notify about updates in the console and for OP users
1 parent 704d43d commit 5619b1c

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

multiworld-bukkit/src/main/java/com/dev7ex/multiworld/MultiWorldPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.dev7ex.multiworld.task.WorldUnloadTask;
1818
import com.dev7ex.multiworld.translation.DefaultTranslationProvider;
1919
import com.dev7ex.multiworld.user.UserProvider;
20+
import com.dev7ex.multiworld.util.PluginUpdater;
2021
import com.dev7ex.multiworld.world.DefaultWorldConfiguration;
2122
import com.dev7ex.multiworld.world.DefaultWorldManager;
2223
import com.dev7ex.multiworld.world.DefaultWorldProvider;
@@ -43,6 +44,7 @@ public final class MultiWorldPlugin extends BukkitPlugin implements MultiWorldBu
4344
private DefaultWorldConfiguration worldConfiguration;
4445

4546
private final WorldCommand worldCommand = new WorldCommand(this);
47+
private final PluginUpdater updater = new PluginUpdater(this);
4648

4749
private DefaultWorldManager worldManager;
4850

@@ -73,6 +75,8 @@ public void onLoad() {
7375
public void onEnable() {
7476
super.getServer().getServicesManager().register(MultiWorldBukkitApi.class, this, this, ServicePriority.Normal);
7577

78+
this.updater.checkAsync();
79+
7680
MultiWorldApiProvider.registerApi(this);
7781
}
7882

multiworld-bukkit/src/main/java/com/dev7ex/multiworld/listener/player/PlayerConnectionListener.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dev7ex.multiworld.listener.player;
22

33
import com.dev7ex.common.collect.map.ParsedMap;
4+
import com.dev7ex.multiworld.MultiWorldPlugin;
45
import com.dev7ex.multiworld.api.bukkit.MultiWorldBukkitApi;
56
import com.dev7ex.multiworld.api.bukkit.event.MultiWorldListener;
67
import com.dev7ex.multiworld.api.bukkit.event.user.WorldUserLoginEvent;
@@ -10,7 +11,10 @@
1011
import com.dev7ex.multiworld.api.user.WorldUserProperty;
1112
import com.dev7ex.multiworld.user.User;
1213
import com.dev7ex.multiworld.user.UserConfiguration;
14+
import com.dev7ex.multiworld.util.Colored;
15+
import com.dev7ex.multiworld.util.PluginUpdater;
1316
import org.bukkit.Bukkit;
17+
import org.bukkit.ChatColor;
1418
import org.bukkit.entity.Player;
1519
import org.bukkit.event.EventHandler;
1620
import org.bukkit.event.EventPriority;
@@ -68,6 +72,13 @@ public void handlePlayerLogin(final PlayerLoginEvent event) {
6872
@EventHandler(priority = EventPriority.NORMAL)
6973
public void handlePlayerJoin(final PlayerJoinEvent event) {
7074
final Player player = event.getPlayer();
75+
final PluginUpdater updater = MultiWorldPlugin.getInstance().getUpdater();
76+
77+
if ((updater.isUpdateAvailable()) && (player.isOp())) {
78+
player.sendMessage(super.getConfiguration().getPrefix() + ChatColor.GRAY + " A new update for MultiWorld is available");
79+
player.sendMessage(super.getConfiguration().getPrefix() + ChatColor.GRAY + " Please note that if you do not do the update then there may be problems or individual functions may be disabled");
80+
player.sendMessage(super.getConfiguration().getPrefix() + ChatColor.GRAY + " https://modrinth.com/plugin/multiworld-bukkit");
81+
}
7182
}
7283

7384
/**
@@ -79,7 +90,9 @@ public void handlePlayerJoin(final PlayerJoinEvent event) {
7990
@EventHandler(priority = EventPriority.NORMAL)
8091
public void handlePlayerQuit(final PlayerQuitEvent event) {
8192
final Player player = event.getPlayer();
82-
final BukkitWorldUser user = super.getUserProvider().getUser(player.getUniqueId()).orElseThrow();
93+
final BukkitWorldUser user = super.getUserProvider()
94+
.getUser(player.getUniqueId())
95+
.orElseThrow();
8396

8497
super.getUserProvider().saveUser(user, WorldUserProperty.LAST_LOGIN,
8598
WorldUserProperty.LAST_LOCATION);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.dev7ex.multiworld.util;
2+
3+
import com.dev7ex.common.bukkit.plugin.BukkitPlugin;
4+
import lombok.AccessLevel;
5+
import lombok.Getter;
6+
import org.bukkit.util.Consumer;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.net.URL;
12+
import java.util.Scanner;
13+
14+
/**
15+
* @author Dev7ex
16+
* @since 05.11.2024
17+
*/
18+
@Getter(AccessLevel.PUBLIC)
19+
public class PluginUpdater {
20+
21+
private final BukkitPlugin plugin;
22+
private boolean updateAvailable = false;
23+
private String newVersion;
24+
25+
public PluginUpdater(@NotNull final BukkitPlugin plugin) {
26+
this.plugin = plugin;
27+
}
28+
29+
public void checkAsync() {
30+
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
31+
try (final InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.plugin.getPluginIdentification().spigotResourceId()).openStream()) {
32+
try (final Scanner scanner = new Scanner(inputStream)) {
33+
final String currentVersion = this.plugin.getDescription().getVersion();
34+
this.newVersion = scanner.next();
35+
36+
if (!currentVersion.equalsIgnoreCase(this.newVersion)) {
37+
if (this.compareVersions(currentVersion, this.newVersion) == -1) {
38+
this.updateAvailable = true;
39+
this.plugin.getServer().getScheduler().runTask(this.plugin, this::logUpdateMessage);
40+
}
41+
}
42+
}
43+
44+
} catch (final IOException exception) {
45+
this.updateAvailable = false;
46+
this.plugin.getServer().getScheduler().runTask(this.plugin, this::logNoUpdateMessage);
47+
}
48+
});
49+
}
50+
51+
/**
52+
* Compares two versions in the format x.y.z-SNAPSHOT.
53+
*
54+
* @param currentVersion The current version of the plugin.
55+
* @param onlineVersion The latest available version online.
56+
* @return -1 if the current version is older than the online version,
57+
* 1 if the current version is newer,
58+
* 0 if both versions are equal.
59+
*/
60+
public int compareVersions(@NotNull final String currentVersion, @NotNull final String onlineVersion) {
61+
final String[] currentParts = currentVersion.replace("-SNAPSHOT", "").split("\\.");
62+
final String[] onlineParts = onlineVersion.replace("-SNAPSHOT", "").split("\\.");
63+
64+
for (int i = 0; i < Math.max(currentParts.length, onlineParts.length); i++) {
65+
int currentPart = (i < currentParts.length) ? Integer.parseInt(currentParts[i]) : 0;
66+
int onlinePart = (i < onlineParts.length) ? Integer.parseInt(onlineParts[i]) : 0;
67+
68+
if (currentPart < onlinePart) {
69+
return -1; // current version is older
70+
} else if (currentPart > onlinePart) {
71+
return 1; // current version is newer
72+
}
73+
}
74+
return 0; // versions are equal
75+
}
76+
77+
public void logUpdateMessage() {
78+
this.plugin.getLogger().info("");
79+
this.plugin.getLogger().info("There is a new update for MultiWorld");
80+
this.plugin.getLogger().info("Please note that if you do not update, some functions may be deactivated.");
81+
this.plugin.getLogger().info("Current Version: " + this.plugin.getDescription().getVersion());
82+
this.plugin.getLogger().info("New Version: " + this.newVersion);
83+
this.plugin.getLogger().info("");
84+
}
85+
86+
public void logNoUpdateMessage() {
87+
this.plugin.getLogger().info("");
88+
this.plugin.getLogger().info("Your MultiWorld version is up to date!");
89+
this.plugin.getLogger().info("Version: " + this.plugin.getDescription().getVersion());
90+
this.plugin.getLogger().info("");
91+
}
92+
93+
}

0 commit comments

Comments
 (0)