ReflectionUtils.java 11 KB

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