123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package me.km.utils;
- import cpw.mods.modlauncher.api.INameMappingService;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Set;
- import net.minecraft.block.Block;
- import net.minecraft.block.Blocks;
- import net.minecraft.block.FireBlock;
- import net.minecraft.command.Commands;
- import net.minecraft.entity.ai.goal.GoalSelector;
- import net.minecraft.entity.ai.goal.PrioritizedGoal;
- import net.minecraft.entity.player.PlayerAbilities;
- import net.minecraft.entity.player.ServerPlayerEntity;
- import net.minecraft.resources.DataPackRegistries;
- import net.minecraft.server.MinecraftServer;
- import net.minecraft.server.management.PlayerInteractionManager;
- import net.minecraft.server.management.PlayerList;
- import net.minecraft.util.FoodStats;
- import net.minecraft.world.Explosion;
- import net.minecraft.world.GameRules;
- import net.minecraft.world.GameType;
- import net.minecraft.world.IWorld;
- import net.minecraft.world.server.ServerWorld;
- import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
- import static net.minecraftforge.fml.common.ObfuscationReflectionHelper.remapName;
- import org.apache.logging.log4j.LogManager;
- public class ReflectionUtils {
- // -----------------------------------------------------------------------------------
- // helper stuff
- // -----------------------------------------------------------------------------------
- public static Method getMethod(Class c, String name, Class... pars) {
- try {
- return ObfuscationReflectionHelper.findMethod(c, name, pars);
- } catch(Exception ex) {
- LogManager.getLogger().warn(name + " - " + ex);
- }
- return null;
- }
- public static Field getField(Class c, String field) {
- try {
- Field f = c.getDeclaredField(remapName(INameMappingService.Domain.FIELD, field));
- f.setAccessible(true);
- return f;
- } catch(Exception ex) {
- LogManager.getLogger().warn(field + " - " + ex);
- }
- return null;
- }
- private static <T> void setInt(T t, Field f, int i) {
- try {
- f.setInt(t, i);
- } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
- LogManager.getLogger().warn(f + " - " + ex);
- }
- }
- private static <T> int getInt(T t, Field f, int error) {
- try {
- return f.getInt(t);
- } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
- LogManager.getLogger().warn(f + " - " + ex);
- return error;
- }
- }
- private static <T> void setFloat(T t, Field f, float fl) {
- try {
- f.setFloat(t, fl);
- } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
- LogManager.getLogger().warn(f + " - " + ex);
- }
- }
- private static <T> float getFloat(T t, Field f, float error) {
- try {
- return f.getFloat(t);
- } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
- LogManager.getLogger().warn(f + " - " + ex);
- return error;
- }
- }
- private static <T> boolean getBoolean(T t, Field f, boolean error) {
- try {
- return f.getBoolean(t);
- } catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex) {
- LogManager.getLogger().warn(f + " - " + ex);
- return error;
- }
- }
- public static <T> T getFieldValue(Class<T> cast, Object o, Field f) {
- try {
- return (T) f.get(o);
- } catch(SecurityException | IllegalAccessException | IllegalArgumentException ex) {
- LogManager.getLogger().warn(f + " - " + ex);
- return null;
- }
- }
- public static void setFieldValue(Object instance, Field f, Object value) {
- try {
- f.set(instance, value);
- } catch(SecurityException | IllegalAccessException | IllegalArgumentException ex) {
- LogManager.getLogger().warn(f + " - " + ex);
- }
- }
- public static <T> T getFieldValue(Class<T> cast, Class c, Object instance, String field) {
- Field f = getField(c, field);
- if(f == null) {
- return null;
- }
- return getFieldValue(cast, instance, f);
- }
- // -----------------------------------------------------------------------------------
- // food stats
- // -----------------------------------------------------------------------------------
- private final static Field FOOD_EXHAUSTION_LEVEL = getField(FoodStats.class, "field_75126_c"); // foodExhaustionLevel
- public static void setExhaustion(FoodStats stats, float f) {
- setFloat(stats, FOOD_EXHAUSTION_LEVEL, f);
- }
- public static float getExhaustion(FoodStats stats) {
- return getFloat(stats, FOOD_EXHAUSTION_LEVEL, 0);
- }
- private final static Field FOOD_SATURATION_LEVEL = getField(FoodStats.class, "field_75125_b"); // foodSaturationLevel
- public static void setSaturation(FoodStats stats, float f) {
- setFloat(stats, FOOD_SATURATION_LEVEL, f);
- }
- // -----------------------------------------------------------------------------------
- // player stats
- // -----------------------------------------------------------------------------------
- private final static Field FLY_SPEED = getField(PlayerAbilities.class, "field_75096_f"); // flySpeed
- public static void setFlySpeed(PlayerAbilities cap, float f) {
- setFloat(cap, FLY_SPEED, f);
- }
- private final static Field WALK_SPEED = getField(PlayerAbilities.class, "field_75097_g"); // walkSpeed
- public static void setWalkSpeed(PlayerAbilities cap, float f) {
- setFloat(cap, WALK_SPEED, f);
- }
- // -----------------------------------------------------------------------------------
- // PlayerList
- // -----------------------------------------------------------------------------------
- private final static Method SET_GAMETYPE = getMethod(PlayerList.class, "func_72381_a",
- ServerPlayerEntity.class, ServerPlayerEntity.class, ServerWorld.class); // setPlayerGameTypeBasedOnOther
- public static void setPlayerGameTypeBasedOnOther(PlayerList pl, ServerPlayerEntity target, ServerPlayerEntity source, ServerWorld sw) {
- try {
- SET_GAMETYPE.invoke(pl, target, source, sw);
- } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
- LogManager.getLogger().warn("setPlayerGameTypeBasedOnOther - " + ex);
- }
- }
- // -----------------------------------------------------------------------------------
- // minecraft server
- // -----------------------------------------------------------------------------------
- private final static Field COMMAND_MANAGER = getField(DataPackRegistries.class, "field_240953_c_"); // commands
- public static void setCommandManager(MinecraftServer server, Commands manager) {
- setFieldValue(server.getDataPackRegistries(), COMMAND_MANAGER, manager);
- }
- private final static Field MODE = getField(Explosion.class, "field_222260_b"); // mode
- public static void setNoBreakMode(Explosion ex) {
- Explosion.Mode mode = getFieldValue(Explosion.Mode.class, ex, MODE);
- if(mode == Explosion.Mode.DESTROY) {
- setFieldValue(ex, MODE, Explosion.Mode.BREAK);
- }
- }
- private final static Field INTEGER_VALUE = getField(GameRules.IntegerValue.class, "field_223566_a"); // value
- public static void setIntegerValue(GameRules.IntegerValue rule, int value) {
- setInt(rule, INTEGER_VALUE, value);
- }
- // -----------------------------------------------------------------------------------
- // game type setter without update for dirty hack
- // -----------------------------------------------------------------------------------
- private final static Field GAME_TYPE = getField(PlayerInteractionManager.class, "field_73091_c");
- public static void setGameType(PlayerInteractionManager m, GameType type) {
- setFieldValue(m, GAME_TYPE, type);
- }
- // -----------------------------------------------------------------------------------
- // for clearing ai
- // -----------------------------------------------------------------------------------
- private final static Field GOALS = getField(GoalSelector.class, "field_220892_d");
- public static Set<PrioritizedGoal> getGoals(GoalSelector gs) {
- return getFieldValue(Set.class, gs, GOALS);
- }
- private final static Method SET_FIRE_INFO = getMethod(FireBlock.class, "func_180686_a", Block.class, int.class, int.class);
- public static void setFireInfo(Block b, int encouragement, int flammability) {
- FireBlock fireblock = (FireBlock) Blocks.FIRE;
- try {
- SET_FIRE_INFO.invoke(fireblock, b, encouragement, flammability);
- } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
- LogManager.getLogger().warn("setFireInfo - " + ex);
- }
- }
- }
|