Solved Multiple commands.

Discussion in 'Plugin Development' started by RandomGuy137, May 31, 2014.

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

    RandomGuy137

    Hi guys, new developer here, Basically, I want 2 commands,
    but, the second command in my code just doesn't do anything, no message ingame, no console error.

    Code:
    Code:java
    1. package me.RandomGuy.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.potion.PotionEffect;
    10. import org.bukkit.potion.PotionEffectType;
    11.  
    12. public class main extends JavaPlugin implements Listener {
    13.  
    14. public void onEnable() {
    15. getServer().getPluginManager().registerEvents(this, this);
    16. }
    17.  
    18. public boolean onCommand(CommandSender sender, Command cmd, String label,
    19. String[] a) {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("confuseme")) {
    22. if (player.hasPermission("commands.confuseme")) {
    23. player.sendMessage(ChatColor.GREEN
    24. + "Don't get too sick! /confusemestop to end this madness");
    25. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
    26. 600, 2));
    27. player.addPotionEffect(new PotionEffect(
    28. PotionEffectType.BLINDNESS, 600, 1));
    29. player.addPotionEffect(new PotionEffect(
    30. PotionEffectType.CONFUSION, 600, 2));
    31. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP,
    32. 600, 2));
    33. } else {
    34. player.sendMessage(ChatColor.DARK_PURPLE + "Commands"
    35. + ChatColor.GREEN + ">" + ChatColor.RED
    36. + "You need rank" + ChatColor.DARK_AQUA + "Helper"
    37. + ChatColor.RED + "to use this command!");
    38. }
    39. if (cmd.getName().equalsIgnoreCase("confusemestop")) {
    40. if (player.hasPermission("Commands.confuseme")) {
    41. player.getActivePotionEffects().clear();
    42. player.sendMessage(ChatColor.RED + "Stopped madness");
    43. } else {
    44. player.sendMessage(ChatColor.DARK_PURPLE + "Commands"
    45. + ChatColor.GREEN + ">" + ChatColor.RED
    46. + "You need rank" + ChatColor.DARK_AQUA + "Helper"
    47. + ChatColor.RED + "to use this command!");
    48. }
    49. }
    50. }
    51. return false;
    52. }}
    53.  


    How would I go about this?
     
  2. Offline

    fireblast709

    Your second command is not nested properly (it is nested in the first command) RandomGuy137
     
  3. Offline

    RandomGuy137

    Hm, How would i go about fixing this? Do I need another:
    1. public boolean onCommand(CommandSender sender, Command cmd, String label,
    2. String[] a) {
     
  4. Offline

    Xyplo

    Is It? I see three '}' like there should be... (Near return false)
    EDIT: Ohh I see, you're correct about that. Sorry.


    RandomGuy137 You have put the commands into the plugin.yml correct...?
     
  5. Offline

    RandomGuy137

    Yes
     
  6. Offline

    fireblast709

    Xyplo After a bit of cleanup:
    Code:java
    1. package me.RandomGuy.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.potion.PotionEffect;
    10. import org.bukkit.potion.PotionEffectType;
    11.  
    12. public class main extends JavaPlugin implements Listener
    13. {
    14.  
    15. public void onEnable()
    16. {
    17. getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. public boolean onCommand(CommandSender sender, Command cmd, String label,String[] a)
    21. {
    22. Player player = (Player) sender;
    23. if (cmd.getName().equalsIgnoreCase("confuseme"))
    24. {
    25. if (player.hasPermission("commands.confuseme"))
    26. {
    27. player.sendMessage(ChatColor.GREEN
    28. + "Don't get too sick! /confusemestop to end this madness");
    29. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
    30. 600, 2));
    31. player.addPotionEffect(new PotionEffect(
    32. PotionEffectType.BLINDNESS, 600, 1));
    33. player.addPotionEffect(new PotionEffect(
    34. PotionEffectType.CONFUSION, 600, 2));
    35. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP,
    36. 600, 2));
    37. }
    38. else
    39. {
    40. player.sendMessage(ChatColor.DARK_PURPLE + "Commands"
    41. + ChatColor.GREEN + ">" + ChatColor.RED
    42. + "You need rank" + ChatColor.DARK_AQUA + "Helper"
    43. + ChatColor.RED + "to use this command!");
    44. }
    45. if (cmd.getName().equalsIgnoreCase("confusemestop"))
    46. {
    47. if (player.hasPermission("Commands.confuseme"))
    48. {
    49. player.getActivePotionEffects().clear();
    50. player.sendMessage(ChatColor.RED + "Stopped madness");
    51. }
    52. else
    53. {
    54. player.sendMessage(ChatColor.DARK_PURPLE + "Commands"
    55. + ChatColor.GREEN + ">" + ChatColor.RED
    56. + "You need rank" + ChatColor.DARK_AQUA + "Helper"
    57. + ChatColor.RED + "to use this command!");
    58. }
    59. }
    60. }
    61. return false;
    62. }
    63. }
    You now see that
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("confusemestop"))
    is inside
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("confuseme"))
    2. {
    3. ...
    4. }
     
  7. Offline

    RandomGuy137

    OMG THANKS! :D
     
  8. Offline

    Xyplo

    Solved?

    Put the thread as solved.
     
  9. Offline

    RandomGuy137

    Wait,, fireblast709 it's the same results as before..

    yup, definitely wont work.

    timtower do you know?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  10. Offline

    fireblast709

  11. Offline

    RandomGuy137

    I used what you sent :)

    Oh.. fireblast709

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 1, 2016
  12. Offline

    fireblast709

    RandomGuy137 I didn't fix any of that, I merely pointed at the mistakes :p
     
  13. Offline

    RandomGuy137

    Oh, my bad. Could you? fireblast709
    I'm unsure, Kinda.
     
  14. Offline

    toCoda

    One alternative way of doing this is to just have the /confuseme command, then when someone runs it, add them to a list of players (or player names if you want to be more efficient) currently being confused. If a player uses the command and is already in the set of people currently being confused, run the /confusemestop code instead. E.g.

    Code:java
    1. private HashSet<String> confused;
    2.  
    3. public void onEnable() {
    4. // Do stuff
    5. confused = new HashSet<String>();
    6. }
    7.  
    8. public boolean onCommand(...) {
    9. if (cmd.getName().equalsIgnoreCase("confuseme") {
    10. if (confused.contains(sender.getName()) {
    11. confused.remove(sender.getName());
    12. // confusemestop code
    13. else {
    14. confused.add(sender.getName());
    15. // confuseme code
    16. }
    17. }
    18. }
     
  15. Offline

    Necrodoom

    If you don't know why the code still doesn't work despite fireblast709 telling you its caused by your second command check being inside your first one, I suggest reading a Java tutorial about how if statements work.
     
  16. Offline

    RandomGuy137

    I'm experienced in java actually sir, What I didn't understand is how the commands worked.
    It's solved now anyway, I just re-did it all.
     
  17. regardless of knowing how commands work, normal and nested if statements and how they operate do not change :p
     
Thread Status:
Not open for further replies.

Share This Page