Jump to content

[Events / NBT] Loading not working


Lordmau5

Recommended Posts

I use some event system, to add some functions to the player death and player spawn.

 

If the player dies, it writes something to it's nbt.

Also, after checking it with .getKey, it IS there.

 

At spawn, I check again, if it's there.

But it isn't?!

 

 

Code:

@ForgeSubscribe
public void onEntitySpawn(EntityJoinWorldEvent event)
    {
	if(event.world.isRemote)
	{
		return;
	}
        if(event.entity instanceof EntityPlayer)
        {
        	EntityPlayer thePlayer = (EntityPlayer) event.entity;
        	
        	NBTTagCompound nbtTag = new NBTTagCompound();
        	thePlayer.writeToNBT(nbtTag);

        	System.out.println("Getting entity data...");
        	System.out.println("Has Key DI? : " + nbtTag.hasKey("DeathImprover"));
        	if(nbtTag.hasKey("DeathImprover"))
        	{
        		NBTTagCompound DI = nbtTag.getCompoundTag("DeathImprover");
        		System.out.println("Has DI Compound");
        		nbtTag.removeTag("DeathImprover");
        	}
        }
    }

@ForgeSubscribe
public void onEntityDeath(LivingDeathEvent event)
{
	if(event.entity.worldObj.isRemote)
	{
		return;
	}
	if(event.entity instanceof EntityPlayer)
	{
		EntityPlayer thePlayer = (EntityPlayer) event.entity;

		System.out.println("Player Died!");

		NBTTagCompound entityTempTag = new NBTTagCompound();
		thePlayer.writeToNBT(entityTempTag);

		entityTempTag.setBoolean("DeathImprover", true);
		System.out.println("Saved the Vars!");
		System.out.println("has DI: " + entityTempTag.hasKey("DeathImprover"));

		event.setCanceled(true);
	}
}

Link to comment
Share on other sites

Still telling me on spawn, that the key doesn't exist...

 

Changed code:

@ForgeSubscribe
public void onEntitySpawn(EntityJoinWorldEvent event)
    {
	if(event.world.isRemote)
	{
		return;
	}
        if(event.entity instanceof EntityPlayer)
        {
        	EntityPlayer thePlayer = (EntityPlayer) event.entity;
        	
        	NBTTagCompound wholeTag = thePlayer.getEntityData();
        	
        	NBTTagCompound nbtTag = wholeTag.getCompoundTag(thePlayer.PERSISTED_NBT_TAG);

        	System.out.println("Getting entity data...");
        	System.out.println("Has Key DI? : " + nbtTag.hasKey("DeathImprover"));
        	if(nbtTag.hasKey("DeathImprover"))
        	{
        		NBTTagCompound DI = nbtTag.getCompoundTag("DeathImprover");
        		System.out.println("Has DI Compound");
                
        		nbtTag.removeTag("DeathImprover");
        	}
        }
    }

@ForgeSubscribe
public void onEntityDeath(LivingDeathEvent event)
{
	if(event.entity.worldObj.isRemote)
	{
		return;
	}
	if(event.entity instanceof EntityPlayer)
	{
		EntityPlayer thePlayer = (EntityPlayer) event.entity;

		System.out.println("Player Died!");
		NBTTagList tempTag = new NBTTagList();
		thePlayer.inventory.writeToNBT(tempTag);

		NBTTagCompound entityTempTag = thePlayer.getEntityData();
		thePlayer.writeToNBT(entityTempTag);

		NBTTagCompound persistTag = entityTempTag.getCompoundTag(thePlayer.PERSISTED_NBT_TAG);
		persistTag.setTag("DeathImprover", tempTag);

		System.out.println("Saved the Vars!");

		event.setCanceled(true);
	}
}

Link to comment
Share on other sites

As test, I want to write an boolean variable and check it later.

 

What I actually want to do is, that if my inventory contains a special item (e.g. an egg) it should save my inventory and load it again on spawn, independent of how the gamerule keepInventory is.

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



×
×
  • Create New...

Important Information

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