ReflectionUtils.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package me.km.utils;
  2. import cpw.mods.modlauncher.api.INameMappingService;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.Map;
  7. import net.minecraft.command.Commands;
  8. import net.minecraft.entity.LivingEntity;
  9. import net.minecraft.entity.item.ItemEntity;
  10. import net.minecraft.entity.player.PlayerAbilities;
  11. import net.minecraft.entity.player.ServerPlayerEntity;
  12. import net.minecraft.entity.projectile.AbstractArrowEntity;
  13. import net.minecraft.server.MinecraftServer;
  14. import net.minecraft.server.management.PlayerList;
  15. import net.minecraft.util.DamageSource;
  16. import net.minecraft.util.FoodStats;
  17. import net.minecraft.util.ResourceLocation;
  18. import net.minecraft.world.IWorld;
  19. import net.minecraftforge.common.DimensionManager;
  20. import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
  21. import static net.minecraftforge.fml.common.ObfuscationReflectionHelper.remapName;
  22. import org.apache.logging.log4j.LogManager;
  23. public class ReflectionUtils
  24. {
  25. // -----------------------------------------------------------------------------------
  26. // helper stuff
  27. // -----------------------------------------------------------------------------------
  28. private static Method getMethod(Class c, String name, Class... pars)
  29. {
  30. try
  31. {
  32. return ObfuscationReflectionHelper.findMethod(c, name, pars);
  33. }
  34. catch(Exception ex)
  35. {
  36. LogManager.getLogger().warn(name + " - " + ex);
  37. }
  38. return null;
  39. }
  40. public static Field getField(Class c, String field)
  41. {
  42. try
  43. {
  44. Field f = c.getDeclaredField(remapName(INameMappingService.Domain.FIELD, field));
  45. f.setAccessible(true);
  46. return f;
  47. }
  48. catch(Exception ex)
  49. {
  50. LogManager.getLogger().warn(field + " - " + ex);
  51. }
  52. return null;
  53. }
  54. private static <T> void setInt(T t, Field f, int i)
  55. {
  56. try
  57. {
  58. f.setInt(t, i);
  59. }
  60. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  61. {
  62. LogManager.getLogger().warn(f + " - " + ex);
  63. }
  64. }
  65. private static <T> int getInt(T t, Field f, int error)
  66. {
  67. try
  68. {
  69. return f.getInt(t);
  70. }
  71. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  72. {
  73. LogManager.getLogger().warn(f + " - " + ex);
  74. return error;
  75. }
  76. }
  77. private static <T> void setFloat(T t, Field f, float fl)
  78. {
  79. try
  80. {
  81. f.setFloat(t, fl);
  82. }
  83. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  84. {
  85. LogManager.getLogger().warn(f + " - " + ex);
  86. }
  87. }
  88. private static <T> float getFloat(T t, Field f, float error)
  89. {
  90. try
  91. {
  92. return f.getFloat(t);
  93. }
  94. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  95. {
  96. LogManager.getLogger().warn(f + " - " + ex);
  97. return error;
  98. }
  99. }
  100. private static <T> boolean getBoolean(T t, Field f, boolean error)
  101. {
  102. try
  103. {
  104. return f.getBoolean(t);
  105. }
  106. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  107. {
  108. LogManager.getLogger().warn(f + " - " + ex);
  109. return error;
  110. }
  111. }
  112. public static <T> T getFieldValue(Class<T> cast, Object o, Field f)
  113. {
  114. try
  115. {
  116. return (T) f.get(o);
  117. }
  118. catch(SecurityException | IllegalAccessException | IllegalArgumentException ex)
  119. {
  120. LogManager.getLogger().warn(f + " - " + ex);
  121. return null;
  122. }
  123. }
  124. public static void setFieldValue(Object instance, Field f, Object value)
  125. {
  126. try
  127. {
  128. f.set(instance, value);
  129. }
  130. catch(SecurityException | IllegalAccessException | IllegalArgumentException ex)
  131. {
  132. LogManager.getLogger().warn(f + " - " + ex);
  133. }
  134. }
  135. public static <T> T getFieldValue(Class<T> cast, Class c, Object instance, String field)
  136. {
  137. Field f = getField(c, field);
  138. if(f == null)
  139. {
  140. return null;
  141. }
  142. return getFieldValue(cast, instance, f);
  143. }
  144. // -----------------------------------------------------------------------------------
  145. // food stats
  146. // -----------------------------------------------------------------------------------
  147. private final static Field FOOD_EXHAUSTION_LEVEL = getField(FoodStats.class, "field_75126_c"); // foodExhaustionLevel
  148. public static void setExhaustion(FoodStats stats, float f)
  149. {
  150. setFloat(stats, FOOD_EXHAUSTION_LEVEL, f);
  151. }
  152. public static float getExhaustion(FoodStats stats)
  153. {
  154. return getFloat(stats, FOOD_EXHAUSTION_LEVEL, 0);
  155. }
  156. private final static Field FOOD_SATURATION_LEVEL = getField(FoodStats.class, "field_75125_b"); // foodSaturationLevel
  157. public static void setSaturation(FoodStats stats, float f)
  158. {
  159. setFloat(stats, FOOD_SATURATION_LEVEL, f);
  160. }
  161. // -----------------------------------------------------------------------------------
  162. // player stats
  163. // -----------------------------------------------------------------------------------
  164. private final static Field FLY_SPEED = getField(PlayerAbilities.class, "field_75096_f"); // flySpeed
  165. public static void setFlySpeed(PlayerAbilities cap, float f)
  166. {
  167. setFloat(cap, FLY_SPEED, f);
  168. }
  169. private final static Field WALK_SPEED = getField(PlayerAbilities.class, "field_75097_g"); // walkSpeed
  170. public static void setWalkSpeed(PlayerAbilities cap, float f)
  171. {
  172. setFloat(cap, WALK_SPEED, f);
  173. }
  174. // -----------------------------------------------------------------------------------
  175. // PlayerList
  176. // -----------------------------------------------------------------------------------
  177. private final static Method SET_GAMETYPE = getMethod(PlayerList.class, "func_72381_a",
  178. ServerPlayerEntity.class, ServerPlayerEntity.class, IWorld.class); // setPlayerGameTypeBasedOnOther
  179. public static void setPlayerGameTypeBasedOnOther(PlayerList pl, ServerPlayerEntity target, ServerPlayerEntity source, IWorld worldIn)
  180. {
  181. try
  182. {
  183. SET_GAMETYPE.invoke(pl, target, source, worldIn);
  184. }
  185. catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
  186. {
  187. LogManager.getLogger().warn("setPlayerGameTypeBasedOnOther - " + ex);
  188. }
  189. }
  190. // -----------------------------------------------------------------------------------
  191. // random field gets
  192. // -----------------------------------------------------------------------------------
  193. private final static Field TIME_IN_GROUND = getField(AbstractArrowEntity.class, "field_184552_b"); // timeInGround
  194. public static int getArrowTimeInGround(AbstractArrowEntity arrow)
  195. {
  196. return getInt(arrow, TIME_IN_GROUND, 0);
  197. }
  198. private final static Field ENTITY_ITEM_AGE = getField(ItemEntity.class, "field_70292_b"); // age
  199. public static int getAge(ItemEntity item)
  200. {
  201. return getInt(item, ENTITY_ITEM_AGE, 0);
  202. }
  203. public static void setAge(ItemEntity item, int age)
  204. {
  205. setInt(item, ENTITY_ITEM_AGE, age);
  206. }
  207. // -----------------------------------------------------------------------------------
  208. // minecraft server
  209. // -----------------------------------------------------------------------------------
  210. private final static Field COMMAND_MANAGER = getField(MinecraftServer.class, "field_195579_af"); // commandManager
  211. public static void setCommandManager(MinecraftServer server, Commands manager)
  212. {
  213. setFieldValue(server, COMMAND_MANAGER, manager);
  214. }
  215. // -----------------------------------------------------------------------------------
  216. // DimensionManager
  217. // -----------------------------------------------------------------------------------
  218. private final static Field SAVED_ENTRIES = getField(DimensionManager.class, "savedEntries"); // forge name
  219. public static void removeDimensionSaveEntry(ResourceLocation rl)
  220. {
  221. // the map seems to be always empty ...
  222. getFieldValue(Map.class, null, SAVED_ENTRIES).remove(rl);
  223. }
  224. // -----------------------------------------------------------------------------------
  225. // LivingEntity stuff
  226. // -----------------------------------------------------------------------------------
  227. private final static Method APPLY_POTION_DAMAGE_CALCULATIONS = getMethod(
  228. LivingEntity.class,
  229. "func_70672_c", DamageSource.class, float.class);
  230. public static float applyPotionDamageCalculations(LivingEntity liv, DamageSource ds, float damage)
  231. {
  232. try
  233. {
  234. return (float) APPLY_POTION_DAMAGE_CALCULATIONS.invoke(liv, ds, damage);
  235. }
  236. catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
  237. {
  238. LogManager.getLogger().warn("applyPotionDamageCalculations - " + ex);
  239. return damage;
  240. }
  241. }
  242. private final static Method DAMAGE_ARMOR = getMethod(
  243. LivingEntity.class,
  244. "func_70675_k", float.class);
  245. public static void damageArmor(LivingEntity liv, float damage)
  246. {
  247. try
  248. {
  249. DAMAGE_ARMOR.invoke(liv, damage);
  250. }
  251. catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
  252. {
  253. LogManager.getLogger().warn("damageArmor - " + ex);
  254. }
  255. }
  256. }