Jump to content

help adding NBT data to the player


endershadow

Recommended Posts

I've tried to add NBT data to the player, but to no avail. I have a method that should work (I think) but it doesn't here's my code.

https://github.com/code-lyoko-modding/CodeLyokoMod/blob/master/matt/lyoko/network/PlayerTracker.java

and here is what I tried before what is currently on GitHub.

player.getEntityData().newTag((byte)1, "lifePoints");
player.getEntityData().setByte("lifePoints", (byte)100);

I also tried

player.getEntityData().newTag((byte)1, "lifePoints");
player.getEntityData().setByte("lifePoints", (byte)100);
player.writeToNBT(player.getEntityData());

any help would be appreciated.

Link to comment
Share on other sites

If you can add a listener to when the player is saved, you can use reflection to extract the NBT tag, and add to it. Same for loading the player.

 

Or, if the field is not a new thing on the player, just use reflection to set it if it is private, or just set it if it has a setter or is public.

So, what would happen if I did push that shiny red button over there? ... Really? ... Can I try it? ... Damn.

Link to comment
Share on other sites

How would i go about registering IAdditionalEntityData?

Um, someone had this same problem... half a minute...

 

...oh wait, they never solved it. Oh wait, that was you.  :-[

 

Um, the code you have should work. newTag() is a static method that creates a new tag, and you don't need it. But setByte() should work just fine. Does it not save? If you set it and then read it, does it work?

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

If you want just additional data to your player I would suppose to write an IPlayerTracker or add something if you have one yet. This is a convenient method to get the method onPlayerLogin(EntityPlayer player) called every time a player logs in. Then you get the NBTString (getEntityData()) and read from there. You store your read values in some sort of static Helper-Class (e.g.

class HelperHelper implements IPlayerTracker{
public static final instance = new HelperHelper();
private static HashMap<EntityPlayer, a_class_that_can_store_your_values> playerData = new HashMap<EntityPlayer, a_class_that_can_store_your_values>();

public static a_class_that_can_store_your_values getValuesFor(EntityPlayer player)
{
return playerData.get(player);
}

@Override
public void onPlayerLogin(EntityPlayer player)
{
if(player.getEntityData().hasKey("what_so_ever")
{
	playerData.put(player, readFromNBTStream(player.getEntityData().getCompoundTag("what_so_ever");
}else{
playerData.put(player, new a_class_that_can_store_your_values());
}

@Override
public void onPlayerLogout(EntityPlayer player)
{
NBTTagCompound toWriteTo = new NBTTagCompound();
playerData.get(player).writeToNBTTagCompound(toWriteTo);
player.getEntityData().setCompoundTag("what_so_ever", toWriteTo);
}

private a_class_that_can_store_your_values readFromNBTStream(NBTTagCompound playerCompound)
{
a_class_that_can_store_your_values toReturn = new a_class_that_can_store_your_values();
toReturn.readFromNBTTagCompound(playerCompound);
return toReturn;
}
... include additional methods from IPlayerTracker
}

Please note that this is only what I would suppose, maybe you have to edit it slightly.

Please note: your a_class_that_can_store_your_values has to feature the two public methods readFromNBTTagCompound(NBTTagCompound someName) and writeToNBTTagCompound(NBTTagCompound someOtherName). I hope you know how to read and write from/to NBTTagCompounds correctly. Maybe you also want to edit the method onPlayerLogin slightly cause it will always give you the default values of the NBTTagCompounds. You can check if the player has data already by calling player.getEntityData().hasKey("what_so_ever") and deciding what default values you put in.

Please note.3: you have to register your IPlayerTracker in your @Mod file preferably by typing GameRegistry.registerPlayerTracker(HelperHelper.instance);

Please note.4: also, all text with "_" has to be replaced with any name you choose as far as it is the same you use for the same placeholder.

Please note.5: if I have any typos please be kind and don't mention them if they are not really, really, really, really, really stuhpid ;)

Link to comment
Share on other sites

I'm sorry WorldsEnder, I can't figure out what your code is telling me to do. It's so confusing and error ridden.

How would i go about registering IAdditionalEntityData?

Um, someone had this same problem... half a minute...

 

...oh wait, they never solved it. Oh wait, that was you.  :-[

 

Um, the code you have should work. newTag() is a static method that creates a new tag, and you don't need it. But setByte() should work just fine. Does it not save? If you set it and then read it, does it work?

I've tried that, but it doesn't work.

Link to comment
Share on other sites

Try something like IExtendedEntityProperties. Here are some examples:

Class that implements IExtendedEntityProperties:

https://github.com/ModderPenguin/MinePG/blob/master/source/minepg/rpg_common/rpg/playerinfo/PlayerInformation.java

How to register it:

https://github.com/ModderPenguin/MinePG/blob/master/source/minepg/rpg_common/rpg/handlers/events/GenericEventHandler.java*

Register it like any normal forge event handler.

 

* The first method is the one you want

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Try something like this:

 

 

package matt.lyoko.lib;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

public final class PlayerInformation implements IExtendedEntityProperties
{
public static final String IDENTIFIER = "lyoko_data";
    
    public static PlayerInformation forPlayer(Entity player)
    {
        return (PlayerInformation)player.getExtendedProperties(IDENTIFIER);
    }
    
    public boolean dirty = true;
    
    /** The current amount of life points the player has in their mana bar */
    private int lifePoints = 100;
    
    private final EntityPlayer player;
    
    public PlayerInformation(EntityPlayer player)
    {
        this.player = player;
    }
    
    @Override
    public void init(Entity entity, World world)
    {

    }
    
    @Override
    public void saveNBTData(NBTTagCompound compound)
    {
        NBTTagCompound nbt = new NBTTagCompound();
        nbt.setInteger("lifePoints", lifePoints);
        compound.setCompoundTag(IDENTIFIER, nbt);
    }
    
    @Override
    public void loadNBTData(NBTTagCompound playerNbt)
    {
        NBTTagCompound nbt = playerNbt.getCompoundTag(IDENTIFIER);
        lifePoints = nbt.getInteger("lifePoints");
    }
    
    public int getLifePoints()
    {
        return lifePoints;
    }
    
    public int setLifePoints(int lifePoints)
    {
        if(this.lifePoints != lifePoints)
        {
            this.lifePoints = lifePoints;
            setDirty();
        }
        return this.lifePoints;
    }
    
    public int getMaxLifePoints()
    {
        return 100;
    }
    
    public int decreaseLifePoints(int decrement)
    {
    	lifePoints -= decrement;
        setDirty();
        if (lifePoints < 0)
        {
        	lifePoints = 0;
            setDirty();
        }
        return lifePoints;
    }
    
    public int increaseLifePoints(int increment)
    {
    	lifePoints += increment;
        setDirty();
        if (lifePoints > 100)
        {
        	lifePoints = 100;
            setDirty();
        }
        return lifePoints;
    }
    
    /*
     * marks that this needs to be resend to the client
     */
    public void setDirty()
    {
        dirty = true;
    }
}

 

 

 

And if you are wanting to change the life points, you just have to call pi.increaseLifePoints(amountToIncrease) or pi.decreaseLifePoints(amountToDecrease) or pi.setLifePoints(amountToSetTo) (the variable pi was taken from your event handler variable of it xD

 

[Edit] Also in your main class, you register your event handler twice... So I think if you are wanting to keep with my code, delete that first registry of it then keep the last two  :)

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Try something like this:

 

 

package matt.lyoko.lib;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

public final class PlayerInformation implements IExtendedEntityProperties
{
public static final String IDENTIFIER = "lyoko_data";
    
    public static PlayerInformation forPlayer(Entity player)
    {
        return (PlayerInformation)player.getExtendedProperties(IDENTIFIER);
    }
    
    public boolean dirty = true;
    
    /** The current amount of life points the player has in their mana bar */
    private int lifePoints = 100;
    
    private final EntityPlayer player;
    
    public PlayerInformation(EntityPlayer player)
    {
        this.player = player;
    }
    
    @Override
    public void init(Entity entity, World world)
    {

    }
    
    @Override
    public void saveNBTData(NBTTagCompound compound)
    {
        NBTTagCompound nbt = new NBTTagCompound();
        nbt.setInteger("lifePoints", lifePoints);
        compound.setCompoundTag(IDENTIFIER, nbt);
    }
    
    @Override
    public void loadNBTData(NBTTagCompound playerNbt)
    {
        NBTTagCompound nbt = playerNbt.getCompoundTag(IDENTIFIER);
        lifePoints = nbt.getInteger("lifePoints");
    }
    
    public int getLifePoints()
    {
        return lifePoints;
    }
    
    public int setLifePoints(int lifePoints)
    {
        if(this.lifePoints != lifePoints)
        {
            this.lifePoints = lifePoints;
            setDirty();
        }
        return this.lifePoints;
    }
    
    public int getMaxLifePoints()
    {
        return 100;
    }
    
    public int decreaseLifePoints(int decrement)
    {
    	lifePoints -= decrement;
        setDirty();
        if (lifePoints < 0)
        {
        	lifePoints = 0;
            setDirty();
        }
        return lifePoints;
    }
    
    public int increaseLifePoints(int increment)
    {
    	lifePoints += increment;
        setDirty();
        if (lifePoints > 100)
        {
        	lifePoints = 100;
            setDirty();
        }
        return lifePoints;
    }
    
    /*
     * marks that this needs to be resend to the client
     */
    public void setDirty()
    {
        dirty = true;
    }
}

 

 

 

And if you are wanting to change the life points, you just have to call pi.increaseLifePoints(amountToIncrease) or pi.decreaseLifePoints(amountToDecrease) or pi.setLifePoints(amountToSetTo) (the variable pi was taken from your event handler variable of it xD

 

[Edit] Also in your main class, you register your event handler twice... So I think if you are wanting to keep with my code, delete that first registry of it then keep the last two  :)

 

isn't that what I did? and you're supposed to register it twice. if you look at the mod that did it, they did it the same way I did it.

Link to comment
Share on other sites

  • 7 months later...

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • ChemsXpress (https://chemsxpress.com): ChemsXpress is a platform specializing in the distribution of research chemicals and related products. It offers a wide range of substances for scientific and laboratory use, catering to researchers and professionals. BubatzLand (https://bubatzland.com): BubatzLand focuses on providing high-quality cannabis products. The site offers a variety of strains, edibles, and related accessories, aiming to serve the needs of cannabis enthusiasts and medicinal users. HalluShroom (https://hallushroom.com): HalluShroom specializes in the sale of psychedelic mushrooms and related products. It caters to individuals interested in exploring the benefits and experiences associated with psilocybin and other psychedelic substances. HighAsch (https://highasch.com): HighAsch offers a diverse range of cannabis products, including flowers, concentrates, and edibles. The site aims to provide premium-quality products to both recreational and medicinal users. KetaminHaus (https://ketaminhaus.com): KetaminHaus provides ketamine and related products for research and therapeutic use. The platform focuses on ensuring the availability of high-quality, safe, and effective ketamine for various applications. KokainLabor (https://kokainlabor.com): KokainLabor offers cocaine and other related substances. The site targets individuals seeking high-purity products for personal or research purposes, emphasizing quality and safety. MDPHPWelt (https://mdphpwelt.com): MDPHPWelt specializes in the distribution of MDPHP and other similar research chemicals. It aims to supply researchers with high-quality substances for scientific and experimental use. NembutaLabor (https://nembutalabor.com): NembutaLabor provides Nembutal (pentobarbital) and related products. The site focuses on supplying these substances for research and veterinary purposes, ensuring high standards of quality and safety. NembutalQuelle (https://nembutalquelle.com): NembutalQuelle is dedicated to offering Nembutal and similar barbiturates. It caters to researchers and professionals who require these substances for scientific, medical, or veterinary applications. JBSaves Ltda Brazil (https://jbsavesltdabrazil.com): JBSaves Ltda Brazil is involved in the food and agricultural industry, providing a variety of products and services related to food processing, distribution, and agricultural practices. Bello Alimentos Ltda (https://belloalimentosltda.com): Bello Alimentos Ltda offers a wide range of food products. The site focuses on high-quality, nutritious, and safe food items, catering to consumers and businesses in the food industry. São Salvador Alimentos (https://saosalvadoralimentoss.com): São Salvador Alimentos specializes in the production and distribution of food products. The site provides a variety of options aimed at meeting the dietary needs and preferences of diverse consumers. WietWereld (https://wietwereld.com): WietWereld is a platform dedicated to the cannabis community. It offers information, products, and resources related to cannabis cultivation, consumption, and culture. 14. BeuhParadise (https://beuhparadis.com): BeuhParadise offers a range of cannabis products, including flowers, edibles, and concentrates. The site aims to provide high-quality cannabis products to recreational and medicinal users, emphasizing purity and potency. 15. WietWereld (https://wietwereld.com): WietWereld is a comprehensive resource for cannabis enthusiasts. It provides products, cultivation guides, and community forums for those interested in cannabis culture and cultivation. GrasOase (https://grasoase.com): GrasOase specializes in cannabis products and accessories. The site offers a variety of strains, seeds, and related items for cannabis users, focusing on quality and customer satisfaction. https://chemsxpress.com/ https://bubatzland.com/ https://hallushroom.com/ https://highasch.com/ https://ketaminhaus.com/ https://kokainlabor.com/ https://mdphpwelt.com/ https://nembutalabor.com/ https://nembutalquelle.com/ https://jbsavesltdabrazil.com/ https://belloalimentosltda.com/ https://saosalvadoralimentoss.com/ https://wietwereld.com https://beuhparadis.com https://wietwereld.com https://grasoase.com/
    • For Example I have an Main Mod that adds new Content to Minecraft. But I'm using custom Classes etc. and I want it so that is an api integrated in the Main Mod but can be download seperately like an Module. Is this possible? If you need more Information just comment what you need to know ^^
    • Delete the config of this mod (config folder) and test it again   If there is no change, try other builds  
    • Add the full crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here
  • Topics

×
×
  • Create New...

Important Information

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