Posted August 6, 20205 yr 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 August 6, 20205 yr by kyazuki
August 7, 20205 yr Author My cap value is synced player max health. It's reset every time player join the world.
August 7, 20205 yr Author "scale" is the value. The players max health is set to "scale" multiplied by 20. https://github.com/kyazuki/DietMod/blob/e82c049c1cb76167f99ec54318fbaf1b85e8be64/src/main/java/com/github/kyazuki/dietmod/EventSubscriber.java#L87 Edited August 7, 20205 yr by kyazuki
August 7, 20205 yr Author I fixed! https://github.com/kyazuki/DietMod/blob/3f17baad31d4b91edaf56e75dd90c168f5194aad/src/main/java/com/github/kyazuki/dietmod/EventSubscriber.java#L84
August 7, 20205 yr 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.