Help with subcommands

Discussion in 'Plugin Development' started by Aplenator, Sep 2, 2014.

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

    Aplenator

    I need help with subcommands,
    I am trying to achieve this: /al enable/disable <player>
    To enable/turn on, disable/turn off an effect for the specified player

    Here is what I have come up with so far, I can't figure it out.

    Player s = (Player) sender;
    Player t = (Bukkit.getServer().getPlayer(args[0]));
    // player as string
    // "t"
    String sname = s.getName();

    String tname = t.getName();

    if (cmd.getName().equalsIgnoreCase("al")) {
    if (s instanceof Player) {
    if (args.length == 0 || args.length < 2) {
    s.sendMessage("Incorrect usage");
    if (args[0].equalsIgnoreCase("enable")
    || args[0].equalsIgnoreCase("disable")) {
    if (args[0].equalsIgnoreCase("enable")
    && args.length == 2) {
    //code to affect player
    if (args[0].equalsIgnoreCase("disable)
    //code to affect player

    (sorry unable to use syntax)

    Thanks in advance.
     
  2. Offline

    Necrodoom

    First off, check if sender is player, args[0] exists, and that target isn't null before casting.
    Second, remove the duplicate enable/disable checks, these are useless since you check them both anyway.
    Third, save a collection of players with the setting enabled.
     
  3. Offline

    ChipDev

    if (args[0].equalsIgnoreCase("disable"), You forgot the last ".
     
    hintss likes this.
  4. Offline

    1Rogue

    How about /al toggle <player> ?
     
    Aplenator likes this.
  5. Offline

    Aplenator

    Necrodoom ChipDev
    Here is my original code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @Override
    3. public boolean onCommand(CommandSender sender, Command cmd, String label,
    4. String[] args) {
    5. Player s = (Player) sender;
    6. Player t = (Bukkit.getServer().getPlayer(args[0]));
    7. String sname = s.getName();
    8. String tname = t.getName();
    9. if (cmd.getName().equalsIgnoreCase("al")) {
    10. if (s instanceof Player) {
    11. if (args.length == 0 || args.length < 2) {
    12. s.sendMessage("Incorrect usage");
    13. if (args[0].equalsIgnoreCase("enable")
    14. || args[0].equalsIgnoreCase("disable")) {
    15. if (args[0].equalsIgnoreCase("enable")
    16. && args.length == 2) {
    17. s.sendMessage("Proper usage /al enable <player>");
    18. t.addPotionEffect(
    19. new PotionEffect(
    20. PotionEffectType.WATER_BREATHING,
    21. 100000, 1), true);
    22. t.addPotionEffect(new PotionEffect(
    23. PotionEffectType.NIGHT_VISION, 100000, 1),
    24. true);
    25. s.sendMessage("AquaticLife has been enabled for player "
    26. + tname);
    27. t.sendMessage(sname
    28. + " has enabled AquaticLife for player "
    29. + tname);
    30. if (args[0].equalsIgnoreCase("disable")
    31. && args.length == 2) {
    32. t.addPotionEffect(new PotionEffect(
    33. PotionEffectType.WATER_BREATHING,
    34. 100000, 1), true);
    35. t.addPotionEffect(new PotionEffect(
    36. PotionEffectType.NIGHT_VISION, 100000,
    37. 1), true);
    38. s.sendMessage("AquaticLife has been enabled for player "
    39. + tname);
    40. t.sendMessage(sname
    41. + " has enabled AquaticLife for player "
    42. + tname);
    43. }
    44. }
    45. }
    46. }
    47. }
    48. }
    49. return false;
    50. }
    51. }


    Here is my new code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @Override
    3. public boolean onCommand(CommandSender sender, Command cmd, String label,
    4. String[] args) {
    5. if (cmd.getName().equalsIgnoreCase("l")) {
    6. Player p = (Player) sender;
    7. if (p.hasPermission("pmc.ligtning"));
    8. p.sendMessage(ChatColor.AQUA + "[AquaticLife]" + ChatColor.RED
    9. + "Proper Use /al (enable/disable) <player>");
    10. } else {
    11. if (cmd.getName().equalsIgnoreCase("al")) {
    12. if (args.length == 1) {
    13. if (args[0].equalsIgnoreCase("enable"));
    14. Player p = (Player) sender;
    15. if (p.hasPermission("pmc.reload"));{
    16. Player t = (Bukkit.getServer().getPlayer(args[0]));
    17. String pname = p.getName();
    18. String tname = t.getName();
    19. if (t.hasPermission("al.effect.waterbreathing"));
    20. t.addPotionEffect(new PotionEffect(
    21. PotionEffectType.WATER_BREATHING, 100000, 1, true), true);
    22. if (t.hasPermission("al.effect.nighvsion"));
    23. t.addPotionEffect(new PotionEffect(
    24. PotionEffectType.NIGHT_VISION, 100000, 1, true), true);
    25. p.sendMessage("AquaticLife has been enabled for player " + tname);
    26. t.sendMessage(pname + " has enabled AquaticLife for player " + tname);
    27. }
    28. } else {
    29. if (args[0].equalsIgnoreCase("disable"));
    30. Player p = (Player) sender;
    31. if (p.hasPermission("al.command.disable"));{
    32. Player t = (Bukkit.getServer().getPlayer(args[0]));
    33. String pname = p.getName();
    34. String tname = t.getName();
    35. if (t.hasPermission("al.effect.waterbreathing"));
    36. t.addPotionEffect(new PotionEffect(
    37. PotionEffectType.WATER_BREATHING, 0, 0, true), true);
    38. if (t.hasPermission("al.effect.nighvsion"));
    39. t.addPotionEffect(new PotionEffect(
    40. PotionEffectType.NIGHT_VISION, 0, 0, true), true);
    41. p.sendMessage("AquaticLife has been disabled for player " + tname);
    42. t.sendMessage(pname + " has disabled AquaticLife for player " + tname);
    43. }
    44. }
    45. }
    46. }
    47. return false;
    48. }
    49. }

    I don't know how to check if the target is dull as I'm a newbie :eek:, it's probably the most obvious thing.
    I thought it would be easier using (Bukkit.getServer().getPlayer(args[0]) for an extra argument instead of adding a whole list of players
     
  6. Offline

    ChipDev

    Null*
    if(getServer().getPlayer(args[0] != null) {
    }
     
  7. Offline

    AoH_Ruthless

    Aplenator
    You didn't even read what 1Rogue Necrodoom or ChipDev said, indicate that you didn't understand what they are saying, or show any better code.

    In fact, your code is even worse.
    • You misspelled "al" in your command name.
    • You have semicolons following if-statements.
     
    Aplenator and ChipDev like this.
Thread Status:
Not open for further replies.

Share This Page