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.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.tileentity.SignTileEntity; import net.minecraft.util.FoodStats; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.Explosion; import net.minecraft.world.GameRules; import net.minecraft.world.GameType; import net.minecraft.world.World; import net.minecraft.world.server.ServerWorld; import net.minecraft.world.storage.IServerWorldInfo; 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 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 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 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 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 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 getFieldValue(Class 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 getFieldValue(Class 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); } // ----------------------------------------------------------------------------------- // 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 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); } } private final static Field SIGN_TEXT = getField(SignTileEntity.class, "field_145915_a"); public static ITextComponent getSignText(SignTileEntity ent, int index) { return getFieldValue(ITextComponent[].class, ent, SIGN_TEXT)[index]; } private final static Field WORLD_INFO = getField(World.class, "field_72986_A"); // worldInfo private final static Field SERVER_WORLD_INFO = getField(ServerWorld.class, "field_241103_E_"); // worldInfo public static void setWorldInfo(ServerWorld w, IServerWorldInfo info) { setFieldValue(w, WORLD_INFO, info); setFieldValue(w, SERVER_WORLD_INFO, info); } }