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

The values saved in playerdata file.

However, the values aren't imported when login.

How do I fix?

 

Main:

public static final ResourceLocation CAP_RESOURCE = new ResourceLocation(MODID, "capabilities");

public TestMod() {
  FMLJavaModLoadingContext.get().getModEventBus().addListener(TestMod::onFMLCommonSetup);
}

@SubscribeEvent
public static void onFMLCommonSetup(FMLCommonSetupEvent event) {
  CapabilityManager.INSTANCE.register(IMyCapability.class, new MyCapabilityStorage(), () -> new MyCapability(2.0f));
}

@SubscribeEvent
public static void attachCapability(AttachCapabilitiesEvent<Entity> event) {
  if (!(event.getObject() instanceof PlayerEntity)) return;

  event.addCapability(CAP_RESOURCE, new MyCapabilityProvider());
}

 

Provider:

public class MyCapabilityProvider implements ICapabilitySerializable<INBT> {
  @CapabilityInject(IMyCapability.class)
  public static final Capability<IMyCapability> CAP = null;

  private LazyOptional<IMyCapability> instance = LazyOptional.of(CAP::getDefaultInstance);

  @Nonnull
  @Override
  public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
    return CAP.orEmpty(cap, instance);
  }

  @Override
  public INBT serializeNBT() {
    return (INBT) CAP.getStorage().writeNBT(CAP, instance.orElseThrow(() -> new IllegalArgumentException("at serialize")), null);
  }

  @Override
  public void deserializeNBT(INBT nbt) {
    CAP.getStorage().readNBT(CAP, instance.orElseThrow(() -> new IllegalArgumentException("at deserialize")), null, nbt);
  }
}

 

Storage:

public class MyCapabilityStorage implements Capability.IStorage<IMyCapability> {
  @Override
  public INBT writeNBT(Capability<IMyCapability> capability, IMyCapability instance, Direction side) {
    CompoundNBT tag = new CompoundNBT();
    tag.putFloat("cap1", instance.getCap1());
    tag.putFloat("cap2", instance.getCap2());
    tag.putFloat("cap3", instance.getCap3());
    return tag;
  }

  @Override
  public void readNBT(Capability<IMyCapability> capability, IMyCapability instance, Direction side, INBT nbt) {
    CompoundNBT tag = (CompoundNBT) nbt;
    instance.setCap1(tag.getFloat("cap1"));
    instance.setCap2(tag.getFloat("cap2"));
    instance.setCap3(tag.getFloat("cap3"));
  }
}

 

Edited by kyazuki

  • Author

My cap value is synced player max health.

It's reset every time player join the world.

  • Author

I noticed PlayerEntity.distanceWalkModified reset when login.

How do I get statics correctly on 1.16.1?

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.