From 920f384a5649de3d2e4c1f7ed1be7707a449cc4f Mon Sep 17 00:00:00 2001
From: SBDeveloper <stijnbannink23@gmail.com>
Date: Fri, 5 Aug 2022 14:18:19 +0200
Subject: [PATCH] :fire: Removed unused code, added cleaner Java AWT detection

---
 pom.xml                                       |  9 +++
 .../bukkit/common/map/MapColorPalette.java    | 11 ---
 .../mapreflectionapi/MapReflectionAPI.java    | 80 +++++++++++--------
 .../mapreflectionapi/utils/MainUtil.java      | 43 ++++++++++
 .../mapreflectionapi/utils/UpdateManager.java |  3 +
 5 files changed, 100 insertions(+), 46 deletions(-)
 create mode 100644 src/main/java/tech/sbdevelopment/mapreflectionapi/utils/MainUtil.java

diff --git a/pom.xml b/pom.xml
index fc8bcf5..2d86681 100644
--- a/pom.xml
+++ b/pom.xml
@@ -159,6 +159,15 @@
             <resource>
                 <directory>src/main/resources</directory>
                 <filtering>true</filtering>
+                <includes>
+                    <include>plugin.yml</include>
+                </includes>
+            </resource>
+            <resource>
+                <directory>src/main/resources</directory>
+                <excludes>
+                    <exclude>plugin.yml</exclude>
+                </excludes>
             </resource>
         </resources>
     </build>
diff --git a/src/main/java/com/bergerkiller/bukkit/common/map/MapColorPalette.java b/src/main/java/com/bergerkiller/bukkit/common/map/MapColorPalette.java
index f2cde2c..3feda88 100644
--- a/src/main/java/com/bergerkiller/bukkit/common/map/MapColorPalette.java
+++ b/src/main/java/com/bergerkiller/bukkit/common/map/MapColorPalette.java
@@ -26,7 +26,6 @@ package com.bergerkiller.bukkit.common.map;
 import com.bergerkiller.bukkit.common.map.color.MCSDBubbleFormat;
 import com.bergerkiller.bukkit.common.map.color.MCSDGenBukkit;
 import com.bergerkiller.bukkit.common.map.color.MapColorSpaceData;
-import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI;
 import tech.sbdevelopment.mapreflectionapi.utils.ReflectionUtil;
 
 import java.awt.*;
@@ -47,16 +46,6 @@ public class MapColorPalette {
     public static final byte COLOR_TRANSPARENT = 0;
 
     static {
-        // We NEED java awt or all will just fail to work anyway.
-        try {
-            Class.forName("java.awt.Color");
-        } catch (ClassNotFoundException e) {
-            MapReflectionAPI.getInstance().getLogger().severe("The Java AWT runtime library is not available");
-            MapReflectionAPI.getInstance().getLogger().severe("This is usually because a headless JVM is used for the server");
-            MapReflectionAPI.getInstance().getLogger().severe("Please install and configure a non-headless JVM to have Map Displays work");
-            throw new UnsupportedOperationException("Map Displays require a non-headless JVM (Uses AWT)");
-        }
-
         // Now we know java.awt exists we can initialize this one
         COLOR_MAP_DATA = new MapColorSpaceData();
 
diff --git a/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java b/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java
index c3dae55..cfe00d1 100644
--- a/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java
+++ b/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java
@@ -23,6 +23,7 @@
 
 package tech.sbdevelopment.mapreflectionapi;
 
+import com.bergerkiller.bukkit.common.map.MapColorPalette;
 import com.comphenix.protocol.ProtocolLibrary;
 import org.bstats.bukkit.Metrics;
 import org.bstats.charts.SingleLineChart;
@@ -34,6 +35,7 @@ import tech.sbdevelopment.mapreflectionapi.cmd.MapManagerCMD;
 import tech.sbdevelopment.mapreflectionapi.listeners.MapListener;
 import tech.sbdevelopment.mapreflectionapi.listeners.PacketListener;
 import tech.sbdevelopment.mapreflectionapi.managers.Configuration;
+import tech.sbdevelopment.mapreflectionapi.utils.MainUtil;
 import tech.sbdevelopment.mapreflectionapi.utils.ReflectionUtil;
 import tech.sbdevelopment.mapreflectionapi.utils.UpdateManager;
 
@@ -77,11 +79,15 @@ public class MapReflectionAPI extends JavaPlugin {
             return;
         }
 
-        if (!Bukkit.getPluginManager().isPluginEnabled("BKCommonLib")) {
-            getLogger().severe("MapReflectionAPI requires BKCommonLib to function!");
+        getLogger().info("Loading Java AWT runtime library support...");
+        if (MainUtil.isHeadlessJDK()) {
+            getLogger().severe("MapReflectionAPI requires the Java AWT runtime library, but is not available!");
+            getLogger().severe("This is usually because a headless JVM is used for the server.");
+            getLogger().severe("Please install and configure a non-headless JVM to make this plugin work.");
             Bukkit.getPluginManager().disablePlugin(this);
             return;
         }
+        MapColorPalette.getColor(0, 0, 0); //Initializes the class
 
         if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
             getLogger().severe("MapReflectionAPI requires ProtocolLib to function!");
@@ -126,41 +132,45 @@ public class MapReflectionAPI extends JavaPlugin {
         metrics.addCustomChart(new SingleLineChart("managed_maps", () -> mapManager.getManagedMapsCount()));
 
         if (Configuration.getInstance().isUpdaterCheck()) {
-            UpdateManager updateManager = new UpdateManager(this, UpdateManager.CheckType.SPIGOT);
+            try {
+                UpdateManager updateManager = new UpdateManager(this, UpdateManager.CheckType.SPIGOT);
 
-            updateManager.handleResponse((versionResponse, version) -> {
-                switch (versionResponse) {
-                    case FOUND_NEW:
-                        getLogger().warning("There is a new version available! Current: " + getDescription().getVersion() + " New: " + version.get());
-                        if (Configuration.getInstance().isUpdaterDownload()) {
-                            getLogger().info("Trying to download the update. This could take some time...");
+                updateManager.handleResponse((versionResponse, version) -> {
+                    switch (versionResponse) {
+                        case FOUND_NEW:
+                            getLogger().warning("There is a new version available! Current: " + getDescription().getVersion() + " New: " + version.get());
+                            if (Configuration.getInstance().isUpdaterDownload()) {
+                                getLogger().info("Trying to download the update. This could take some time...");
 
-                            updateManager.handleDownloadResponse((downloadResponse, fileName) -> {
-                                switch (downloadResponse) {
-                                    case DONE:
-                                        getLogger().info("Update downloaded! If you restart your server, it will be loaded. Filename: " + fileName);
-                                        break;
-                                    case ERROR:
-                                        getLogger().severe("Something went wrong when trying downloading the latest version.");
-                                        break;
-                                    case UNAVAILABLE:
-                                        getLogger().warning("Unable to download the latest version.");
-                                        break;
-                                }
-                            }).runUpdate();
-                        }
-                        break;
-                    case LATEST:
-                        getLogger().info("You are running the latest version [" + getDescription().getVersion() + "]!");
-                        break;
-                    case THIS_NEWER:
-                        getLogger().info("You are running a newer version [" + getDescription().getVersion() + "]! This is probably fine.");
-                        break;
-                    case UNAVAILABLE:
-                        getLogger().severe("Unable to perform an update check.");
-                        break;
-                }
-            }).check();
+                                updateManager.handleDownloadResponse((downloadResponse, fileName) -> {
+                                    switch (downloadResponse) {
+                                        case DONE:
+                                            getLogger().info("Update downloaded! If you restart your server, it will be loaded. Filename: " + fileName);
+                                            break;
+                                        case ERROR:
+                                            getLogger().severe("Something went wrong when trying downloading the latest version.");
+                                            break;
+                                        case UNAVAILABLE:
+                                            getLogger().warning("Unable to download the latest version.");
+                                            break;
+                                    }
+                                }).runUpdate();
+                            }
+                            break;
+                        case LATEST:
+                            getLogger().info("You are running the latest version [" + getDescription().getVersion() + "]!");
+                            break;
+                        case THIS_NEWER:
+                            getLogger().info("You are running a newer version [" + getDescription().getVersion() + "]! This is probably fine.");
+                            break;
+                        case UNAVAILABLE:
+                            getLogger().severe("Unable to perform an update check.");
+                            break;
+                    }
+                }).check();
+            } catch (IllegalStateException ex) {
+                ex.printStackTrace();
+            }
         }
 
         getLogger().info("MapReflectionAPI is enabled!");
diff --git a/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/MainUtil.java b/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/MainUtil.java
new file mode 100644
index 0000000..76762c5
--- /dev/null
+++ b/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/MainUtil.java
@@ -0,0 +1,43 @@
+/*
+ * This file is part of MapReflectionAPI.
+ * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package tech.sbdevelopment.mapreflectionapi.utils;
+
+public class MainUtil {
+    private MainUtil() {
+    }
+
+    /**
+     * Gets whether this is a headless JDK that doesn't contain the Java AWT library
+     *
+     * @return True if java.awt is not available
+     */
+    public static boolean isHeadlessJDK() {
+        try {
+            Class.forName("java.awt.Color");
+            return false;
+        } catch (ClassNotFoundException ex) {
+            return true;
+        }
+    }
+}
diff --git a/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/UpdateManager.java b/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/UpdateManager.java
index 362240a..b461a78 100644
--- a/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/UpdateManager.java
+++ b/src/main/java/tech/sbdevelopment/mapreflectionapi/utils/UpdateManager.java
@@ -74,6 +74,9 @@ public class UpdateManager {
      * @param plugin The plugin instance
      */
     public UpdateManager(Plugin plugin, CheckType type) {
+        if ("%%__RESOURCE__%%".equals("%%__" + "RES" + "OU" + "RCE" + "__%%"))
+            throw new IllegalStateException("Resource ID not set!");
+
         this.plugin = plugin;
         this.currentVersion = new Version(plugin.getDescription().getVersion());
         this.type = type;