forked from OneLiteFeatherNET/BetterGoPaint
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
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
62 changes: 62 additions & 0 deletions
62
src/main/java/net/thenextlvl/gopaint/version/VersionChecker.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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.thenextlvl.gopaint.version; | ||
|
||
import me.mrafonso.hangar4j.HangarClient; | ||
import me.mrafonso.hangar4j.impl.Platform; | ||
import me.mrafonso.hangar4j.impl.version.HangarVersion; | ||
import org.bukkit.Bukkit; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
public class VersionChecker { | ||
private final HangarClient hangarClient = new HangarClient(null); | ||
|
||
public void retrieveLatestSupportedVersion(Consumer<Optional<Version>> success) { | ||
Objects.requireNonNull(hangarClient.getVersions("goPaintAdvanced")) | ||
.thenAcceptAsync(versions -> success.accept(versions.result().stream() | ||
.filter(this::isSupported) | ||
.map(Version::parse) | ||
.filter(Objects::nonNull) | ||
.max(Version::compareTo))); | ||
} | ||
|
||
private boolean isSupported(HangarVersion version) { | ||
return version.platformDependencies().get(Platform.PAPER) | ||
.contains(Bukkit.getMinecraftVersion()); | ||
} | ||
|
||
public record Version(int major, int minor, int build) implements Comparable<Version> { | ||
|
||
public static @Nullable Version parse(HangarVersion version) { | ||
return parse(version.name()); | ||
} | ||
|
||
public static @Nullable Version parse(String string) { | ||
try { | ||
var split = string.split("\\.", 3); | ||
return new Version( | ||
Integer.parseInt(split[0]), | ||
Integer.parseInt(split[1]), | ||
Integer.parseInt(split[2]) | ||
); | ||
} catch (Exception ignored) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public int compareTo(@NotNull Version version) { | ||
return major() != version.major() ? Integer.compare(major(), version.major()) | ||
: minor() != version.minor() ? Integer.compare(minor(), version.minor()) | ||
: Integer.compare(build(), version.build()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return major() + "." + minor() + "." + build(); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/net/thenextlvl/gopaint/version/package-info.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@TypesAreNotNullByDefault | ||
@FieldsAreNotNullByDefault | ||
@MethodsReturnNotNullByDefault | ||
@ParametersAreNotNullByDefault | ||
package net.thenextlvl.gopaint.version; | ||
|
||
import core.annotation.FieldsAreNotNullByDefault; | ||
import core.annotation.MethodsReturnNotNullByDefault; | ||
import core.annotation.ParametersAreNotNullByDefault; | ||
import core.annotation.TypesAreNotNullByDefault; |