Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [Solved] [1.12.2] Why is client player null?
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
SapphireSky

[Solved] [1.12.2] Why is client player null?

By SapphireSky, November 13 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

SapphireSky    1

SapphireSky

SapphireSky    1

  • Tree Puncher
  • SapphireSky
  • Members
  • 1
  • 40 posts
Posted November 13 (edited)

This is driving me nuts and it's probably some stupid overlook. I've done this literally a dozen times but lost all my computer data so I can't check my old code.

I'm trying to replace the vanilla HUD with my own, and I can remove the vanilla one just fine but for some reason as soon as I try to access the client player it tells me the player is null.

 

Main event handler

	@SidedProxy(clientSide = "ww.souls.net.ClientProxy", serverSide = "ww.souls.net.ServerProxy")
	public static ServerProxy proxy;

	@EventHandler
	public void init(FMLInitializationEvent event)
	{
		proxy.init();
    }

 

Registering render event on client

@SideOnly(Side.CLIENT)
public class ClientProxy extends ServerProxy
{
	@Override
	public void init()
	{
		MinecraftForge.EVENT_BUS.register(new PlayerHud());
	}
}

 

Render event


	private void render()
	{
		//OpenGL stuff

		float health = player.getHealth() / mc.player.getMaxHealth(); // <- Crashes when first accessing 'null' player here
		float hunger = player.getFoodStats().getFoodLevel() / 20f;
		float exp = player.experience;

		mc.getTextureManager().bindTexture(res);

		ItemStack mainhand = player.getHeldItemMainhand();
		ItemStack offhand = player.getHeldItemOffhand();

		int y = screenheight - 100;
		int x = 20;
		drawTexturedModalRect(x, y, offhand.getCount() == 0 ? 0 : 46, 23, 22, 35);
		drawTexturedModalRect(x, y - 23, 23, 0, 22, 72);

		//OpenGL stuff
	}

	@SubscribeEvent
	public void onRender(RenderGameOverlayEvent.Pre event)
	{
		if (event.getType() == ElementType.ARMOR || event.getType() == ElementType.FOOD || event.getType() == ElementType.HEALTH
				|| event.getType() == ElementType.HEALTHMOUNT || event.getType() == ElementType.HOTBAR
				|| event.getType() == ElementType.JUMPBAR)
		{
			event.setCanceled(true);
			return;
		}

		if (event.getType() == ElementType.EXPERIENCE)
		{
			render();
			renderEnemyHealth();
			event.setCanceled(true);
		}
	}

 

Edited November 14 by SapphireSky
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6679

diesieben07

diesieben07    6679

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6679
  • 45657 posts
Posted November 13
5 hours ago, SapphireSky said:

player.getHealth()

Where is this player variable coming from?

  • Quote

Share this post


Link to post
Share on other sites

SapphireSky    1

SapphireSky

SapphireSky    1

  • Tree Puncher
  • SapphireSky
  • Members
  • 1
  • 40 posts
Posted November 14
On 11/13/2019 at 3:39 AM, diesieben07 said:

Where is this player variable coming from?

Oops, thought I included that up there.

It's in the class, defined in constructor.

	private Minecraft mc;
	private EntityPlayer player;

	public PlayerHud()
	{
		mc = Minecraft.getMinecraft();
		player = mc.player;
      	//....
	}

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6679

diesieben07

diesieben07    6679

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6679
  • 45657 posts
Posted November 14

You cannot do that. The player can and will change. You have to always get it from the game.

  • Quote

Share this post


Link to post
Share on other sites

SapphireSky    1

SapphireSky

SapphireSky    1

  • Tree Puncher
  • SapphireSky
  • Members
  • 1
  • 40 posts
Posted November 14
19 minutes ago, diesieben07 said:

You cannot do that. The player can and will change. You have to always get it from the game.

That's how I've always gotten it to work before... And I have it working that way in 1.14.

Isn't Minecraft.getMinecraft().player literally "getting it from the game"?

How else am I supposed to get the client player then?

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6679

diesieben07

diesieben07    6679

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6679
  • 45657 posts
Posted November 14
6 minutes ago, SapphireSky said:

Isn't Minecraft.getMinecraft().player literally "getting it from the game"?

Correct. But you get it from the game once, at which point it does not exist yet, and remember the value you got and never ask again.

You must ask every time and not remember the value.

  • Quote

Share this post


Link to post
Share on other sites

SapphireSky    1

SapphireSky

SapphireSky    1

  • Tree Puncher
  • SapphireSky
  • Members
  • 1
  • 40 posts
Posted November 14

Ah okay, that makes a lot more sense now. Thanks.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • BattleDash
      Get all players connected to a bungee server

      By BattleDash · Posted 41 minutes ago

      Hello all, I'm trying to make a mod that can tell you all the players on a bungee server you're connected to, I've never worked with Forge before and this API is very abstract to me compared to plugin development which is what I normally do. Does anyone know how I would go about getting a list of Player Entities of every player on the network you're connected to?
    • Draco18s
      Trouble getting contents of a Chest

      By Draco18s · Posted 1 hour ago

      It isn't merged, so it won't work yet.
    • saxon564
      [1.14.4] [UNSOLVED] Server Thread Freezes After Entity Explodes

      By saxon564 · Posted 2 hours ago

      Does anyone else have any thoughts as to what might be causing this issue?
    • diesieben07
      [1.12.2] How can I close GUI in Forge?

      By diesieben07 · Posted 2 hours ago

      You cannot call Minecraft methods from a separate thread. You need to wait a tick using ClientTickEvent.
    • bismuth210
      [1.12.2] Killing fireworks in unloaded chunks

      By bismuth210 · Posted 2 hours ago

      I'm creating a custom gamemode using forge in which players get teleported around regularly. I've run into a problem when I do the following:   I spawn a firework rocket near a player I teleport the player to a different location I wait a couple of seconds (or minutes) I teleport the player back to the same location as in step 1. Doing this will show the firework spawned in step 1 in step 4, despite significant time having passed in 3. This video shows what I mean:   I suspect the reason for why this happens is because once I teleport the player somewhere else, the chunk with the firework is no longer loaded and doesn't get updated.   Is there a simple way for me to simply "get rid" of all active fireworks shortly before teleporting players so that this doesn't occur? Or do I really have to forcibly keep all chunks loaded? To be more clear: I don't want to disable fireworks all together, but I don't want remnants of old fireworks showing up when I teleport players. "Killing" all firework rockets when I teleport a player would work fine, but I don't know if/how I can do that.   I've tried using /kill @e[type=!player] But that doesn't work for firework rockets apparently.
  • Topics

    • BattleDash
      0
      Get all players connected to a bungee server

      By BattleDash
      Started 41 minutes ago

    • MattNL
      5
      Trouble getting contents of a Chest

      By MattNL
      Started 12 hours ago

    • saxon564
      12
      [1.14.4] [UNSOLVED] Server Thread Freezes After Entity Explodes

      By saxon564
      Started Friday at 05:11 AM

    • Filip4223
      5
      [1.12.2] How can I close GUI in Forge?

      By Filip4223
      Started 3 hours ago

    • bismuth210
      0
      [1.12.2] Killing fireworks in unloaded chunks

      By bismuth210
      Started 2 hours ago

  • Who's Online (See full list)

    • DaemonUmbra
    • predator9800
    • EfrenB
    • imacatlolol
    • tday93
    • Cerandior
    • DanielMens
    • MattNL
    • Guy123
    • alox
    • DragonITA
    • Alkia
    • Simon_kungen
    • salvestrom
    • Kharmod
    • diesieben07
    • bluemetalsword
    • Redstoneguy129
    • MrMarioMaster34
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [Solved] [1.12.2] Why is client player null?
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community