powns Posted October 9, 2017 Posted October 9, 2017 All i pretty much want to do is modify the vanilla Potion class. I want to change the field Potion.heal by replacing it with a new instance of a custom potion class that extends Potion. The code which i have in my post init method can be found down below: try{ Class potionClass = Potion.class; Field f = potionClass.getField("heal"); f.setAccessible(true); f.set(Potion.heal, ChromaPotion.recoloredHeal); } catch(Exception e){ e.printStackTrace(); } Even though i do set the field's accesibilty to true it still throws an IllegalAccessException... Could someone please explain me where I messed up? (yes i am aware that reflection isn't the most efficient way of doing stuff) Quote
jabelar Posted October 9, 2017 Posted October 9, 2017 Are you sure there is a field called "heal"? I looked at the Potion class and these are the fields I see: public class Potion extends net.minecraftforge.registries.IForgeRegistryEntry.Impl<Potion> { public static final RegistryNamespaced<ResourceLocation, Potion> REGISTRY = net.minecraftforge.registries.GameData.getWrapper(Potion.class); private final Map<IAttribute, AttributeModifier> attributeModifierMap = Maps.<IAttribute, AttributeModifier>newHashMap(); private final boolean isBadEffect; private final int liquidColor; private String name = ""; private int statusIconIndex = -1; private double effectiveness; private boolean beneficial; Also, if you really need to use Reflection (you usually don't though, so we should see if there is a way to accomplish what you want without it) you need to use the ReflectionHelper class. This is because the field and method names are obfuscated in the real game but in our development environment they are deobfuscated. So you need to reference the SRG mapped name as well, and the ReflectionHelper helps with that. If you don't, it will appear to work in the development environment but will fail when you build your mod. 1 Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
jeffryfisher Posted October 9, 2017 Posted October 9, 2017 5 hours ago, powns said: Even though i do set the field's accesibilty to true it still throws an IllegalAccessException 1) Step through your code in the debugger and tell us what you learn. Remember that in your console, you can click on the line number of an exception to be taken to the line that cracked. It's often useful to set a breakpoint just upstream. 2) Post the log file so we can see the full exception, "caused by", stack etc 1 Quote The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.
powns Posted October 10, 2017 Author Posted October 10, 2017 15 hours ago, jabelar said: Are you sure there is a field called "heal"? I looked at the Potion class and these are the fields I see: public class Potion extends net.minecraftforge.registries.IForgeRegistryEntry.Impl<Potion> { public static final RegistryNamespaced<ResourceLocation, Potion> REGISTRY = net.minecraftforge.registries.GameData.getWrapper(Potion.class); private final Map<IAttribute, AttributeModifier> attributeModifierMap = Maps.<IAttribute, AttributeModifier>newHashMap(); private final boolean isBadEffect; private final int liquidColor; private String name = ""; private int statusIconIndex = -1; private double effectiveness; private boolean beneficial; Also, if you really need to use Reflection (you usually don't though, so we should see if there is a way to accomplish what you want without it) you need to use the ReflectionHelper class. This is because the field and method names are obfuscated in the real game but in our development environment they are deobfuscated. So you need to reference the SRG mapped name as well, and the ReflectionHelper helps with that. If you don't, it will appear to work in the development environment but will fail when you build your mod. 10 hours ago, jeffryfisher said: 1) Step through your code in the debugger and tell us what you learn. Remember that in your console, you can click on the line number of an exception to be taken to the line that cracked. It's often useful to set a breakpoint just upstream. 2) Post the log file so we can see the full exception, "caused by", stack etc I got it to work after some time, i had to remove the "final" modifier before being able to change it. And for Jabelar, idk how its in new versions of forge but in 1.8.9, the version im coding in, this does work. Thank you both for your time! Quote
jabelar Posted October 10, 2017 Posted October 10, 2017 You need to note what I said about using Reflection helper. Even though your code might work in a development environment, it won't work once you build it unless you handle the obfuscated name of the field. It is very likely not called "heal" in the actual Minecraft jar. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.