Skip to content

Commit

Permalink
Honestly overkill, allows addons to check for updates though
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsinco committed Dec 13, 2024
1 parent 3c581c9 commit 4f1f952
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 128 deletions.
13 changes: 6 additions & 7 deletions src/main/java/com/dre/brewery/BreweryPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import com.dre.brewery.storage.StorageInitException;
import com.dre.brewery.utility.Logging;
import com.dre.brewery.utility.MinecraftVersion;
import com.dre.brewery.utility.UpdateChecker;
import com.dre.brewery.utility.releases.ReleaseChecker;
import com.github.Anon8281.universalScheduler.UniversalScheduler;
import com.github.Anon8281.universalScheduler.scheduling.schedulers.TaskScheduler;
import io.papermc.lib.PaperLib;
Expand All @@ -76,8 +76,6 @@
@Getter
public class BreweryPlugin extends JavaPlugin {

private static final int RESOURCE_ID = 114777;

private @Getter static AddonManager addonManager;
private @Getter static TaskScheduler scheduler;
private @Getter static BreweryPlugin instance;
Expand Down Expand Up @@ -202,16 +200,17 @@ public void onEnable() {
placeholderAPIHook.getInstance().register();
}

if (config.isUpdateCheck()) {
UpdateChecker.run(RESOURCE_ID);
}

Logging.log("Using scheduler&7: &a" + scheduler.getClass().getSimpleName());
Logging.log("Environment&7: &a" + Logging.getEnvironmentAsString());
if (!PaperLib.isPaper()) {
Logging.log("&aBreweryX performs best on Paper-based servers. Please consider switching to Paper for the best experience. &7https://papermc.io");
}
Logging.log("BreweryX enabled!");

ReleaseChecker releaseChecker = ReleaseChecker.getInstance();
releaseChecker.checkForUpdate().thenAccept(updateAvailable -> {
releaseChecker.notify(Bukkit.getConsoleSender());
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.dre.brewery.commands.SubCommand;
import com.dre.brewery.configuration.files.Lang;
import com.dre.brewery.utility.Logging;
import com.dre.brewery.utility.UpdateChecker;
import com.dre.brewery.utility.releases.ReleaseChecker;
import org.bukkit.command.CommandSender;

import java.util.List;
Expand All @@ -45,7 +45,9 @@ public void execute(BreweryPlugin breweryPlugin, Lang lang, CommandSender sender
}
}

Logging.msg(sender, "&2BreweryX version&7: &av" + breweryPlugin.getDescription().getVersion() + " &7(Latest: v" + UpdateChecker.getLatestVersion() + ")");
ReleaseChecker rc = ReleaseChecker.getInstance();

Logging.msg(sender, "&2BreweryX version&7: &av" + rc.localVersion() + " &7(Latest: v" + rc.getResolvedLatestVersion() + ")");
Logging.msg(sender, "&2Original authors&7: &aGrafe, TTTheKing, Sn0wStorm");
Logging.msg(sender, "&dBreweryX authors&7: &aJsinco, Mitality, Nadwey, Szarkans, Vutka1");
Logging.msg(sender, "&2Loaded addons&7: &a" + addonString);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/dre/brewery/configuration/files/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package com.dre.brewery.configuration.files;

import com.dre.brewery.BreweryPlugin;
import com.dre.brewery.configuration.AbstractOkaeriConfigFile;
import com.dre.brewery.configuration.annotation.DefaultCommentSpace;
import com.dre.brewery.configuration.annotation.Footer;
Expand All @@ -34,6 +33,7 @@
import com.dre.brewery.configuration.sector.capsule.ConfiguredDataManager;
import com.dre.brewery.storage.DataManagerType;
import com.dre.brewery.utility.Logging;
import com.dre.brewery.utility.releases.ReleaseChecker;
import eu.okaeri.configs.annotation.Header;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -64,7 +64,7 @@ public void onFirstCreation() {
private String language = "en";

@LocalizedComment("config.updateCheck")
private boolean updateCheck = true;
private ReleaseChecker.ReleaseCheckerType resolveUpdatesFrom = ReleaseChecker.ReleaseCheckerType.GITHUB;

@LocalizedComment("config.autosave")
private int autosave = 10;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/dre/brewery/listeners/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.dre.brewery.configuration.files.Lang;
import com.dre.brewery.utility.*;
import com.dre.brewery.utility.MaterialUtil;
import com.dre.brewery.utility.releases.ReleaseChecker;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Sound;
Expand Down Expand Up @@ -299,11 +300,11 @@ public void onPlayerLogin(PlayerLoginEvent event) {

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
UpdateChecker.notify(event.getPlayer());
BPlayer bplayer = BPlayer.get(event.getPlayer());
if (bplayer != null) {
bplayer.join(event.getPlayer());
}
ReleaseChecker.getInstance().notify(event.getPlayer());
}

@EventHandler
Expand Down
102 changes: 0 additions & 102 deletions src/main/java/com/dre/brewery/utility/UpdateChecker.java

This file was deleted.

109 changes: 109 additions & 0 deletions src/main/java/com/dre/brewery/utility/releases/ReleaseChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* BreweryX Bukkit-Plugin for an alternate brewing process
* Copyright (C) 2024 The Brewery Team
*
* This file is part of BreweryX.
*
* BreweryX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BreweryX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BreweryX. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.dre.brewery.utility.releases;

import com.dre.brewery.BreweryPlugin;
import com.dre.brewery.configuration.ConfigManager;
import com.dre.brewery.configuration.files.Config;
import com.dre.brewery.configuration.files.Lang;
import com.dre.brewery.utility.releases.impl.GitHubReleaseChecker;
import com.dre.brewery.utility.releases.impl.NoImplReleaseChecker;
import com.dre.brewery.utility.releases.impl.SpigotReleaseChecker;
import lombok.Getter;
import org.bukkit.command.CommandSender;

import java.util.concurrent.CompletableFuture;

@Getter
public abstract class ReleaseChecker {

protected static final String CONST_UNRESOLVED = "Unresolved";
private static ReleaseChecker instance;

protected String resolvedLatestVersion = CONST_UNRESOLVED; // Latest version of BX resolved from the source


public abstract CompletableFuture<String> resolveLatest();

public abstract CompletableFuture<Boolean> checkForUpdate();


public void notify(CommandSender receiver) {
if (receiver.hasPermission("brewery.update") && isUpdateAvailable()) {
ConfigManager.getConfig(Lang.class)
.sendEntry(receiver, "Etc_UpdateAvailable", "v" + localVersion(), "v" + resolvedLatestVersion);
}
}


public boolean isUpdateAvailable() {
if (resolvedLatestVersion.equals(CONST_UNRESOLVED)) {
return false;
}
int local = parseVersion(localVersion());
int resolved = parseVersion(resolvedLatestVersion);
return resolved > local;
}


// Singleton

public static ReleaseChecker getInstance() {
if (instance != null) {
return instance;
}
Config config = ConfigManager.getConfig(Config.class);
switch (config.getResolveUpdatesFrom()) {
case GITHUB -> instance = new GitHubReleaseChecker("BreweryTeam", "BreweryX");
case SPIGOT -> instance = new SpigotReleaseChecker(114777);
case NONE -> instance = new NoImplReleaseChecker();
}
return instance;
}


// Util
public String localVersion() {
String versionString = BreweryPlugin.getInstance().getDescription().getVersion();
if (versionString.contains(";")) {
// I don't care about the branch
return versionString.split(";")[0];
}
return versionString;
}

public int parseVersion(String version) {
StringBuilder sb = new StringBuilder();
for (char c : version.toCharArray()) {
if (Character.isDigit(c)) {
sb.append(c);
}
}
return Integer.parseInt(sb.toString());
}


public enum ReleaseCheckerType {
GITHUB,
SPIGOT,
NONE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* BreweryX Bukkit-Plugin for an alternate brewing process
* Copyright (C) 2024 The Brewery Team
*
* This file is part of BreweryX.
*
* BreweryX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BreweryX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BreweryX. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.dre.brewery.utility.releases.impl;

import com.dre.brewery.utility.Logging;
import com.dre.brewery.utility.releases.ReleaseChecker;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;

public class GitHubReleaseChecker extends ReleaseChecker {

private static final String CONST_URL = "https://api.github.com/repos/%s/%s/releases/latest";
private static final String CONST_JSON_FIELD = "tag_name";

private final String link;

public GitHubReleaseChecker(String owner, String repo) {
this.link = String.format(CONST_URL, owner, repo);
}

@Override
public CompletableFuture<String> resolveLatest() {
if (resolvedLatestVersion != null) {
return CompletableFuture.completedFuture(resolvedLatestVersion);
}
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(link))
.GET()
.build();

return CompletableFuture.supplyAsync(() -> {
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject();
this.resolvedLatestVersion = jsonResponse.get(CONST_JSON_FIELD).getAsString();
return this.resolvedLatestVersion;
} catch (IOException | InterruptedException e) {
Logging.warningLog("Failed to resolve latest BreweryX version from GitHub. (No connection?)");
return CONST_UNRESOLVED;
}
});
}

@Override
public CompletableFuture<Boolean> checkForUpdate() {
return resolveLatest().thenApply(v -> {
if (v.equals(CONST_UNRESOLVED)) {
return false;
}
return isUpdateAvailable();
});
}
}
Loading

0 comments on commit 4f1f952

Please sign in to comment.