Jump to content

Player capabilities not saving/loading[Solved]


UberAffe

Recommended Posts

Capability not getting saved/loaded on world save/load. Using the same writeNBT and loadNBT methods for updating the client with changes works, so the methods themselves are writing to nbt and reading from it fine.

 

public class DefaultDrafter implements IDrafter{
        public static class DrafterStorage implements IStorage<IDrafter>{

	public static final DrafterStorage drafterStorage = new DrafterStorage();

	@Override
	public NBTBase writeNBT(Capability<IDrafter> capability, IDrafter instance, EnumFacing side) {
		NBTTagCompound nbtDrafterCapabilities = new NBTTagCompound();
		NBTTagCompound nbtLuxinLevel = new NBTTagCompound();
		NBTTagCompound nbtLuxinCap = new NBTTagCompound();
		NBTTagCompound nbtLuxinAvailable = new NBTTagCompound();
		NBTTagCompound nbtDraftables = new NBTTagCompound();
		nbtDrafterCapabilities.setFloat("draft_cap", ((DefaultDrafter)instance).draftCap);
		nbtDrafterCapabilities.setFloat("draft_used", ((DefaultDrafter)instance).draftUsed);
		nbtDrafterCapabilities.setInteger("sick_ticks", ((DefaultDrafter)instance).sickTicks);
		nbtDrafterCapabilities.setString("drafting_selection", ((DefaultDrafter)instance).draftingSelection);
		for(String luxin : ((DefaultDrafter)instance).luxinLevel.keySet())
		{
			nbtLuxinLevel.setFloat(luxin, ((DefaultDrafter)instance).luxinLevel.get(luxin));
			nbtLuxinCap.setFloat(luxin, ((DefaultDrafter)instance).luxinCap.get(luxin));
			nbtLuxinAvailable.setFloat(luxin, ((DefaultDrafter)instance).luxinAvailable.get(luxin));
		}
		for(String draft : ((DefaultDrafter)instance).draftables.keySet())
			nbtDraftables.setTag(draft, CoreFactory.saveNBT(((DefaultDrafter)instance).draftables.get(draft)));
		nbtDrafterCapabilities.setTag(CAP, nbtLuxinCap);
		nbtDrafterCapabilities.setTag(LEVEL, nbtLuxinLevel);
		nbtDrafterCapabilities.setTag(AVAILABLE, nbtLuxinAvailable);
		nbtDrafterCapabilities.setTag(DRAFTABLE, nbtDraftables);
		return nbtDrafterCapabilities;
	}

	@Override
	public void readNBT(Capability<IDrafter> capability, IDrafter instance, EnumFacing side, NBTBase nbt) {
		NBTTagCompound nbtExtendedProperties = (NBTTagCompound) nbt;
		NBTTagCompound nbtLuxinLevel = nbtExtendedProperties.getCompoundTag(LEVEL);
		NBTTagCompound nbtLuxinCap = nbtExtendedProperties.getCompoundTag(CAP);
		NBTTagCompound nbtLuxinAvailable = nbtExtendedProperties.getCompoundTag(AVAILABLE);
		NBTTagCompound nbtDraftables = nbtExtendedProperties.getCompoundTag(DRAFTABLE);
		((DefaultDrafter)instance).draftCap = nbtExtendedProperties.getFloat("draft_cap");
		((DefaultDrafter)instance).draftUsed = nbtExtendedProperties.getFloat("draft_used");
		((DefaultDrafter)instance).sickTicks = nbtExtendedProperties.getInteger("sick_ticks");
		((DefaultDrafter)instance).draftingSelection = nbtExtendedProperties.getString("drafting_selection");
		for(String luxin : nbtLuxinLevel.getKeySet())
		{
			((DefaultDrafter)instance).luxinLevel.put(luxin, nbtLuxinLevel.getFloat(luxin));
			((DefaultDrafter)instance).luxinCap.put(luxin, nbtLuxinCap.getFloat(luxin));
			((DefaultDrafter)instance).luxinAvailable.put(luxin, nbtLuxinAvailable.getFloat(luxin));
		}
		for(String draft : ((DefaultDrafter)instance).draftables.keySet())
			((DefaultDrafter)instance).draftables.put(draft, CoreFactory.loadNBT((NBTTagCompound) nbtDraftables.getTag(draft)));
	}

}
}

public class DraftingProvider implements ICapabilityProvider{

@CapabilityInject(IDrafter.class)
public static Capability<IDrafter> CHROMATPLAYER = null;

private IDrafter drafter = null;

public DraftingProvider(){}

public DraftingProvider(IDrafter drafter){
	this.drafter = drafter;
}

@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
	return CHROMATPLAYER != null && capability == CHROMATPLAYER;
}

@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	if (CHROMATPLAYER != null && capability == CHROMATPLAYER) return (T)drafter;
	return null;
}

CommonProxy{
        public void preInit(){
                CapabilityManager.INSTANCE.register(IDrafter.class, DrafterStorage.drafterStorage, DefaultDrafter.class);
        }
}

 

and this in my EventHandler

@SubscribeEvent
public void AttachCapability(AttachCapabilitiesEvent.Entity e)
{
	if(!e.getEntity().hasCapability(DraftingProvider.CHROMATPLAYER, null) && e.getEntity() instanceof EntityPlayer)
		e.addCapability(Refs.DRAFTER, new DraftingProvider(new DefaultDrafter()));
}

 

is there something I am missing?

 

------------------------

Solution:

Implement INBTSerializable in your provider.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Link to comment
Share on other sites

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Patreon: http://www.patreon.com/lexmanos
Paypal: http://paypal.me/LexManos

BitCoin: 1Q8rWvUNMM2T1ZfDaFeeYQyVXtYoeT6tTn

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
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.