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.

Featured Replies

Posted

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

  • Author

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

  • Author

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.

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.