-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed errorMessage variables to exceptions UpdateChecker is now static
- Loading branch information
Showing
2 changed files
with
19 additions
and
53 deletions.
There are no files selected for viewing
52 changes: 9 additions & 43 deletions
52
...owlib/src/main/java/io/github/arcaneplugins/blackwidow/lib/cmdblocking/UpdateChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters