ReflectionUtils.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package me.km.utils;
  2. import java.lang.reflect.Field;
  3. import java.util.Map;
  4. import java.util.UUID;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.entity.item.EntityItem;
  8. import net.minecraft.entity.passive.EntityVillager;
  9. import net.minecraft.entity.player.EntityPlayerMP;
  10. import net.minecraft.entity.player.PlayerCapabilities;
  11. import net.minecraft.entity.projectile.EntityArrow;
  12. import net.minecraft.server.management.PlayerList;
  13. import net.minecraft.util.FoodStats;
  14. import net.minecraftforge.fml.relauncher.ReflectionHelper;
  15. public class ReflectionUtils
  16. {
  17. // -----------------------------------------------------------------------------------
  18. // helper stuff
  19. // -----------------------------------------------------------------------------------
  20. private static Field getField(Class c, String... field)
  21. {
  22. try
  23. {
  24. return ReflectionHelper.findField(c, field);
  25. }
  26. catch(SecurityException | ReflectionHelper.UnableToFindFieldException ex)
  27. {
  28. System.out.println(String.join(", ", field) + " - " + ex);
  29. }
  30. return null;
  31. }
  32. private static <T> void setInt(T t, Field f, int i)
  33. {
  34. try
  35. {
  36. f.setInt(t, i);
  37. }
  38. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  39. {
  40. System.out.println(f + " - " + ex);
  41. }
  42. }
  43. private static <T> int getInt(T t, Field f, int error)
  44. {
  45. try
  46. {
  47. return f.getInt(t);
  48. }
  49. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  50. {
  51. System.out.println(f + " - " + ex);
  52. return error;
  53. }
  54. }
  55. private static <T> void setFloat(T t, Field f, float fl)
  56. {
  57. try
  58. {
  59. f.setFloat(t, fl);
  60. }
  61. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  62. {
  63. System.out.println(f + " - " + ex);
  64. }
  65. }
  66. private static <T> float getFloat(T t, Field f, float error)
  67. {
  68. try
  69. {
  70. return f.getFloat(t);
  71. }
  72. catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
  73. {
  74. System.out.println(f + " - " + ex);
  75. return error;
  76. }
  77. }
  78. private static <T> T getFieldValue(Class<T> cast, Object o, Field f)
  79. {
  80. try
  81. {
  82. return (T) f.get(o);
  83. }
  84. catch(SecurityException | IllegalAccessException | IllegalArgumentException ex)
  85. {
  86. System.out.println(f + " - " + ex);
  87. return null;
  88. }
  89. }
  90. // -----------------------------------------------------------------------------------
  91. // villager stuff
  92. // -----------------------------------------------------------------------------------
  93. private final static Field CARRER_LEVEL = getField(EntityVillager.class, "field_175562_bw", "careerLevel");
  94. public static void setCareerLevel(EntityVillager v, int level)
  95. {
  96. setInt(v, CARRER_LEVEL, level);
  97. }
  98. // -----------------------------------------------------------------------------------
  99. // food stats
  100. // -----------------------------------------------------------------------------------
  101. private final static Field FOOD_EXHAUSTION_LEVEL = getField(FoodStats.class, "field_75126_c", "foodExhaustionLevel");
  102. public static void setExhaustion(FoodStats stats, float f)
  103. {
  104. setFloat(stats, FOOD_EXHAUSTION_LEVEL, f);
  105. }
  106. public static float getExhaustion(FoodStats stats)
  107. {
  108. return getFloat(stats, FOOD_EXHAUSTION_LEVEL, 0);
  109. }
  110. private final static Field FOOD_SATURATION_LEVEL = getField(FoodStats.class, "field_75125_b", "foodSaturationLevel");
  111. public static void setSaturation(FoodStats stats, float f)
  112. {
  113. setFloat(stats, FOOD_SATURATION_LEVEL, f);
  114. }
  115. public static float getSaturation(FoodStats stats)
  116. {
  117. return getFloat(stats, FOOD_SATURATION_LEVEL, 0);
  118. }
  119. // -----------------------------------------------------------------------------------
  120. // player stats
  121. // -----------------------------------------------------------------------------------
  122. private final static Field FLY_SPEED = getField(PlayerCapabilities.class, "field_75096_f", "flySpeed");
  123. public static void setFlySpeed(PlayerCapabilities cap, float f)
  124. {
  125. setFloat(cap, FLY_SPEED, f);
  126. }
  127. private final static Field WALK_SPEED = getField(PlayerCapabilities.class, "field_75097_g", "walkSpeed");
  128. public static void setWalkSpeed(PlayerCapabilities cap, float f)
  129. {
  130. setFloat(cap, WALK_SPEED, f);
  131. }
  132. // -----------------------------------------------------------------------------------
  133. // random field gets
  134. // -----------------------------------------------------------------------------------
  135. private final static Field TIME_IN_GROUND = getField(EntityArrow.class, "field_184552_b", "timeInGround");
  136. public static int getArrowTimeInGround(EntityArrow arrow)
  137. {
  138. return getInt(arrow, TIME_IN_GROUND, 0);
  139. }
  140. private final static Field UUID_TO_PLAYER_MAP = getField(PlayerList.class, "field_177454_f", "uuidToPlayerMap");
  141. public static Map<UUID, EntityPlayerMP> getUuidToPlayerMap(PlayerList list)
  142. {
  143. return getFieldValue(Map.class, list, UUID_TO_PLAYER_MAP);
  144. }
  145. private final static Field ENTITY_ITEM_AGE = getField(EntityItem.class, "field_70292_b", "age");
  146. public static int getAge(EntityItem item)
  147. {
  148. return getInt(item, UUID_TO_PLAYER_MAP, 0);
  149. }
  150. private final static Field BLOCK_MATERIAL = getField(Block.class, "field_149764_J", "blockMaterial");
  151. public static Material getBlockMaterial(Block b)
  152. {
  153. return getFieldValue(Material.class, b, BLOCK_MATERIAL);
  154. }
  155. }