Posted July 6, 20205 yr 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!
July 6, 20205 yr Use LazyOptional#map and LazyOptional#orElse. private boolean returnFromACapability() { return player.getCapability(CapabilityProvider.SOME_CAP).map(cd -> { return cd.getBooleanValue(); }).orElse(false); }
July 6, 20205 yr Author 2 minutes ago, sciwhiz12 said: Use LazyOptional#map and LazyOptional#orElse. Absolute lifesaver, thank you
July 6, 20205 yr 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.
July 7, 20205 yr 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 July 7, 20205 yr by squidlex
July 7, 20205 yr Watch this: https://www.youtube.com/watch?v=FyCYva9DhsI 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.
July 7, 20205 yr Author 3 hours ago, Draco18s said: Watch this: https://www.youtube.com/watch?v=FyCYva9DhsI Finally got around to watching this, For anyone stumbling upon this thread I would highly recommend! Helped me see how much I'm treating a simple Minecraft mod like a huge project for my work, thanks for sharing Draco .
July 7, 20205 yr 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.
July 8, 20205 yr 7 hours ago, diesieben07 said: No timestamp 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.