Jump to content

[1.12.2] Capability Won't Load/Update in Custom HUD Element


TristanaGW

Recommended Posts

Hey, I'm working on a mod based on Dark Souls, just as a little project for fun and to teach myself a little about Java. I'm trying to make a Humanity counter and I've looked all over different tutorials and the documentation, but I can't figure out for the life of me what the problem might be. It could be a quirk I'm missing about Java, or it could just be I've implemented my capabilities bad. I honestly have no clue.
Anyhow, here's the code that I'm using for my HUD element renderer: (apologies for the sloppy and silly code quirks of mine lmao. i'll rewrite it if i wanna get serious with it)

Spoiler

package com.tristanagw.soulsmod.gui;

import java.awt.Color;

import com.tristanagw.soulsmod.HumanityProvider;
import com.tristanagw.soulsmod.capabilities.IHumanity;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class GuiHumanity extends Gui {
    Minecraft mc = Minecraft.getMinecraft();
	private static final ResourceLocation texture = new ResourceLocation("soulsmod", "textures/gui/humanity_bg.png");
	private final int tex_width = 40, tex_height = 40;
	public GuiHumanity(Minecraft mc) {
		super();
		this.mc = mc;
	}
	
	@SubscribeEvent
    public void renderOverlay(RenderGameOverlayEvent event) {
        if (event.getType() == RenderGameOverlayEvent.ElementType.TEXT) {
            mc.renderEngine.bindTexture(texture);
            drawTexturedModalRect(4, 4, 0, 0, tex_width, tex_height);
        	IHumanity Humanity = mc.player.getCapability(HumanityProvider.CAPABILITY_HUMANITY, null);
        	int humanityAmount = Humanity.getHumanity();
            


    		String string = String.format("%02d",humanityAmount);
//    		String string = String.format("%02d",(int) mc.player.getHealth());
    		int xPos = (4 - (this.mc.fontRenderer.getStringWidth(string)/2))+(tex_width/2);
    		int yPos = (4 - 4) + (tex_height/2);
    		this.mc.fontRenderer.drawString(string, xPos, yPos, new Color(255, 255, 255).getRGB());
        }
    }
}

 

As you can see in the code, I've added a test line to swap out with the Humanity variable get with the player health, and that works just fine. I run the debugger, swap it to that and it goes to twenty and counts down when damaged, and goes back up when regen'd. But swapping the Humanity variable back in pops it back down to 0. (I just tried it while on a server too and Humanity didn't work on that either...)

If the Humanity scripts need to be seen, let me know, but just know that it's largely based off of the MCHorse code here. (I should mention my event handler doesn't work either, but the methods I have for adding and reducing Humanity work just fine and send my test messages and saves the Humanity to the playerdata NBT (or world data in the case of single player which may be its own problem but I wont worry about it for now.))

Anyway, knowing me I've probably missed something, but hopefully you've got enough to help me troubleshoot my way out of this. Thanks in advance!

Link to comment
Share on other sites

You need to sync your capability data from server to client.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Apologies if this is a really silly question, but the storage stuff is handled by the writeNBT/readNBT stuff right? I've had that class up for the whole time and it's still not worked. Or is that *just* for read/writing on the client? Do I need to do some intense reading into the SimplImpl stuff?

Link to comment
Share on other sites

1 hour ago, TristanaGW said:

Apologies if this is a really silly question, but the storage stuff is handled by the writeNBT/readNBT stuff right? I've had that class up for the whole time and it's still not worked. Or is that *just* for read/writing on the client? Do I need to do some intense reading into the SimplImpl stuff?

writeNBT/readNBT is for the server so that the data can be saved and loaded from the hard drive. That's it.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.