ReflectionUtils.java 10 KB

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