From ba4195263b58718cf37445194e185a8347c8b03e Mon Sep 17 00:00:00 2001 From: TheLonelyWolf <42873246+TheLonelyWolf1@users.noreply.github.com> Date: Sat, 4 May 2024 22:17:04 +0200 Subject: [PATCH] Reimplement MC Version Checks and add Warning at Startup. CommandPanels now warns the owner inside the console if the server runs an unsupported Version --- .../commandpanels/CommandPanels.java | 18 ++- .../commandpanels/MinecraftVersions.java | 105 ++++++++++++++++++ 2 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 src/me/rockyhawk/commandpanels/MinecraftVersions.java diff --git a/src/me/rockyhawk/commandpanels/CommandPanels.java b/src/me/rockyhawk/commandpanels/CommandPanels.java index 60036d0..2f909e2 100644 --- a/src/me/rockyhawk/commandpanels/CommandPanels.java +++ b/src/me/rockyhawk/commandpanels/CommandPanels.java @@ -112,6 +112,16 @@ public class CommandPanels extends JavaPlugin{ public void onEnable() { Bukkit.getLogger().info("[CommandPanels] RockyHawk's CommandPanels v" + this.getDescription().getVersion() + " Plugin Loading..."); + // Check if Server is not supported by this version of CP + if(MinecraftVersions.lessThanOrEqualTo("1.20.5")){ + Bukkit.getLogger().warning("======================================================"); + Bukkit.getLogger().warning("CommandPanels v" + this.getDescription().getVersion() + " is developed for Minecraft 1.20.5 and newer!"); + Bukkit.getLogger().warning("You're running on: " + Bukkit.getServer().getVersion()); + Bukkit.getLogger().warning("Please Upgrade the server or downgrade the plugin!"); + Bukkit.getLogger().warning("You can find other versions of the plugin on: https://commandpanels.net/download"); + Bukkit.getLogger().warning("======================================================"); + } + //register config files this.blockConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder() + File.separator + "blocks.yml")); panelData.dataConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder() + File.separator + "data.yml")); @@ -519,17 +529,17 @@ public class CommandPanels extends JavaPlugin{ } //returns true if the item is the MMO Item - public boolean isMMOItem(ItemStack itm, String type, String id){ + public boolean isMMOItem(ItemStack itm, String type, String id) { try { if (getServer().getPluginManager().isPluginEnabled("MMOItems")) { NBTItem nbt = NBTItem.get(itm); - if (nbt.getType().equalsIgnoreCase(type) && nbt.getString("MMOITEMS_ITEM_ID").equalsIgnoreCase(id)){ + if (nbt.getType().equalsIgnoreCase(type) && nbt.getString("MMOITEMS_ITEM_ID").equalsIgnoreCase(id)) { return true; } itm.getType(); } - }catch (Exception ex){ - debug(ex,null); + } catch (Exception ex) { + debug(ex, null); } return false; } diff --git a/src/me/rockyhawk/commandpanels/MinecraftVersions.java b/src/me/rockyhawk/commandpanels/MinecraftVersions.java new file mode 100644 index 0000000..971d03f --- /dev/null +++ b/src/me/rockyhawk/commandpanels/MinecraftVersions.java @@ -0,0 +1,105 @@ +package me.rockyhawk.commandpanels; + +import org.bukkit.Bukkit; + +import java.util.Arrays; + +public class MinecraftVersions { + + /** + * Splits the MC Version in multiple parts and returns them. + * + * @return String[] + */ + private static String[] getMinecraftVersionParts(){ + String serverVersion = Bukkit.getServer().getVersion(); + String[] versionParts = serverVersion.split(" "); // Splitting the version string by space + return versionParts[2].split("\\."); // Splitting the Minecraft version by dot + } + + /** + * Gets the major MC version from the Server + * Example: Returns the 20 of 1.20.6. + * + * @return String + */ + private static String getMajorVersion(){ + String[] minecraftVersionParts = getMinecraftVersionParts(); + // Returns the 20 of 1.20.6 + return minecraftVersionParts[1]; + } + + /** + * Gets the minor MC version from the Server + * Example: Returns the 6 of 1.20.6. + * + * @return String + */ + private static String getMinorVersion(){ + String[] minecraftVersionParts = getMinecraftVersionParts(); + + // Returns the 6 of 1.20.6 + return minecraftVersionParts[2]; + + } + + /** + * Checks if the version of the Server is older or on the specified Version. + * + * @param specifiedVersion The version to check against + * @return String + */ + public static boolean lessThanOrEqualTo(String specifiedVersion){ + // Split the Version into its parts + String[] specifiedVersionParts = specifiedVersion.split("\\."); + + // Get the parts from the Version of the Server. + // The .replace turns 1.20.4) into 1.20.4 + int serverMajorVersion = Integer.parseInt(String.valueOf(getMajorVersion()).replace(")", "")); + int serverMinorVersion = Integer.parseInt(String.valueOf(getMinorVersion()).replace(")", "")); + + // Get the parts from the specified version + int specifiedMajorVersion = Integer.parseInt(specifiedVersionParts[1]); + int specifiedMinorVersion = Integer.parseInt(specifiedVersionParts[2]); + + // If Major version is less or equal than the specified version. + if(serverMajorVersion < specifiedMajorVersion){ + return true; + } // If Major version equal and Minor Version is less or equal than specified version. + else if (serverMajorVersion == specifiedMajorVersion && serverMinorVersion <= specifiedMinorVersion) { + return true; + }else{ + return false; + } + } + + /** + * Checks if the version of the Server is newer or on the specified Version. + * + * @param specifiedVersion The version to check against + * @return String + */ + public static boolean greaterThanOrEqualTo(String specifiedVersion){ + // Split the Version into its parts + String[] specifiedVersionParts = specifiedVersion.split("\\."); + + // Get the parts from the Version of the Server + // The .replace turns 1.20.4) into 1.20.4 + int serverMajorVersion = Integer.parseInt(String.valueOf(getMajorVersion()).replace(")", "")); + int serverMinorVersion = Integer.parseInt(String.valueOf(getMinorVersion()).replace(")", "")); + + // Get the parts from the specified version + int specifiedMajorVersion = Integer.parseInt(specifiedVersionParts[1]); + int specifiedMinorVersion = Integer.parseInt(specifiedVersionParts[2]); + + // If Major version is less or equal than the specified version. + if(serverMajorVersion > specifiedMajorVersion){ + return true; + } // If Major version equal and Minor Version is less or equal than specified version. + else if (serverMajorVersion == specifiedMajorVersion && serverMinorVersion >= specifiedMinorVersion) { + return true; + }else{ + return false; + } + } +}