Solved Plugin gets enabled, just doesn't perform the commands

Discussion in 'Plugin Development' started by mattmcmullen1, Jul 25, 2013.

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

    mattmcmullen1

    So, in this plugin, when the player does /swiftness it applies speed and sends them a message, so on and so forth. But when I do /swiftness, nothing shows up. Not even "Unknown command" or "You do not have permission". Just nothing at all. No errors in the console either.
    Heres my code:
    Code:java
    1. package me.mattmcmullen1.quickbrew;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.potion.PotionEffect;
    13. import org.bukkit.potion.PotionEffectType;
    14.  
    15. public class quickbrew extends JavaPlugin implements Listener{
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17. public static quickbrew plugin;
    18.  
    19. public void onDisable(){
    20. PluginDescriptionFile pdfFile = this.getDescription();
    21. this.logger.info(pdfFile.getName() + " has been disabled");
    22. }
    23.  
    24. public void onEnable(){
    25. PluginDescriptionFile pdfFile = this.getDescription();
    26. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been enabled");
    27. getConfig().options().copyDefaults(true);
    28. saveConfig();
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args){
    32. Player player = (Player) sender;
    33. if(command.equalsIgnoreCase("swiftness")){
    34. if(args.length == 0){
    35. player.sendMessage(ChatColor.BLUE + "Swiftness has been applied");
    36. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1200, 1));
    37. }
    38. if(command.equalsIgnoreCase("fireresistance")){
    39. if(args.length == 0){
    40. player.sendMessage(ChatColor.BLUE + "Fire Resistance has been applied");
    41. player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1200, 1));
    42. }
    43. if(command.equalsIgnoreCase("invisibility")){
    44. if(args.length == 0){
    45. player.sendMessage(ChatColor.BLUE + "Invisibility has been applied");
    46. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1200, 1));
    47. }
    48. }
    49. }
    50. }
    51. return false;
    52. }
    53. }


    I'm still new to this. I've googled anything I could, and still nothing. Thanks for the help!
     
  2. Offline

    Tarestudio

    mattmcmullen1
    Show your plugin.yml, pls. you have to register the commands there.
     
  3. Offline

    mattmcmullen1

    http://pastebin.com/63WgRdUk
    Sorry, should've known to do that!

    Tarestudio I feel like it's going to be a silly error. It's three where I'm at, and I'm tired, but want to finish this

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

    Tarestudio

    mattmcmullen1
    1. You should check if sender is really a player
    2. return true, if a command did the job
    3. before your 'return false' send the player a message as test, command and args.length could be usefull. You have no usage defined for commands, so this cant be printed on false.
     
  5. Offline

    mattmcmullen1

    I'm sorry... I'm new at this, can you explain a little better please. Sorry :3

    Tarestudio also, I don't know why I was registering a listener when I don't have an event to listen for, so I deleted that

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

    Tarestudio

    mattmcmullen1
    -.- I found the problem, you nested the if-conditions... Try this
    Code:java
    1. - snip -
    2. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args){
    3. if (sender instanceof Player) {
    4. Player player = (Player) sender;
    5. if(command.equalsIgnoreCase("swiftness")){
    6. if(args.length == 0){
    7. player.sendMessage(ChatColor.BLUE + "Swiftness has been applied");
    8. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1200, 1));
    9. return true;
    10. }
    11. } else if(command.equalsIgnoreCase("fireresistance")){
    12. if(args.length == 0){
    13. player.sendMessage(ChatColor.BLUE + "Fire Resistance has been applied");
    14. player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1200, 1));
    15. return true;
    16. }
    17. } else if(command.equalsIgnoreCase("invisibility")){
    18. if(args.length == 0){
    19. player.sendMessage(ChatColor.BLUE + "Invisibility has been applied");
    20. player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1200, 1));
    21. return true;
    22. }
    23. }
    24. }
    25. return false;
    26. }
    27. - snip -
     
  7. Offline

    mattmcmullen1

    Ohh, so if I need, as I thought a new if, I need to use else?

    EDIT: That worked! Thank you for helping me, sorry for taking your time. But, I really appreciate the help!
     
  8. Offline

    Tarestudio

    mattmcmullen1
    No, the 'else' is a little optimization, if the method found that command equals 'swiftness' it dont have to check it for been eqaul to 'fireresistance'.
    The problem was you wrote the condition like:
    Code:java
    1. if (command.equalsIgnoreCase("swiftness")){
    2. ...
    3. if(command.equalsIgnoreCase("fireresistance")){
    4. ...
    5. if(command.equalsIgnoreCase("invisibility")){
    6. ...
    7. }
    8. }
    9. }


    That means, only if commands equals 'swiftness' it tries to check if commands equals 'fireresistance' and only if this was true, it would check for 'invisibility'... this could never be the case.
     
  9. Offline

    mattmcmullen1

    Tarestudio Ohh. That's basically what I meant, but in my "noob" terms. Thanks for the help!
     
Thread Status:
Not open for further replies.

Share This Page