Jump to content

[1.7.10] Get NBT player data for GUI Overlay


Nereidregulus

Recommended Posts

Hello everyone!

I'm trying to make a kind of experience bar for my mod.

So I've created a RenderGameOverlayEvent event to show the Gui bar on screen, but to make it work, I need to access to the player NBT data to know how much xp does the player have.

I've tried

DataSaver props = DataSaver.get(this.mc.thePlayer);

but it only show the initial data define by the NBT class.(in this case 0, even if my player level is not 0)

Is there any way to do that?

 

Here is my code:

 

DataSaver

public class DataSaver implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "DataSaver";

private EntityPlayer player;

private int level, xplevel;

public DataSaver(EntityPlayer player)
{
	this.player = player;
	this.level = this.xplevel = 0;
}

public static void register(EntityPlayer player)
{
	player.registerExtendedProperties(DataSaver.EXT_PROP_NAME, new DataSaver(player));
}


public static DataSaver get(EntityPlayer player)
{
	return (DataSaver) player.getExtendedProperties(EXT_PROP_NAME);
}

@Override
public void saveNBTData(NBTTagCompound compound)
{

	NBTTagCompound properties = new NBTTagCompound();


	properties.setInteger("CurrentLevel", this.level);
	properties.setInteger("XpLelvel", this.xplevel);


	compound.setTag(EXT_PROP_NAME, properties);
}

// Load whatever data you saved
@Override
public void loadNBTData(NBTTagCompound compound)
{

	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);

	this.level = properties.getInteger("CurrentLevel");
	this.xplevel = properties.getInteger("XpLelvel");

}


@Override
public void init(Entity entity, World world)
{

}

public void printInfo()
{
	System.out.println("[Xp Player] XpTotal:" + this.xplevel);
	System.out.println("[Xp Player] Level:" + this.level);
}

public int returnXpLevel()
{
	return this.xplevel;
}

public int returnLevel()
{
	return this.level;
}




public void addXpToPlayer(int xp)
{
	this.xplevel+=xp;
	int xptotal=this.xplevel;
	//Xp / mob: y=2 +(x/5)^1.902
	//Xp to up lvl y=10+(2+(x/5)^1.4)^3
	//Math.pow(a,b) => a^b
	//this.level=10+(int)Math.pow(Math.pow(xptotal/5, 1.4)+2, 1.4);
	if(xptotal>=19)
	this.level=(int)(5*Math.pow(Math.pow(xptotal-10, 0.333)-2, 0.714));
	else this.level=0;
	System.out.println("[Xp Player] Test equation:" + Math.pow(xptotal-10, 0.33));

	System.out.println("[Xp Player] XpTotal:" + this.xplevel);
	System.out.println("[Xp Player] Level:" + this.level);
}



public void resetxp()
{
	this.xplevel=0;
	this.level=0;

	System.out.println("[Xp Player] XpTotal:" + this.xplevel);
	System.out.println("[Xp Player] Level:" + this.level);
}

}

 

GuiXpBar


public class GuiXpBar extends Gui
{

  private Minecraft mc;
  NBTTagCompound nbt;
  EntityPlayer p;
  private static final ResourceLocation texture = new ResourceLocation(RefStrings.MODID + ":" + "textures/gui/bar.png");

  public GuiXpBar(Minecraft mc)
  {
    super();
    
    this.mc = mc;
  }

  


  @SubscribeEvent
  public void onRenderExperienceBar(RenderGameOverlayEvent event)
  {

    if(event.isCancelable() || event.type != ElementType.EXPERIENCE)
    {      
      return;
    }

    p = mc.thePlayer;
    nbt = p.getEntityData();

DataSaver props = DataSaver.get(this.mc.thePlayer);
          
        int xPos = 2;
  		int yPos = 2;
  		this.mc.getTextureManager().bindTexture(texture);


  		GL11.glEnable(GL11.GL_BLEND);
  		GL11.glDisable(GL11.GL_DEPTH_TEST);
  		GL11.glDepthMask(false);
  		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  		GL11.glDisable(GL11.GL_ALPHA_TEST);

  		drawTexturedModalRect(xPos, yPos, 0, 0, 64, 5);

  		int xpbarwidth = (int)(((float) props.returnXpLevel() / props.returnLevel()) * 49);
  		//drawTexturedModalRect(xPos + 3, yPos + 3, 0, 9, xpbarwidth, 3);
  		String s = "Level:" + props.returnLevel();
  		yPos += 10;
  		this.mc.fontRenderer.drawString(s, xPos + 1, yPos, 0);
  		this.mc.fontRenderer.drawString(s, xPos - 1, yPos, 0);
  		this.mc.fontRenderer.drawString(s, xPos, yPos + 1, 0);
  		this.mc.fontRenderer.drawString(s, xPos, yPos - 1, 0);
  		this.mc.fontRenderer.drawString(s, xPos, yPos, 8453920);

  		GL11.glDisable(GL11.GL_BLEND);
  		GL11.glEnable(GL11.GL_DEPTH_TEST);
  		GL11.glDepthMask(true);
  }
}

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.