Jump to content

[1.10] How does NBT Data work with ItemStacks


Recommended Posts

Posted

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);
	}

}

 

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

Posted
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^^

Posted

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);
	}

}

 

Posted

Yes. I use subcompund. But the other ones: I dont know how to do it. Ok the Exception Catch I could change easily. But never used TextFormatting and Tooltips in the lang file

 

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

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

    • logs too big for one pastebin https://pastebin.com/ZjUGHu3u  https://pastebin.com/RqCUZf3X  https://pastebin.com/6ZPS99nD
    • You probably used jd-gui to open it, didn't you? Nothing wrong with that, I also made that mistake, except that Notch was a smart guy and he obfuscated the code. That's why you only see files called "a", "b", "c" and then a file that combines them all. As I said, use RetroMCP to deobfuscate the code so that you will 100% understand it and be able to navigate it.
    • Decompiling minecraft indev, infdev, alpha, beta or whichever legacy version is really easy. I'm not a plug, I just also got interested in modding legacy versions (Infdev to be specific). Use https://github.com/MCPHackers/RetroMCP-Java Once you install their client and the Zulu Architecture that they say they recommend (or use your own Java). I encountered some problems, so I run it with: "java -jar RetroMCP-Java-CLI.jar". You should run it in a seperate folder (not in downloads), otherwise the files and folders will go all over the place. How to use RetroMCP: Type setup (every time you want change version), copy-paste the version number from their list (they support indev), write "decompile" and done! The code will now be deobfuscated and filenames will be normal, instead of "a", "b" and "c"! Hope I helped you, but I don't expect you to reply, as this discussion is 9 years old! What a piece of history!  
    • I know that this may be a basic question, but I am very new to modding. I am trying to have it so that I can create modified Vanilla loot tables that use a custom enchantment as a condition (i.e. enchantment present = item). However, I am having trouble trying to implement this; the LootItemRandomChanceWithEnchantedBonusCondition constructor needs a Holder<Enchantment> and I am unable to use the getOrThrow() method on the custom enchantment declared in my mod's enchantments class. Here is what I have so far in the GLM:   protected void start(HolderLookup.Provider registries) { HolderLookup.RegistryLookup<Enchantment> registrylookup = registries.lookupOrThrow(Registries.ENCHANTMENT); LootItemRandomChanceWithEnchantedBonusCondition lootItemRandomChanceWithEnchantedBonusCondition = new LootItemRandomChanceWithEnchantedBonusCondition(0.0f, LevelBasedValue.perLevel(0.07f), registrylookup.getOrThrow(*enchantment here*)); this.add("nebu_from_deepslate", new AddItemModifier(new LootItemCondition[]{ LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.DEEPSLATE).build(), LootItemRandomChanceCondition.randomChance(0.25f).build(), lootItemRandomChanceWithEnchantedBonusCondition }, OrichalcumItems.NEBU.get())); }   Inserting Enchantments.[vanilla enchantment here] actually works but trying to declare an enchantment from my custom enchantments class as [mod enchantment class].[custom enchantment] does not work even though they are both a ResourceKey and are registered in Registries.ENCHANTMENT. Basically, how would I go about making it so that a custom enchantment declared as a ResourceKey<Enchantment> of value ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath([modid], [name])), declared in a seperate enchantments class, can be used in the LootItemRandomChanceWithEnchantedBonusCondition constructor as a Holder? I can't use getOrThrow() because there is no level or block entity/entity in the start() method and it is running as datagen. It's driving me nuts.
  • Topics

×
×
  • Create New...

Important Information

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