-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You can now: set the title of the chest inventory, send a message to …
…players at an interval showing their earnings.
- Loading branch information
Showing
9 changed files
with
293 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/main/java/codes/biscuit/sellchest/utils/ReflectionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package codes.biscuit.sellchest.utils; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ReflectionUtils { | ||
|
||
/* | ||
* The server version string to location NMS & OBC classes | ||
*/ | ||
private static String versionString; | ||
|
||
/* | ||
* Cache of NMS classes that we've searched for | ||
*/ | ||
private static Map<String, Class<?>> loadedNMSClasses = new HashMap<>(); | ||
|
||
/* | ||
* Cache of OBS classes that we've searched for | ||
*/ | ||
private static Map<String, Class<?>> loadedOBCClasses = new HashMap<>(); | ||
|
||
/* | ||
* Cache of methods that we've found in particular classes | ||
*/ | ||
private static Map<Class<?>, Map<String, Method>> loadedMethods = new HashMap<>(); | ||
|
||
/* | ||
* Cache of fields that we've found in particular classes | ||
*/ | ||
private static Map<Class<?>, Map<String, Field>> loadedFields = new HashMap<>(); | ||
|
||
/** | ||
* Gets the version string for NMS & OBC class paths | ||
* | ||
* @return The version string of OBC and NMS packages | ||
*/ | ||
public static String getVersion() { | ||
if (versionString == null) { | ||
String name = Bukkit.getServer().getClass().getPackage().getName(); | ||
versionString = name.substring(name.lastIndexOf('.') + 1) + "."; | ||
} | ||
|
||
return versionString; | ||
} | ||
|
||
/** | ||
* Get an NMS Class | ||
* | ||
* @param nmsClassName The name of the class | ||
* @return The class | ||
*/ | ||
public static Class<?> getNMSClass(String nmsClassName) { | ||
if (loadedNMSClasses.containsKey(nmsClassName)) { | ||
return loadedNMSClasses.get(nmsClassName); | ||
} | ||
|
||
String clazzName = "net.minecraft.server." + getVersion() + nmsClassName; | ||
Class<?> clazz; | ||
|
||
try { | ||
clazz = Class.forName(clazzName); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
return loadedNMSClasses.put(nmsClassName, null); | ||
} | ||
|
||
loadedNMSClasses.put(nmsClassName, clazz); | ||
return clazz; | ||
} | ||
|
||
/** | ||
* Get a class from the org.bukkit.craftbukkit package | ||
* | ||
* @param obcClassName the path to the class | ||
* @return the found class at the specified path | ||
*/ | ||
public synchronized static Class<?> getOBCClass(String obcClassName) { | ||
if (loadedOBCClasses.containsKey(obcClassName)) { | ||
return loadedOBCClasses.get(obcClassName); | ||
} | ||
|
||
String clazzName = "org.bukkit.craftbukkit." + getVersion() + obcClassName; | ||
Class<?> clazz; | ||
|
||
try { | ||
clazz = Class.forName(clazzName); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
loadedOBCClasses.put(obcClassName, null); | ||
return null; | ||
} | ||
|
||
loadedOBCClasses.put(obcClassName, clazz); | ||
return clazz; | ||
} | ||
|
||
/** | ||
* Get a classes constructor | ||
* | ||
* @param clazz The constructor class | ||
* @param params The parameters in the constructor | ||
* @return The constructor object | ||
*/ | ||
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... params) { | ||
try { | ||
return clazz.getDeclaredConstructor(params); | ||
} catch (NoSuchMethodException e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Get a method from a class that has the specific paramaters | ||
* | ||
* @param clazz The class we are searching | ||
* @param methodName The name of the method | ||
* @param params Any parameters that the method has | ||
* @return The method with appropriate paramaters | ||
*/ | ||
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) { | ||
if (!loadedMethods.containsKey(clazz)) { | ||
loadedMethods.put(clazz, new HashMap<>()); | ||
} | ||
|
||
Map<String, Method> methods = loadedMethods.get(clazz); | ||
|
||
if (methods.containsKey(methodName)) { | ||
return methods.get(methodName); | ||
} | ||
|
||
try { | ||
Method method = clazz.getDeclaredMethod(methodName, params); | ||
methods.put(methodName, method); | ||
loadedMethods.put(clazz, methods); | ||
return method; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
methods.put(methodName, null); | ||
loadedMethods.put(clazz, methods); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Get a field with a particular name from a class | ||
* | ||
* @param clazz The class | ||
* @param fieldName The name of the field | ||
* @return The field object | ||
*/ | ||
public static Field getField(Class<?> clazz, String fieldName) { | ||
if (!loadedFields.containsKey(clazz)) { | ||
loadedFields.put(clazz, new HashMap<>()); | ||
} | ||
|
||
Map<String, Field> fields = loadedFields.get(clazz); | ||
|
||
if (fields.containsKey(fieldName)) { | ||
return fields.get(fieldName); | ||
} | ||
|
||
try { | ||
Field field = clazz.getDeclaredField(fieldName); | ||
fields.put(fieldName, field); | ||
loadedFields.put(clazz, fields); | ||
return field; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
fields.put(fieldName, null); | ||
loadedFields.put(clazz, fields); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.