Jump to content

[1.10.2] Can I have some help with NBT


American2050

Recommended Posts

For some reason NBT is one of the things I always seam to have problems with, I probably always confuse with what the methods do and mess them and never get clear how to use them.

 

In this case, I'm messing with the Pigs, I know they have some information on them, like for example, if they are equipped with a Saddle or not.

 

As for now I'm just reading it and printing on console, just to make sure I get it right.

 

//I comment the code to see if I clear myself what each line of code it doing

 

//Create and NBTTag to hold the information stored (Why isn't this loaded already with the Pig info that already exists? or is it?)
					 NBTTagCompound pigData = theEntity.getEntityData();

					 //Write into the NBT that we created the info on the pig (Is this really necesary?)
					 ((EntityPig)theEntity).writeEntityToNBT(pigData);

					 //Set a new tag "pigInfo" (This is a String) with my data, in this case a TIMER that is an int.
					 pigData.setInteger(pigInfo, TIMER);

					 //Store the modified NBT that now contains my data back into the entity
					 ((EntityPig)theEntity).readEntityFromNBT(pigData);


					 //Now from this and below, just to print the values out and see they working

					 //Again create an NBT (Maybe this step here isn't needed at all right? we have already pigData created above
					 pigData = theEntity.getEntityData();

					 //Read the pig info into the NBT
					 ((EntityPig)theEntity).writeEntityToNBT(pigData);

					 //Print out the Vanilla data and the one added by me
					 System.out.println(pigData.getInteger(pigInfo));
					 System.out.println(pigData.getBoolean("Saddle"));

 

Am I doing it correctly, am I doing extra steps that are not needed?

 

Thanks a lot. Hopefully some day I wont have any problems with NBT anymore.

Link to comment
Share on other sites

I'm calling it on an event:

 

@SubscribeEvent
 public void onEntityTick(LivingEvent event)

 

After checking all this is meet:

 

Entity theEntity = event.getEntity();
	 World theWorld = theEntity.worldObj;
	 BlockPos thePosition = new BlockPos(theEntity.posX, theEntity.posY, theEntity.posZ);

	 if(!theWorld.isRemote){

		 if(theEntity!=null && theEntity instanceof EntityPig){

			 if(theEntity.hasCustomName()){

				 if(Utilities.isValidPigName(theEntity.getCustomNameTag()))
				 {

 

So basically any Pig in game named certain names will call this code (As for now it's setting the value over and over, I know, but once I know I'm doing it right, I will check all this in my TODO list:

 

//CHECK IF THE ENTITY ALREADY HAS OUR TIMER AND IF IT REACHED 0

 

 

//ENTITY DOESNT HAS OUR TIMER - LETS CREATE IT

 

 

//ENTITY HAS OUR TIMER BUT IT DIDNT REACH 0 LESS DECREASE TIMER

Link to comment
Share on other sites

This has nothing to do with NBT. Nothing.

 

To check if a pig is saddled, call

EntityPig::getSaddled

. It will return true if the pig is wearing a saddle. What other information are you trying to get?

Isn't the information about having a saddle stored on a Pig saved on an NBT in the entity?

 

Please read the original post again, I guess you missed what the point was, I just mention the Saddle as an example of what's already on the NBT and how do I add my custom information into it.

 

The important part here is that Im adding more info into the NBT

 

//Set a new tag "pigInfo" (This is a String) with my data, in this case a TIMER that is an int.

pigData.setInteger(pigInfo, TIMER);

Link to comment
Share on other sites

Isn't the information about having a saddle stored on a Pig saved on an NBT in the entity?

 

No.  It's stored in the entity's fields.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.