Solved Targetting a Player

Discussion in 'Plugin Development' started by random_username, Oct 12, 2013.

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

    random_username

    Hello, I was trying to make commands to give people effects. This is part of my onCommand:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("effects")){
    2. if (Sender.hasPermission("ecore.effects")){
    3. if(args.length == 0){
    4. Sender.sendMessage("Done!");
    5. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 90));
    6. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1000000, 90));
    7. player.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 1000000, 90));
    8. player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 1000000, 10));
    9. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000000, 90));
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 1000000, 90));
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000000, 90));
    12. player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1000000, 90));
    13. player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000, 10));
    14. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 90));
    15. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 90));
    16. player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1000000, 90));
    17. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000, 90));
    18.  
    19. }if(args.length == 1){
    20. Player target = Bukkit.getPlayer(args[0]);
    21. if(target == null){
    22. Sender.sendMessage(getConfig().getString("prefix").replace("&", "§") + ChatColor.RED + "Player not found.");
    23. }else{
    24. target.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 90));
    25. target.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1000000, 90));
    26. target.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 1000000, 90));
    27. target.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 1000000, 10));
    28. target.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000000, 90));
    29. target.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 1000000, 90));
    30. target.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000000, 90));
    31. target.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1000000, 90));
    32. target.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000, 10));
    33. target.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 90));
    34. target.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 90));
    35. target.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1000000, 90));
    36. target.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000, 90));
    37. Sender.sendMessage(getConfig().getString("Prefix").replace("&", "§") + "done!");
    38. }
    39. }
    40. }else{
    41. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + getConfig().getString("noperm").replace("&", "§"));
    42. }
    43. }
    44. else if(cmd.getName().equalsIgnoreCase("noeffects")){
    45. if (Sender.hasPermission("ecore.noeffects")){
    46. if(args.length == 0){
    47. Sender.sendMessage("Done!");
    48. for (PotionEffect effect : player.getActivePotionEffects())
    49. player.removePotionEffect(effect.getType());
    50. }if(args.length == 1){
    51. Player target = Bukkit.getPlayer(args[0]);
    52. if(target == null){
    53. Sender.sendMessage(getConfig().getString("prefix").replace("&", "§") + ChatColor.RED + "Player not found.");
    54. }else{
    55. for (PotionEffect effect : target.getActivePotionEffects())
    56. target.removePotionEffect(effect.getType());
    57. Sender.sendMessage(getConfig().getString("Prefix").replace("&", "§") + ChatColor.BLUE + "effects taken from " + args[0]);
    58. }
    59. }
    60. }
    61. else{
    62. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + getConfig().getString("noperm").replace("&", "§"));
    63. }
    64.  
    65. }

    Everything is working fine when I target an online player, but if the target is an offline player, I get an error :
    Code:
    Caused by: java.lang.NullPointerException
            at me.Kait18.effects.effects.onCommand(EnstopiaCore.java:263)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    any idea on how to solve it? Thanks for the help :)
     
  2. Offline

    SuperOmegaCow

    random_username what is on line 263 in the class EnstopiaCore also have return false;
     
  3. Offline

    random_username

    line 263 is:
    Code:java
    1. Sender.sendMessage(getConfig().getString("prefix").replace("&", "§") + ChatColor.RED + "Player not found.");

    Line 22 on my post :) return false is at the end, this part is at the middle of my OnCommand
     
  4. Offline

    SuperOmegaCow

  5. Offline

    calebbfmv

    What are you replacing here?
    replace("&", "§") + ChatColor.RED + "Player not found.");
     
  6. Offline

    Loogeh

  7. Offline

    random_username

    In string prefix I am using color codes with &, I'm replacing & for §.

    Yes, it does :)

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

    Loogeh

  9. Offline

    random_username


    Code:
    #This is the Default Configuration.
     
    #Prefix for all messages:
    Prefix: '&b[Enstopia] '
     
    #Message if player does not have permission:
    noperm: '&3You do not have permission to do this.'
     
  10. Offline

    Loogeh

    random_username Probably because you're getting the string "prefix" in your code but in the config it's called "Prefix". Try making it all lower case in the config.
     
  11. Offline

    random_username

    This worked, thanks! :D
     
    calebbfmv likes this.
Thread Status:
Not open for further replies.

Share This Page