Jump to content

[1.11.2] Capabilties not saving


jomoetnt

Recommended Posts

Hey everyone!

I've been working on this mod where the evil penguins and righteous penguins are at war with each other and recently I took a break. I implemented Capabilities for a float called trust. I'm not too sure why it's not working but it's just doing what it was before as if nothing changed. 

 

My event handler:

Spoiler

public class myForgeEventHandler {
	@SubscribeEvent 
	public void onPlayerLogsIn(PlayerLoggedInEvent event) 
	{ 
		EntityPlayer player = event.player; 
		ITrust trust = player.getCapability(TrustProvider.TRUST, null);
	} 
	@SubscribeEvent 
	public void onPlayerClone(PlayerEvent.Clone event) 
	{ 
		EntityPlayer player = event.getEntityPlayer(); 
		ITrust trust = player.getCapability(TrustProvider.TRUST, null); 
		ITrust oldTrust = event.getOriginal().getCapability(TrustProvider.TRUST, null); 
	
		trust.set(oldTrust.getTrust()); 
	}
}

 

The trust interface:

Spoiler

public interface ITrust {
	public void set(float points); 

	public float getTrust(); 
}

 

The trust class:

Spoiler

public class Trust implements ITrust{
	private float trust = 0;
	@Override
	public void set(float points) {
		this.trust = points; 
	}
	@Override
	public float getTrust() {
		return trust;
	}
}

 

The CapabilityHandler:

Spoiler

public class CapabiltyHandler {
	public static final ResourceLocation TRUST = new ResourceLocation(themodtostartallmods.MODID, "trust"); 

	@SubscribeEvent 
	public void attachCapability(AttachCapabilitiesEvent.Entity event) 
	{ 
		if (!(event.getEntity() instanceof EntityPlayer)) return; 

		event.addCapability(TRUST, new TrustProvider()); 
	} 
}

 

The trust provider:

Spoiler

public class TrustProvider implements ICapabilitySerializable<NBTBase>{
	@CapabilityInject(ITrust.class) 
	public static final Capability<ITrust> TRUST = null; 

	private ITrust instance = TRUST.getDefaultInstance(); 

	@Override 
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) 
	{ 
		return capability == TRUST; 
	} 
	@Override 
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) 
	{ 
		return capability == TRUST ? TRUST.<T> cast(this.instance) : null; 
	} 
	@Override 
	public NBTBase serializeNBT() 
	{ 
		return TRUST.getStorage().writeNBT(TRUST, this.instance, null); 
	} 
	@Override 
	public void deserializeNBT(NBTBase nbt) 
	{ 
		TRUST.getStorage().readNBT(TRUST, this.instance, null, nbt); 
	}
}

 

The trust storage:

Spoiler

public class TrustStorage implements IStorage<ITrust>{
	@Override 
	public NBTBase writeNBT(Capability<ITrust> capability, ITrust instance, EnumFacing side) 
	{ 
	return new NBTTagFloat(instance.getTrust()); 
	} 

	@Override 
	public void readNBT(Capability<ITrust> capability, ITrust instance, EnumFacing side, NBTBase nbt) 
	{ 
	instance.set(((NBTPrimitive) nbt).getFloat()); 
	} 
}

 

My main class: 

Spoiler

	@EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
		MinecraftForge.EVENT_BUS.register(myForgeEventHandler.class);
        MinecraftForge.EVENT_BUS.register(CapabiltyHandler.class);
		CapabilityManager.INSTANCE.register(ITrust.class, new TrustStorage(), Trust.class);
    }

 

 

Any help would be appreciated!

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.