Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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!

Use LazyOptional#map and LazyOptional#orElse.

private boolean returnFromACapability() {
    return player.getCapability(CapabilityProvider.SOME_CAP).map(cd -> {
        return cd.getBooleanValue(); 
	}).orElse(false);
}

 

  • Author
2 minutes ago, sciwhiz12 said:

Use LazyOptional#map and LazyOptional#orElse.

Absolute lifesaver, thank you :)

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)

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.

  • Author
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

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.

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.

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.