From d30626d11f9990901e1cf41ccb9ed5f6e2aba5a0 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 19 Mar 2016 00:39:42 -0400 Subject: [PATCH] Cleaner reflection --- .../com/plotsquared/bukkit/BukkitMain.java | 24 ++----------------- .../plotsquared/bukkit/chat/Reflection.java | 22 +++-------------- 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 6fce37ca5..07e42ca7c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -78,7 +78,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.OfflinePlayer; -import org.bukkit.Server; import org.bukkit.World; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Entity; @@ -91,8 +90,6 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -655,25 +652,8 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { @Override public String getNMSPackage() { - final Server server = Bukkit.getServer(); - final Class bukkitServerClass = server.getClass(); - String[] pas = bukkitServerClass.getName().split("\\."); - if (pas.length == 5) { - return pas[3]; - } - try { - final Method getHandle = bukkitServerClass.getDeclaredMethod("getHandle"); - final Object handle = getHandle.invoke(server); - final Class handleServerClass = handle.getClass(); - pas = handleServerClass.getName().split("\\."); - if (pas.length == 5) { - return pas[3]; - } - } catch (IllegalAccessException | InvocationTargetException | SecurityException | NoSuchMethodException | IllegalArgumentException e) { - e.printStackTrace(); - } - PS.debug("Unknown NMS package: " + StringMan.getString(pas)); - return "1_8_R3"; + String name = Bukkit.getServer().getClass().getPackage().getName(); + return name.substring(name.lastIndexOf('.') + 1); } @Override diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java b/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java index 309f79e0c..eb716f75a 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java @@ -1,6 +1,6 @@ package com.plotsquared.bukkit.chat; -import org.bukkit.Bukkit; +import com.intellectualcrafters.plot.PS; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -29,7 +29,6 @@ public final class Reflection { * The map maps [types to maps of [method names to maps of [parameter types to method instances]]]. */ private static final Map, Map>, Method>>> _loadedMethods = new HashMap<>(); - private static String _versionString; private Reflection() { @@ -41,16 +40,7 @@ public final class Reflection { * @return The version string of the OBC and NMS packages, including the trailing dot. */ public synchronized static String getVersion() { - if (_versionString == null) { - if (Bukkit.getServer() == null) { - // The server hasn't started, static initializer call? - return null; - } - final String name = Bukkit.getServer().getClass().getPackage().getName(); - _versionString = name.substring(name.lastIndexOf('.') + 1) + "."; - } - - return _versionString; + return PS.get().IMP.getNMSPackage(); } /** @@ -162,13 +152,7 @@ public final class Reflection { field.setAccessible(true); loaded.put(name, field); return field; - } catch (NoSuchFieldException e) { - // Error loading - e.printStackTrace(); - // Cache field as not existing - loaded.put(name, null); - return null; - } catch (SecurityException e) { + } catch (NoSuchFieldException | SecurityException e) { // Error loading e.printStackTrace(); // Cache field as not existing