Posted February 27, 201312 yr 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? http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/
February 27, 201312 yr you can use NBT and the persistant tag - http://www.minecraftforge.net/forum/index.php/topic,5483.msg29791.html#msg29791. mnn.getNativeLang() != English If I helped you please click on the "thank you" button.
February 27, 201312 yr Author 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? http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/
February 27, 201312 yr 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.
February 27, 201312 yr Author And how can i add datawatchers to the player without actually editing a base class? http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/
February 27, 201312 yr I think you just use free ones (not 100% sure though, I don't have access to the MC code right now). mnn.getNativeLang() != English If I helped you please click on the "thank you" button.
February 27, 201312 yr Author the dataWatcher field is protected in the Entity class. is there any way to acces it? http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/
February 28, 201312 yr Author *facepalm* should have checked for a getter 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? http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/
February 28, 201312 yr you can browse this (ugly, but working) code, it should contain all needed parts : https://github.com/mnn/jaffas/blob/master/src/minecraft/monnef/jaffas/food/entity/EntityJaffaPainting.java mnn.getNativeLang() != English If I helped you please click on the "thank you" button.
March 1, 201312 yr Author 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? http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/
March 1, 201312 yr 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.
March 1, 201312 yr Author 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 http://www.minecraftforum.net/topic/1937703-162smpforge-pet-mastery-hatch-level-battle/
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.