Jump to content

Recommended Posts

Posted (edited)

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

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.