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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • version used : minecraft forge 1.20.1 Logs : https://bytebin.lucko.me/rtG4EA0vfb hi! so when i try to create a new minecraft world in single player, it just crashes?? does anyone has an idea what could be causing this ?
    • Hey today I was trying to play in 1.21 modpack that I made on fabric (my version of fabric is in 0.16.0) with this list of mods : - Entity Model Features (by traben_0) - Entity Texture Features (by traben_0) - Advanced Chat (by Wesley8081) - Ambient Environment (by Jaredlll08) - Ambient Souds 6 (by CreativeMD) - Animatica (by FoundationGames) - Armor Statues (by Fuzs) - Auto HUD (by Crendgrim) - Bad Packets (by badasintended) - Balm (by BlayTheNinth) - Better Beds (by Motschen) - Better Chunk Loading (by someaddon) - Better Compatibility Checker (by Gaz-) - Better FPS [Render Distance] (by someaddon) - Better Ping Display (by Quintinity) - Better Statistics Screen (by TheCSDev) - Better Armor HUD (by efekos) - Better F3 ( by cominixo) - Blur + (by Motschen) - Bobby (by Johni0702) - Cat Jam (by shmove) - Catalogue (by MrCrayfish) - Chat Heads (by dzwdz) - Cherished Worlds (by TheIllusiveC4) - Chunk Sending (by someaddon) - Chunks Fade In (by kerudion) - Chunky (by pop4959) - Cloth Config (by shedaniel) - Clumps (by Jaredlll08) - Cobweb (CrystalSpider) - Collective (by Serilum) - Configured (by MrCrayfish) - Connectivity (by someaddon) - Continuity (by Pepper_Bell) - Creative Core (by CreativeMD) - Cupboard (by someaddon) - Cut Through (by Fuzs) - Dark Loading Screen (by Neecko5b84) - Debugify (by XanderIsDev) - Double Doors (by Serilum) - Dynamic FPS (by juliand665) - Dynamic Lights (by Tschipcraft) - Easy Anvils (by Fuzs) - Easy Magic (by Fuzs) - Easy Shulker Boxes (by Fuzs) - Eating Animation (by theone-ss) - Enhanced Attack Indicator (by Mineash) - Enhanced Block Entities (by FoundationGames) - Enhanced Visuals (by CreativeMD) - Entity Culling (by tr7zw) - Environmental Water (by AgentMindStorm) - Essential (by SparkUniverse_) - Extra Thicc Packets (by JaselUmena) - Fabric (by modmuss50) - Fabric Language Kotlin (by modmuss50) - Falling Leaves (by RandomMcSomethin) - Fancy Block Particles (by Hantonik) - Fancy Health Bar (by efekos) - Farsight (by someaddon) - Fast IP Ping (by Fallen_Breath) - Ferrite Core (by  malte0811) - Fish On The Line (by Serilum) - Fix GPU Memory Leak  (by someaddon) - Forge Config API Port (by Fuzs) - FPS Reducer (by bre2el) - Freecam (by hashalite) - Fzzy Config (by fzzyhmstrs) - Get It Together (by bl4ckscor3) - Global Packs (by JTK222) - Harvest With Ease (by CrystalSpider) - Ichuntil Util (by iChun) - Immediately Fast (by RaphilMC) - Immersive UI (by SSKirillSS) - Indium (by comp500) - Inventory HUD+ (by dmitrylovin) - Iris Shaders (by coderbot) - Item Physic Full (by CreativeMD) - Jade (by Snownee) - Journey Map (by techbrew) - Just Enough Breeding (by Christofmeg) - Just Enough Items (by mezz) - Just Enough Professions (by Mrbysco) - Language Reload (by Jerozgen) - Leaves Be Gone (by Fuzs) - Let Me Despawn (by frikinjay) - Let Sleeping Dogs Lie (by iChun) - Lithium (by JellySquid) - Loading Backgrounds (by Lupin) - Mineblock's Repeated Utilities (by itsmineblock11) - Menulogue (by MrCrayfish) - Mob Plaques (by Fuzs) - Mod Menu (by Prospector) - Model Gap Fix (by MehVahdJukaar) - Mouse Tweaks (by YaLTeR) - Nether Portal Fix (by BlayTheNinth) - Nicer Skies (by ztereohype) - Noisium (by Steveplays28) - Not Enough Animations (by tr7zw) - Opti GUI (by opekope2) - Packet Fixer (by TonimatasDEV) - Particle Core (by fzzyhmstrs) - Passive Shields (by Serilum) - Pick Up Notifier (by Fuzs) - Ping Wheel (by LukenSkyne) - Puzzle (by Motschen) - Puzzles LIB (by Fuzs) - Reese's Sodium Options (by FlashyReese) - Resource Pack Overrides (by Fuzs) - Server Country Flags (by khajiitos) - Server Performance (by someaddon) - Skin Layer 3D (by tr7zw) - Sodium (by JellySquid) - Sodium Extra (by FlashyReese) - Sodium Shadowy Path Blocks (by Rynnavinx) - Sound Physics Remastered (by henkelmax) - Sounds (by itsmineblock11) - Spark (by lucko) - Stack Refill (by Serilum) - Structure Essentials (by someaddon) - TCD Commons API (by TheCSDev) - Text Placeholder API (by Patbox) - Villager Names (by Serilum) - Visual Overhaul (by Motschen) - Visual Workbench (by Fuzs) - Visuality (by PinkGoosik) - Wakes (by Goby56) - Wavey Capes (by tr7zw) - YDM'S Weapon Master  (by YourDailyModderx) - Yet Another Config LIB (by XanderIsDev) - Zoomify (by XanderIsDev) And When I load it it work, I can create a world and even let him open forever at least I didn't move cause if so the game just crash and tell me this message :   Encountered an unexpected exception net.minecraft.class_148: Ticking entity at net.minecraft.server.MinecraftServer.method_3813(MinecraftServer.java:1025) at net.minecraft.server.MinecraftServer.method_3748(MinecraftServer.java:912) at net.minecraft.class_1132.method_3748(class_1132.java:114) at net.minecraft.server.MinecraftServer.method_29741(MinecraftServer.java:697) at net.minecraft.server.MinecraftServer.method_29739(MinecraftServer.java:281) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.class_4706.isEmpty()" because "tickets" is null at net.minecraft.class_3204.canNoneExpire(class_3204.java:1034) at net.minecraft.class_3204.handler$dln000$lithium$unregisterExpiringTicket(class_3204.java:1075) at net.minecraft.class_3204.method_17645(class_3204.java:172) at net.minecraft.class_3204.method_17292(class_3204.java:194) at net.minecraft.class_3215.method_17300(class_3215.java:443) at com.betterchunkloading.chunk.PlayerChunkData.checkPrediction(PlayerChunkData.java:406) at com.betterchunkloading.chunk.PlayerChunkData.checkDirection(PlayerChunkData.java:353) at com.betterchunkloading.chunk.PlayerChunkData.trackPlayerMovement(PlayerChunkData.java:177) at com.betterchunkloading.chunk.PlayerChunkData.onChunkChanged(PlayerChunkData.java:130) at com.betterchunkloading.event.EventHandler.onPlayerTick(EventHandler.java:200) at net.minecraft.class_3222.handler$zed000$betterchunkloading$onPlayerTick(class_3222.java:2618) at net.minecraft.class_3222.method_5773(class_3222.java:564) at net.minecraft.class_3218.mixinextras$bridge$method_5773$202(class_3218.java) at net.minecraft.class_3218.wrapOperation$eei000$puzzleslib$tickNonPassenger(class_3218.java:7797) at net.minecraft.class_3218.method_18762(class_3218.java:770) at net.minecraft.class_1937.method_18472(class_1937.java:498) at net.minecraft.class_3218.method_31420(class_3218.java:408) at net.minecraft.class_5574.method_31791(class_5574.java:54) at net.minecraft.class_3218.method_18765(class_3218.java:372) at net.minecraft.server.MinecraftServer.method_3813(MinecraftServer.java:1021) ... 5 more Sorry guys if it obvious but actually I'm an old player of Minecraft but I'm new in modding so yup. But Thanks everyone by advance to help me with this.
    • These forums are for Minecraft Forge, not Fabric. You would probably be better served posting this question wherever fabric support is given, most likely their discord server.
    • Add the new latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here
    • Update: I tried to reproduce the bug and found out that if I place a flare on a block and then dismantle it with the pickarang (Quark), the flare gets no NBT Tag after getting assigned an empty one through the offhand.
  • Topics

×
×
  • Create New...

Important Information

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