matthew123 Posted November 19, 2021 Posted November 19, 2021 I did not fully understand capabilities (specifically the capability provider). I am now facing an issue I believe has to do with an instance of my capability class lingering. I arrived at this conclusion by logging inside the capability class's constructor. Upon joining my single player world, the log indicates that the class was instanced. Upon leaving, then joining again, the message no longer appears. I believe this has to do with the static LazyOptional field doohickey inside this class: public class CapabilityProvider implements ICapabilitySerializable<INBT> { @CapabilityInject(ITheCapability.class) public static Capability<ITheCapability> CAPABILITY = null; private static final LazyOptional<ITheCapability> lazyOptional = LazyOptional.of(TheCapability::new); @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction side) { // don't use the one line if statement. if (capability == CAPABILITY) { return lazyOptional.cast(); } return LazyOptional.empty(); } @Override public INBT serializeNBT() { return CAPABILITY.getStorage().writeNBT(CAPABILITY, lazyOptional.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null); } @Override public void deserializeNBT(INBT nbt) { CAPABILITY.getStorage().readNBT(CAPABILITY, lazyOptional.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt); } } That static field obviously persists and essentially turns my class into a singleton, which is not what I want. Can anyone explain the usage of LazyOptional and how to fix my implementation? Quote
matthew123 Posted November 19, 2021 Author Posted November 19, 2021 14 minutes ago, diesieben07 said: Make the field non-static. Problem solved. I am ashamed -frowns- Guess I can't call myself a programmer anymore. Quote
Recommended Posts
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.