Jump to content

Recommended Posts

Posted

I have an item that I need to change/add too the player's food and saturation without it being eaten/food. All the methods I need are in foodStats (Which is a FoodStats object) but it has the protected modifier, and I don't see a setter, only a getter. Any ideas on how I could manipulate it?

Posted

FoodStats has methods, call them. You can modify food and saturation with those.

 

Okay, scope is one area of Java that I find a little different than other languages, so maybe I'm wrong. 

 

The FoodStats() class has public methods, but the foodStats field in the EntityPlayer is a protected instance of FoodStats.  So I don't think he can call the methods, right?

 

EntityPlayer does have an addExhaustion() method that is public and affects foodStats, but that seems to be about it.

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

Posted

Yes, but can't I modify foodStats with a getter?

 

This is where I trip up in Java (compared to some other languages), but I think diesieben07 is confirming that the getter is actually passing the reference (people argue whether this is technically pass by reference or not, but that's the way I think of it) to the actual private field and so you can modify it once it is passed to you.  In other words, the returned value from the getter is not a copy, but rather the original field!

 

(In contrast, if the getter was properly encapsulated I think it would either have to create a copy, or otherwise break down the getting so that the private field wasn't passed in a modifiable way.)

 

If you look at the comment for the getter it says it gets the FoodStats object.

 

That's Java for you.  Note that I think this works differently with primitive types because Java doesn't make those objects.  So a getter for a list or something will pass the list, but getter for an int should not allow it to be modified.

 

I'm really just groping here though so you should get it confirmed from more reputable Java programmer.

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

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.