Jump to content

How to access protected methods inside sorted packages?


Jamezo97

Recommended Posts

tldr: How do I either turn off class sorting while forge is being installed, or how do I get around the inability to access protected methods and fields?

--------------------------------------------------------------------------

Hey guys,

I'm the author of the 'MyPeople Mod', and am currently trying to convert my mod over to Forge.

 

I was originally using Risugami's Modloader, but many dislike Risguami's Modloader and Forge is used by many now, and I can do much more with Forge.

 

So I installed forge fine, opened up eclipse, created a new package, and then imported my source code. I fixed as many errors that I could, where fields were just renamed and such, or package declarations were now different. So that went well too.

 

However I then looked in the error list, and almost 75% of all the errors I now have to fix, are accessor errors.

 

For example, in my mod the clone try's to make mob's attack it (like a mob would attack a player), so to do that, I check to see whether the entity is using the new AI system, with the method: "protected boolean isAIEnabled()"

 

However, because my class is inside a different package than the 'net.minecraft.entity' package, I can't access that method because it is set to 'protected', and to access it I need to be in the same package.

 

Most of my errors are all because of the protected keyword. With Risugami's Modloader, it doesn't sort the classes into packages, so I just place all my code into the main package of 'net.minecraft.src' and because it's in the same package as 'EntityLiving', it can run the method fine.

 

So, my question is:

How do I either turn off class sorting while forge is being installed, or how do I get around this?

 

Thankyou! :) If you need any more information please just ask.

Link to comment
Share on other sites

as reflection always gives a performance hit.

I thought so as well, but under some circumstances there's virtually no cost at all. Method invoking via reflection according to this http://stackoverflow.com/a/414823/1017211 has the same cost as calling it directly. I don't know about fields, but it could be optimized too. (This all is true probably only if the object from reflection is cached.)

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Link to comment
Share on other sites

I can think of 3 ways right away:

  • Make your mod a coremod and use Access Transformers
  • Use Java reflection
  • Make a utility class inside the package you need access to which only contains methods like this:

public static void accessRandomMethod(RandomType instance) {
    instance.randomMethod();
}

I'd go for method 1 or 3 as reflection always gives a performance hit.

Ah, yes well I looked at the first option, and that looks, complicated :/

The second option, I had thought of that, but that requires updating all the method names every time minecraft updates :/

And the third otpion, I thought of that also, but when the mod is compiled, the class would just be wasted space because it's all put back into one package.

 

So I think i'll just go with the second option, it's only going to take up an extra few bytes worth of class files, so I think it's the best way to go.

Thankyou!

:)

Link to comment
Share on other sites

Everything is turned to public at runtime because of the runtime deobfusication, so your best, and easiest bet is to use a custom decompile time access transformer config. However, I would be weary, as 90% of the time, if you're trying to access a private or protected function there are better ways to do it. However it seems you did find a case where it should be publicized. I welcome the PR for it.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

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.