Posted January 4, 20178 yr 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.
January 4, 20178 yr Author 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
January 4, 20178 yr Author 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);
January 4, 20178 yr 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.
January 4, 20178 yr Author NBT is for saving to disk. If you want to attach additional data onto entities that are not yours use Capabilities. Thanks, gonna take a look into this From what I was reading I will probably need to create my own also. Hopefully I can get it going, otherway I will ask. Thanks once again
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.