Jump to content

Accessing protected variables in net.minecraft.src?


Recommended Posts

As I'm porting my mod to Forge, the mod now has its own package


import net.minecraft.src.*;


There are many places in my code where I need to access or change the value of a protected variable, i.e

entitymob.entityToAttack in the following method:


public static void repelMobs(Entity entity1, Double dist, World worldObj)
	List list = worldObj.getEntitiesWithinAABBExcludingEntity(entity1, entity1.boundingBox.expand(dist, 4D, dist));
        for(int i = 0; i < list.size(); i++)
        	Entity entity = (Entity) list.get(i);
            if(!(entity instanceof EntityMob))
            EntityMob entitymob = (EntityMob) entity;
  entitymob.entityToAttack = null;


However that variable entityToAttack is now unaccessible from my package, as it is a protected variable that does not have a Getter/Setter.  Is there a way around this?  I guess it could be done with reflection (which I'm not too familiar with). Or if there is an easier way, I'm all ears. Thanks in advance!


Link to comment
Share on other sites

A child class of a base class can always access the protected members of that base class.

Reflection can access other protected vars, but has a speed cost.

If you ever need to do it in a speed sensitive area of code (hundreds of times in a tight loop for example), then submit a patch to have forge change it to public.


There are a few other things, but that is the gist.

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.

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.

  • Create New...

Important Information

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