Skip to content

Commit

Permalink
Update Checker
Browse files Browse the repository at this point in the history
Created an Update Checker
  • Loading branch information
JustinDevB committed Nov 24, 2021
1 parent db5d3c9 commit 96221a9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/me/justindevb/VulcanReplay/VulcanReplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import me.justindevb.VulcanReplpay.util.UpdateChecker;

public class VulcanReplay extends JavaPlugin {

private HashMap<UUID, PlayerCache> playerCache = new HashMap<>();
Expand All @@ -28,6 +30,13 @@ public void onEnable() {

initBstats();

checkForUpdate();

}

@Override
public void onDisable() {
this.playerCache.clear();
}

private void registerListener() {
Expand Down Expand Up @@ -93,6 +102,7 @@ private void checkVulcanApi() {
*/
private void initConfig() {
FileConfiguration config = getConfig();
config.addDefault("General.Check-Update", true);
config.addDefault("General.Nearby-Range", 30);

List<String> list = new ArrayList<>();
Expand Down Expand Up @@ -164,4 +174,16 @@ public void log(String msg, boolean severe) {
getLogger().log(Level.INFO, msg);
}

private void checkForUpdate() {
if (!getConfig().getBoolean("General.Check-Update"))
return;
new UpdateChecker(this, 97845).getVersion(version -> {
if (this.getDescription().getVersion().equals(version))
log("You are up to date!", false);
else
log("There is an update available! Download at: https://www.spigotmc.org/resources/vulcan-replay.97845/",
true);
});
}

}
36 changes: 36 additions & 0 deletions src/main/java/me/justindevb/VulcanReplpay/util/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.justindevb.VulcanReplpay.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import java.util.function.Consumer;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

//From: https://www.spigotmc.org/wiki/creating-an-update-checker-that-checks-for-updates
public class UpdateChecker {

private final JavaPlugin plugin;
private final int resourceId;

public UpdateChecker(JavaPlugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}

public void getVersion(final Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try (InputStream inputStream = new URL(
"https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream();
Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (IOException exception) {
plugin.getLogger().info("Unable to check for updates: " + exception.getMessage());
}
});
}
}
Binary file modified target/classes/me/justindevb/VulcanReplay/VulcanReplay.class
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven
#Wed Nov 24 09:35:49 CST 2021
#Wed Nov 24 10:02:13 CST 2021
groupId=me.justindevb
artifactId=VulcanReplay
version=1.3
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ me\justindevb\VulcanReplay\DiscordWebhook$EmbedObject.class
me\justindevb\VulcanReplay\VulcanListener$1.class
me\justindevb\VulcanReplay\PlayerListener.class
me\justindevb\VulcanReplay\DiscordWebhook$EmbedObject$Footer.class
me\justindevb\VulcanReplpay\util\UpdateChecker.class
me\justindevb\VulcanReplay\DiscordWebhook$EmbedObject$Field.class
me\justindevb\VulcanReplay\PlayerCache.class
me\justindevb\VulcanReplay\DiscordWebhook$1.class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
C:\Users\Justin\eclipse-workspace\VulcanReplay\src\main\java\me\justindevb\VulcanReplpay\util\UpdateChecker.java
C:\Users\Justin\eclipse-workspace\VulcanReplay\src\main\java\me\justindevb\VulcanReplay\PlayerCache.java
C:\Users\Justin\eclipse-workspace\VulcanReplay\src\main\java\me\justindevb\VulcanReplay\PlayerListener.java
C:\Users\Justin\eclipse-workspace\VulcanReplay\src\main\java\me\justindevb\VulcanReplay\VulcanListener.java
Expand Down
Binary file modified target/original-VulcanReplay-1.3.jar
Binary file not shown.

0 comments on commit 96221a9

Please sign in to comment.