Need help with if/else

Discussion in 'Plugin Development' started by edragy, Jul 13, 2012.

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

    edragy

    I am using this code so that when a player types /wool it changes the colour of the wool to whatever colour they type as args[0], what I want to add is that when they change the colour of wool sucsessfully, it prints a message to their screen saying "You changed the colour of (amount of wool) to (colour changed to) from (colour the wool used to be)." Here is the code:
    Code:java
    1. else if (cmd.getName().equalsIgnoreCase("wool")) {
    2. String commandName = cmd.getName();
    3. Player user;
    4. if (sender instanceof Player)
    5. {
    6. user = (Player) sender;
    7. }else{
    8. return false;
    9. }
    10. if(user.getInventory().getItemInHand().getTypeId() == 35){
    11. if(args.length == 1){
    12. if(args[0].equalsIgnoreCase("white"))
    13. user.getInventory().getItemInHand().setDurability((short)0);
    14. else if(args[0].equalsIgnoreCase("orange"))
    15. user.getInventory().getItemInHand().setDurability((short)1);
    16. else if(args[0].equalsIgnoreCase("magenta"))
    17. user.getInventory().getItemInHand().setDurability((short)2);
    18. else if(args[0].equalsIgnoreCase("lightblue"))
    19. user.getInventory().getItemInHand().setDurability((short)3);
    20. else if(args[0].equalsIgnoreCase("yellow"))
    21. user.getInventory().getItemInHand().setDurability((short)4);
    22. else if(args[0].equalsIgnoreCase("lime"))
    23. user.getInventory().getItemInHand().setDurability((short)5);
    24. else if(args[0].equalsIgnoreCase("pink"))
    25. user.getInventory().getItemInHand().setDurability((short)6);
    26. else if(args[0].equalsIgnoreCase("grey"))
    27. user.getInventory().getItemInHand().setDurability((short)7);
    28. else if(args[0].equalsIgnoreCase("silver"))
    29. user.getInventory().getItemInHand().setDurability((short)8);
    30. else if(args[0].equalsIgnoreCase("cyan"))
    31. user.getInventory().getItemInHand().setDurability((short)9);
    32. else if(args[0].equalsIgnoreCase("purple"))
    33. user.getInventory().getItemInHand().setDurability((short)10);
    34. else if(args[0].equalsIgnoreCase("blue"))
    35. user.getInventory().getItemInHand().setDurability((short)11);
    36. else if(args[0].equalsIgnoreCase("brown"))
    37. user.getInventory().getItemInHand().setDurability((short)12);
    38. else if(args[0].equalsIgnoreCase("green"))
    39. user.getInventory().getItemInHand().setDurability((short)13);
    40. else if(args[0].equalsIgnoreCase("red"))
    41. user.getInventory().getItemInHand().setDurability((short)14);
    42. else if(args[0].equalsIgnoreCase("black"))
    43. user.getInventory().getItemInHand().setDurability((short)15);
    44. else
    45. setDamageByString(user, args[0]);
    46. }else{
    47. user.sendMessage(ChatColor.YELLOW + "Cloth Colors:" + ChatColor.GREEN + " white, orange, magenta, lightblue, yellow, lime, pink, grey, silver, cyan, purple, blue, brown, green, red, and black.");
    48. }
    49. return true;
    50. }else{
    51. user.sendMessage(ChatColor.RED + "Error: " + ChatColor.YELLOW + "You must have wool in your hand to use this command.");
    52. return true;
    53. }
    54. }
     
  2. Offline

    lololmaker

    I would do new private void, cast three arguments: player, color one, color two than form a message.
    Also with Java 1.7 I would use switch instead of that if statements.
     
  3. Offline

    edragy

    Can I have some code for that, sorry I am learning :D
     
  4. Offline

    Waterflames

  5. Offline

    lololmaker

    It would be something like this:
    Code:
    String StringToCompare = "Random String";
    String StringAsVariable = "It can also be variable";
     
    switch(StringToCompare){
    case "string one":
        //Your code
    case "string two":
        //Your code
    case StringAsVariable:
        //Your code
    Default:
        //Code if none matches
    }
    You can do it both ways, but for onCommand, it doesn't really matter, but standart is with if and else if statements. Just informational.
     
  6. This is not recommend, this only works whit java 7, most of the server s are not uptodate, they use java6 still, so your plugin is not able to run on most servers you can buy
     
  7. Offline

    lololmaker

    I know, but if you code for specific server which supports Java 7, this can save you a lot of time.
     
  8. Offline

    Cowboys1919

    To me, if (var.equalsIgnoreCase("val")) is no harder than a switch statement, so i just always do that with compatibility in mind.
     
  9. Offline

    lololmaker

    Well, it depends how are you used to do it ;)
    I use it if I have method which checks for permission, each integer represents a permission.
     
Thread Status:
Not open for further replies.

Share This Page