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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I had an amazing experience with The Hack Angels. I had given up hope, but they managed to recover my bitcoins from an online scammer. I was able to retrieve what I had thought was lost forever because of their knowledge and commitment. I heartily endorse the services of The Hack Angels to anyone in need of help recovering cryptocurrency. They really are authorities in their domain.  (Web: https://thehackangels.com)     Whats Ap; +1 520) - 200, 23  20 ) ,  Mail Box; support@thehackangels. com
    • I got my model to render from the models renderToBuffer method. But still not quite what I want. I want to render the model from my renderer's render method. I feel that having access to the renderer and it's methods will open some doors for me later down the line. //EntityRendererProvider.Context pContext = ; I want this //ToaPlayerRenderer render = new ToaPlayerRenderer(pContext, false); // if I can get the above line to work, having the methods from the renderer class would be incredibly helpful down the line RenderType rendertype = model.renderType(p.getSkinTextureLocation()); // this should be something like render.getTextureLocation() VertexConsumer vertexconsumer = buffer.getBuffer(rendertype); model.renderToBuffer(stack, vertexconsumer, paLights, 1, 1, 1, 1, 1); // I don't want the render to happen here since it doesn't use the renderer //model.render(p, 1f, pTicks, stack, buffer, paLights); I want to render the model using this It is certainly getting closer though. Probably. I am still worried that even if pContext is initialized this new instance of the renderer class will still hit me with the classic and all too familiar "can't use static method in non-static context"
    • Hello, I am learning how to create Multipart Entities and I tried creating a PartEntity based on the EnderDragonPart code. However, when I tested summoning the entity in the game, the PartEntity appeared at position x 0, y 0, z 0 within the game. I tried to make it follow the main entity, and after testing again, the part entity followed the main entity but seemed to teleport back to x 0, y 0, z 0 every tick (I'm just guessing). I don't know how to fix this can someone help me? My github https://github.com/SteveKK666/Forge-NewWorld-1.20.1/tree/master/src/main/java/net/kk/newworldmod/entity/custom Illustration  https://drive.google.com/file/d/157SPvyQCE8GcsRXyQQkD4Dyhalz6LjBn/view?usp=drive_link Sorry for my English; I’m not very good at it. 
    • its still crashing with the same message
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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