Minor performance improvement in MassiveCoreEngineCommandRegistration

This commit is contained in:
Olof Larsson 2015-01-08 09:22:40 +01:00
parent 16c3a15cdc
commit 8dac1eeece
2 changed files with 37 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package com.massivecraft.massivecore; package com.massivecraft.massivecore;
import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -138,16 +139,18 @@ public class MassiveCoreEngineCommandRegistration extends EngineAbstract
// GETTERS // GETTERS
// -------------------------------------------- // // -------------------------------------------- //
protected static Field SERVER_DOT_COMMAND_MAP = ReflectionUtil.getField(Bukkit.getServer().getClass(), "commandMap");
public static SimpleCommandMap getSimpleCommandMap() public static SimpleCommandMap getSimpleCommandMap()
{ {
Server server = Bukkit.getServer(); Server server = Bukkit.getServer();
return (SimpleCommandMap) ReflectionUtil.getField(server.getClass(), "commandMap", server); return (SimpleCommandMap) ReflectionUtil.getField(SERVER_DOT_COMMAND_MAP, server);
} }
protected static Field SIMPLE_COMMAND_MAP_DOT_KNOWN_COMMANDS = ReflectionUtil.getField(SimpleCommandMap.class, "knownCommands");
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static Map<String, Command> getSimpleCommandMapDotKnownCommands(SimpleCommandMap simpleCommandMap) public static Map<String, Command> getSimpleCommandMapDotKnownCommands(SimpleCommandMap simpleCommandMap)
{ {
return (Map<String, Command>) ReflectionUtil.getField(SimpleCommandMap.class, "knownCommands", simpleCommandMap); return (Map<String, Command>) ReflectionUtil.getField(SIMPLE_COMMAND_MAP_DOT_KNOWN_COMMANDS, simpleCommandMap);
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -50,6 +50,38 @@ public class ReflectionUtil
} }
} }
// -------------------------------------------- //
// FIELD GET
// -------------------------------------------- //
public static Field getField(Class<?> clazz, String fieldName)
{
try
{
Field field = clazz.getDeclaredField(fieldName);
makeAccessible(field);
return field;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public static Object getField(Field field, Object object)
{
try
{
return field.get(object);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
// -------------------------------------------- // // -------------------------------------------- //
// FIELD SIMPLE: GET & SET & TRANSFER // FIELD SIMPLE: GET & SET & TRANSFER
// -------------------------------------------- // // -------------------------------------------- //