Player Inventory Saving on EntityDeathEvent

Discussion in 'Plugin Development' started by FatAussieFatBoy, Sep 15, 2013.

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

    FatAussieFatBoy

    Hi Everyone (who reads my post) I am trying to make a Kit Plugin that saves a Players Inventory on Death so when the Player Dies he doesn't have to type the Command to get his kit again... I have been at this for a While and really need help with this, I have also tried going on BukkitDev IRC and Got some help but nothing Works for me...

    Here is my Code :

    Code:java
    1. package me.FUGProductions.KitSigns;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Effect;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.EntityDeathEvent;
    15. import org.bukkit.event.player.PlayerRespawnEvent;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.plugin.PluginManager;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19. import org.bukkit.potion.PotionEffect;
    20. import org.bukkit.potion.PotionEffectType;
    21.  
    22. public class Main extends JavaPlugin implements Listener{
    23.  
    24. public void onEnable() {
    25. PluginManager pm = getServer().getPluginManager();
    26. pm.registerEvents(this, this);
    27. getLogger().info("Enabled!");
    28. }
    29.  
    30. public Main plugin;
    31. public HashMap<Player, ItemStack[]> invsave = new HashMap<Player, ItemStack[]>();
    32. public HashMap<Player, ItemStack[]> armorsave = new HashMap<Player, ItemStack[]>();
    33.  
    34. ChatColor GREY = ChatColor.GRAY;
    35. ChatColor GREEN = ChatColor.GREEN;
    36. ChatColor RED = ChatColor.RED;
    37. ChatColor BLUE = ChatColor.BLUE;
    38. ChatColor WHITE = ChatColor.WHITE;
    39. ChatColor GOLD = ChatColor.GOLD;
    40. ChatColor PURPLE = ChatColor.LIGHT_PURPLE;
    41. ChatColor BOLD = ChatColor.BOLD;
    42.  
    43. //Inventory Clear Event
    44. public static void InventoryClear(Player p) {
    45. p.getInventory().setArmorContents(null);
    46. p.getInventory().clear();
    47. for (PotionEffect effect : p.getActivePotionEffects())
    48. p.removePotionEffect(effect.getType());
    49. }
    50.  
    51. //Archer Kit
    52. public void Archer(Player p) {
    53. ItemStack boots = new ItemStack(305, 1);
    54. ItemStack leggings = new ItemStack(304, 1);
    55. ItemStack chestplate = new ItemStack(303, 1);
    56. ItemStack helmet = new ItemStack(302, 1);
    57. ItemStack bow = new ItemStack(261, 1);
    58. PotionEffect effect = new PotionEffect(PotionEffectType.SPEED, 20000, 0);
    59. bow.addEnchantment(Enchantment.ARROW_INFINITE, 1);
    60. bow.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 4);
    61. bow.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    62. p.addPotionEffect(effect);
    63.  
    64. p.getInventory().setBoots(boots);
    65. p.getInventory().setLeggings(leggings);
    66. p.getInventory().setChestplate(chestplate);
    67. p.getInventory().setHelmet(helmet);
    68. p.getInventory().addItem(bow);
    69. p.getInventory().addItem(new ItemStack(262, 1));
    70.  
    71. }
    72.  
    73. //Knight Kit
    74. public void Knight(Player p) {
    75. ItemStack boots = new ItemStack(309, 1);
    76. ItemStack leggings = new ItemStack(308, 1);
    77. ItemStack chestplate = new ItemStack(307, 1);
    78. ItemStack helmet = new ItemStack(306, 1);
    79. ItemStack sword = new ItemStack(276, 1);
    80. PotionEffect effect = new PotionEffect(PotionEffectType.REGENERATION, 20000, 0);
    81. sword.addEnchantment(Enchantment.KNOCKBACK, 2);
    82. sword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    83. p.addPotionEffect(effect);
    84.  
    85. p.getInventory().setBoots(boots);
    86. p.getInventory().setLeggings(leggings);
    87. p.getInventory().setChestplate(chestplate);
    88. p.getInventory().setHelmet(helmet);
    89. p.getInventory().addItem(sword);
    90.  
    91. }
    92.  
    93. //Tank Kit
    94. public void Tank(Player p) {
    95. ItemStack boots = new ItemStack(313, 1);
    96. ItemStack leggings = new ItemStack(312, 1);
    97. ItemStack chestplate = new ItemStack(311, 1);
    98. ItemStack helmet = new ItemStack(310, 1);
    99. ItemStack sword = new ItemStack(268, 1);
    100. PotionEffect effect = new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 20000, 1);
    101. PotionEffect effect1 = new PotionEffect(PotionEffectType.SLOW, 20000, 0);
    102. sword.addEnchantment(Enchantment.FIRE_ASPECT, 2);
    103. sword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    104. p.addPotionEffect(effect);
    105. p.addPotionEffect(effect1);
    106.  
    107. p.getInventory().setBoots(boots);
    108. p.getInventory().setLeggings(leggings);
    109. p.getInventory().setChestplate(chestplate);
    110. p.getInventory().setHelmet(helmet);
    111. p.getInventory().addItem(sword);
    112.  
    113. }
    114.  
    115. //Commands
    116. @SuppressWarnings("deprecation")
    117. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    118. Player p = (Player) sender;
    119.  
    120. if(cmd.getName().equalsIgnoreCase("Heal")) {
    121. p.sendMessage(GREEN + p.getName() + GREY + " You have successfully been Healed!");
    122. p.setHealth(20);
    123. p.setFoodLevel(20);
    124. }
    125. if(cmd.getName().equalsIgnoreCase("InvClear")) {
    126. p.sendMessage(GREEN + p.getName() + GREY + " You have successfully Cleared your Inventory");
    127. InventoryClear(p);
    128. }
    129. if(cmd.getName().equalsIgnoreCase("KitHelp")) {
    130. p.sendMessage(GREEN + "This is the KitSigns Help Menu");
    131. p.sendMessage(GREEN + "COMMANDS");
    132. p.sendMessage(GOLD + "/Heal" + GREY + ": Used to heal you Player!");
    133. p.sendMessage(GOLD + "/InvClear" + GREY + ": Used to Clear your Inventory!");
    134. p.sendMessage(GOLD + "/KitHelp" + GREY + ": Shows the Help Menu (this menu)");
    135. p.sendMessage(GOLD + "/Kit 'KitName'" + GREY + ": Give you the Desired Kit");
    136. }
    137. if(cmd.getName().equalsIgnoreCase("Kit")) {
    138. switch(args[0].toLowerCase()) {
    139. case "archer" :
    140. p.sendMessage("[" + GOLD + "FUNKits" + WHITE + "] " + GREEN + p.getName() + GREY + " You are now using the :" + GREEN + " Archer" + GREY + " Class!");
    141. InventoryClear(p);
    142. Archer(p);
    143. p.getWorld().playEffect(p.getLocation(),Effect.MOBSPAWNER_FLAMES,4);
    144. break;
    145.  
    146. case "knight" :
    147. p.sendMessage("[" + GOLD + "FUNKits" + WHITE + "] " + GREEN + p.getName() + GREY + " You are now using the :" + GREEN + " Knight" + GREY + " Class!");
    148. InventoryClear(p);
    149. Knight(p);
    150. p.getWorld().playEffect(p.getLocation(),Effect.MOBSPAWNER_FLAMES,4);
    151. break;
    152.  
    153. case "tank" :
    154. p.sendMessage("[" + GOLD + "FUNKits" + WHITE + "] " + GREEN + p.getName() + GREY + " You are now using the :" + GREEN + " Tank" + GREY + " Class!");
    155. InventoryClear(p);
    156. Tank(p);
    157. p.getWorld().playEffect(p.getLocation(),Effect.MOBSPAWNER_FLAMES,4);
    158. break;
    159. }
    160. }
    161. return false;
    162. }
    163.  
    164. @EventHandler
    165. public void onPlayerSpawn(PlayerRespawnEvent event, Player p) {
    166. p.getInventory().setContents(invsave.get(p));
    167. p.getInventory().setArmorContents(armorsave.get(p));
    168. }
    169.  
    170. @EventHandler
    171. public void onPlayerDeath(EntityDeathEvent event) {
    172. if(event.getEntity() instanceof Player) {
    173. Player p = (Player) event.getEntity();
    174. if(event.getDrops() != null) {
    175. invsave.put(p, p.getInventory().getContents());
    176. event.getDrops().clear();
    177. Bukkit.broadcastMessage("[" + GOLD + "FUNKits" + WHITE + "] " + RED + p.getName() + GREY + " Has been Killed!");
    178. }else{
    179. Bukkit.broadcastMessage("[" + GOLD + "FUNKits" + WHITE + "] " + RED + p.getName() + GREY + " Has been Killed!");
    180. }
    181. }
    182. }
    183.  
    184.  
    185. public void onDisable() {
    186. getLogger().info("Disabled!");
    187. }
    188.  
    189. }


    All of this Code works and has no Errors but still no Good... any Help would be Greatly Appreciated... and Yes I am using HashMaps but any other Method will be Welcomed to this Forum...

    FatAussieFatBoy <3
     
  2. Offline

    marshmallowz

    Wrond section, go to Plugin Development not Resources in Plugin Development.
     
  3. Offline

    FatAussieFatBoy

    Can someone move my Post for me then If possible, sorry I'm new to the whole Bukkit Thing
     
  4. Offline

    Jade

  5. Offline

    InflamedSebi

    why do u want to save the inventory? what about just cancel the dropping of the items, so he would simply keep it ...

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.LOW)
    3. public void blockCommand(PlayerDeathEvent evt) {
    4. Player player = evt.getEntity();
    5. evt.getDrops().clear();
    6. }
     
Thread Status:
Not open for further replies.

Share This Page