Jump to content

[1.15.2] Make a method return a value from a capability


squidlex

Recommended Posts

Hi there,

 

I need a method to return a capability for a specific player.

Here's what I'm trying to achieve (obviously this code doesn't work).

private boolean returnFromACapability {
	player.getCapability(CapabilityProvider.SOME_CAP).ifPresent(cd -> {
				return cd.getBooleanValue(); // The method returnFromACapibility would return this
			});
	return false;
}

I don't want to make any static variables as instances of this class are used on the Server side by different players and I'd rather not make a load of public variables outside this method as I have many similar ones and my code would get very messy.

 

I think I'm too close to this and there's probably a super simple solution, so I figured I'd ask here.

Thanks for your help!

Link to comment
Share on other sites

Or refactor your code to access the capability directly instead of through a method so you can use ifPresent properly. 

(That is, your method serves no purpose)

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

20 hours ago, Draco18s said:

method serves no purpose

That's very true, I only have this as a method as my mod add's one feauture with loads of checks (capability, command, potion effect, config options etc.), so it helps readability to seperate them all into methods instead of having one massive if statement.

 

Actually looking over everything, I'll do it using ifPresent() to avoid any issues, thanks!

Edited by squidlex
Link to comment
Share on other sites

  • Like 1
  • Haha 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Whole video is worth a watch, but that time stamp is the presenter going through and refactoring a complex method out of existence one line at a time.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

7 hours ago, diesieben07 said:

No timestamp :P

Ha, whoops. 58:00

When I copied the link I thought I grabbed "at time" but apparently not!

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.