Jump to content

Best way of adding a new variable.


shucke

Recommended Posts

What is the best way to add a new variable to a player on client side and server side?

I'm trying to make a mana bar and a new inventory for the player but i can't figure out how to add a variable to the EntityPlayer.

So i was wondering if there are any hooks for adding new variables to the player and saving them?

Link to comment
Share on other sites

I searched a little bit and this is what i found.

http://www.minecraftforum.net/topic/1597083-how-to-save-custom-data-with-a-player/

also using NBT.

now i want to figure out where i should call this code from.

are there any methods in for example my base class to do this kind of stuff?

Link to comment
Share on other sites

now i want to figure out where i should call this code from.

Well, you'd use it on every place where you need value of your custom "variable" (to be precise it should be called field) - e.g. when rendering mana bar you'd pull from player's entity NBT compound from that persistent compound and from that probably integer with actual mana status so you could draw it on player's screen (it should be probably cached, because accessing NBT every frame seems a bit demanding). Something about NBT is on the wiki - http://www.minecraftforge.net/wiki/How_to_use_NBT_Tag_Compound.

 

NBT is saved only on server side so you need to handle synchronization of your custom data, probably using watchers in entity itself (for integers, inventory is I believe synchronized using the container with gui).

 

are there any methods in for example my base class to do this kind of stuff?

I don't know what you mean by this O_o.

 

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Link to comment
Share on other sites

*facepalm*

should have checked for a getter :S

 

EDIT:

So i got the NBT working but the DataWatcher still isnt working properly.

Because i need to add a new datawatcher but i dont know how to do that properly.

 

I have never worked with Datawatchers before so this is all new to me.

So when i want to "Watch" my mana i get the error that it don't exist.

So i tried to check if it exists and add a new DataWatcher if it doesnt.

But how do i check if a datawatcher exists?

 

i tried:

if(player.getDataWatcher().getWatchableObjectInt(25) == 0)

but it gave me an error for a nullpointer exception and when i replace == 0 with == null it gives me an error in eclipse that an int cant be null.

Is there a way to check if the datawatcher exist and if it doesnt add it to the datawatcher?

Link to comment
Share on other sites

Thanks! it helped me understand datawatchers a whole lot more :)

but i still dont get how i can intialize the datawatcher.

you did it in EntityInit().

but since i wont edit the player's base class i cant add any code to the entityInit method.

so how can i intilize the new datawatcher?

Link to comment
Share on other sites

You could try initializing datawatchers in EntityJoinWorldEvent (only for players) or using IPlayerTracker. I haven't done this before so I might be wrong.

 

The down side of this solution is that your mod might collide with other mods, if they are using datawatcher with same ID. Safer (and a bit harder to implement) would be utilize custom packets. It's also possible that with PlayerAPI it would be easier to implement, but I don't use it so I can't really tell.

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Link to comment
Share on other sites

i tried to add the datawatcher to the player by using the playerLoggedIn event in the  my ConnectionHandler.

but when i try to get the datawatchers value it still gives me an null pointer exception.

this is what i added to the connection handler:

@Override
public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager) {
	EntityPlayer play = (EntityPlayer) player;
	play.getDataWatcher().addObject(25, new Integer(0));
}

i also registred it corretly.

 

EDIT:

nvm i figgured it out.

the playerLoggedIn is only called on the server so i send a packet to the client saying add a datawatcher to the player and the problem was solved.

thx for ur help :D

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.