ReflectionUtils.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.Set;
  9. import java.util.function.Function;
  10. import net.minecraft.block.Block;
  11. import net.minecraft.block.Blocks;
  12. import net.minecraft.block.FireBlock;
  13. import net.minecraft.command.Commands;
  14. import net.minecraft.entity.ai.goal.GoalSelector;
  15. import net.minecraft.entity.ai.goal.PrioritizedGoal;
  16. import net.minecraft.entity.player.PlayerAbilities;
  17. import net.minecraft.entity.player.ServerPlayerEntity;
  18. import net.minecraft.server.MinecraftServer;
  19. import net.minecraft.server.management.PlayerInteractionManager;
  20. import net.minecraft.server.management.PlayerList;
  21. import net.minecraft.util.FoodStats;
  22. import net.minecraft.util.ResourceLocation;
  23. import net.minecraft.world.Explosion;
  24. import net.minecraft.world.GameRules;
  25. import net.minecraft.world.GameType;
  26. import net.minecraft.world.IWorld;
  27. import net.minecraft.world.World;
  28. import net.minecraft.world.biome.Biome;
  29. import net.minecraft.world.biome.provider.BiomeProvider;
  30. import net.minecraft.world.gen.feature.IFeatureConfig;
  31. import net.minecraft.world.gen.feature.structure.Structure;
  32. import net.minecraft.world.storage.PlayerData;
  33. import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
  34. import static net.minecraftforge.fml.common.ObfuscationReflectionHelper.remapName;
  35. import org.apache.logging.log4j.LogManager;
  36. public class ReflectionUtils {
  37. // -----------------------------------------------------------------------------------
  38. // helper stuff
  39. // -----------------------------------------------------------------------------------
  40. public static Method getMethod(Class c, String name, Class... pars) {
  41. try {
  42. return ObfuscationReflectionHelper.findMethod(c, name, pars);
  43. } catch(Exception ex) {
  44. LogManager.getLogger().warn(name + " - " + ex);
  45. }
  46. return null;
  47. }
  48. public static Field getField(Class c, String field) {
  49. try {
  50. Field f = c.getDeclaredField(remapName(INameMappingService.Domain.FIELD, field));
  51. f.setAccessible(true);
  52. return f;
  53. } catch(Exception ex) {
  54. LogManager.getLogger().warn(field + " - " + ex);
  55. }
  56. return null;
  57. }
  58. private static <T> void setInt(T t, Field f, int i) {
  59. try {
  60. f.setInt(t, i);
  61. } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
  62. LogManager.getLogger().warn(f + " - " + ex);
  63. }
  64. }
  65. private static <T> int getInt(T t, Field f, int error) {
  66. try {
  67. return f.getInt(t);
  68. } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
  69. LogManager.getLogger().warn(f + " - " + ex);
  70. return error;
  71. }
  72. }
  73. private static <T> void setFloat(T t, Field f, float fl) {
  74. try {
  75. f.setFloat(t, fl);
  76. } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
  77. LogManager.getLogger().warn(f + " - " + ex);
  78. }
  79. }
  80. private static <T> float getFloat(T t, Field f, float error) {
  81. try {
  82. return f.getFloat(t);
  83. } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
  84. LogManager.getLogger().warn(f + " - " + ex);
  85. return error;
  86. }
  87. }
  88. private static <T> boolean getBoolean(T t, Field f, boolean error) {
  89. try {
  90. return f.getBoolean(t);
  91. } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
  92. LogManager.getLogger().warn(f + " - " + ex);
  93. return error;
  94. }
  95. }
  96. public static <T> T getFieldValue(Class<T> cast, Object o, Field f) {
  97. try {
  98. return (T) f.get(o);
  99. } catch(SecurityException | IllegalAccessException | IllegalArgumentException ex) {
  100. LogManager.getLogger().warn(f + " - " + ex);
  101. return null;
  102. }
  103. }
  104. public static void setFieldValue(Object instance, Field f, Object value) {
  105. try {
  106. f.set(instance, value);
  107. } catch(SecurityException | IllegalAccessException | IllegalArgumentException ex) {
  108. LogManager.getLogger().warn(f + " - " + ex);
  109. }
  110. }
  111. public static <T> T getFieldValue(Class<T> cast, Class c, Object instance, String field) {
  112. Field f = getField(c, field);
  113. if(f == null) {
  114. return null;
  115. }
  116. return getFieldValue(cast, instance, f);
  117. }
  118. // -----------------------------------------------------------------------------------
  119. // food stats
  120. // -----------------------------------------------------------------------------------
  121. private final static Field FOOD_EXHAUSTION_LEVEL = getField(FoodStats.class, "field_75126_c"); // foodExhaustionLevel
  122. public static void setExhaustion(FoodStats stats, float f) {
  123. setFloat(stats, FOOD_EXHAUSTION_LEVEL, f);
  124. }
  125. public static float getExhaustion(FoodStats stats) {
  126. return getFloat(stats, FOOD_EXHAUSTION_LEVEL, 0);
  127. }
  128. private final static Field FOOD_SATURATION_LEVEL = getField(FoodStats.class, "field_75125_b"); // foodSaturationLevel
  129. public static void setSaturation(FoodStats stats, float f) {
  130. setFloat(stats, FOOD_SATURATION_LEVEL, f);
  131. }
  132. // -----------------------------------------------------------------------------------
  133. // player stats
  134. // -----------------------------------------------------------------------------------
  135. private final static Field FLY_SPEED = getField(PlayerAbilities.class, "field_75096_f"); // flySpeed
  136. public static void setFlySpeed(PlayerAbilities cap, float f) {
  137. setFloat(cap, FLY_SPEED, f);
  138. }
  139. private final static Field WALK_SPEED = getField(PlayerAbilities.class, "field_75097_g"); // walkSpeed
  140. public static void setWalkSpeed(PlayerAbilities cap, float f) {
  141. setFloat(cap, WALK_SPEED, f);
  142. }
  143. // -----------------------------------------------------------------------------------
  144. // PlayerList
  145. // -----------------------------------------------------------------------------------
  146. private final static Method SET_GAMETYPE = getMethod(PlayerList.class, "func_72381_a",
  147. ServerPlayerEntity.class, ServerPlayerEntity.class, IWorld.class); // setPlayerGameTypeBasedOnOther
  148. public static void setPlayerGameTypeBasedOnOther(PlayerList pl, ServerPlayerEntity target, ServerPlayerEntity source, IWorld worldIn) {
  149. try {
  150. SET_GAMETYPE.invoke(pl, target, source, worldIn);
  151. } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
  152. LogManager.getLogger().warn("setPlayerGameTypeBasedOnOther - " + ex);
  153. }
  154. }
  155. // -----------------------------------------------------------------------------------
  156. // minecraft server
  157. // -----------------------------------------------------------------------------------
  158. private final static Field COMMAND_MANAGER = getField(MinecraftServer.class, "field_195579_af"); // commandManager
  159. public static void setCommandManager(MinecraftServer server, Commands manager) {
  160. setFieldValue(server, COMMAND_MANAGER, manager);
  161. }
  162. private final static Field MODE = getField(Explosion.class, "field_222260_b"); // mode
  163. public static void setNoBreakMode(Explosion ex) {
  164. Explosion.Mode mode = getFieldValue(Explosion.Mode.class, ex, MODE);
  165. if(mode == Explosion.Mode.DESTROY) {
  166. setFieldValue(ex, MODE, Explosion.Mode.BREAK);
  167. }
  168. }
  169. private final static Field INTEGER_VALUE = getField(GameRules.IntegerValue.class, "field_223566_a"); // value
  170. public static void setIntegerValue(GameRules.IntegerValue rule, int value) {
  171. setInt(rule, INTEGER_VALUE, value);
  172. }
  173. // -----------------------------------------------------------------------------------
  174. // game type setter without update for dirty hack
  175. // -----------------------------------------------------------------------------------
  176. private final static Field GAME_TYPE = getField(PlayerInteractionManager.class, "field_73091_c");
  177. public static void setGameType(PlayerInteractionManager m, GameType type) {
  178. setFieldValue(m, GAME_TYPE, type);
  179. }
  180. // -----------------------------------------------------------------------------------
  181. // for clearing ai
  182. // -----------------------------------------------------------------------------------
  183. private final static Field GOALS = getField(GoalSelector.class, "field_220892_d");
  184. public static Set<PrioritizedGoal> getGoals(GoalSelector gs) {
  185. return getFieldValue(Set.class, gs, GOALS);
  186. }
  187. private final static Method SET_FIRE_INFO = getMethod(FireBlock.class, "func_180686_a");
  188. public static void setFireInfo(Block b, int encouragement, int flammability) {
  189. FireBlock fireblock = (FireBlock) Blocks.FIRE;
  190. try {
  191. SET_FIRE_INFO.invoke(fireblock, b, encouragement, flammability);
  192. } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
  193. LogManager.getLogger().warn("setFireInfo - " + ex);
  194. }
  195. }
  196. private final static Field PLAYER_DATA = getField(MinecraftServer.class, "field_240766_e_");
  197. public static PlayerData getPlayerDataManager(MinecraftServer ms) {
  198. return getFieldValue(PlayerData.class, ms, PLAYER_DATA);
  199. }
  200. }