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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
    • Instructions on how to install newer Java can be found in the FAQ
    • That's just plain wrong... newer versions are much better optimised and start a lot faster than 1.8.9, both Forge and Minecraft itself. Comparing Fabric 1.21 with Forge 1.8 is like comparing apples and oranges... one's brand new and the other's over a decade old.
  • Topics

×
×
  • Create New...

Important Information

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