Need with itemstacks and getting them back if you die

Discussion in 'Plugin Development' started by PerezHD, Aug 19, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PerezHD

    Well hey guys, I was wondering on what is a way to make itemstacks come back into your inventory after you have died or gone through death?

    CODE:
    Code:java
    1. package com.perezhd.plugins.roguehub;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import net.minecraft.server.v1_7_R3.NBTTagCompound;
    7. import net.minecraft.server.v1_7_R3.NBTTagList;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Material;
    12. import org.bukkit.OfflinePlayer;
    13. import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemStack;
    14. import org.bukkit.enchantments.Enchantment;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.entity.PlayerDeathEvent;
    19. import org.bukkit.event.player.PlayerJoinEvent;
    20. import org.bukkit.event.player.PlayerQuitEvent;
    21. import org.bukkit.inventory.Inventory;
    22. import org.bukkit.inventory.ItemStack;
    23. import org.bukkit.inventory.meta.ItemMeta;
    24.  
    25. public class Join
    26. implements Listener
    27. {
    28. @EventHandler
    29. public void playerStartup(PlayerJoinEvent e)
    30. {
    31. Player p = e.getPlayer();
    32. Inventory i = p.getInventory();
    33.  
    34. ItemStack chest = new ItemStack(Material.CHEST);
    35. ItemMeta chestmeta = chest.getItemMeta();
    36. chestmeta.setDisplayName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "KIT SELECTOR" + ChatColor.GRAY + " (Right-Click)");
    37. List<String> chestlore = new ArrayList<String>();
    38. chestlore.add(ChatColor.GRAY + "Right click to open the kit selector!");
    39. chestmeta.setLore(chestlore);
    40. chest.setItemMeta(chestmeta);
    41.  
    42. ItemStack donorkits = new ItemStack(Material.WHEAT);
    43. ItemMeta donorkitsmeta = donorkits.getItemMeta();
    44. donorkitsmeta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD + "DONOR KIT SELECTOR" + ChatColor.GRAY + " (Right-Click)");
    45. List<String> donorkitslore = new ArrayList<String>();
    46. donorkitslore.add(ChatColor.GRAY + "Right click to open the donor kit selector!");
    47. donorkitsmeta.setLore(donorkitslore);
    48. donorkits.setItemMeta(donorkitsmeta);
    49.  
    50. ItemStack web = new ItemStack(Material.WEB);
    51. ItemMeta webmeta = web.getItemMeta();
    52. webmeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "RANKS" + ChatColor.GRAY + " (Right-Click)");
    53. List<String> weblore = new ArrayList<String>();
    54. weblore.add(ChatColor.GRAY + "Open this to view or ranks!");
    55. webmeta.setLore(weblore);
    56. web.setItemMeta(webmeta);
    57.  
    58. ItemStack book = new ItemStack(Material.PAPER);
    59. ItemMeta bookmeta = book.getItemMeta();
    60. bookmeta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "FORUMS" + ChatColor.GRAY + " (Right-Click)");
    61. List<String> booklore = new ArrayList<String>();
    62. booklore.add(ChatColor.GRAY + "Right click me for a link to our forums!");
    63. bookmeta.setLore(booklore);
    64. book.setItemMeta(bookmeta);
    65.  
    66. ItemStack Emerald = new ItemStack(Material.EMERALD);
    67. ItemMeta Emeraldmeta = Emerald.getItemMeta();
    68. Emeraldmeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "SHOP" + ChatColor.GRAY + " (Right-Click)");
    69. List<String> Emeraldlore = new ArrayList<String>();
    70. Emeraldlore.add(ChatColor.GRAY + "Right click me to view our store!");
    71. Emeraldmeta.setLore(Emeraldlore);
    72. Emerald.setItemMeta(Emeraldmeta);
    73.  
    74.  
    75. i.clear();
    76. i.setItem(0, (chest));
    77. i.setItem(1, (donorkits));
    78. i.setItem(5, (web));
    79. i.setItem(7, (book));
    80. i.setItem(8, (Emerald));
    81. }
    82.  
    83. public void onLeave(PlayerQuitEvent e) {
    84. e.setQuitMessage("");
    85. }
    86. }


    Please just show me how to make it so they get these items stacks on death event
     
  2. Offline

    br456

    PerezHD
    You need to create a PlayerDeathEvent, and schedule a delayed task to give the players their items after they spawn
     
  3. Offline

    iBecameALoaf

    PerezHD Just use a hashmap, when a player dies, save their inventory contents, and when they respawn, get the info from the hashmap and give it to the player.
     
  4. Offline

    Totom3

  5. Offline

    Dragonphase

    PerezHD

    You could store the contents of the player's inventory as Metadata when they die, and retrieve the items from the Metadata when they respawn. That way, you won't need to use a delayed task:

    Code:java
    1. //On death:
    2. player.setMetadata("items", new FixedMetadataValue(plugin, player.getInventory().getContents()));
    3.  
    4. //On respawn:
    5. if (player.hasMetadata("items")){
    6. for (MetadataValue value : player.getMetadata("items")){
    7. if (value.getOwningPlugin().getName().equalsIgnoreCase(plugin.getName())){
    8. ItemStack[] contents = (ItemStack[]) value.value();
    9. player.getInventory().setContents(contents);
    10. }
    11. }
    12. }
     
Thread Status:
Not open for further replies.

Share This Page