Fixed ReflectionUtils code order

This commit is contained in:
SBDeveloper 2022-07-01 12:13:25 +02:00
parent 6d1dab78aa
commit 3ca16d49b8
3 changed files with 45 additions and 34 deletions

View file

@ -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);

View file

@ -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");

View file

@ -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 ]
softdepend: [ BKCommonLib, ProtocolLib ]