Skip to content

Commit 64b556c

Browse files
authored
🚧 currentVersion system
1 parent 080c4a9 commit 64b556c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/main/java/io/github/sstudiosdev/BetterPvP.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.bukkit.plugin.java.JavaPlugin;
1717

1818
import java.io.*;
19+
import java.net.URL;
1920
import java.nio.file.Files;
21+
import java.util.Scanner;
2022

2123
/**
2224
* Main class of the BetterPvP plugin.
@@ -31,6 +33,11 @@ public final class BetterPvP extends JavaPlugin {
3133
private VaultHookManager vaultHookManager;
3234
private boolean loadLicenseFile;
3335

36+
private String currentVersion;
37+
private String latestVersion;
38+
39+
private static final String PLUGIN_VERSION = "1.0.0";
40+
3441
/**
3542
* Method called when the plugin is enabled.
3643
*/
@@ -48,6 +55,9 @@ public void onEnable() {
4855
// Display information in the console
4956
displayConsoleInfo();
5057

58+
currentVersion = PLUGIN_VERSION;
59+
Bukkit.getScheduler().runTaskAsynchronously(this, this::checkForUpdates);
60+
5161
// Copy Apache-2.0 license file from resources to plugin folder
5262
if (mainConfig.getBoolean("load-license-file")) {
5363
try {
@@ -141,4 +151,38 @@ private void sendMessageToConsole(String message) {
141151
Bukkit.getConsoleSender().sendMessage(ChatColorUtil.colorize(message));
142152
}
143153

154+
private void checkForUpdates() {
155+
try {
156+
URL url = new URL("https://api.github.com/repos/Sstudios-Dev/Betterpvp-2.0/releases/latest");
157+
158+
Scanner scanner = new Scanner(url.openStream());
159+
StringBuilder response = new StringBuilder();
160+
while (scanner.hasNext()) {
161+
response.append(scanner.nextLine());
162+
}
163+
scanner.close();
164+
165+
latestVersion = response.toString().split("\"tag_name\":\"")[1].split("\",")[0];
166+
167+
// Notificar si hay una nueva versión disponible
168+
if (isNewVersionAvailable()) {
169+
notifyPlayers();
170+
} else {
171+
getLogger().info("No updates are available. Current version: " + currentVersion);
172+
}
173+
} catch (IOException e) {
174+
getLogger().warning("Error checking for updates: " + e.getMessage());
175+
}
176+
}
177+
178+
private boolean isNewVersionAvailable() {
179+
return latestVersion != null && !latestVersion.equalsIgnoreCase(currentVersion);
180+
}
181+
182+
private void notifyPlayers() {
183+
getLogger().info("A new version of BetterPvP is available! Current version: " + currentVersion + ", Latest version: " + latestVersion);
184+
185+
Bukkit.getOnlinePlayers().forEach(player -> player.sendMessage("A new version of BetterPvP is available! Current version: " + currentVersion + ", Latest version: " + latestVersion));
186+
}
187+
144188
}

0 commit comments

Comments
 (0)