-
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.
Adds a version checker as defined in #3
- Loading branch information
Showing
10 changed files
with
396 additions
and
9 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.github.arcaneplugins.blackwidow.lib.cmdblocking; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.util.Scanner; | ||
import java.util.function.Consumer; | ||
|
||
public class UpdateChecker { | ||
public UpdateChecker(String resourceName){ | ||
this.resourceName = resourceName; | ||
} | ||
|
||
private final String resourceName; | ||
private String errorMessage; | ||
|
||
public boolean hadError(){ | ||
return errorMessage != null; | ||
} | ||
|
||
public String getErrorMessage(){ | ||
return errorMessage; | ||
} | ||
|
||
public boolean getLatestVersion(Consumer<String> consumer){ | ||
|
||
try (InputStream inputStream = new URI( | ||
"https://hangar.papermc.io/api/v1/projects/" + | ||
resourceName + "/latest?channel=Release") | ||
.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
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
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
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
28 changes: 28 additions & 0 deletions
28
.../io/github/arcaneplugins/blackwidow/plugin/bukkit/listener/bukkit/PlayerJoinListener.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,28 @@ | ||
package io.github.arcaneplugins.blackwidow.plugin.bukkit.listener.bukkit; | ||
|
||
import io.github.arcaneplugins.blackwidow.plugin.bukkit.BlackWidow; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
|
||
public class PlayerJoinListener implements Listener { | ||
private final BlackWidow plugin; | ||
|
||
public PlayerJoinListener(final BlackWidow plugin){ | ||
this.plugin = plugin; | ||
} | ||
|
||
@EventHandler(ignoreCancelled = true) | ||
public void handle(final PlayerJoinEvent event){ | ||
if (!plugin.bukkitVersionChecker.getNotifyPlayers()) { | ||
return; | ||
} | ||
|
||
final String message = plugin.bukkitVersionChecker.getNotifyMessage(); | ||
if (message == null){ | ||
return; | ||
} | ||
|
||
event.getPlayer().sendMessage(message); | ||
} | ||
} |
172 changes: 172 additions & 0 deletions
172
...ain/java/io/github/arcaneplugins/blackwidow/plugin/bukkit/logic/BukkitVersionChecker.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,172 @@ | ||
package io.github.arcaneplugins.blackwidow.plugin.bukkit.logic; | ||
|
||
import io.github.arcaneplugins.blackwidow.lib.cmdblocking.UpdateChecker; | ||
import io.github.arcaneplugins.blackwidow.plugin.bukkit.BlackWidow; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.scheduler.BukkitRunnable; | ||
import org.bukkit.scheduler.BukkitTask; | ||
import org.spongepowered.configurate.CommentedConfigurationNode; | ||
|
||
import java.io.InvalidObjectException; | ||
|
||
public class BukkitVersionChecker { | ||
public BukkitVersionChecker(final BlackWidow plugin){ | ||
this.plugin = plugin; | ||
} | ||
|
||
private final BlackWidow plugin; | ||
private BukkitTask notifyTask; | ||
boolean logUpdates; | ||
private boolean notifyPlayers; | ||
private String notifyMessage; | ||
private int lastTimerDuration; | ||
|
||
public void load(final boolean isStartup){ | ||
final CommentedConfigurationNode settings = plugin.settings().root() | ||
.node("cmd-blocking", "update-checker"); | ||
|
||
final boolean enabled = settings.node("enabled").getBoolean(true); | ||
final boolean runOnStartup = settings.node("run-on-startup").getBoolean(true); | ||
final int repeatTimerDuration = settings.node("repeat-timer-duration-mins").getInt(); | ||
logUpdates = settings.node("log-updates").getBoolean(true); | ||
notifyPlayers = settings.node("notify-players-with-permission").getBoolean(true); | ||
|
||
if (!enabled){ | ||
disableChecker(); | ||
return; | ||
} | ||
|
||
startTimerIfNeeded(repeatTimerDuration); | ||
|
||
if (runOnStartup && isStartup){ | ||
getLatestVersion(); | ||
} | ||
} | ||
|
||
public boolean getNotifyPlayers(){ | ||
return notifyPlayers; | ||
} | ||
|
||
public String getNotifyMessage(){ | ||
return notifyMessage; | ||
} | ||
|
||
private void disableChecker(){ | ||
if (notifyTask != null && !notifyTask.isCancelled()){ | ||
notifyTask.cancel(); | ||
notifyTask = null; | ||
} | ||
} | ||
|
||
private void startTimerIfNeeded(final int repeatTimerDuration){ | ||
if (repeatTimerDuration <= 0){ | ||
disableChecker(); | ||
return; | ||
} | ||
|
||
if (notifyTask != null && !notifyTask.isCancelled() | ||
&& repeatTimerDuration == lastTimerDuration){ | ||
return; | ||
} | ||
|
||
disableChecker(); | ||
|
||
final BukkitRunnable runnable = new BukkitRunnable() { | ||
@Override | ||
public void run() { | ||
getLatestVersion(); | ||
} | ||
}; | ||
|
||
lastTimerDuration = repeatTimerDuration; | ||
final long delay = repeatTimerDuration * 20L * 60L; | ||
this.notifyTask = runnable.runTaskTimerAsynchronously(plugin, delay, delay); | ||
} | ||
|
||
private void getLatestVersion(){ | ||
new BukkitRunnable() { | ||
@Override | ||
public void run() { | ||
checkForLatestVersion(); | ||
} | ||
}.runTaskAsynchronously(plugin); | ||
} | ||
|
||
private void checkForLatestVersion(){ | ||
// this function | ||
final UpdateChecker updateChecker = new UpdateChecker("BlackWidow"); | ||
|
||
try{ | ||
updateChecker.getLatestVersion(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"); | ||
return; | ||
} | ||
|
||
final VersionInfo thisVersion; | ||
final VersionInfo hangarVersion; | ||
boolean isOutOfDate; | ||
boolean isNewerVersion = false; | ||
boolean wasUpToDate = false; | ||
|
||
try{ | ||
thisVersion = new VersionInfo(currentVersion); | ||
hangarVersion = new VersionInfo(latestVersion); | ||
|
||
isOutOfDate = (thisVersion.compareTo(hangarVersion) < 0); | ||
isNewerVersion = (thisVersion.compareTo(hangarVersion) > 0); | ||
} | ||
catch (InvalidObjectException e){ | ||
plugin.getLogger().warning("Got exception creating version objects: " + e.getMessage()); | ||
isOutOfDate = !currentVersion.equals(latestVersion); | ||
} | ||
|
||
if (isNewerVersion){ | ||
notifyMessage = "Your BlackWindow version is a pre-release. Latest release version is " + latestVersion + ". (You're running " + currentVersion + ")"; | ||
} | ||
else if (isOutOfDate){ | ||
notifyMessage = "Your BlackWidow version is outdated! Please update to " + latestVersion + " as soon as possible. (You're running " + currentVersion + ")"; | ||
} | ||
else{ | ||
notifyMessage = "Your BlackWidow version is up to date (You're running " + currentVersion + ")"; | ||
wasUpToDate = true; | ||
} | ||
|
||
if (logUpdates){ | ||
plugin.getLogger().info(notifyMessage); | ||
} | ||
|
||
if (!wasUpToDate){ | ||
notifyPlayers(); | ||
} | ||
}); | ||
} | ||
catch (Exception e){ | ||
plugin.getLogger().warning("Error getting latest version: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private void notifyPlayers(){ | ||
if (!notifyPlayers || notifyMessage == null){ | ||
return; | ||
} | ||
|
||
final String requiredPermission = "blackwidow.notifyupdates"; | ||
|
||
for (final Player player : Bukkit.getOnlinePlayers()){ | ||
if (!player.isValid()){ | ||
continue; | ||
} | ||
if (!player.hasPermission(requiredPermission)){ | ||
continue; | ||
} | ||
|
||
player.sendMessage(notifyMessage); | ||
} | ||
} | ||
} |
Oops, something went wrong.