Jump to content

[1.7.2]NBT data not being created when shift clicking out of Crafting Bench


Taji34

Recommended Posts

Basically, I'm adding a tag to my item which denotes its owner (who crafted it) for later use in multiplayer functions. However, I noticed when I shift click the output of the crafting bench into my inventory, it does not add the NBT tag. When I click and drag it into my inventory it adds the tag fine. Any insights on to why this is happening? I though maybe onCreated() or addInformation() wasn't being called, but I found out that is not the case after using some print statements to find out when they were called.

 

Here is my code for the specific item:

IdentityDisc.java:

 

package com.taji34.troncraft;

import java.util.List;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

public class IdentityDisc extends Item {
public IdentityDisc(){
	setCreativeTab(CreativeTabs.tabMisc);
	setMaxStackSize(1);
	setTextureName("troncraft:identitydisc");
	setUnlocalizedName("identitydisc");
}
@Override
public void onCreated(ItemStack itemStack, World world, EntityPlayer player)
{
	super.onCreated(itemStack, world, player);
    itemStack.stackTagCompound = new NBTTagCompound();
    itemStack.stackTagCompound.setString("Owner", player.getDisplayName());
}
public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4)
{
	if (itemStack.stackTagCompound != null) 
	{
		String owner = itemStack.stackTagCompound.getString("Owner");
		list.add("Owner: " + owner);
	}
}
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));

        if (!par2World.isRemote)
        {
            par2World.spawnEntityInWorld(new IdentityDiscEntity(par2World, par3EntityPlayer, par1ItemStack.copy()));
        }
    	
    	if (!par3EntityPlayer.capabilities.isCreativeMode)
        {
            --par1ItemStack.stackSize;
        }

        return par1ItemStack;
    }
}

 

Link to comment
Share on other sites

Does this same behavior exists if you grab the item out of creative inventory?

 

I have a setup where I wanted to know how the object was created, so I cycle through the player inventory every so often and apply and NBT tag for normal, creative, creative restricted.  Later if they are not in the restricted area, I delete the later.

 

What might be of use, is that i noticed some very odd behavior where i couldn't stack the items depending on where they came from.  If I then dropped them or used them in certain crafting stations and then returned them they would stack, but I never really noticed the pattern. 

 

I haven't bothered to look into it more because it was a minor annoyance.

 

I think it is probably related though and if we pool our notes, we might figure it out.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Well onCreated is server side and addInformation is client side. Make sure the tags are shared.

 

@delpi

You could override Item#getSubItems to add the NBT data to your items in creative menu.

Stacking can depend on a lot of stuff, including NBT. It is also worth remembering that the same instance of ItemStack can't stack with itself, so ItemStack#copy() is useful when making a custom crafting process.

Link to comment
Share on other sites

I've never used the getSubItems before.  Just looked at it and I didn't immediately spot how it was called (not home so can't use dev environment search).  Does this get called everytime the menu comes up?  Also it appears to be client side. 

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Well onCreated is server side and addInformation is client side. Make sure the tags are shared.

 

This is delving a bit further than my knowledge of forge. How would I make sure the tags are being shared?

 

Does this same behavior exists if you grab the item out of creative inventory?

 

If I grab the item out of the creative inventory, it does not add the tag. I'm gonna play around with it a bit to see if I can find the exact reason why the tag is not getting added. I just noticed this happens with another item of mine as well. Even though onCreated() seems to be called, the tag is never made and the stackTagCompound seems to be null. I found this out because my other item uses info from the NBT tag to vary function, and when I used it, it threw a NullPointer Exception.

Link to comment
Share on other sites

I'm not sure if datawatchers work on items or just entities.  You could look at that.  If not, just write some packets.

 

Alright, I think packets are the way to go cause after some experimenting, I'm getting three different behaviors and I think it just needs to be solved with packets to ensure the client and server both know what is happening. I haven't worked with packets and know that the packet system was recently just rewritten, could you point me to a tutorial that's updated (I'll look myself, but in case I can't find any)? Also, what exactly would I be doing with the packets, would I be sending a packet to the client saying that the item has been created and then send a packet from the client to the server with the info on what to do next?

Link to comment
Share on other sites

Given what you are doing, I think you just need to tell the server what the client did.  That will make it work.

 

If the client glitches or such, you can add the other, but I don't think you need.

 

I think this is similar to what I use:

 

http://www.minecraftforge.net/wiki/Netty_Packet_Handling

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

So you're saying that when addInformation() is called, I should send a packet to the server? I'm a little confused at what all I should be sending to the server, could you explain a bit further? I'm reading up on using the SimpleNetworkWrapper for packet handling, and I understand what all goes into that, I'm just not sure how to translate it to my specific situation. Do I send the parameters of addInformation() over to the server and then repeat what I did in addInformation()? Or what? Something is just not clicking and I don't know what I need to send in the packet in order to tell the server what the client did. I'm sorry if I am missing something blatantly obvious.

Link to comment
Share on other sites

The addInformation is only supposed to read the info from the ItemStack.

If packets are needed, send them from the onCreated method.

 

You know, I'm thinking this is more of an event problem then anything, I think when I am shift clicking into my inventory, it's passing the item stack differently, and that's where my problem is. I'm thinking this because after some testing, the only place onCreated() isn't being called is when taking the item out of the creative menu. I'm gonna explore the crafting handler and see if i can find how it handles shift clicking.

Link to comment
Share on other sites

That would make a lot of sense actually. Seeing as every item is actually a copy of an original, and I believe the creative inventory contains those originals (don't quote me on that). So when you click to grab the item, it just copies it... But that doesn't explain the crafting... which is the same... Hmm...

We all stuff up sometimes... But I seem to be at the bottom of that pot.

Link to comment
Share on other sites

I tracked the issue down to a method in ContainerWorkbench called transferStackInSlot() which is called when the item is shift clicked. Problem is I'm not really sure how to change the behavior without making my own crafting bench. It seems to work with two instances of the itemstack, passing one or the other to different methods. If anyone has any ideas on how to help me change the behavior, that would be great, otherwise I'm just gonna change when my NBT tags get added.

Link to comment
Share on other sites

 @SubscribeEvent
public void onCrafting(PlayerEvent.ItemCraftedEvent event) {	
} 

 

Have you thought of using events?

 

That event is called on any crafting results getting pulled out. you can modify the output stack before it is given to the player. If you shift click a stack out, it will be called 64 times, one for each item.

Link to comment
Share on other sites

 @SubscribeEvent
public void onCrafting(PlayerEvent.ItemCraftedEvent event) {	
} 

 

Have you thought of using events?

 

That event is called on any crafting results getting pulled out. you can modify the output stack before it is given to the player. If you shift click a stack out, it will be called 64 times, one for each item.

 

Sorry, I've been super scatter-brained trying to figure this one out. Events do seem like the way to go. However, the event seems to never be happening. This is my code:

 

TajiEvents.java:

 

package com.taji34.troncraft;

import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;

public class TajiEvents {
@SubscribeEvent
public void entityAttacked(LivingHurtEvent event)
{
	EntityLiving attackedEnt = (EntityLiving) event.entityLiving;
	DamageSource attackSource = event.source;
	if (attackSource.getSourceOfDamage() != null)
	{
		EntityPlayer player = (EntityPlayer) attackSource.getSourceOfDamage();
		if(player.getHeldItem() != null)
		{
			ItemStack itemstack = player.getHeldItem();
			if (itemstack.getDisplayName().equals("Baton"))
			{
				NBTTagCompound tag = itemstack.getTagCompound();
				int damageAmmount = tag.getInteger("Damage");
				event.ammount = damageAmmount;
			}
		}
	}
}

@SubscribeEvent
public void onCrafted(PlayerEvent.ItemCraftedEvent event) {	
	System.out.println(event.crafting.getItem().toString());
	if(event.crafting.getItem() instanceof IdentityDisc){
		System.out.println("I work");	
	}
}
}

 

 

My other event is working, however, the onCrafted event is never printing to the console, so it seems that it is never happening. Any Ideas?

Link to comment
Share on other sites

Event handlers must be registered on the proper BUS... there are at least 3 to choose from, so care must be taken to check.

Link to comment
Share on other sites

Event handlers must be registered on the proper BUS... there are at least 3 to choose from, so care must be taken to check.

 

Ahhhh, Okay. This is how I'm registering all my events currently:

 

Troncraft.java:

 

    @EventHandler
    public void init(FMLPreInitializationEvent event){       	
        (other code)
    	MinecraftForge.EVENT_BUS.register(new TajiEvents());
    	
    }

 

Link to comment
Share on other sites

Make a new class for your FML events, so they are separate from your forge events. Then register the class with this bus.

 FMLCommonHandler.instance().bus() 

 

Thanks, that worked! For future reference, how would I what are forge events and what are FML events?

 

Edit: So the event is running when I shift click the item, but the tags are still not being added. Here's my code for the event:

 

	public void onCrafted(PlayerEvent.ItemCraftedEvent event) {
	if(event.crafting.getItem() instanceof IdentityDisc){
		System.out.println("I Ran");
		event.crafting.stackTagCompound = new NBTTagCompound();
	    event.crafting.stackTagCompound.setString("Owner", event.player.getDisplayName());
	}
}

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.