Jump to content

[1.13.2] Mapping non-empty LazyOptional to empty LazyOptional


Recommended Posts

Posted

I have this method that gets a LazyOptional of a player's inventory IItemHandler and searches for the first slot with an item that matches a predicate. If it finds a valid item, it returns a LazyOptional of a single-slot IItemHandler that wraps that slot. If it doesn't, it returns an empty LazyOptional.

 

Currently there's no direct way for LazyOptional#map to transform a non-empty LazyOptional into an empty one, so I've had to use EmptyHandler.INSTANCE as a sentinel value and call LazyOptional#filter to transform that value into an empty LazyOptional.

 

My main concern is that LazyOptional#filter has a comment inside saying "Should we allow this function at all?" due to it being a non-lazy operation, so I'm somewhat reluctant to use it.

 

Java's Optional#map allows the function to return null to create an empty Optional, but Forge's LazyOptional#map uses a NonNullFunction so returning null isn't an option.

 

Is there a better way to map a non-empty LazyOptional into an empty one?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
55 minutes ago, lehjr said:

What about returning Optional.empty() ?

 

Do you mean changing the lambda passed to LazyOptional#map to return Optional<IItemHandler> instead of just IItemHandler? I suppose that could work, though it seems a bit weird to nest the two different optional classes like that.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.