Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[SOLVED][1.8] How do I assign NBT Values to other items in an Inventory?

Featured Replies

Posted

I'm attempting to use the well-known "Coordinate Cache" item to assign NBT values to another item.

 

Here's the Coordinate Cache item Class:

 

package com.example.ubiquity.items;

import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class SwordCache extends Item{
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
	if(!playerIn.isSneaking()){
		if(stack.getTagCompound() == null ){
			stack.setTagCompound(new NBTTagCompound());
		}
		NBTTagCompound posnbt = new NBTTagCompound();
		posnbt.setInteger("posX", pos.getX());
		posnbt.setInteger("posY", pos.getY());
		posnbt.setInteger("posZ", pos.getZ());
		stack.getTagCompound().setTag("coords", posnbt);
		stack.setStackDisplayName(EnumChatFormatting.GOLD + "Ubiquitous Link");
	}return true;
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn){
if (playerIn.isSneaking()){
		if(stack.getTagCompound() != null){
			stack.getTagCompound().removeTag("coords");
			stack.clearCustomName();
		}
	}
	return stack;
}
@Override
@SideOnly(Side.CLIENT)
@SuppressWarnings("unchecked")
public void addInformation(ItemStack stack, EntityPlayer playerIn, List list, boolean advanced){
	if(stack.getTagCompound() != null){
		if(stack.getTagCompound().hasKey("coords")){
			NBTTagCompound posnbt = (NBTTagCompound) stack.getTagCompound().getTag("coords");
			int posX = posnbt.getInteger("posX");
			int posY = posnbt.getInteger("posY");
			int posZ = posnbt.getInteger("posZ");
			list.add("This item is bound to");
			list.add("X: " + posX + " Y: " + posY + " Z: " + posZ);
		}
	}
}
@Override
@SideOnly(Side.CLIENT)
public boolean hasEffect(ItemStack stack){
	NBTTagCompound nbtTagCompound = stack.getTagCompound();
	if(nbtTagCompound != null){
		return stack.getTagCompound().hasKey("coords");
	}
	return stack.isItemEnchanted();
}
}

 

My question is about this line in the OnItemUse method...

if(!playerIn.isSneaking()){

 

How can I change this to see if the player has another item, called "Coordinate Key", and then store the following NBT information in said item?

Iterate through the player's inventory and, if you find the ItemStack containing said item, you can add NBT to it with the instance you have from your for loop.

 

Pseudo-code with potentially dodgy syntax:

for (ItemStack stack : player.inventory) {
  if (stack != null && stack.getItem() == itemYouWant) {
     // modify and/or set the stack's tag compound
  }
}

  • Author

Iterate through the player's inventory and, if you find the ItemStack containing said item, you can add NBT to it with the instance you have from your for loop.

 

Pseudo-code with potentially dodgy syntax:

for (ItemStack stack : player.inventory) {
  if (stack != null && stack.getItem() == itemYouWant) {
     // modify and/or set the stack's tag compound
  }
}

 

How would I parse itemYouWant?

  • Author

Items.stick? Items.apple? YourModItems.crystalKey? Whichever Item you are searching for. And be sure to put a

'break;' statement after you find it, since you don't need to keep looking.

 

Ah, okay. I was specifically talking about defining a mod item, wasn't sure how to do that, sorry!

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.