123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package me.km.utils;
- import java.lang.reflect.Field;
- import java.util.Map;
- import java.util.UUID;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.item.EntityItem;
- import net.minecraft.entity.passive.EntityVillager;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.entity.player.PlayerCapabilities;
- import net.minecraft.entity.projectile.EntityArrow;
- import net.minecraft.server.management.PlayerList;
- import net.minecraft.util.FoodStats;
- import net.minecraftforge.fml.relauncher.ReflectionHelper;
- public class ReflectionUtils
- {
- // -----------------------------------------------------------------------------------
- // helper stuff
- // -----------------------------------------------------------------------------------
-
- private static Field getField(Class c, String... field)
- {
- try
- {
- return ReflectionHelper.findField(c, field);
- }
- catch(SecurityException | ReflectionHelper.UnableToFindFieldException ex)
- {
- System.out.println(String.join(", ", 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)
- {
- System.out.println(f + " - " + ex);
- }
- }
-
- private static <T> int getInt(T t, Field f, int error)
- {
- try
- {
- return f.getInt(t);
- }
- catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
- {
- System.out.println(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)
- {
- System.out.println(f + " - " + ex);
- }
- }
-
- private static <T> float getFloat(T t, Field f, float error)
- {
- try
- {
- return f.getFloat(t);
- }
- catch(SecurityException | IllegalArgumentException | IllegalAccessException | NullPointerException ex)
- {
- System.out.println(f + " - " + ex);
- return error;
- }
- }
-
- private static <T> T getFieldValue(Class<T> cast, Object o, Field f)
- {
- try
- {
- return (T) f.get(o);
- }
- catch(SecurityException | IllegalAccessException | IllegalArgumentException ex)
- {
- System.out.println(f + " - " + ex);
- return null;
- }
- }
-
- // -----------------------------------------------------------------------------------
- // villager stuff
- // -----------------------------------------------------------------------------------
-
- private final static Field CARRER_LEVEL = getField(EntityVillager.class, "field_175562_bw", "careerLevel");
-
- public static void setCareerLevel(EntityVillager v, int level)
- {
- setInt(v, CARRER_LEVEL, level);
- }
-
- // -----------------------------------------------------------------------------------
- // 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);
- }
-
- public static float getSaturation(FoodStats stats)
- {
- return getFloat(stats, FOOD_SATURATION_LEVEL, 0);
- }
-
- // -----------------------------------------------------------------------------------
- // player stats
- // -----------------------------------------------------------------------------------
-
- private final static Field FLY_SPEED = getField(PlayerCapabilities.class, "field_75096_f", "flySpeed");
-
- public static void setFlySpeed(PlayerCapabilities cap, float f)
- {
- setFloat(cap, FLY_SPEED, f);
- }
-
- private final static Field WALK_SPEED = getField(PlayerCapabilities.class, "field_75097_g", "walkSpeed");
-
- public static void setWalkSpeed(PlayerCapabilities cap, float f)
- {
- setFloat(cap, WALK_SPEED, f);
- }
-
- // -----------------------------------------------------------------------------------
- // random field gets
- // -----------------------------------------------------------------------------------
-
- private final static Field TIME_IN_GROUND = getField(EntityArrow.class, "field_184552_b", "timeInGround");
-
- public static int getArrowTimeInGround(EntityArrow arrow)
- {
- return getInt(arrow, TIME_IN_GROUND, 0);
- }
-
- private final static Field UUID_TO_PLAYER_MAP = getField(PlayerList.class, "field_177454_f", "uuidToPlayerMap");
-
- public static Map<UUID, EntityPlayerMP> getUuidToPlayerMap(PlayerList list)
- {
- return getFieldValue(Map.class, list, UUID_TO_PLAYER_MAP);
- }
-
- private final static Field ENTITY_ITEM_AGE = getField(EntityItem.class, "field_70292_b", "age");
-
- public static int getAge(EntityItem item)
- {
- return getInt(item, UUID_TO_PLAYER_MAP, 0);
- }
-
- private final static Field BLOCK_MATERIAL = getField(Block.class, "field_149764_J", "blockMaterial");
-
- public static Material getBlockMaterial(Block b)
- {
- return getFieldValue(Material.class, b, BLOCK_MATERIAL);
- }
- }
|