Lordmau5 Posted May 20, 2013 Posted May 20, 2013 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); } } Quote
Lordmau5 Posted May 20, 2013 Author Posted May 20, 2013 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); } } Quote
Lordmau5 Posted May 20, 2013 Author Posted May 20, 2013 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. Quote
Recommended Posts
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.