Skip to content

Commit

Permalink
Improve Update Checker
Browse files Browse the repository at this point in the history
Changed errorMessage variables to exceptions
UpdateChecker is now static
  • Loading branch information
stumper66 committed Nov 13, 2024
1 parent 228a7c4 commit c6f99e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,58 +1,24 @@
package io.github.arcaneplugins.blackwidow.lib.cmdblocking;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Scanner;
import java.util.function.Consumer;

/**
* @author stumper66
* @since 1.1.0
*/
public class UpdateChecker {

public UpdateChecker(
final String resourceName
) {
this.resourceName = resourceName;
}

private final String resourceName;
private String errorMessage = null;

public boolean success() {
return errorMessage != null;
}

@Nullable
public String errorMessage() {
return errorMessage;
}

// returns if successful or not
public boolean checkLatestVersion(
final @NotNull Consumer<String> consumer
) {
try (InputStream stream = new URI(
public static void getLatestVersion(final String resourceName, final Consumer<String> consumer)
throws IOException, URISyntaxException {
try (InputStream inputStream = new URI(
"https://hangar.papermc.io/api/v1/projects/" +
resourceName + "/latest?channel=Release")
.toURL().openStream()
) {
final Scanner scanner = new Scanner(stream);
if (scanner.hasNext()) {
.toURL().openStream()){

final Scanner scanner = new Scanner(inputStream);
if (scanner.hasNext()){
consumer.accept(scanner.next());
}
return true;
} catch (FileNotFoundException e) {
errorMessage = "Error checking for latest version, file not found: " + e.getMessage();
} catch (Exception e) {
errorMessage = "Error checking for latest version. " + e.getMessage();
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.scheduler.BukkitTask;
import org.spongepowered.configurate.CommentedConfigurationNode;

import java.io.FileNotFoundException;
import java.io.InvalidObjectException;

/**
Expand Down Expand Up @@ -100,17 +101,17 @@ public void run() {
}

private void checkForLatestVersion() {
// this function
final UpdateChecker updateChecker = new UpdateChecker("BlackWidow");

try {
final boolean result = updateChecker.checkLatestVersion(latestVersion -> {
UpdateChecker.getLatestVersion("BlackWidow", latestVersion -> {
//noinspection deprecation
final String currentVersion = plugin.getDescription().getVersion()
.split(" ")[0];

if (latestVersion == null && logUpdates) {
plugin.getLogger().warning("Error check for latest version, string was null");
if (latestVersion == null) {
if (logUpdates){
plugin.getLogger().warning("Error check for latest version, string was null");
}

return;
}

Expand Down Expand Up @@ -148,10 +149,9 @@ private void checkForLatestVersion() {
notifyPlayers();
}
});

if (!result) {
plugin.getLogger().warning("Error getting latest version: " + updateChecker.errorMessage());
}
} catch (FileNotFoundException e) {
// this exception occurs if the dest URL is incorrect such as a typo in the resource name
plugin.getLogger().warning("Error getting latest version, file not found: " + e.getMessage());
} catch (Exception e) {
plugin.getLogger().warning("Error getting latest version: " + e.getMessage());
}
Expand Down

0 comments on commit c6f99e2

Please sign in to comment.