Jump to content

[1.14.4] Capability NBT for an ItemStack not loading


Recommended Posts

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);
    }

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.