Solved Dead Code - Plugin

Discussion in 'Plugin Development' started by Venexor, Mar 12, 2014.

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

    Venexor

    Hi,

    I am coding a plugin to try new things I have learned within the Bukkit API.

    Please excuse the vulgarity of the plugin because I try to make the plugin seem funny to my friends and at the same time I test the new things I have learned.

    The error I receive in the code is "Dead Code" which means that a chunk of my code does not run so it's considered "unused".

    Here is the code:
    Code:java
    1. package me.dev.venexor;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.potion.PotionEffect;
    8. import org.bukkit.potion.PotionEffectType;
    9.  
    10. class Command implements CommandExecutor
    11. {
    12. public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args)
    13. {
    14. if (label.equalsIgnoreCase("mf") || label.equalsIgnoreCase("multifun"))
    15. {
    16. sender.sendMessage("§d§m--------------§8[§5 MultiFun §8]§d§m--------------");
    17. sender.sendMessage("§f- §b/mf §8: §aOpens up help menu!");
    18. sender.sendMessage("§f- §b/fuck §8: §aUse /fuck §f[§aname§f] §7| §f[§aall§f]");
    19. sender.sendMessage("§f- §b/blow §8: §aUse /blow §f[§aname§f] §7| §f[§aall§f]");
    20. sender.sendMessage("§f- §b/love §8: §aUse /love §f[§aname§f] §7| §f[§aall§f]");
    21. sender.sendMessage("§f- §b/anal §8: §aUse /anal §f[§aname§f] §7| §f[§aall§f]");
    22. sender.sendMessage("§d§m--------------§8[§5 MultiFun §8]§d§m--------------");
    23. }
    24.  
    25. else if(label.equalsIgnoreCase("fuck"))
    26. {
    27. Player p = (Player) sender;
    28. String player = p.getName();
    29. if(p.hasPermission("multifun.fuck"))
    30. {
    31. if(args.length == 1)
    32. {
    33. if(args[0].equalsIgnoreCase("all"))
    34. {
    35. p.sendMessage("§8[§5MultiFun§8]§d You have fucked everyone online!");
    36. for(Player online : Bukkit.getOnlinePlayers())
    37. {
    38. if(!online.getDisplayName().equalsIgnoreCase(player))
    39. {
    40. online.sendMessage("§8[§5MultiFun§8]§d You have been fucked by §a" + player + "§d!");
    41. online.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1));
    42. online.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 4));
    43. }
    44. }
    45. }
    46.  
    47. else
    48. {
    49. Player t = Bukkit.getPlayer(args[0]);
    50. String target = t.getName();
    51. if(t != null)
    52. {
    53. if(target.equalsIgnoreCase(player))
    54. {
    55. p.sendMessage("§8[§5MultiFun§8]§d You cannot fuck yourself!");
    56. }
    57. else
    58. {
    59. p.sendMessage("§8[§5MultiFun§8]§d You have fucked §a" + target + "§d!");
    60. t.sendMessage("§8[§5MultiFun§8]§d You have been fucked by §a" + player + "§d!");
    61. t.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1));
    62. t.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 4));
    63. }
    64.  
    65. }
    66. else
    67. {
    68. p.sendMessage("§8[§5MultiFun§8]§d You can only fuck players that are online!");
    69. }
    70.  
    71. }
    72.  
    73. }
    74.  
    75.  
    76. else
    77. {
    78. p.sendMessage("§8[§5MultiFun§8]§d Use /fuck [player] [all]");
    79. }
    80.  
    81. }
    82.  
    83. else
    84. {
    85. p.sendMessage("§8[§5MultiFun§8]§d You do not have permission to execute this command!");
    86. }
    87. }
    88. return false;
    89. }
    90. }
    91.  


    The dead code is:
    Code:java
    1. {
    2. p.sendMessage("§8[§5MultiFun§8]§d You can only fuck players that are online!");
    3. }


    Although, I don't know why this chunk is seen as dead code because it is supposed to display an error message saying that the player the person tried to "fuck" is offline so they cannot "fuck" them.

    I tested this plugin with the error message on the server and what I expected came out to be true - a Null Pointer Exception when I tried to do /fuck <name of offline player>.

    I hope you can help me find my error and I hope you guys can again excuse the vulgar language and concepts.
     
  2. Offline

    xa112

    Test if the player is null before getting their name. Also you might want to look at ChatColor. And the bukkit.getPlayer would be better as bukkit.getPlayerExact.
     
  3. Offline

    Garris0n

    1. It's unreachable because you set a string equal to t.getName() before doing the null check on t. This means that if t is null you'll get a NullPointerException before getting to the if statement, which in turn means the if statement will never return false. Either a NullPointerException will be thrown or t will not be null.
    2. You should really look in to using the ChatColor enum instead of raw color codes.

    It took me forever to actually figure out what the problem was because I kept having random outbursts of laughter while reading the code. Seriously, what the /fuck are you writing? ಠ_ಠ
     
    SGrayMe, bobacadodl and Voltex like this.
  4. Offline

    Venexor

    I used getPlayerExact before but I don't like it very much because it requires me to write the full name instead of a short name. And what is wrong with using § instead of ChatColor.<Color Name>? If that's what you're talking about?

    UPDATE: The plugin now works flawlessly!
    I give my thanks to Garris0n and xa112!
     
Thread Status:
Not open for further replies.

Share This Page