When typing in command, keep on getting the same command in chat

Discussion in 'Plugin Development' started by ChumChum, Jan 29, 2012.

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

    ChumChum

    main class
    Show Spoiler

    Code:java
    1.  
    2. package me.ChumChumx.Cuicide;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.command.ConsoleCommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Cuicide extends JavaPlugin {
    10.  
    11. @Override
    12. public void onDisable() {
    13. System.out.println("[Cuicide] Has been Disabled!");
    14.  
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. System.out.println("[Cuicide] Has Been Enabled!");
    20. }
    21.  
    22. public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    23. String commandLabel, String[] args) {
    24. Player player = ((Player)sender);
    25. if (cmd.getName().equalsIgnoreCase("die")) {
    26. if(player.hasPermission("Cuicide.die")){
    27. player.setHealth(0);
    28. this.getServer().broadcastMessage("PlayerName just did Suicide!");
    29. return true;
    30. } else {
    31. player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    32. }
    33. }
    34. return false;
    35. } }
    36.  
    37.  
    38.  
    39.  


    The plugin.yml
    Show Spoiler

    Code:
    name: Cuicide
    main: me.ChumChumx.Cuicide.Cuicide
    version: 0.1
    commands:
        die:
          description: does suicide
          aliases: d
          usage: /<command>
    

    [​IMG]
     
  2. Offline

    Slayer9x9

    Your command handling method seems to look funny.
    Here is how I would handle commands in your case:
    Code:
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
    {
         // stuff...
         //
        
         if (command.getName().equals("die"))
         {
              // more stuff...
              //
         }
         else
         {
              // even more stuff...
              //
         }
    }
    
     
  3. Offline

    psanker

    What Slayer said and be sure nesting of code is proper. I can't really tell because of your strange formatting, but you may be returning false even if the player has permission to use the command. Try this:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args){
        // Check if sender is player...
        Player p = (Player) sender;
     
        if (command.getName().equals("die") && p.hasPermission("Cuicide.die")) {
              // Execute code
              return true;
        } else {
              return false;
        }
    }
     
  4. Offline

    Milkywayz

    This is your entire code, copy and paste it.
    Code:
    package me.ChumChumx.Cuicide;
     
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Cuicide extends JavaPlugin {
     
    @Override
    public void onDisable() {
    System.out.println("[Cuicide] Has been Disabled!");
     
    }
     
    @Override
    public void onEnable() {
    System.out.println("[Cuicide] Has Been Enabled!");
    }
    public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    String commandLabel, String[] args) {
    Player player = ((Player)sender);
    String PlayerName  = sender.getName();
    if (cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die")) {
    player.setHealth(0);
    this.getServer().broadcastMessage(PlayerName + "just did Suicide!");
    return true;
    } else {
    player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    }
    return false;
    } }
    
    BTW i only helped because I wanted help like this before...
    Basically what the plugin you made and i fixed does, is that it checks if the player typed /die and it also checked if it had permission. && means and, its kinda like redstone logic gates... If 1 is true and 1 is true then the statement is true, if the player typed /die and he has permissions then set his health to 0. If the player type /die and doesn't have permission then display else (why you try and do suicide). || aka OR, if the player typed /die or has permission, as long as one of those is true then the whole statement is true, If you use OR in the specific statement you used, you could simply remove permission support and it'd let everyone kill themselves. But this is was a good lesson for permissions for you.
     
  5. Offline

    ChumChum

    Im just a beginner , i just learned all this stuff a couple of days ago, this is my first plugin :p
    Thanks for the help

    It gives me an error for PlayerName
    Show Spoiler

    Code:
    DescriptionResourcePathLocationType
    The value of the local variable PlayerName is not usedCuicide.java/Cuicide/src/me/ChumChumx/Cuicideline 24Java Problem
    


    This is the main class right now.
    Show Spoiler

    Code:java
    1.  
    2. package me.ChumChumx.Cuicide;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.command.ConsoleCommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Cuicide extends JavaPlugin {
    10.  
    11. @Override
    12. public void onDisable() {
    13. System.out.println("[Cuicide] Has been Disabled!");
    14.  
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. System.out.println("[Cuicide] Has Been Enabled!");
    20. }
    21.  
    22. public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    23. String commandLabel, String[] args) {
    24. Player player = ((Player)sender);
    25. String PlayerName = sender.getName();
    26. if (cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die")) {
    27. player.setHealth(0);
    28. this.getServer().broadcastMessage("PlayerName just did Suicide!");
    29. return true;
    30. } else {
    31. player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    32. }
    33. return false;
    34. } }
    35.  



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

    Milkywayz

    Code:
    String PlayerName  = sender.getName(); [/CODE
     
    Add this under Player player = sender.getName();
     
    You wouldn't have ran into that error if you just copying it :p
     
  7. Offline

    ItsHarry

    Why does everyone use cmd.getName(); ?
    There's a dedicated parameter, String label, which does the same thing o-o
     
    ArcheCane likes this.
  8. Offline

    ChumChum

    I like to learn, by typing it :)

    Okay, i copied and pasted, this is what i get.
    Show Spoiler

    Code:
    DescriptionResourcePathLocationType
    The value of the local variable PlayerName is not usedCuicide.java/Cuicide/src/me/ChumChumx/Cuicideline 24Java Problem
    

    same thing...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  9. Offline

    Darkman2412

    It doesn't do the same thing. Example:
    Command b is an alias of a.
    When a player types /b, the label = b; cmd.getName() = a.
    When a player types /a, label = a; cmd.getName() = a.

    So: label represents the alias, cmd.getName(): the regular command
     
  10. Offline

    ChumChum

    I fixed it, fixed the plugin.yml and still, repeats the command in the chat!

    The main class:
    Show Spoiler

    Code:java
    1.  
    2. package me.ChumChumx.Cuicide;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.command.ConsoleCommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Cuicide extends JavaPlugin {
    10.  
    11. @Override
    12. public void onDisable() {
    13. System.out.println("[Cuicide] Has been Disabled!");
    14.  
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. System.out.println("[Cuicide] Has Been Enabled!");
    20. }
    21. public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    22. String commandLabel, String[] args) {
    23. Player player = ((Player)sender);
    24. String PlayerName = sender.getName();
    25. if (cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die")) {
    26. player.setHealth(0);
    27. this.getServer().broadcastMessage(PlayerName + "just did Suicide!");
    28. return true;
    29. } else {
    30. player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    31. }
    32. return false;
    33. } }
    34.  



    And the plugin.yml
    Show Spoiler

    Code:
    name: Cuicide
    main: me.ChumChumx.Cuicide.Cuicide
    version: 0.1
    commands:
        die:
          description: Allows you to do suicide
          permission: Cuicide.die
          usage: /<command>
          aliases: d
    
     
  11. Offline

    Milkywayz

    I compile in JavaSE-1.6... That may be the issue. Since I'm on mac It would be stupid to use Java-7.
     
  12. Offline

    ChumChum

    I use JavaSE-1.7
    EDIT: so use JaveSE-1.6
     
  13. Offline

    psanker

    Do NOT reference the name of the player from the CommandSender. Be sure to reference the player's name from the Player. That is your issue.

    Replace:
    Code:
    String playerName = sender.getName();
    With:
    Code:
    String playerName = player.getName();
    What you should have:
    Code:
    Player player = (Player) sender;
    String playerName  = player.getName(); // Use lowercase on instances of classes. Use uppercase on class names
    
     
  14. Offline

    ItsHarry

    Oh, okay :D
     
    Darkman2412 likes this.
  15. Offline

    ChumChum

    ugh. still same thing, show's it in the chat..

    main class... again.
    Show Spoiler

    Code:java
    1.  
    2. package me.ChumChumx.Cuicide;
    3.  
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.command.ConsoleCommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Cuicide extends JavaPlugin {
    10.  
    11. @Override
    12. public void onDisable() {
    13. System.out.println("[Cuicide] Has been Disabled!");
    14.  
    15. }
    16.  
    17. @Override
    18. public void onEnable() {
    19. System.out.println("[Cuicide] Has Been Enabled!");
    20. }
    21. public boolean onCommandEvent(ConsoleCommandSender sender, CommandSender cmd,
    22. String commandLabel, String[] args) {
    23. Player player = (Player) sender;
    24. String playerName = player.getName();
    25. if (cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die")) {
    26. player.setHealth(0);
    27. this.getServer().broadcastMessage(playerName + "just did Suicide!");
    28. return true;
    29. } else {
    30. player.sendMessage("WHY YOU TRY AND DO SUICIDE?");
    31. }
    32. return false;
    33. } }
    34.  


    The plugin.yml....
    Show Spoiler

    Code:
    name: Cuicide
    main: me.ChumChumx.Cuicide.Cuicide
    version: 0.1
    commands:
        die:
          description: Allows you to do suicide
          permission: Cuicide.die
          usage: /<command>
          aliases: d
    

    Maybe it's something with the plugin.yml or the main class, i cant find it.
    it dosent show any errors....
     
  16. Offline

    Slayer9x9

    It looks like at around this point:
    Code:
    if (cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die"))
    
    one of these conditionals are returning false.
    Output them using System.out.println() in your code and follow your server's output in the console.
     
  17. Offline

    ChumChum

    So put
    System.out.printIn()
    under
    if (cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die"))
    ?
     
  18. Offline

    Slayer9x9

    Not exactly. Something like "System.out.println(cmd.getName());" and "System.out.println(sender.hasPermission("Cuicide.die");" right abouve your conditionals.
    That way you can see exactly what they are, and troubleshoot from there.

    Just curious, how much programming experience do you have?
     
  19. Offline

    ChumChum

    A little bit, this is my first plugin :p
    So its supposed to be like this?
    Show Spoiler

    Code:
    System.out.println(sender.hasPermission("Cuicide.die");
    cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die");
    
     
  20. Offline

    Slayer9x9

    No, more like this:
    Code:
    System.out.println(cmd.getName());
    System.out.println(sender.hasPermission("Cuicide.die");
    cmd.getName().equalsIgnoreCase("die") && sender.hasPermission("Cuicide.die");
    
    The whole point of "System.out.println()" is to output to your console some given value.
    The reason why I recommended you use this is so you can debug and see exactly where your program flows.
    "System.out.println()" will not do anything to your code, so simply adding these lines won't change anything.
    Just use this technique to analyze where your program goes and then you can find the underlining problem.

    Not to be mean or anything, but if you don't know the very basics like "System.out.println()", I suggest you really learn Java before attempting to code any plugins all by yourself.
     
Thread Status:
Not open for further replies.

Share This Page