ReflectionUtils.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package me.km.utils;
  2. import java.lang.reflect.Field;
  3. import java.util.Map;
  4. import java.util.Random;
  5. import java.util.UUID;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.item.EntityItem;
  10. import net.minecraft.entity.passive.EntityVillager;
  11. import net.minecraft.entity.player.EntityPlayerMP;
  12. import net.minecraft.entity.player.PlayerCapabilities;
  13. import net.minecraft.entity.projectile.EntityArrow;
  14. import net.minecraft.server.management.PlayerList;
  15. import net.minecraft.util.FoodStats;
  16. import net.minecraft.world.Explosion;
  17. import net.minecraftforge.fml.relauncher.ReflectionHelper;
  18. public class ReflectionUtils
  19. {
  20. // -----------------------------------------------------------------------------------
  21. // helper stuff
  22. // -----------------------------------------------------------------------------------
  23. private static Field getField(Class c, String... field)
  24. {
  25. try
  26. {
  27. return ReflectionHelper.findField(c, field);
  28. }
  29. catch(SecurityException | ReflectionHelper.UnableToFindFieldException ex)
  30. {
  31. System.out.println(String.join(", ", field) + " - " + ex);
  32. }
  33. return null;
  34. }
  35. private static <T> void setInt(T t, Field f, int i)
  36. {
  37. try
  38. {
  39. f.setInt(t, i);
  40. }
  41. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  42. {
  43. System.out.println(f + " - " + ex);
  44. }
  45. }
  46. private static <T> int getInt(T t, Field f, int error)
  47. {
  48. try
  49. {
  50. return f.getInt(t);
  51. }
  52. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  53. {
  54. System.out.println(f + " - " + ex);
  55. return error;
  56. }
  57. }
  58. private static <T> void setFloat(T t, Field f, float fl)
  59. {
  60. try
  61. {
  62. f.setFloat(t, fl);
  63. }
  64. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  65. {
  66. System.out.println(f + " - " + ex);
  67. }
  68. }
  69. private static <T> float getFloat(T t, Field f, float error)
  70. {
  71. try
  72. {
  73. return f.getFloat(t);
  74. }
  75. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  76. {
  77. System.out.println(f + " - " + ex);
  78. return error;
  79. }
  80. }
  81. private static <T> boolean getBoolean(T t, Field f, boolean error)
  82. {
  83. try
  84. {
  85. return f.getBoolean(t);
  86. }
  87. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  88. {
  89. System.out.println(f + " - " + ex);
  90. return error;
  91. }
  92. }
  93. private static <T> T getFieldValue(Class<T> cast, Object o, Field f)
  94. {
  95. try
  96. {
  97. return (T) f.get(o);
  98. }
  99. catch(SecurityException | IllegalAccessException | IllegalArgumentException ex)
  100. {
  101. System.out.println(f + " - " + ex);
  102. return null;
  103. }
  104. }
  105. // -----------------------------------------------------------------------------------
  106. // villager stuff
  107. // -----------------------------------------------------------------------------------
  108. private final static Field CARRER_LEVEL = getField(EntityVillager.class, "field_175562_bw", "careerLevel");
  109. public static void setCareerLevel(EntityVillager v, int level)
  110. {
  111. setInt(v, CARRER_LEVEL, level);
  112. }
  113. // -----------------------------------------------------------------------------------
  114. // food stats
  115. // -----------------------------------------------------------------------------------
  116. private final static Field FOOD_EXHAUSTION_LEVEL = getField(FoodStats.class, "field_75126_c", "foodExhaustionLevel");
  117. public static void setExhaustion(FoodStats stats, float f)
  118. {
  119. setFloat(stats, FOOD_EXHAUSTION_LEVEL, f);
  120. }
  121. public static float getExhaustion(FoodStats stats)
  122. {
  123. return getFloat(stats, FOOD_EXHAUSTION_LEVEL, 0);
  124. }
  125. private final static Field FOOD_SATURATION_LEVEL = getField(FoodStats.class, "field_75125_b", "foodSaturationLevel");
  126. public static void setSaturation(FoodStats stats, float f)
  127. {
  128. setFloat(stats, FOOD_SATURATION_LEVEL, f);
  129. }
  130. public static float getSaturation(FoodStats stats)
  131. {
  132. return getFloat(stats, FOOD_SATURATION_LEVEL, 0);
  133. }
  134. // -----------------------------------------------------------------------------------
  135. // player stats
  136. // -----------------------------------------------------------------------------------
  137. private final static Field FLY_SPEED = getField(PlayerCapabilities.class, "field_75096_f", "flySpeed");
  138. public static void setFlySpeed(PlayerCapabilities cap, float f)
  139. {
  140. setFloat(cap, FLY_SPEED, f);
  141. }
  142. private final static Field WALK_SPEED = getField(PlayerCapabilities.class, "field_75097_g", "walkSpeed");
  143. public static void setWalkSpeed(PlayerCapabilities cap, float f)
  144. {
  145. setFloat(cap, WALK_SPEED, f);
  146. }
  147. // -----------------------------------------------------------------------------------
  148. // explosion stuff
  149. // -----------------------------------------------------------------------------------
  150. private final static Field IS_FLAMING = getField(Explosion.class, "field_77286_a", "isFlaming");
  151. private final static Field IS_SMOKING = getField(Explosion.class, "field_82755_b", "isSmoking");
  152. private final static Field EXPLOSION_RNG = getField(Explosion.class, "field_77290_i", "explosionRNG");
  153. private final static Field EXPLOSION_SIZE = getField(Explosion.class, "field_77280_f", "explosionSize");
  154. private final static Field EXPLODER = getField(Explosion.class, "field_77283_e", "exploder");
  155. public static boolean isFlaming(Explosion ex)
  156. {
  157. return getBoolean(ex, IS_FLAMING, false);
  158. }
  159. public static boolean isSmoking(Explosion ex)
  160. {
  161. return getBoolean(ex, IS_SMOKING, false);
  162. }
  163. public static Random getRandom(Explosion ex)
  164. {
  165. return getFieldValue(Random.class, ex, EXPLOSION_RNG);
  166. }
  167. public static float getSize(Explosion ex)
  168. {
  169. return getFloat(ex, EXPLOSION_SIZE, 1); // 1 for preventing zero dividing
  170. }
  171. public static void setSize(Explosion ex, float f)
  172. {
  173. setFloat(ex, EXPLOSION_SIZE, f);
  174. }
  175. public static Entity getExploder(Explosion ex)
  176. {
  177. return getFieldValue(Entity.class, ex, EXPLODER);
  178. }
  179. // -----------------------------------------------------------------------------------
  180. // random field gets
  181. // -----------------------------------------------------------------------------------
  182. private final static Field TIME_IN_GROUND = getField(EntityArrow.class, "field_184552_b", "timeInGround");
  183. public static int getArrowTimeInGround(EntityArrow arrow)
  184. {
  185. return getInt(arrow, TIME_IN_GROUND, 0);
  186. }
  187. private final static Field UUID_TO_PLAYER_MAP = getField(PlayerList.class, "field_177454_f", "uuidToPlayerMap");
  188. public static Map<UUID, EntityPlayerMP> getUuidToPlayerMap(PlayerList list)
  189. {
  190. return getFieldValue(Map.class, list, UUID_TO_PLAYER_MAP);
  191. }
  192. private final static Field ENTITY_ITEM_AGE = getField(EntityItem.class, "field_70292_b", "age");
  193. public static int getAge(EntityItem item)
  194. {
  195. return getInt(item, ENTITY_ITEM_AGE, 0);
  196. }
  197. public static void setAge(EntityItem item, int age)
  198. {
  199. setInt(item, ENTITY_ITEM_AGE, age);
  200. }
  201. private final static Field BLOCK_MATERIAL = getField(Block.class, "field_149764_J", "blockMaterial");
  202. public static Material getBlockMaterial(Block b)
  203. {
  204. return getFieldValue(Material.class, b, BLOCK_MATERIAL);
  205. }
  206. }