Просмотр исходного кода

Fix SQL on 1.17 (hacky) and optimized CompatibilityManager

nossr50 4 лет назад
Родитель
Сommit
7e28799f94

+ 8 - 0
Changelog.txt

@@ -1,5 +1,13 @@
 Version 2.1.199
+    Fixed a bug that caused MySQL/MariaDB to malfunction for 1.17 (see notes)
     Renamed Deepslate Lapis Lazuli Ore to Deepslate Lapis Ore in experience.yml and config.yml
+    Added some code to prevent mcMMO from breaking if it doesn't recognize the version of the game
+    Optimized CompatibilitySupportLayer - this handles some of the logic for supporting multiple versions of the game
+    Added Unit Tests for MinecraftGameVersion
+
+    NOTES:
+    I have temporarily disabled SSL for MySQL/MariaDB for 1.17 ( proper fix coming soon )
+
 Version 2.1.198
     Fixed a bug where Smelting didn't work with the new 1.17 materials
     Updated dependency Adventure to 4.8.0 (thanks TheBusyBiscuit)

+ 2 - 1
src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java

@@ -51,7 +51,8 @@ public final class SQLDatabaseManager implements DatabaseManager {
         String connectionString = "jdbc:mysql://" + mcMMO.p.getGeneralConfig().getMySQLServerName()
                 + ":" + mcMMO.p.getGeneralConfig().getMySQLServerPort() + "/" + mcMMO.p.getGeneralConfig().getMySQLDatabaseName();
 
-        if(mcMMO.p.getGeneralConfig().getMySQLSSL())
+        if(!mcMMO.getCompatibilityManager().getMinecraftGameVersion().isAtLeast(1, 17, 0) //Temporary hack for SQL and 1.17 support
+                && mcMMO.p.getGeneralConfig().getMySQLSSL())
             connectionString +=
                     "?verifyServerCertificate=false"+
                     "&useSSL=true"+

+ 1 - 1
src/main/java/com/gmail/nossr50/mcMMO.java

@@ -483,7 +483,7 @@ public class mcMMO extends JavaPlugin {
         return upgradeManager;
     }
 
-    public static CompatibilityManager getCompatibilityManager() {
+    public static @Nullable CompatibilityManager getCompatibilityManager() {
         return platformManager.getCompatibilityManager();
     }
 

+ 22 - 50
src/main/java/com/gmail/nossr50/util/compat/CompatibilityManager.java

@@ -30,10 +30,10 @@ import java.util.HashMap;
  */
 //TODO: I need to rewrite this crap
 public class CompatibilityManager {
-    private HashMap<CompatibilityType, Boolean> supportedLayers;
+    private @NotNull HashMap<CompatibilityType, Boolean> supportedLayers;
     private boolean isFullyCompatibleServerSoftware = true; //true if all compatibility layers load successfully
-    private final MinecraftGameVersion minecraftGameVersion;
-    private final NMSVersion nmsVersion;
+    private final @NotNull MinecraftGameVersion minecraftGameVersion;
+    private final @NotNull NMSVersion nmsVersion;
 
     /* Compatibility Layers */
 //    private PlayerAttackCooldownExploitPreventionLayer playerAttackCooldownExploitPreventionLayer;
@@ -42,7 +42,7 @@ public class CompatibilityManager {
     private AbstractMasterAnglerCompatibility masterAnglerCompatibility;
     private WorldCompatibilityLayer worldCompatibilityLayer;
 
-    public CompatibilityManager(MinecraftGameVersion minecraftGameVersion) {
+    public CompatibilityManager(@NotNull MinecraftGameVersion minecraftGameVersion) {
         mcMMO.p.getLogger().info("Loading compatibility layers...");
         this.minecraftGameVersion = minecraftGameVersion;
         this.nmsVersion = determineNMSVersion();
@@ -77,24 +77,8 @@ public class CompatibilityManager {
     }
 
     private void initWorldCompatibilityLayer() {
-        if(minecraftGameVersion.getMinorVersion().asInt() > 17
-                || (minecraftGameVersion.getMinorVersion().asInt() >= 16 && minecraftGameVersion.getPatchVersion().asInt() >= 4)
-                || minecraftGameVersion.getMajorVersion().asInt() >= 2) {
-            if(hasNewWorldMinHeightAPI()) {
-                worldCompatibilityLayer = new WorldCompatibilityLayer_1_16_4();
-            } else {
-                worldCompatibilityLayer = new WorldCompatibilityLayer() {
-                    @Override
-                    public int getMinWorldHeight(@NotNull World world) {
-                        return WorldCompatibilityLayer.super.getMinWorldHeight(world);
-                    }
-
-                    @Override
-                    public int getMaxWorldHeight(@NotNull World world) {
-                        return WorldCompatibilityLayer.super.getMaxWorldHeight(world);
-                    }
-                };
-            }
+        if(minecraftGameVersion.isAtLeast(1, 17, 0)) {
+            worldCompatibilityLayer = new WorldCompatibilityLayer_1_16_4();
         } else {
             worldCompatibilityLayer = new WorldCompatibilityLayer() {
                 @Override
@@ -111,37 +95,15 @@ public class CompatibilityManager {
     }
 
     private void initMasterAnglerLayer() {
-        if(minecraftGameVersion.getMinorVersion().asInt() >= 16 || minecraftGameVersion.getMajorVersion().asInt() >= 2) {
-            if(hasNewFishingHookAPI()) {
-                masterAnglerCompatibility = new MasterAnglerCompatibilityLayer();
-            }
+        if(minecraftGameVersion.isAtLeast(1, 16, 3)) {
+            masterAnglerCompatibility = new MasterAnglerCompatibilityLayer();
         } else {
             masterAnglerCompatibility = null;
         }
     }
 
-    private boolean hasNewWorldMinHeightAPI() {
-        try {
-            Class<?> checkForClass = Class.forName("org.bukkit.World");
-            checkForClass.getMethod("getMinHeight");
-            return true;
-        } catch (ClassNotFoundException | NoSuchMethodException e) {
-            return false;
-        }
-    }
-
-    private boolean hasNewFishingHookAPI() {
-        try {
-            Class<?> checkForClass = Class.forName("org.bukkit.entity.FishHook");
-            checkForClass.getMethod("getMinWaitTime");
-            return true;
-        } catch (ClassNotFoundException | NoSuchMethodException e) {
-            return false;
-        }
-    }
-
     private void initBungeeSerializerLayer() {
-        if(minecraftGameVersion.getMinorVersion().asInt() >= 16) {
+        if(minecraftGameVersion.isAtLeast(1, 16, 0)) {
             bungeeSerializerCompatibilityLayer = new BungeeModernSerializerCompatibilityLayer();
         } else {
             bungeeSerializerCompatibilityLayer = new BungeeLegacySerializerCompatibilityLayer();
@@ -151,7 +113,7 @@ public class CompatibilityManager {
     }
 
     private void initPersistentDataLayer() {
-        if(minecraftGameVersion.getMinorVersion().asInt() >= 14 || minecraftGameVersion.getMajorVersion().asInt() >= 2) {
+        if(minecraftGameVersion.isAtLeast(1, 14, 2)) {
             persistentDataLayer = new SpigotPersistentDataLayer_1_14();
         } else {
 
@@ -162,7 +124,7 @@ public class CompatibilityManager {
     }
 
     //TODO: move to text manager
-    public void reportCompatibilityStatus(CommandSender commandSender) {
+    public void reportCompatibilityStatus(@NotNull CommandSender commandSender) {
         if(isFullyCompatibleServerSoftware) {
             commandSender.sendMessage(LocaleLoader.getString("mcMMO.Template.Prefix",
                     "mcMMO is fully compatible with the currently running server software."));
@@ -179,7 +141,7 @@ public class CompatibilityManager {
         commandSender.sendMessage(LocaleLoader.getString("mcMMO.Template.Prefix", "NMS Status - " + nmsVersion.toString()));
     }
 
-    public boolean isCompatibilityLayerOperational(CompatibilityType compatibilityType) {
+    public boolean isCompatibilityLayerOperational(@NotNull CompatibilityType compatibilityType) {
         return supportedLayers.get(compatibilityType);
     }
 
@@ -192,6 +154,12 @@ public class CompatibilityManager {
     }
 
     private @NotNull NMSVersion determineNMSVersion() {
+        //This bit here helps prevent mcMMO breaking if it isn't updated but the game continues to update
+        if(minecraftGameVersion.isAtLeast(1, 17, 0)) {
+            return NMSVersion.NMS_1_17;
+        }
+
+        //Messy but it works
         if (minecraftGameVersion.getMajorVersion().asInt() == 1) {
             switch (minecraftGameVersion.getMinorVersion().asInt()) {
                 case 12:
@@ -237,4 +205,8 @@ public class CompatibilityManager {
     public @NotNull WorldCompatibilityLayer getWorldCompatibilityLayer() {
         return worldCompatibilityLayer;
     }
+
+    public @Nullable MinecraftGameVersion getMinecraftGameVersion() {
+        return minecraftGameVersion;
+    }
 }

+ 33 - 0
src/main/java/com/gmail/nossr50/util/platform/MinecraftGameVersion.java

@@ -27,4 +27,37 @@ public class MinecraftGameVersion extends MajorMinorPatchVersion {
         super(majorVerNumber, minorVerNumber);
     }
 
+    /**
+     * Returns whether or not the Minecraft version is at least equal to or higher than a target version
+     * @param majorVerNumber target major version number - for example 1.16.5 , the 1 is the major version
+     * @param minorVerNumber target minor version number - for example 1.16.5, the 16 is the minor version
+     * @param patchVerNumber target patch version number - for example 1.16.5, the 5 is the patch version number
+     *
+     * @return returns true if Minecraft is at least a certain version
+     */
+    public boolean isAtLeast(int majorVerNumber, int minorVerNumber, int patchVerNumber) {
+        //First check if the major version is higher, if it is we have no need to check minor version or patch version
+
+        if(getMajorVersion().asInt() > majorVerNumber) {
+            return true; //Major version is one higher and hierarchically more important than the other versions
+        }
+
+        if(getMajorVersion().asInt() < majorVerNumber) {
+            return false; //Major version is below, so return false
+        }
+
+        //Major version meets the requirement, check minor version
+
+        if(getMinorVersion().asInt() > minorVerNumber) {
+            return true; //Minor version is one higher and hierarchically more important than patch version, so exit here
+        }
+
+        if(getMinorVersion().asInt() < minorVerNumber) {
+            return false; //Minor version is at least one version behind, return false
+        }
+
+        //Minor version meets the requirement, check patch version
+        return getPatchVersion().asInt() >= patchVerNumber;
+    }
+
 }

+ 65 - 0
src/test/java/com/gmail/nossr50/util/platform/MinecraftGameVersionTest.java

@@ -0,0 +1,65 @@
+package com.gmail.nossr50.util.platform;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class MinecraftGameVersionTest {
+
+    @Test
+    public void testAtLeast() {
+        //TODO: Remove redundant tests
+        MinecraftGameVersion oneEightEight = new MinecraftGameVersion(1, 8, 8);
+        MinecraftGameVersion oneSixteenFive = new MinecraftGameVersion(1, 16, 5);
+        MinecraftGameVersion oneTwo = new MinecraftGameVersion(1, 2);
+
+        //1.8.8
+        assertTrue(oneEightEight.isAtLeast(1, 8, 7));
+        assertFalse(oneEightEight.isAtLeast(1, 9, 0));
+
+        //1.16.5
+        assertTrue(oneSixteenFive.isAtLeast(1, 15, 2));
+        assertFalse(oneSixteenFive.isAtLeast(1, 17, 0));
+
+        //1.2
+        assertTrue(oneTwo.isAtLeast(1, 2, 0));
+
+        //Test major version number
+        MinecraftGameVersion majorVersionTest = new MinecraftGameVersion(2, 0, 0);
+
+        assertFalse(majorVersionTest.isAtLeast(3, 0, 0));
+        assertFalse(majorVersionTest.isAtLeast(3, 1, 0));
+        assertFalse(majorVersionTest.isAtLeast(3, 0, 2));
+
+        assertTrue(majorVersionTest.isAtLeast(2, 0, 0));
+        assertTrue(majorVersionTest.isAtLeast(1, 0, 0));
+
+
+        //Test minor version number
+        MinecraftGameVersion minorVersionTest = new MinecraftGameVersion(0, 3, 0);
+
+        assertFalse(minorVersionTest.isAtLeast(0, 4, 0));
+        assertFalse(minorVersionTest.isAtLeast(1, 4, 0));
+        assertFalse(minorVersionTest.isAtLeast(0, 4, 1));
+
+        assertTrue(minorVersionTest.isAtLeast(0, 1, 0));
+        assertTrue(minorVersionTest.isAtLeast(0, 2, 0));
+        assertTrue(minorVersionTest.isAtLeast(0, 2, 1));
+        assertTrue(minorVersionTest.isAtLeast(0, 3, 0));
+
+        //Test patch version number
+
+        MinecraftGameVersion patchVersionTest = new MinecraftGameVersion(0, 0, 5);
+
+        assertFalse(patchVersionTest.isAtLeast(1, 0, 0));
+        assertFalse(patchVersionTest.isAtLeast(0, 0, 6));
+        assertFalse(patchVersionTest.isAtLeast(0, 1, 4));
+        assertFalse(patchVersionTest.isAtLeast(1, 1, 4));
+
+        assertTrue(patchVersionTest.isAtLeast(0, 0, 1));
+        assertTrue(patchVersionTest.isAtLeast(0, 0, 2));
+        assertTrue(patchVersionTest.isAtLeast(0, 0, 3));
+        assertTrue(patchVersionTest.isAtLeast(0, 0, 4));
+        assertTrue(patchVersionTest.isAtLeast(0, 0, 5));
+    }
+}