Jump to content

[1.7.10] Why does a stack's NBTTagCompound return null?


HappyKiller1O1

Recommended Posts

Ok so, I set my itemstack's stackTagCompound to a new NBTTagCompound and, need to retrieve the String I set in it for handling in another method inside the same class. Pretty simple right? Wrong. When I try to retrieve the string, the stackTagCompound is null even after setting it. Here are my methods:

 

//Where it is set (yes I craft a new one every time)
public void onCreated(ItemStack stack, World world, EntityPlayer player) {
	if(world.isRemote)
		return;

	NBTTagCompound cmp = stack.stackTagCompound;

	if(cmp == null) {
		System.out.println("NBT TAG SET");

		cmp = new NBTTagCompound();
		stack.stackTagCompound = cmp;
	}

	System.out.println(stack.stackTagCompound);

	cmp.setString("MiningSize", "3x3");
}

//Where I try to retrieve it
public boolean onBlockDestroyed(ItemStack stack, World world, Block block, int x, int y, int z, EntityLivingBase entity) {
	EntityPlayer player = (EntityPlayer)entity;

	System.out.println("CALLED");

	if(world != null && player != null) {
		System.out.println("WORLD WAS NOT NULL NOR WAS PLAYER");

		if(player.inventory.getCurrentItem() != null) {
			if(player.inventory.getCurrentItem().getItem().equals(CrewMod.crewHammer)) {
				int direction = MathHelper.floor_double((double)(player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

				ItemStack hammer = stack;

				//System.out.println("ITEM IN HAND IS CREW HAMMER");

				//Block block = event.block;

				//World world = event.world;

				//int x = event.x;
				//int y = event.y;
				//int z = event.z;

				NBTTagCompound cmp = hammer.stackTagCompound;

				//if(event.isCanceled())
					//return;

				String miningArea = null;

				if(cmp != null) {
					miningArea = cmp.getString("MiningSize");

					//System.out.println("CMP WAS NOT NULL");
				}

				System.out.println(miningArea);

				this.breakBlock(world, player, hammer, x, y, z, direction, miningArea);
			}else {
				super.onBlockDestroyed(stack, world, block, x, y, z, entity);
			}
		}else {
			super.onBlockDestroyed(stack, world, block, x, y, z, entity);
		}
	}

	return true;
}

 

And, when retrieving it although, the ItemStack parameter is suppose to contain the item and all of it's info; it does not. I know I must be doing something incorrectly and would love some knowledge on the subject.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Pretty simple right? Wrong.

 

This gave me chuckle so I actually coded this shit on my own.

 

public void onCreated(ItemStack stack, World world, EntityPlayer player)
{
	if(!world.isRemote)
	{
		System.out.println("THE STACK: " + stack);

		NBTTagCompound cmp = stack.getTagCompound();

		System.out.println("THE TAG: " + cmp);

		if(cmp == null)
		{
			System.out.println("NEW TAG");

			cmp = new NBTTagCompound();
			cmp.setString("MiningSize", "3x3");

			stack.setTagCompound(cmp);
		}

		System.out.println(cmp);
		System.out.println(stack.getTagCompound());
	}
}

 

Output:

 

[03:08:47] [server thread/INFO] [sTDOUT]: [x:onCreated:30]: THE STACK: 1xitem.sword_fuck@0
[03:08:47] [server thread/INFO] [sTDOUT]: [x:onCreated:34]: THE TAG: null
[03:08:47] [server thread/INFO] [sTDOUT]: [x:onCreated:38]: NEW TAG
[03:08:47] [server thread/INFO] [sTDOUT]: [x:onCreated:46]: {MiningSize:"3x3",}
[03:08:47] [server thread/INFO] [sTDOUT]: [x:onCreated:47]: {MiningSize:"3x3",}

 

Those getters/setters are just wrappers from 1.8 update.

public NBTTagCompound getTagCompound()
    {
        return this.stackTagCompound;
    }
public void setTagCompound(NBTTagCompound nbt)
    {
        this.stackTagCompound = nbt;
    }

 

EDIT

I added print on stack's nbt just to show that it actually works.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Well see, did you try calling it with an onBlockDestroyed method to see if it is still set? Because, although this is nearly the same as what I coded; it seems like it doesn't not fix my problem.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

See, put your code in there to test it and again, it is null with the onBlockDestroyed method. I have noticed though, the onCreated method gets called two times for server. Is this natural?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

[03:41:05] [Client thread/INFO] [sTDOUT]: [x:onBlockDestroyed:57]: 1xitem.sword_fuck@0
[03:41:05] [Client thread/INFO] [sTDOUT]: [x:onBlockDestroyed:58]: {MiningSize:"3x3",}
[03:41:06] [server thread/INFO] [sTDOUT]: [x:onBlockDestroyed:57]: 1xitem.sword_fuck@0
[03:41:06] [server thread/INFO] [sTDOUT]: [x:onBlockDestroyed:58]: {MiningSize:"3x3",}

 

@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, Block blockIn, BlockPos pos, EntityLivingBase playerIn)
{
	System.out.println(stack);
	System.out.println(stack.getTagCompound());
	return false;
}

 

Yes, I did.

 

Your problem is in code, not NBT (most likely). I'll have a look.

 

EDIT

No, it's not possible, it is called only once on server, end of story.

Click on that method, see callbacks -you MUST be doing something wrong :C

 

EDIT 2

I am out for next ~8h, learn to debug your code, put Syso(nbt) EVERYWHERE.

Don't do redirections like this: (what's the point, you are using it once).

NBTTagCompound cmp = hammer.stackTagCompound;

cmp.getString("MiningSize");

 

Other than that - I have no idea xD

 

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I'm not sure what's happening. It sets it two times on the server for me when it obviously is only suppose to set it once. All I do is override the onCreated method. Am I suppose to call a super?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Now see, I did that and all I can get is that somehow, the server thread is called with the client thread then recalled for when the client thread is not needed.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I added at the start of the onCreated method "System.out.println(Thread.getCurrentThread().getName())" and this is what I got:

 

[21:32:04] [Client thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:38]: Client thread
[21:32:04] [Client thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:38]: Client thread
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:38]: Server thread
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:41]: THE STACK: 0xitem.CrewHammer@0
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:45]: THE TAG: null
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:48]: NEW TAG
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:56]: {MiningSize:"3x3",}
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:57]: {MiningSize:"3x3",}
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:38]: Server thread
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:41]: THE STACK: 0xitem.CrewHammer@0
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:45]: THE TAG: {MiningSize:"3x3",}
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:56]: {MiningSize:"3x3",}
[21:32:04] [server thread/INFO] [sTDOUT]: [com.happykiller.crewmod.items.tools.CrewHammer:onCreated:57]: {MiningSize:"3x3",}

 

When the method is first called, it displays two client threads and, one server thread. Then, on the second pass, just one server thread. Does this mean something?

 

I'm not sure if this is what you meant to do. I'm not really sure how to see what calls my method and why. onCreated is abstract so, do you want me to find every place it is used?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Um, ok I might sound a little bit uneducated but, how would I use those?

 

EDIT:

Scratch that, I looked it up. :P So, it seems to be called two times in the SlotCrafting class on line 70 and 62 but, I'm not sure why. It is also called through the onCrafting method in ItemStack. Maybe if I override the onCrafting method in ItemStack it would not run twice?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

@Override is just annotation, it doesn't do anything besides making sure you actually made override. (When you apply @Override and you make mistake, IDE will tell you that).

 

As to problems - When you don't know what to do and think (I think) it shouldn't be like it, update to latest 1.7.10 (re-set up workspace, make test item, see if it's still called twice). It might be some mistake in code, for one I know - in 1.8 it's called once for each side.

 

Note that it is still NOT the problem. Even if onCrafting is called twice the NBT still works (as you can see in prints). Is there still some problem?

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I'm trying to override it now. I will update my workspace. I know it sets the NBT Compound but I can't retrieve it correctly... in 1.8, you can retrieve the compound that was set from the onBlockDestroyed?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Ok this is really weird, so I deleted the class to see what errors it gives me and, it should only give three considering the class is only used in three places and, it gave me six. It seems like eclipse is duplicating the paths somehow:

 

c5HuwVf.jpg?1

 

Basically it says "org.eclipse.jdt.core.external.folders/src/main/java/com/happykiller/crewmod" which is my main directory... How is this possible??

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

So, I think I've figured out my problem and, this seems to be an error with forge. When I shift click an item out of the crafting result slot, it runs the on created two times on both client and server. But, when just grabbing it out, it creates it once. I'm not sure if this is fixed in later forge versions but, it should be fixed ASAP considering this will be a big damper to my mod that requires the setting of an NBTTagCompound with onCreated.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I actually already created something like that. O.o I took your last post as I should not be using the onCreated method and instead, make a separate one to handle it.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I'm not sure what you're going at. I might sound ignorant towards Java but I am still learning. You said to create a new thread but I have no clue how to do that. O.o

 

EDIT:

 

Sorry! You said no new thread is required! I read it wrong. :P I'm not sure what your answer is though. xD

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

a) This is not a forge bug, this is vanilla behavior.

b) I personally advise against the use of onCreated. It is pretty much useless. Make your NBT lazy (wrap access to it in a method that creates it if it's not there yet). Then you don't need to create it eagerly in onCreated.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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.



×
×
  • Create New...

Important Information

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