Jump to content

Recommended Posts

Posted

I'm trying to use Capabilities to add a kind of player currency. But I can't get it to work.

I've followed a couple of tutorials on it pretty much exactly, but when I try to render the amount on the corner of the screen it's always null.

 

I apologise if this post is large, I guess the site appearance just changed and there's no preview option?

 

ScreenTag

(the gui with currency amount that's trying to be rendered)


	private Minecraft mc;
	private final int colour = new Color(255, 85, 255).getRGB();

	public ScreenTag(Minecraft mc)
	{
		this.mc = mc;
	}

	@SubscribeEvent
	public void onRenderGui(RenderGameOverlayEvent.Post event)
	{
		if (event.getType() != ElementType.EXPERIENCE)
		{
			return;
		}
		IEchoesCapability echoes = mc.thePlayer.getCapability(EchoesManager.ECHOES, null);

		//
		if (echoes == null) return; // <- This always fires. Why is this always null?
		//
      
		drawCenteredString(mc.fontRendererObj,
				TextFormatting.RED + "Blood Echoes: " + TextFormatting.WHITE + echoes.getEchoes(), 20, 20, colour);
		drawCenteredString(mc.fontRendererObj,
				TextFormatting.RED + "Insight: " + TextFormatting.WHITE + echoes.getInsight(), 20, 50, colour);
	}

 

IEchoesCapability

(I don't think I'd need to post the Echoes class that implements this interface since it's just variable setters/getters)


	public boolean useEchoes(int amount);

	public void addEchoes(int amount);

	public void setEchoes(int amount);

	public int getEchoes();

	//

	public boolean useInsight(int amount);

	public void addInsight(int amount);

	public void setInsight(int amount);

	public int getInsight();

 

EchoesStorage implements IStorage<IEchoesCapability>


	@Override
	public NBTBase writeNBT(Capability<IEchoesCapability> capability, IEchoesCapability instance, EnumFacing side)
	{
		NBTTagCompound nbt = new NBTTagCompound();
		nbt.setInteger("echoes", instance.getEchoes());
		nbt.setInteger("insight", instance.getInsight());
		return nbt;
	}

	@Override
	public void readNBT(Capability<IEchoesCapability> capability, IEchoesCapability instance, EnumFacing side,
			NBTBase nbt)
	{
		instance.setEchoes(((NBTTagCompound) nbt).getInteger("echoes"));
		instance.setInsight(((NBTTagCompound) nbt).getInteger("insight"));
	}

 

Capabilities

(event handler)

	public static final ResourceLocation ECHOES = new ResourceLocation(Main.MODID, "echoes");

	@SubscribeEvent
	public void attach(AttachCapabilitiesEvent.Entity event)
	{
		if (event.getEntity() instanceof EntityPlayer)
		{
			event.addCapability(ECHOES, new EchoesManager());
		}
	}

	@SubscribeEvent
	public void clone(PlayerEvent.Clone event)
	{
		if (event.isWasDeath())
		{
			IEchoesCapability original = event.getOriginal().getCapability(EchoesManager.ECHOES, null);
			IEchoesCapability cloned = event.getEntityPlayer().getCapability(EchoesManager.ECHOES, null);
			cloned.setEchoes(original.getEchoes());
			cloned.setInsight(original.getInsight());
		}
	}

 

EchoesManager implements ICapabilitySerializable<NBTBase>


	@CapabilityInject(IEchoesCapability.class)
	public static final Capability<IEchoesCapability> ECHOES = null;

	private IEchoesCapability instance = ECHOES.getDefaultInstance();

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

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing)
	{
		return capability == ECHOES ? ECHOES.<T>cast(instance) : null;
	}

	@Override
	public NBTBase serializeNBT()
	{
		return ECHOES.getStorage().writeNBT(ECHOES, instance, null);
	}

	@Override
	public void deserializeNBT(NBTBase nbt)
	{
		ECHOES.getStorage().readNBT(ECHOES, instance, null, nbt);
	}

 

inside Main class


	@EventHandler
	public void init(FMLInitializationEvent event)
	{
		proxy.init();
		MinecraftForge.EVENT_BUS.register(new ScreenTag(Minecraft.getMinecraft()));
	}

 

and inside ServerProxy


	public void init()
	{
		CapabilityManager.INSTANCE.register(IEchoesCapability.class, new EchoesStorage(), Echoes.class);

		MinecraftForge.EVENT_BUS.register(new Capabilities());
	}

 

I think that should be everything relevant.

The whole mod kinda depends on this working properly, so could someone tell me what exactly I'm doing wrong?

Posted
1 minute ago, Animefan8888 said:

You will need to send a packet to sync the Capability.

 

I realise that I need to use packets to sync the amounts.

But without packets it should still render, just the amount would always be 0, right?

Posted
Just now, Animefan8888 said:

Is it an int, or an Integer?

They're both ints.

 

		IEchoesCapability echoes = mc.thePlayer.getCapability(EchoesManager.ECHOES, null);

		if (echoes == null) return;

This always returns when trying to render. So the capability itself is null, not any of the variables inside it.

Posted (edited)
14 minutes ago, Eria8 said:

inside Main class



	@EventHandler
	public void init(FMLInitializationEvent event)
	{
		proxy.init();
		MinecraftForge.EVENT_BUS.register(new ScreenTag(Minecraft.getMinecraft()));
	}

 

You can't reference client-only classes like Minecraft from common code, otherwise you'll crash the dedicated server.

 

 

Quote

and inside ServerProxy



	public void init()
	{
		CapabilityManager.INSTANCE.register(IEchoesCapability.class, new EchoesStorage(), Echoes.class);

		MinecraftForge.EVENT_BUS.register(new Capabilities());
	}

 

 

Capabilities and most event handlers need to be registered on both physical sides, so this should be done from your @Mod class.

 

The class you specify in the serverSide property of the @SidedProxy annotation will only be loaded by the dedicated server.

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.