123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- 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.HashMap;
- import java.util.Map;
- import net.minecraft.command.Commands;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.LivingEntity;
- import net.minecraft.entity.item.ItemEntity;
- import net.minecraft.entity.player.PlayerAbilities;
- import net.minecraft.entity.player.ServerPlayerEntity;
- import net.minecraft.server.MinecraftServer;
- import net.minecraft.server.management.PlayerList;
- import net.minecraft.state.IProperty;
- import net.minecraft.state.properties.BlockStateProperties;
- import net.minecraft.util.DamageSource;
- import net.minecraft.util.FoodStats;
- import net.minecraft.util.ResourceLocation;
- import net.minecraft.world.Explosion;
- import net.minecraft.world.GameRules;
- import net.minecraft.world.IWorld;
- import net.minecraft.world.World;
- import net.minecraft.world.storage.WorldInfo;
- import net.minecraftforge.common.DimensionManager;
- 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
- // -----------------------------------------------------------------------------------
-
- private 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, IWorld.class); // setPlayerGameTypeBasedOnOther
-
- public static void setPlayerGameTypeBasedOnOther(PlayerList pl, ServerPlayerEntity target, ServerPlayerEntity source, IWorld worldIn)
- {
- try
- {
- SET_GAMETYPE.invoke(pl, target, source, worldIn);
- }
- catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
- {
- LogManager.getLogger().warn("setPlayerGameTypeBasedOnOther - " + ex);
- }
- }
-
- // -----------------------------------------------------------------------------------
- // random field gets
- // -----------------------------------------------------------------------------------
-
- private final static Field ENTITY_ITEM_AGE = getField(ItemEntity.class, "field_70292_b"); // age
-
- public static int getAge(ItemEntity item)
- {
- return getInt(item, ENTITY_ITEM_AGE, 0);
- }
-
- public static void setAge(ItemEntity item, int age)
- {
- setInt(item, ENTITY_ITEM_AGE, age);
- }
-
- // -----------------------------------------------------------------------------------
- // minecraft server
- // -----------------------------------------------------------------------------------
-
- private final static Field COMMAND_MANAGER = getField(MinecraftServer.class, "field_195579_af"); // commandManager
-
- public static void setCommandManager(MinecraftServer server, Commands manager)
- {
- setFieldValue(server, COMMAND_MANAGER, manager);
- }
-
- // -----------------------------------------------------------------------------------
- // DimensionManager
- // -----------------------------------------------------------------------------------
-
- private final static Field SAVED_ENTRIES = getField(DimensionManager.class, "savedEntries"); // forge name
- public static void removeDimensionSaveEntry(ResourceLocation rl)
- {
- // the map seems to be always empty ...
- getFieldValue(Map.class, null, SAVED_ENTRIES).remove(rl);
- }
-
- // -----------------------------------------------------------------------------------
- // LivingEntity stuff
- // -----------------------------------------------------------------------------------
-
- private final static Method APPLY_POTION_DAMAGE_CALCULATIONS = getMethod(
- LivingEntity.class,
- "func_70672_c", DamageSource.class, float.class);
-
- public static float applyPotionDamageCalculations(LivingEntity liv, DamageSource ds, float damage)
- {
- try
- {
- return (float) APPLY_POTION_DAMAGE_CALCULATIONS.invoke(liv, ds, damage);
- }
- catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
- {
- LogManager.getLogger().warn("applyPotionDamageCalculations - " + ex);
- return damage;
- }
- }
-
- private final static Method DAMAGE_ARMOR = getMethod(
- LivingEntity.class,
- "func_70675_k", float.class);
-
- public static void damageArmor(LivingEntity liv, float damage)
- {
- try
- {
- DAMAGE_ARMOR.invoke(liv, damage);
- }
- catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
- {
- LogManager.getLogger().warn("damageArmor - " + ex);
- }
- }
-
- // -----------------------------------------------------------------------------------
- // explosion stuff
- // -----------------------------------------------------------------------------------
-
- private final static Field MODE = getField(Explosion.class, "field_222260_b"); // mode
- private final static Field EXPLODER = getField(Explosion.class, "field_77283_e"); // exploder
- private final static Field SIZE = getField(Explosion.class, "field_77280_f"); // size
- 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);
- }
- }
-
- public static Entity getExploder(Explosion ex)
- {
- return getFieldValue(Entity.class, ex, EXPLODER);
- }
-
- public static float getSize(Explosion ex)
- {
- return getFloat(ex, SIZE, 1);
- }
-
- // -----------------------------------------------------------------------------------
- // world info
- // -----------------------------------------------------------------------------------
-
- private final static Field WORLD_INFO = getField(World.class, "field_72986_A"); // worldInfo
- public static void setWorldInfo(World w, WorldInfo info)
- {
- setFieldValue(w, WORLD_INFO, info);
- }
-
- // -----------------------------------------------------------------------------------
- // gamerule integer value
- // -----------------------------------------------------------------------------------
-
- 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);
- }
- }
|