Solved error with OnPlayerDeath Event

Discussion in 'Plugin Development' started by drpk, Jul 24, 2014.

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

    drpk

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerDeath(final PlayerDeathEvent e){
    4. Player player = e.getEntity().getPlayer();
    5. cooldown.remove(player.getName());
    6. }

    with that code I get an error on e and the parentheses after with a "; expected" and "Unexpected token" I also get these errors with final "; expected" and "Expression Expected"
    Please help!
     
  2. Offline

    DannyDog

    drpk
    Are you closing that method with a }?
     
  3. Offline

    drpk

    DannyDog Oh yeah, I have, but I hastily copied and pasted it :p
     
  4. Offline

    messageofdeath

    Make sure 'cooldown' is a list of strings.
     
  5. Offline

    DannyDog

    drpk
    Not sure what kind of error you're getting, I copy and pasted that in my ide and there's no errors?
     
  6. Offline

    drpk

  7. Offline

    DannyDog

    drpk
    But it needs to store a string.
    An arrayList can store objects too.
    So List<String>
     
  8. Offline

    drpk

    this is my code
    Code:java
    1. package me.drpk.GKits;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.PlayerDeathEvent;
    12. import org.bukkit.event.player.PlayerMoveEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.PlayerInventory;
    15. import org.bukkit.potion.PotionEffect;
    16. import org.bukkit.potion.PotionEffectType;
    17.  
    18. import java.util.ArrayList;
    19.  
    20. /**
    21. * Created by USER on 7/24/2014.
    22. */
    23. public class Poseidon implements Listener {
    24.  
    25. static ArrayList<Player> cooldown = new ArrayList<Player>();
    26.  
    27. @EventHandler
    28. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    29. if (!(sender instanceof Player)) {
    30. sender.sendMessage(ChatColor.RED + "Only players can get kits!");
    31. return true;
    32. }
    33.  
    34. final Player p = (Player) sender;
    35. PlayerInventory pi = p.getInventory();
    36.  
    37. if (cmd.getName().equalsIgnoreCase("Poseidon")) {
    38. if (cooldown.contains(p)) {
    39. p.sendMessage(ChatColor.RED + "You cannot get another kit yet!");
    40. return true;
    41. }
    42. if (cooldown.contains(p)) {
    43. pi.addItem(new ItemStack(Material.IRON_SWORD, 1));
    44. pi.addItem(new ItemStack(Material.IRON_PICKAXE, 1));
    45. public void onPlayerMove (final PlayerMoveEvent evt){
    46. Material m = evt.getPlayer().getLocation().getBlock().getType();
    47. if (m == Material.STATIONARY_WATER || m == Material.WATER) {
    48.  
    49. p.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 10, 1));
    50. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10, 1));
    51. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 10, 1));
    52. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 1));
    53.  
    54.  
    55. }
    56. p.sendMessage(ChatColor.GREEN + "You got your kit!");
    57. cooldown.add(p);
    58.  
    59.  
    60. return true;
    61. }
    62. if (cooldown.contains(p)) {
    63. p.sendMessage(ChatColor.RED + "You cannot get a kit at this time.");
    64. return true;
    65. }
    66.  
    67. }
    68. }
    69. }
    70.  
    71.  
    72.  

    this is my Death Event class
    Code:java
    1. package me.yarocks.kits;
    2.  
    3. import me.yarocks.kits.Poseidon;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.entity.PlayerDeathEvent;
    9.  
    10.  
    11. /**
    12.  * Created by USER on 7/24/2014.
    13.  */
    14. public class DeathEvent{
    15. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    16. public void onPlayerDeathEvent(PlayerDeathEvent evt){
    17. final Player p = (Player) sender;
    18. Poseidon.cooldown.remove(p);
    19.  
    20. }
    21. }
    22. }
     
  9. Offline

    DannyDog

    drpk
    Line 45.
    You have a event method inside the onCommand method.
    Close the onCommand using }'s and add @EventHandler above your onPlayerMove method.
    Also you'd need to assign the player to a variable in your onPlayerMove method
    So Player p = evt.getPlayer();
     
  10. Offline

    ChipDev

    Code:java
    1.  
    2.  
    3. death event inside of onCommand..?
     
  11. Offline

    LCastr0

    Code:java
    1.  
    2.  
    3. Make the ArrayList a public static ArrayList<Player> instead of static ArrayList<Player>, because if you don't say it's public, only that class will be able to access it
     
  12. Offline

    Gater12

    drpk
    Cannot make a method inside a method silly.

    Actually classes in the same package as that class will be able to access it unless declared private.
     
  13. Offline

    DannyDog

    What?...?
     
  14. Offline

    Dragonphase

    LCastr0
    You shouldn't encourage the usage of statics where statics aren't necessary. The list could be stored in the plugin's main class and passed by reference to the CommandExecutor and the Listener without accessing it in a static context.
     
    fireblast709 likes this.
  15. Offline

    drpk

    my original problem is back
    Code:java
    1. package me.drpk.GKits;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.PlayerDeathEvent;
    12. import org.bukkit.event.player.PlayerMoveEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.PlayerInventory;
    15. import org.bukkit.potion.PotionEffect;
    16. import org.bukkit.potion.PotionEffectType;
    17.  
    18. import java.util.ArrayList;
    19.  
    20. /**
    21. * Created by USER on 7/24/2014.
    22. */
    23. public class Poseidon implements Listener {
    24.  
    25. static ArrayList<Player> cooldown = new ArrayList<Player>();
    26.  
    27. @EventHandler
    28. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    29. if (!(sender instanceof Player)) {
    30. sender.sendMessage(ChatColor.RED + "Only players can get kits!");
    31. return true;
    32. }
    33.  
    34. final Player p = (Player) sender;
    35. PlayerInventory pi = p.getInventory();
    36.  
    37. if (cmd.getName().equalsIgnoreCase("Poseidon")) {
    38. if (cooldown.contains(p)) {
    39. p.sendMessage(ChatColor.RED + "You cannot get another kit yet!");
    40. return true;
    41. }
    42. if (cooldown.contains(p)) {
    43. pi.addItem(new ItemStack(Material.IRON_SWORD, 1));
    44. pi.addItem(new ItemStack(Material.IRON_PICKAXE, 1));
    45.  
    46.  
    47. public void onPlayerMove(final PlayerMoveEvent evt){
    48. Material m = evt.getPlayer().getLocation().getBlock().getType();
    49. if (m == Material.STATIONARY_WATER || m == Material.WATER) {
    50.  
    51. p.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 10, 1));
    52. p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10, 1));
    53. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 10, 1));
    54. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10, 1));
    55.  
    56.  
    57. }
    58. p.sendMessage(ChatColor.GREEN + "You got your kit!");
    59. cooldown.add(p);
    60.  
    61.  
    62. return true;
    63. }
    64. if (cooldown.contains(p)) {
    65. p.sendMessage(ChatColor.RED + "You cannot get a kit at this time.");
    66. return true;
    67. }
    68.  
    69. }
    70. }
    71. }
    72.  

    line 47 semicolon expected after the onPlayerMove and unexpected token on the parentheses after evt :/
     
  16. Offline

    Necrodoom

    drpk Thats because you are still trying to fit a method inside a method, which doesnt make sense and is impossible in java.
    Your mistakes show that you dont have much experience with java, you may want to read up on a basic java tutorial before proceeding, such as the oracle java tutorial.
     
  17. Offline

    Garris0n

    > Methods inside methods.
    > Questionable cooldown practices.
    > Player lists.
    > Unnecessary use of static.
    > Syntax errors.

    It's like the whole section wrapped up in one thread...
     
    Dragonphase likes this.
  18. Offline

    Necrodoom

    Garris0n All that is left is someone spoonfeeding incorrect code without explanation.
     
    Dragonphase and Garris0n like this.
  19. Offline

    Dragonphase

    Garris0n
    Show Spoiler
    Show Spoiler
    Show Spoiler
    Show Spoiler
    Show Spoiler
    Methodception
     
  20. Offline

    ChipDev

    DannyDog
     
  21. Offline

    DannyDog

    ChipDev
    Oh yea, didn't see that, but i think OP knows now.
     
Thread Status:
Not open for further replies.

Share This Page