Jump to content

[1.10] How does NBT Data work with ItemStacks


Darki

Recommended Posts

Hi,

I am working on a mod. I've made a Syring, when you rightclicking it , it will fill with the players blood (i save the player's name and uuid as nbttag in the item). When you hover over the filled syring the Name of the "victim" will be shown (i check the nbt data). My problem is: It seems that the nbt data is for all items the same. When i used a syring and look in the Creative menu the syring in it shows me the name of mine. How can i do it that the nbt is bound to the itemstack?

If you need my code:

NBT Data (Syring-Use):

public class ItemSyringEmpty extends BasicItem{

	public ItemSyringEmpty() {
		super("You can fill the Syring by rightclicking.", "§7Status: §c§lEmpty");
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
		if(itemStackIn.getUnlocalizedName().contains("itemSyringEmpty")) {
			int slot = getSlot(playerIn, itemStackIn);
			if(slot != -1) {
				ItemStack itemStack = new ItemStack(Diseases.instance.items.itemSyringFilled);
				NBTTagCompound data;
				if(itemStack.hasTagCompound())
					data = itemStack.getTagCompound();
				else
					data = new NBTTagCompound();
				data.setString("bloodFrom", playerIn.getName());
				data.setString("bloodFromID", playerIn.getUniqueID().toString());
				itemStack.setTagCompound(data);
				playerIn.inventory.mainInventory[slot] = itemStack;
			}
		}
		return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
	}
	
	private int getSlot(EntityPlayer player, ItemStack item) {
		try {
			return player.inventory.getSlotFor(item);
		}catch (Exception e) {
			return -1;
		}
	}
}

NBT Data (Syring-Hover-Lore):

public class ItemSyringFilled extends BasicItem{

	public ItemSyringFilled() {
		super("Its filled with Blood. It can be \nused to inject medicine or \nyour own virus by rightclicking.", "§7Status: §a§lFilled");
	}
	
	@Override
	public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
		if(stack.hasTagCompound()) {
			if(stack.getTagCompound().hasKey("bloodFrom")) {
				this.addLine("§7Blood from: §4" + stack.getTagCompound().getString("bloodFrom"));
			}
		}
		super.addInformation(stack, playerIn, tooltip, advanced);
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
		if(itemStackIn.getUnlocalizedName().contains("itemSyringFilled")) {
			//TODO inject
		}
		return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
	}

}

 

Link to comment
Share on other sites

41 minutes ago, Darki said:

if(itemStackIn.getUnlocalizedName().contains("itemSyringEmpty")) {

Why the fuck are you comparing unlocalized name strings? Jesus christ, itemStackIn.getItem() == MainMod.SyringeEmpty

43 minutes ago, Darki said:

playerIn.inventory.mainInventory[slot] = itemStack;

You do know what ActionResult<ItemStack> means, yes? It means that the item stack that is a part of the ActionResult return is the item that gets put into the player's active inventory slot...

 

As for your asked question, you're doing something really, seriously wrong. I need to see more code, probably your BasicItem class and both syringes in completion.

 

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.

Link to comment
Share on other sites

Quote

Why the fuck are you comparing unlocalized name strings? Jesus christ, itemStackIn.getItem() == MainMod.SyringeEmpty

Hmm. Yeah i see. Thats better...

Quote

You do know what ActionResult<ItemStack> means, yes? It means that the item stack that is a part of the ActionResult return is the item that gets put into the player's active inventory slot...

Nope. I didn't know that. Thanks for the info^^

Here the rest of my Code:

BasicItem:

public abstract class BasicItem extends Item{

	private String[] extraLore;
	private String[] description;
	
	public BasicItem(String description, String...extraLore) {
		setCreativeTab(Diseases.TAB);
		this.description = description.split("\n");
		this.extraLore = extraLore;
	}
	
	public void addLine(String line) {
		List<String> list = new ArrayList<String>();
		for(String str : extraLore) {
			list.add(str);
		}
		if(!list.contains(line))
			list.add(line);
		extraLore = list.toArray(new String[list.size()]);
	}
	
	@Override
	public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
		if(extraLore.length > 0) {
			for(String line : extraLore) {
				tooltip.add(line);
			}
			tooltip.add(" ");
		}
		for(String line : description) {
			tooltip.add("§7" + line);
		}
	}
}

And you do have the syrings on the top^^

Link to comment
Share on other sites

Ok. Here we go:

public class ItemSyringEmpty extends BasicItem{

	public ItemSyringEmpty() {
		super("SyringEmpty", "You can fill the Syring by rightclicking.", "§7Status: §c§lEmpty");
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
		if(itemStackIn.getItem() == Diseases.instance.items.itemSyringEmpty) {
			int slot = getSlot(playerIn, itemStackIn);
			if(slot != -1) {
				ItemStack itemStack = new ItemStack(Diseases.instance.items.itemSyringFilled);
				NBTTagCompound data = itemStack.getSubCompound("itemData", true);
				data.setString("bloodFrom", playerIn.getName());
				data.setString("bloodFromID", playerIn.getUniqueID().toString());
				playerIn.inventory.mainInventory[slot] = itemStack;
			}
		}
		return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
	}
	
	@Override
	public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) {
		if(stack.getItem() == Diseases.instance.items.itemSyringEmpty) {
			int slot = getSlot(player, stack);
			if(slot != -1) {
				ItemStack itemStack = new ItemStack(Diseases.instance.items.itemSyringFilled);
				NBTTagCompound data = itemStack.getSubCompound("itemData", true);
				data.setString("bloodFrom", entity.getName());
				data.setString("bloodFromID", entity.getUniqueID().toString());
				Random random = new Random();
				if(random.nextInt(10) == 0) {
					int amount = random.nextInt(2) + 1;
					while(amount != 0) {
						amount--;
						Diseases.instance.dataManager.addDisease(itemStack, EnumDisease.values()[random.nextInt(EnumDisease.values().length)]);
					}
				}
				player.inventory.mainInventory[slot] = itemStack;
			}
		}
		return super.onLeftClickEntity(stack, player, entity);
	}
	
	private int getSlot(EntityPlayer player, ItemStack item) {
		try {
			return player.inventory.getSlotFor(item);
		}catch (Exception e) {
			return -1;
		}
	}

I didnt change the line with removing item from inventory by now but i will....^^

public class ItemSyringFilled extends BasicItem{

	public ItemSyringFilled() {
		super("SyringFilled", "Its filled with Blood. It can be \nused to inject medicine or \nyour own virus by rightclicking.", "§7Status: §a§lFilled");
	}
	
	@Override
	public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
		if(stack.hasTagCompound()) {
			if(stack.getSubCompound("itemData", true).hasKey("bloodFrom")) {
				this.addLine("§7Blood from: §4" + stack.getSubCompound("itemData", true).getString("bloodFrom"));
			}
			if(stack.getSubCompound("itemData", true).hasKey("diseases")) {
				
			}
		}
		super.addInformation(stack, playerIn, tooltip, advanced);
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
		if(itemStackIn.getItem() == Diseases.instance.items.itemSyringFilled) {
			//TODO inject
		}
		return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
	}

}

 

Link to comment
Share on other sites

Just now, diesieben07 said:

This is the most important and the source of your problem.

Ok this I did solve, i hope:

@Override
	public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
		if(itemStackIn.getItem() == Diseases.instance.items.itemSyringEmpty) {
			ItemStack itemStack = new ItemStack(Diseases.instance.items.itemSyringFilled);
			NBTTagCompound data = itemStack.getSubCompound("itemData", true);
			data.setString("bloodFrom", playerIn.getName());
			data.setString("bloodFromID", playerIn.getUniqueID().toString());
			return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemStack);
		}
		return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
	}

 

2 minutes ago, diesieben07 said:

What do you not understand about these?

I dont know how to use it. And can it be dynamic then? So that the item shows the name of the "victim". Can you just explain how to use it (i dont want code^^) just an explaination.

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.