From 3ca16d49b8b4d1b759e9cee502a3a410775ed148 Mon Sep 17 00:00:00 2001 From: SBDeveloper Date: Fri, 1 Jul 2022 12:13:25 +0200 Subject: [PATCH] Fixed ReflectionUtils code order --- .../mapreflectionapi/MapReflectionAPI.java | 18 ++++-- .../util/ReflectionUtils.java | 59 ++++++++++--------- src/main/resources/plugin.yml | 2 +- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java b/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java index 7c20b33..4a62c76 100644 --- a/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java +++ b/src/main/java/tech/sbdevelopment/mapreflectionapi/MapReflectionAPI.java @@ -24,20 +24,19 @@ package tech.sbdevelopment.mapreflectionapi; import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; import org.bukkit.Bukkit; import org.bukkit.map.MapView; import org.bukkit.plugin.java.JavaPlugin; import tech.sbdevelopment.mapreflectionapi.api.MapManager; import tech.sbdevelopment.mapreflectionapi.listeners.MapListener; import tech.sbdevelopment.mapreflectionapi.listeners.PacketListener; +import tech.sbdevelopment.mapreflectionapi.util.ReflectionUtils; import java.util.logging.Level; public class MapReflectionAPI extends JavaPlugin { private static MapReflectionAPI instance; private static MapManager mapManager; - private ProtocolManager protocolManager; public static MapReflectionAPI getInstance() { if (instance == null) throw new IllegalStateException("The plugin is not enabled yet!"); @@ -57,14 +56,25 @@ public class MapReflectionAPI extends JavaPlugin { getLogger().info("MapReflectionAPI v" + getDescription().getVersion() + ""); getLogger().info("Made by © Copyright SBDevelopment 2022"); + if (!ReflectionUtils.supports(12)) { + getLogger().severe("MapReflectionAPI only supports Minecraft 1.12 - 1.19!"); + Bukkit.getPluginManager().disablePlugin(this); + return; + } + if (!Bukkit.getPluginManager().isPluginEnabled("BKCommonLib")) { getLogger().severe("MapReflectionAPI requires BKCommonLib to function!"); Bukkit.getPluginManager().disablePlugin(this); return; } - protocolManager = ProtocolLibrary.getProtocolManager(); - protocolManager.addPacketListener(new PacketListener(this)); + if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) { + getLogger().severe("MapReflectionAPI requires ProtocolLib to function!"); + Bukkit.getPluginManager().disablePlugin(this); + return; + } + + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketListener(this)); try { mapManager = new MapManager(this); diff --git a/src/main/java/tech/sbdevelopment/mapreflectionapi/util/ReflectionUtils.java b/src/main/java/tech/sbdevelopment/mapreflectionapi/util/ReflectionUtils.java index 0f1c566..7c4e830 100644 --- a/src/main/java/tech/sbdevelopment/mapreflectionapi/util/ReflectionUtils.java +++ b/src/main/java/tech/sbdevelopment/mapreflectionapi/util/ReflectionUtils.java @@ -62,6 +62,36 @@ public final class ReflectionUtils { * Performance is not a concern for these specific statically initialized values. */ public static final String VERSION; + + static { // This needs to be right below VERSION because of initialization order. + // This package loop is used to avoid implementation-dependant strings like Bukkit.getVersion() or Bukkit.getBukkitVersion() + // which allows easier testing as well. + String found = null; + for (Package pack : Package.getPackages()) { + String name = pack.getName(); + + // .v because there are other packages. + if (name.startsWith("org.bukkit.craftbukkit.v")) { + found = pack.getName().split("\\.")[3]; + + // Just a final guard to make sure it finds this important class. + // As a protection for forge+bukkit implementation that tend to mix versions. + // The real CraftPlayer should exist in the package. + // Note: Doesn't seem to function properly. Will need to separate the version + // handler for NMS and CraftBukkit for softwares like catmc. + try { + Class.forName("org.bukkit.craftbukkit." + found + ".entity.CraftPlayer"); + break; + } catch (ClassNotFoundException e) { + found = null; + } + } + } + if (found == null) + throw new IllegalArgumentException("Failed to parse server version. Could not find any package starting with name: 'org.bukkit.craftbukkit.v'"); + VERSION = found; + } + /** * The raw minor version number. * E.g. {@code v1_17_R1} to {@code 17} @@ -95,35 +125,6 @@ public final class ReflectionUtils { */ private static final MethodHandle SEND_PACKET; - static { // This needs to be right below VERSION because of initialization order. - // This package loop is used to avoid implementation-dependant strings like Bukkit.getVersion() or Bukkit.getBukkitVersion() - // which allows easier testing as well. - String found = null; - for (Package pack : Package.getPackages()) { - String name = pack.getName(); - - // .v because there are other packages. - if (name.startsWith("org.bukkit.craftbukkit.v")) { - found = pack.getName().split("\\.")[3]; - - // Just a final guard to make sure it finds this important class. - // As a protection for forge+bukkit implementation that tend to mix versions. - // The real CraftPlayer should exist in the package. - // Note: Doesn't seem to function properly. Will need to separate the version - // handler for NMS and CraftBukkit for softwares like catmc. - try { - Class.forName("org.bukkit.craftbukkit." + found + ".entity.CraftPlayer"); - break; - } catch (ClassNotFoundException e) { - found = null; - } - } - } - if (found == null) - throw new IllegalArgumentException("Failed to parse server version. Could not find any package starting with name: 'org.bukkit.craftbukkit.v'"); - VERSION = found; - } - static { Class entityPlayer = getNMSClass("server.level", "EntityPlayer"); Class worldServer = getNMSClass("server.level", "WorldServer"); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 030ee7f..025688e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,4 +5,4 @@ api-version: 1.13 authors: [ inventivetalent, SBDeveloper ] description: This API helps developer with viewing images on maps. website: https://sbdevelopment.tech -softdepend: [ BKCommonLib ] \ No newline at end of file +softdepend: [ BKCommonLib, ProtocolLib ] \ No newline at end of file