Jump to content

Issues with trying to modify vanilla classes


powns

Recommended Posts

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)

Link to comment
Share on other sites

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.

  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

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

  • Like 1

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.