Skip to content

Commit

Permalink
added update notification
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Jul 21, 2024
1 parent df1c72c commit 00c71a9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/net/thenextlvl/gopaint/GoPaintPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.thenextlvl.gopaint.listener.ConnectListener;
import net.thenextlvl.gopaint.listener.InteractListener;
import net.thenextlvl.gopaint.listener.InventoryListener;
import net.thenextlvl.gopaint.version.VersionChecker;
import org.bstats.bukkit.Metrics;
import org.bukkit.Axis;
import org.bukkit.Bukkit;
Expand All @@ -36,6 +37,7 @@
import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

@Accessors(fluent = true)
Expand Down Expand Up @@ -68,12 +70,30 @@ public class GoPaintPlugin extends JavaPlugin implements GoPaintProvider {
.create()
).validate().save();

private final VersionChecker versionChecker = new VersionChecker();
private final Metrics metrics = new Metrics(this, 22279);

public GoPaintPlugin() {
registerServices();
}

@Override
@SuppressWarnings("UnstableApiUsage")
public void onLoad() {
versionChecker.retrieveLatestSupportedVersion(latest -> latest.ifPresent(version -> {
var running = VersionChecker.Version.parse(getPluginMeta().getVersion());
if (version.equals(running)) {
getComponentLogger().info("You are running the latest version of goPaintAdvanced");
} else if (version.compareTo(Objects.requireNonNull(running)) > 0) {
getComponentLogger().warn("An update for goPaintAdvanced is available");
getComponentLogger().warn("You are running version {}, the latest supported version is {}", running, version);
getComponentLogger().warn("Update at https://modrinth.com/plugin/gopaintadvanced or https://hangar.papermc.io/TheNextLvl/goPaintAdvanced");
} else {
getComponentLogger().warn("You are running a snapshot version of goPaintAdvanced");
}
}));
}

@Override
public void onEnable() {
registerListeners();
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/net/thenextlvl/gopaint/version/VersionChecker.java
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 src/main/java/net/thenextlvl/gopaint/version/package-info.java
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;

0 comments on commit 00c71a9

Please sign in to comment.