3.6-beta
add force rankup and prestige add update notification on join more reliable legacy server version check added permissions: rankup.admin (grants rankup.checkversion, rankup.reload, rankup.force, and rankup.notify), rankup.notify (receive update notiifications on login), rankup.force (force a player to rankup or prestige)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package sh.okx.rankup.util;
|
||||
|
||||
import com.google.common.base.Suppliers;
|
||||
import java.util.function.Supplier;
|
||||
import org.bukkit.Material;
|
||||
|
||||
public class ItemUtil {
|
||||
private static Supplier<Boolean> flattenedSupplier = Suppliers.memoize(ItemUtil::isServerFlattenedPrivate);
|
||||
|
||||
private static boolean isServerFlattenedPrivate() {
|
||||
try {
|
||||
Material.valueOf("BLACK_STAINED_GLASS_PANE");
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a server is post-flattening or pre-flattening.
|
||||
* The flattening is the name for a process where, instead of using durability to represent
|
||||
* similar items, Mojang decided to use distinct item types for each item.
|
||||
* This caused many {@link Material} names to change, making some things incompatible.
|
||||
* The flattening happened in 1.13.
|
||||
*
|
||||
* @return true if the server is post-flattening (server versions 1.13, 1.14, 1.15) or false if it is pre-flattening (1.12, 1.11, 1.10 etc)
|
||||
*/
|
||||
public static boolean isServerFlattened() {
|
||||
return flattenedSupplier.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package sh.okx.rankup.util;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import sh.okx.rankup.util.VersionChecker.VersionCheckerCallback;
|
||||
|
||||
public class UpdateNotifier {
|
||||
private static final String PREFIX = ChatColor.GREEN + "" + ChatColor.BOLD + "Rankup " + ChatColor.RESET;
|
||||
|
||||
private final VersionChecker checker;
|
||||
|
||||
public UpdateNotifier(VersionChecker checker) {
|
||||
this.checker = checker;
|
||||
}
|
||||
|
||||
public void notify(CommandSender sender, boolean join) {
|
||||
if (!checker.hasChecked() && !join) {
|
||||
send(sender, false, ChatColor.YELLOW + "Checking version...");
|
||||
}
|
||||
checker.checkVersion(new VersionCheckerCallback() {
|
||||
@Override
|
||||
public void onLatestVersion(String version) {
|
||||
if (!join) {
|
||||
send(sender, false, ChatColor.GREEN + "You are on the latest version.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOutdatedVersion(String currentVersion, String latestVersion) {
|
||||
send(sender, join, ChatColor.YELLOW + "A new version is available: " + ChatColor.GOLD + latestVersion
|
||||
+ ChatColor.YELLOW + ". You are on: " + ChatColor.GOLD + currentVersion
|
||||
+ ChatColor.GOLD + "\nhttps://www.spigotmc.org/resources/rankup.17933/");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPreReleaseVersion(String version) {
|
||||
send(sender, join, ChatColor.RED + "You are on a pre-release version.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure() {
|
||||
if (!join) {
|
||||
send(sender, join, ChatColor.RED + "Error while checking version.");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void send(CommandSender sender, boolean prefix, String message) {
|
||||
if (prefix) {
|
||||
sender.sendMessage(PREFIX + message);
|
||||
} else {
|
||||
sender.sendMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package sh.okx.rankup.util;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.CharStreams;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class VersionChecker {
|
||||
|
||||
private static final int RESOURCE_ID = 17933;
|
||||
|
||||
private final Plugin plugin; // used exclusively for scheduling
|
||||
private final String currentVersion;
|
||||
private String latestVersion;
|
||||
private boolean checked = false;
|
||||
|
||||
public VersionChecker(Plugin plugin) {
|
||||
this.currentVersion = plugin.getDescription().getVersion();
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the version checker has already made an asynchronous call to the web server to check
|
||||
* the version, so future checks will run instantly.
|
||||
* @return true if the version checker already knows the latest version, false otherwise
|
||||
*/
|
||||
public boolean hasChecked() {
|
||||
return checked;
|
||||
}
|
||||
|
||||
public void checkVersion(VersionCheckerCallback callback) {
|
||||
if (latestVersion != null) {
|
||||
if (currentVersion.equals(latestVersion)) {
|
||||
callback.onLatestVersion(currentVersion);
|
||||
} else {
|
||||
callback.onOutdatedVersion(currentVersion, latestVersion);
|
||||
}
|
||||
} else {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin,
|
||||
() -> checkVersionSync(new SyncVersionCheckerCallback(plugin, callback)));
|
||||
}
|
||||
}
|
||||
|
||||
private void checkVersionSync(VersionCheckerCallback callback) {
|
||||
if (currentVersion.contains("alpha")
|
||||
|| currentVersion.contains("beta")
|
||||
|| currentVersion.contains("rc")) {
|
||||
checked = true;
|
||||
callback.onPreReleaseVersion(currentVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
latestVersion = getLatestVersion();
|
||||
checked = true;
|
||||
if (currentVersion.equals(latestVersion)) {
|
||||
callback.onLatestVersion(currentVersion);
|
||||
} else {
|
||||
callback.onOutdatedVersion(currentVersion, latestVersion);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
callback.onFailure();
|
||||
}
|
||||
}
|
||||
|
||||
private String getLatestVersion() throws IOException {
|
||||
URL url = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID);
|
||||
return CharStreams.toString(new InputStreamReader(url.openStream(), Charsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* A callback used when a version check runs
|
||||
*/
|
||||
public interface VersionCheckerCallback {
|
||||
|
||||
/**
|
||||
* Called when the plugin is already on the latest version
|
||||
*
|
||||
* @param version the current, and latest, version of the plugin
|
||||
*/
|
||||
void onLatestVersion(String version);
|
||||
|
||||
/**
|
||||
* Called when the plugin is on a version other than the latest on the SpigotMC plugin page.
|
||||
*
|
||||
* @param currentVersion the current version of the plugin specified in plugin.yml
|
||||
* @param latestVersion the latest version of the plugin specified on SpigotMC.
|
||||
*/
|
||||
void onOutdatedVersion(String currentVersion, String latestVersion);
|
||||
|
||||
/**
|
||||
* Called when the plugin is on a pre-release version and is exempt to the usual update system.
|
||||
* @param version the current version of the plugin
|
||||
*/
|
||||
void onPreReleaseVersion(String version);
|
||||
|
||||
/**
|
||||
* Called when the version checker was unable to retrieve the latest version
|
||||
*/
|
||||
void onFailure();
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of {@link VersionCheckerCallback} that is called asynchronously, and then
|
||||
* forwards the calls an underlying VersionCheckerCallback synchronously on the main Bukkit thread.
|
||||
*/
|
||||
class SyncVersionCheckerCallback implements VersionCheckerCallback {
|
||||
private final Plugin plugin;
|
||||
private final VersionCheckerCallback callback;
|
||||
|
||||
SyncVersionCheckerCallback(Plugin plugin, VersionCheckerCallback callback) {
|
||||
this.plugin = plugin;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLatestVersion(String version) {
|
||||
doSync(() -> callback.onLatestVersion(version));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOutdatedVersion(String currentVersion, String latestVersion) {
|
||||
doSync(() -> callback.onOutdatedVersion(currentVersion, latestVersion));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPreReleaseVersion(String version) {
|
||||
doSync(() -> callback.onPreReleaseVersion(version));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure() {
|
||||
doSync(callback::onFailure);
|
||||
}
|
||||
|
||||
private void doSync(Runnable r) {
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user