From cfdd0c728acb0d4671f565c811c4fbb4970ae790 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Wed, 8 May 2019 00:20:34 +0200 Subject: [PATCH] Added a few methods in ServerCompatibility --- .../commands/BentoBoxVersionCommand.java | 4 +- .../bentobox/panels/ManagementPanel.java | 6 +-- .../versions/ServerCompatibility.java | 40 +++++++++++++------ 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java b/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java index b372d84fd..d3179bb7b 100644 --- a/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java +++ b/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java @@ -31,8 +31,8 @@ public class BentoBoxVersionCommand extends CompositeCommand { @Override public boolean execute(User user, String label, List args) { - ServerCompatibility.ServerSoftware serverSoftware = ServerCompatibility.getInstance().getServerSoftware(getPlugin().getServer()); - ServerCompatibility.ServerVersion serverVersion = ServerCompatibility.getInstance().getServerVersion(getPlugin().getServer()); + ServerCompatibility.ServerSoftware serverSoftware = ServerCompatibility.getInstance().getServerSoftware(); + ServerCompatibility.ServerVersion serverVersion = ServerCompatibility.getInstance().getServerVersion(); user.sendMessage("commands.bentobox.version.server", TextVariables.NAME, serverSoftware != null ? serverSoftware.toString() : user.getTranslation("general.invalid"), diff --git a/src/main/java/world/bentobox/bentobox/panels/ManagementPanel.java b/src/main/java/world/bentobox/bentobox/panels/ManagementPanel.java index 7b9fd6208..68ea73cfc 100644 --- a/src/main/java/world/bentobox/bentobox/panels/ManagementPanel.java +++ b/src/main/java/world/bentobox/bentobox/panels/ManagementPanel.java @@ -205,9 +205,9 @@ public class ManagementPanel { builder.item(6, reloadItem); // BentoBox state icon - ServerCompatibility.Compatibility compatibility = ServerCompatibility.getInstance().checkCompatibility(BentoBox.getInstance()); - ServerCompatibility.ServerSoftware serverSoftware = ServerCompatibility.getInstance().getServerSoftware(BentoBox.getInstance().getServer()); - ServerCompatibility.ServerVersion serverVersion = ServerCompatibility.getInstance().getServerVersion(BentoBox.getInstance().getServer()); + ServerCompatibility.Compatibility compatibility = ServerCompatibility.getInstance().checkCompatibility(); + ServerCompatibility.ServerSoftware serverSoftware = ServerCompatibility.getInstance().getServerSoftware(); + ServerCompatibility.ServerVersion serverVersion = ServerCompatibility.getInstance().getServerVersion(); PanelItemBuilder compatibilityItemBuilder = new PanelItemBuilder() .name(user.getTranslation(LOCALE_REF + "information.state.name")) diff --git a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java index 5a63aeceb..18537eb0c 100644 --- a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java +++ b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java @@ -1,12 +1,9 @@ package world.bentobox.bentobox.versions; import org.bukkit.Bukkit; -import org.bukkit.Server; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; -import world.bentobox.bentobox.BentoBox; - /** * Checks and ensures the current server software is compatible with BentoBox. * @author Poslovitch @@ -120,13 +117,12 @@ public class ServerCompatibility { /** * Checks the compatibility with the current server software and returns the {@link Compatibility}. * Note this is a one-time calculation: further calls won't change the result. - * @param plugin BentoBox instance to provide. * @return the {@link Compatibility}. */ - public Compatibility checkCompatibility(BentoBox plugin) { + public Compatibility checkCompatibility() { if (result == null) { // Check the server version first - ServerVersion version = getServerVersion(Bukkit.getServer()); + ServerVersion version = getServerVersion(); if (version == null || version.getCompatibility().equals(Compatibility.INCOMPATIBLE)) { // 'Version = null' means that it's not listed. And therefore, it's implicitly incompatible. @@ -135,7 +131,7 @@ public class ServerCompatibility { } // Now, check the server software - ServerSoftware software = getServerSoftware(Bukkit.getServer()); + ServerSoftware software = getServerSoftware(); if (software == null || software.getCompatibility().equals(Compatibility.INCOMPATIBLE)) { // 'software = null' means that it's not listed. And therefore, it's implicitly incompatible. @@ -163,13 +159,12 @@ public class ServerCompatibility { /** * Returns the {@link ServerSoftware} entry corresponding to the current server software, may be null. - * @param server the {@link Server} instance, must not be null. * @return the {@link ServerSoftware} run by this server or null. * @since 1.3.0 */ @Nullable - public ServerSoftware getServerSoftware(@NonNull Server server) { - String serverSoftware = server.getVersion().substring(4).split("-")[0]; + public ServerSoftware getServerSoftware() { + String serverSoftware = Bukkit.getServer().getVersion().substring(4).split("-")[0]; try { return ServerSoftware.valueOf(serverSoftware.toUpperCase()); } catch (IllegalArgumentException e) { @@ -179,17 +174,36 @@ public class ServerCompatibility { /** * Returns the {@link ServerVersion} entry corresponding to the current server software, may be null. - * @param server the {@link Server} instance, must not be null. * @return the {@link ServerVersion} run by this server or null. * @since 1.3.0 */ @Nullable - public ServerVersion getServerVersion(@NonNull Server server) { - String serverVersion = server.getBukkitVersion().split("-")[0].replace(".", "_"); + public ServerVersion getServerVersion() { + String serverVersion = Bukkit.getServer().getBukkitVersion().split("-")[0].replace(".", "_"); try { return ServerVersion.valueOf("V" + serverVersion.toUpperCase()); } catch (IllegalArgumentException e) { return null; } } + + /** + * Returns whether the server runs on the specified version. + * @param version the {@link ServerVersion} to check. + * @return {@code true} if the server runs on this version, {@code false} otherwise. + * @since 1.5.0 + */ + public boolean isVersion(@NonNull ServerVersion version) { + return version.equals(getServerVersion()); + } + + /** + * Returns whether the server runs on the specified software. + * @param software the {@link ServerSoftware} to check. + * @return {@code true} if the server runs on this software, {@code false} otherwise. + * @since 1.5.0 + */ + public boolean isSoftware(@NonNull ServerSoftware software) { + return software.equals(getServerSoftware()); + } }