OnDeath

Discussion in 'Plugin Development' started by MrLizardDogMan, Feb 25, 2014.

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

    MrLizardDogMan

    I want to be able to set back the player's health and Maxhealth to 20 when a player dies.
    I try this, but it doesn't work.

    Code:java
    1. public class onDeathListener implements Listener {
    2.  
    3. @EventHandler
    4. public void onPlayerDeath(PlayerDeathEvent event){
    5. Player player = event.getEntity();
    6. player.setMaxHealth(20.0);
    7. player.setHealth(20.0);
    8. }
    9. }


    What would be the correct code for this?
     
  2. Offline

    NathanWolf

    You probably also need to cancel the event to keep the player from dying.
     
  3. Offline

    MrLizardDogMan

    But i need them to die to go towards PvPStats
     
  4. Offline

    Heirteir

    MrLizardDogMan
    I am still not understanding why your doing this are you doing this so they can keep there inventory on death or what?
     
  5. Offline

    MrLizardDogMan

    Well this is for KitPvP, and each class has a different Maxhealth, and i want that to be reset when they die.
    Code:java
    1. package me.MrLizard.KitPvP;
    2.  
    3. import net.minecraft.server.v1_7_R1.NBTTagList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.entity.PlayerDeathEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19. import org.bukkit.potion.PotionEffect;
    20. import org.bukkit.potion.PotionEffectType;
    21.  
    22.  
    23. public class KitPvP extends JavaPlugin{
    24.  
    25. public static KitPvP plugin;
    26.  
    27. @Override
    28. public void onDisable() {
    29. }
    30.  
    31. @Override
    32. public void onEnable() {
    33. //Register Events
    34. plugin.getServer().getPluginManager().registerEvents((Listener) this, this);
    35. }
    36.  
    37. public class onDeathListener implements Listener {
    38.  
    39. @EventHandler
    40. public void onPlayerDeath(PlayerDeathEvent event){
    41. Player player = event.getEntity();
    42. player.setMaxHealth(20.0);
    43. player.setHealth(20.0);
    44. }
    45. }
    46. //Commands
    47. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    48. if (!(sender instanceof Player)) {
    49. sender.sendMessage("You cannot run this command through the console!");
    50. return false;
    51. }
    52. Player player = (Player) sender;
    53. if(cmd.getName().equalsIgnoreCase("Lobby")){
    54. //Reset Player to Default
    55. player.setFoodLevel(20);
    56. player.setMaxHealth(20.0);
    57. player.setHealth(20.0);
    58. ((CraftPlayer)player).getHandle().inventory.b(new NBTTagList());
    59. player.removePotionEffect(PotionEffectType.SPEED);
    60. player.removePotionEffect(PotionEffectType.SLOW);
    61. //Sends Player Joining Message
    62. player.sendMessage(ChatColor.GOLD + "Joining Lobby...");
    63. //Teleporting Player
    64. player.teleport(new Location(Bukkit.getWorld("world"), -785, 27, 195, 180, 2));
    65. }
    66.  
    67. //Classes
    68. else if(cmd.getName().equalsIgnoreCase("join")){
    69. if (args.length == 0){
    70. // Showing the usage of the command here would be good.
    71. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + " Usage: /join [ARENA] [CLASS]");
    72. }
    73. else if (args.length == 1 && args[0].equalsIgnoreCase("mall")){
    74. //EDIT TO CLASS LOBBY
    75. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    76. }
    77. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("knight")){
    78. //Reset
    79. player.setFoodLevel(20);
    80. player.setMaxHealth(20.0);
    81. player.setHealth(20.0);
    82. player.setGameMode(GameMode.SURVIVAL);
    83. ((CraftPlayer)player).getHandle().inventory.b(new NBTTagList());
    84. player.removePotionEffect(PotionEffectType.SPEED);
    85. player.removePotionEffect(PotionEffectType.SLOW);
    86. // Player chose knight!
    87. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + " Joining Mall As Knight");
    88. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    89. player.setGameMode(GameMode.SURVIVAL);
    90. //Items
    91. ItemStack[] armor = new ItemStack[4];
    92. armor[0] = new ItemStack(Material.CHAINMAIL_BOOTS, 1);
    93. armor[1] = new ItemStack(Material.CHAINMAIL_LEGGINGS, 1);
    94. armor[2] = new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1);
    95. armor[3] = new ItemStack(Material.CHAINMAIL_HELMET, 1);
    96. player.getInventory().setArmorContents(armor);
    97. //Items
    98. ItemStack[] items = {new ItemStack(Material.IRON_SWORD), new ItemStack(Material.COOKED_BEEF, 32)};
    99. player.getInventory().addItem(items);
    100. }
    101. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("archer")){
    102. //Reset
    103. player.setFoodLevel(20);
    104. player.setMaxHealth(20.0);
    105. player.setHealth(20.0);
    106. player.setGameMode(GameMode.SURVIVAL);
    107. ((CraftPlayer)player).getHandle().inventory.b(new NBTTagList());
    108. player.removePotionEffect(PotionEffectType.SPEED);
    109. player.removePotionEffect(PotionEffectType.SLOW);
    110. // Player chose archer!
    111. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + " Joining Mall As Archer");
    112. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    113. ItemStack[] armor = new ItemStack[4];
    114. armor[0] = new ItemStack(Material.LEATHER_BOOTS, 1);
    115. armor[1] = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    116. armor[2] = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    117. armor[3] = new ItemStack(Material.LEATHER_HELMET, 1);
    118. player.getInventory().setArmorContents(armor);
    119. }
    120. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("tank")){
    121. //Reset
    122. player.setFoodLevel(20);
    123. player.setMaxHealth(40.0);
    124. player.setHealth((double)40);
    125. player.setGameMode(GameMode.SURVIVAL);
    126. ((CraftPlayer)player).getHandle().inventory.b(new NBTTagList());
    127. player.removePotionEffect(PotionEffectType.SPEED);
    128. player.removePotionEffect(PotionEffectType.SLOW);
    129. // Player chose Tank!
    130. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + " Joining Mall As Tank");
    131. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    132. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100000000, 2));
    133. ItemStack[] armor = new ItemStack[4];
    134. armor[0] = new ItemStack(Material.CHAINMAIL_BOOTS, 1);
    135. armor[1] = new ItemStack(Material.CHAINMAIL_LEGGINGS, 1);
    136. armor[2] = new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1);
    137. armor[3] = new ItemStack(Material.CHAINMAIL_HELMET, 1);
    138. player.getInventory().setArmorContents(armor);;
    139. ItemStack[] items = {new ItemStack(Material.IRON_SWORD), new ItemStack(Material.COOKED_BEEF, 32)};
    140. player.getInventory().addItem(items);
    141. }
    142. else if (args.length == 2 && args[0].equalsIgnoreCase("mall") && args[1].equalsIgnoreCase("scout")){
    143. //Reset
    144. ((CraftPlayer)player).getHandle().inventory.b(new NBTTagList());
    145. player.removePotionEffect(PotionEffectType.SPEED);
    146. player.removePotionEffect(PotionEffectType.SLOW);
    147. player.setMaxHealth(10.0);
    148. player.setHealth(10.0);
    149. player.setFoodLevel(20);
    150. player.setGameMode(GameMode.SURVIVAL);
    151. // Player chose Scout!
    152. player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "Kit" + ChatColor.GRAY + "]" + ChatColor.LIGHT_PURPLE + " Joining Mall As Scout");
    153. player.teleport(new Location(Bukkit.getWorld("world"), -967.5, 12, 580.5));
    154. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100000000, 1));
    155. ItemStack[] armor = new ItemStack[4];
    156. armor[0] = new ItemStack(Material.LEATHER_BOOTS, 1);
    157. armor[1] = new ItemStack(Material.LEATHER_LEGGINGS, 1);
    158. armor[2] = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
    159. armor[3] = new ItemStack(Material.LEATHER_HELMET, 1);
    160. player.getInventory().setArmorContents(armor);
    161. ItemStack[] items = {new ItemStack(Material.STONE_SWORD), new ItemStack(Material.APPLE, 16)};
    162. player.getInventory().addItem(items);
    163. }
    164. }
    165. return false;
    166. }
    167. }
    168.  
    169.  
    170.  
     
  6. Offline

    Heirteir

    MrLizardDogMan
    After Seeing Your Code I noticed one error that you need to make sure you check in any plugin you make.
    you setup your listener wrong.
    in this case it's going to be
    Code:java
    1. Bukkit.getServer().getPluginManager.registerEvents(new onDeathListener(), this);

    I don't understand why you put it like that, But that should fix the problem.
    also ill explain it to
    "new onDeathListener" is saying that the class onDeathListener is a listener
    then "this" is saying that this plugin(your kitpvp plugin) is the plugin that is implementing the listener.
    I hope this helped you!
    heirteir
     
  7. Offline

    qlimax5000

    Heirteir
    I don't really get what you want but if you want to just instantly respawn the player at their respawn point then use:
    Code:java
    1. getServer().getScheduler().runTaskLater(MAIN_CLASS_INSTANCE, new Runnable() {
    2. public void run() {
    3. ((CraftPlayer) player).getHandle().playerConnection.a(new PacketPlayInClientCommand(EnumClientCommand.PERFORM_RESPAWN));
    4. }
    5. }, 3);

    Then if you want to teleport them or whatever just use the PlayerRespawnEvent :)
     
  8. Offline

    MrLizardDogMan

    Ok this is what i have so far,

    Code:java
    1. @Override
    2. public void onEnable() {
    3. //Register Events
    4. plugin.getServer().getPluginManager().registerEvents((Listener) this, this);
    5. bukkit.getServer().getPluginManager().registerEvents(new onDeathListener(), this);
    6.  
    7. @EventHandler
    8. public void onPlayerDeath{
    9. Player player = event.getEntity();
    10. player.setMaxHealth(20.0);
    11. player.setHealth(20.0);
    12. }

    Could someone explain how to do it correctly?
     
  9. Offline

    badboystee


    Just Made This Up

    CODE:
    NOT TESTED!
    Code:java
    1. package com.ste.Help;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.PlayerDeathEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Help extends JavaPlugin implements Listener {
    11.  
    12. public void onEnable() {
    13. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    14. }
    15.  
    16. @EventHandler
    17. public void onPlayerDeath(PlayerDeathEvent e) {
    18. Player p = (Player) e.getEntity();
    19. p.setMaxHealth(20);
    20. p.setHealth(20);
    21. }
    22. }

     
  10. Offline

    MrLizardDogMan

    badboystee In line 13, "registerEvents" i get this error
    Nevermind, Thanks badboystee !

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  11. Offline

    badboystee

    Okay no problem maybe a like if I helped
     
Thread Status:
Not open for further replies.

Share This Page