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

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?

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

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.