Jump to content

Recommended Posts

Posted

Hey everyone.

 

I've been fiddling around with NBTTagCompounds lately and I can't quite decipher entirely how it works.

 

I am currently making a pretty simple item that stores the number of entities killed and adds to the number every time a monster is killed by the player :) Here is my code.

 

@ForgeSubscribe
public void soulBagListener(LivingDeathEvent event)
{
	if(event.source.getDamageType().equals("player") && event.entityLiving instanceof EntityMob)
	{
		EntityPlayer player = (EntityPlayer)event.source.getEntity();
		String entityLivingName = event.entityLiving.getEntityName();

		for(int i = 0; i < player.inventory.getSizeInventory(); i++)
		{
		  if(player.inventory.getStackInSlot(i) != null)
			if(player.inventory.getStackInSlot(i).getItem() instanceof ItemSoulBag)
			{
				ItemStack item = player.inventory.getStackInSlot(i);

				NBTTagCompound nbt = item.getTagCompound();		
				if(nbt == null)
					item.setTagCompound(new NBTTagCompound());
				if(!nbt.hasKey("tsouls"))
					nbt.setInteger("tsouls", 0);
				nbt.setInteger("tsouls", nbt.getInteger("tsouls") + 1);

				if(!nbt.hasKey("tsouls" + "-" + entityLivingName))
					nbt.setInteger("tsouls" + "-" + entityLivingName, 0);			
				nbt.setInteger("tsouls" + "-" + entityLivingName, nbt.getInteger("tsouls" + "-" + entityLivingName) + 1);

				return;
			}
		}
	}

}

 

Essentially, it loops through all of the inventory slots and finds the first SoulBagItem, then executing the code.

 

The first part of the code adds +1 to the "total soul count" named "tsouls" which is displayed rather easily on the ItemStack's tooltip. That part is easy.

 

The second part of the code adds a +1 to the "specific mob soul count" named "tsouls-" + entity name. This is what I am having trouble with, as I highly doubt this is how NBT tag compounds should work. I want to loop through the NBTTagCompound and add each entity specific counts to the tooltip as well, but I can't do that with my current system.

 

Can anyone explain to me what I am doing wrong or critique my system? And also help me understand the differences between

1. NBTTagCompound

2. NBTTagList

  - specifically "subtag"

3. NBTBase

 

And before I possibly get flamed, I have looked at a fairly decent tutorial on the MC forums, as well as the Forge wiki, but they don't explain everything.

 

Thanks in advance,

 

Dr. E.

Follow me on twitter! @keepablock

Read up on whats new! www.catacombs.co

width=700 height=60http://electronic-chronic.com/assets/keep-a-block/wikilink/wikilink_logo.png[/img]

Posted

Look at NBTTagCompoundList (IIRC the class name correctly)

Essentially it's a list of tags that you can add to a tag, then each one of the tags in the list can hold the mob name and the count.

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.

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

    • Please read the FAQ and post logs as described there.
    • Upon starting the server I get; [main/ERROR] [minecraft/Main]: Failed to start the minecraft server net.minecraftforge.fml.LoadingFailedException: Loading errors encountered: [     Framework (framework) has failed to load correctly §7java.lang.NoClassDefFoundError: net/minecraft/client/gui/components/toasts/Toast ] I suspect there is a (possibly a few) client-only mods installed on my server. Any help would be appreciated! (Yes I know there are a lot of mods...) Here is the crash log:   https://paste.ee/p/pRz5mhMl#s=0
    • That's basically what the failure does, my apologies for failing to specify.  It just tries again on the next tick until it detects the entities for that chunk are loaded, and then tries to load the entity.  From there it gets into different failure states depending on what goes wrong, but in short, if the entity fails to load once the entity list becomes available, the request is cleared and must be resubmitted by the end user.  There should be few cases where that actually happens. Yes, that is my understanding of forceloading.  That's why on a successful summon, it removes the forceload.  Otherwise it does just leave the chunks loaded long term. Thank you for your help, any knowledge is useful!  I don't often mess with forceloading and my prior experience is 1.16 so I'm also a bit out of my depth haha.
    • I will have to do more research about 1.18 chunk loading. You were unclear about how your code manages with the entity load failure. If you simply used a loop, I suggest submitting a tick task to the next tick which does the same thing, checking if the entities are loaded and if so teleporting the right one else submitting another tick task etc. Also I think forceloading permanently force loads the chunk, and it only starts to unload when you make a subsequent call to mark the chunk as not forceloaded. I may be completely wrong, I dont know much about 1.18, most of my experience is 1.20. Good luck I hope you figure it out after all this time 😅
    • i managed to fix it by reinstalling the modpack and re-add all the extra mods I've had previously.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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