Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capability contains key check #1510

Open
Swedz opened this issue Sep 3, 2024 · 5 comments
Open

Capability contains key check #1510

Swedz opened this issue Sep 3, 2024 · 5 comments
Labels
enhancement New (or improvement to existing) feature or request

Comments

@Swedz
Copy link

Swedz commented Sep 3, 2024

It would be nice to be able to check if a capability exists for a given key. The only ways to do it right now is with reflection or a mixin. For example, in BlockCapability a method to check there is a provider registered for a Block (this would apply to all capabilities).

My use case for this is to check if an Item (from an ItemStack), specifically a BlockItem, has a capability registered for the Block.

@Swedz Swedz added the enhancement New (or improvement to existing) feature or request label Sep 3, 2024
@Swedz
Copy link
Author

Swedz commented Sep 3, 2024

The way I described it may not be the best way to have something like this, since the provider may not actually add the capability to the given block. For my use case, I can use a default BlockState and BlockEntity with a BlockPos of BlockPos.ZERO (since its irrelevant) which works fine. However some sort of way to check this without this kind of hackiness would be nice.

@Shadows-of-Fire
Copy link
Contributor

For whatever reason we supply this functionality from within the RegisterCapabilitiesEvent but make it inaccessible afterwards. Likely just an oversight; we should migrate the isXRegistered methods from the event to instance methods on the Capability objects.

@Technici4n
Copy link
Member

Please explain why you would want to query whether a provider exists. This is generally a bad pattern.

@Swedz
Copy link
Author

Swedz commented Sep 3, 2024

I have a machine that connects multiple machines together and there's some requirements a machine needs to fill to be acceptable for it. It needs to have one of a few different types of capabilities (IItemHandler and IFluidHandler for example). I am rendering a highlight in world when one of these acceptable machines are held in hand. I realized that I personally don't need it for my use case since I can provide a fake BlockState and BlockEntity, but there's still utility in being able to check this in cases where faking this stuff may not be as easy of a solution as it was for me.

@Technici4n
Copy link
Member

There is no guarantee at all that the presence of a provider indicates the presence of a capability. A default BlockEntity sounds dangerous too. I don't think there is really a solution for what you are trying to do. A simple example: GrandPower registers a Capabilities.EnergyStorage.BLOCK provider for every single block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New (or improvement to existing) feature or request
Projects
None yet
Development

No branches or pull requests

3 participants