Solved switch statement

Discussion in 'Plugin Development' started by Jace_oio, Aug 1, 2013.

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

    Jace_oio

    Hi,
    How would I be able to get the ChatColor of a string in a switch statement
    Example:

    Code:
                String string = ChatColor.GREEN + "hi";
                    switch(string) {
                    case "hi":
                        Bukkit.broadcastMessage("hi");
                        break;
                    }
    How would I be able to set case hi green within the switch statement ?
    Example:
    case ChatColor.GREEN + "hi":
    Somthing like that XD
     
  2. Offline

    Rocoty

    It's not possible. Use normal if-elseif statements
     
  3. Offline

    lDucks

    There are a few issues, the string isn't "hi" because you're adding a color to it, changing the content of the string.

    Also, you can't use String's as a switch variable unless you're using Java SE 7:

    So, I don't know what you mean by get the color, but a switch case with a String would look like this:

    Code:
                String string = "hi";
                    switch(string) {
                    case "hi":
                        Bukkit.broadcastMessage(ChatColor.GREEN + "hi");
                        break;
                    }
     
  4. Offline

    callum2904

    im not sure but wouldnt you have it like this
    Code:java
    1. String string = args[0];
    2. switch(string) {
    3. case "hi":
    4. Bukkit.broadcastMessage(ChatColor.GREEN + "hi");
    5. break;
    6. case "something else":
    7. Bukkit.broadcastMessage(ChatColor.RED + "something else");
    8. break;
    9. default:
    10. break;
    11. }


    this way you could have the string equal to the arguments of a command and if it the args is equal to one of the statements then it will do what ever is in it and the case default: is there so that if it doesnt equal any of the others it wont carry on checking
     
  5. Offline

    tommycake50

    Could this work?
    Code:java
    1.  
    2. String string = ChatColor.GREEN + "hi";
    3. switch(string) {
    4. case "§ahi":
    5. Bukkit.broadcastMessage("hi");
    6. break;
    7. }
    8.  
     
    Rocoty likes this.
  6. Offline

    Rocoty

    I think what the OP is doing is this:

    The string variable contains ChatColors, and therefore they would like to check to see whether the string is equal to another string with the same ChatColors.

    I can see three solutions:
    1. Do as I mentioned above.
    2. switch (ChatColor.stripColor(string)) { (may not be desirable in certain situations)
    3. Use the ChatColor literals (Example: "§3hi")
     
    tommycake50 likes this.
  7. Offline

    Jace_oio

  8. Offline

    tommycake50

Thread Status:
Not open for further replies.

Share This Page