Posted July 12, 20205 yr I have created a custom Capability in order to store ItemStack specific information on a particular item. The Capability functionality works in that each itemstack has its own information. However, the NBT does not appear to be saving and loading correctly, as the values reset upon exiting and reopening the world. After doing some debugging, I determined that the writeNBT function in the storage class is being called, but the readNBT function is not. Is there something I need to do to my Item class to make the Capability Storage work properly? Or is something in my Capability implementation incorrect? Interface: public interface GunInfo { public void setClip(int clip); public void setReload(int reload); public void setRecoil(float recoil, float yaw, float antiyaw, float antirec); public void setReloading(boolean reload); public void setCooldown(int cd); public int getClip(); public int getReload(); public float getRecoil(); public float getYaw(); public float getAntiYaw(); public float getAntiRecoil(); public boolean isReloading(); public int getCooldown(); } Storage class: public class GunInfoStorage implements IStorage<GunInfo> { @Override public INBT writeNBT(Capability<GunInfo> capability, GunInfo instance, Direction side) { CompoundNBT tag = new CompoundNBT(); tag.putInt("clip", instance.getClip()); tag.putInt("reload", instance.getReload()); tag.putBoolean("reload", instance.isReloading()); return tag; } @Override public void readNBT(Capability<GunInfo> capability, GunInfo instance, Direction side, INBT nbt) { CompoundNBT tag = (CompoundNBT) nbt; instance.setClip(tag.getInt("clip")); instance.setReload(tag.getInt("reload")); instance.setReloading(tag.getBoolean("reload")); } } Provider Class: public class GunInfoProvider implements GunInfo, ICapabilitySerializable<INBT> { @CapabilityInject(GunInfo.class) public static Capability<GunInfo> guninfo = null; private LazyOptional<GunInfo> instance = LazyOptional.of(guninfo::getDefaultInstance); @Override public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { return cap == guninfo ? instance.cast() : LazyOptional.empty(); } @Override public INBT serializeNBT() { return guninfo.getStorage().writeNBT(guninfo, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null); } @Override public void deserializeNBT(INBT nbt) { guninfo.getStorage().readNBT(guninfo, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null, nbt); } public int currentClip; public int currentReload; public float recoil, yawRecoil, antiYaw, antiRecoil; public boolean reloading; public int cooldown; public void setClip(int clip) { currentClip = clip; } public void setReload(int reload) { currentReload = reload; } public void setRecoil(float rec, float yaw, float antiyaw, float antirec) { recoil = rec; yawRecoil = yaw; antiYaw = antiyaw; antiRecoil = antirec; } @Override public void setReloading(boolean reload) { reloading = reload; } @Override public void setCooldown(int cd) { cooldown = cd; } @Override public int getClip() { return currentClip; } @Override public int getReload() { return currentReload; } @Override public float getRecoil() { return recoil; } @Override public float getYaw() { return yawRecoil; } @Override public float getAntiYaw() { return antiYaw; } @Override public float getAntiRecoil() { return antiRecoil; } @Override public boolean isReloading() { return reloading; } @Override public int getCooldown() { return cooldown; } } AttachCapabilities event function: @Mod.EventBusSubscriber public class FmlEvents { static int capid = 0; @CapabilityInject(GunInfo.class) public static Capability<GunInfo> guninfo = null; @SubscribeEvent public static void onAttachCapabilities(AttachCapabilitiesEvent<ItemStack> event) { if (event.getObject().getItem() instanceof ItemGun) { event.addCapability(new ResourceLocation("gunmod", "guninfo" + capid++), new GunInfoProvider()); } } } And here is where I register the capability: private void setup(final FMLCommonSetupEvent event) { CapabilityManager.INSTANCE.register(GunInfo.class, new GunInfoStorage(), GunInfoProvider::new); }
July 14, 20205 yr Howdy Your code looks right on a quick read through Try putting a breakpoint into both ItemStack:: private void forgeInit() { and ItemStack::deserializeNBT That should show you whether the capabilities are being constructed, serialised and deserialised properly. I have a working tutorial example here https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe32_inventory_item But I don't see any significant difference between yours and mine. -TGG
July 14, 20205 yr Author Thank you for the help, I will make sure to check out your example. Unfortunately I solved the problem sometime yesterday. I don't know if this is the appropriate fix but it works for anyone else who might have the same issue: In the provider class I have an ItemStack variable that is the Stack that the capability is affecting, and In the serializeNBT method I make sure to add the capability tags to the ItemStack like so: @Override public INBT serializeNBT() { INBT nbt = guninfo.getStorage().writeNBT(guninfo, this.instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")), null); if(owner.hasTag()) { owner.getTag().merge((CompoundNBT)nbt); } else { owner.setTag((CompoundNBT)nbt); } return nbt; } This causes both NBT functions to be called correctly.
July 15, 20205 yr Author 3 hours ago, diesieben07 said: This is completely broken. You must register your capability under the same name every time, because otherwise it obviously won't load correctly next time (because Forge now thinks it is a different capability). Alright, thanks for the clarification. I wasn't sure from the Capability docs whether it was each instance must have a different name, or each custom capability registered must have a different name.
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.