Solved "break labelxxx;" and "labelxxx:" Help.

Discussion in 'Plugin Development' started by chikenlitle99, Apr 25, 2014.

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

    chikenlitle99

    I have problems with "break label;"
    In "break labelxxx;" I get an error that says "undefinided label: labelxxx"
    and in "labelxxx:" "unreachable statement".

    How I can fix it?

    Here's the code, it is long
    Code:java
    1. public Color getColor(String c){
    2. String str;
    3. switch ((str = c.toUpperCase()).hashCode()){
    4. case 1:
    5. if (str.equals("MAROON")) {
    6. break label402;
    7. }
    8. break;
    9. case 2:
    10. if (str.equals("ORANGE")) {
    11. break label414;
    12. }
    13. break;
    14. case 3:
    15. if (str.equals("PURPLE")) {
    16. break label418;
    17. }
    18. break;
    19. case 4:
    20. if (str.equals("SILVER")) {
    21. break label426;
    22. }
    23. break;
    24. case 5:
    25. if (str.equals("YELLOW")) {
    26. break label438;
    27. }
    28. break;
    29. case 6:
    30. if (str.equals("RED")) {
    31. break label422;
    32. }
    33. case 7:
    34. if ((goto 374) || (str.equals("AQUA"))) {
    35. break;
    36. }
    37. break;
    38. case 8:
    39. if (str.equals("BLUE")) {
    40. break label382;
    41. }
    42. break;
    43. case 9:
    44. if (str.equals("GRAY")) {
    45. break label390;
    46. }
    47. break;
    48. case 10:
    49. if (str.equals("LIME")) {
    50. break label398;
    51. }
    52. break;
    53. case 11:
    54. if (str.equals("NAVY")) {
    55. break label406;
    56. }
    57. break;
    58. case 12:
    59. if (str.equals("TEAL")) {
    60. break label430;
    61. }
    62. break;
    63. case 13:
    64. if (str.equals("BLACK")) {
    65. break label378;
    66. }
    67. break;
    68. case 14:
    69. if (str.equals("GREEN")) {
    70. break label394;
    71. }
    72. break;
    73. case 15:
    74. if (str.equals("OLIVE")) {
    75. break label410;
    76. }
    77. break;
    78. case 16:
    79. if (str.equals("WHITE")) {
    80. break label434;
    81. }
    82. break;
    83. case 17:
    84. if (str.equals("FUCHSIA")) {
    85. break label1386;
    86. }
    87. }
    88. return Color.AQUA;
    89. label378:
    90. return Color.BLACK;
    91. label382:
    92. return Color.BLUE;
    93. label386:
    94. return Color.FUCHSIA;
    95. label390:
    96. return Color.GRAY;
    97. label394:
    98. return Color.GREEN;
    99. label398:
    100. return Color.LIME;
    101. label402:
    102. return Color.MAROON;
    103. label406:
    104. return Color.NAVY;
    105. label410:
    106. return Color.OLIVE;
    107. label414:
    108. return Color.ORANGE;
    109. label418:
    110. return Color.PURPLE;
    111. label422:
    112. return Color.RED;
    113. label426:
    114. return Color.SILVER;
    115. label430:
    116. return Color.TEAL;
    117. label434:
    118. return Color.WHITE;
    119. label438:
    120. return Color.YELLOW;
    121. }
     
  2. chikenlitle99 I don't think labels do what you think they do.

    Just replace the break label parts with the correct return parts and also consider that there will be a Color.valueOf(String s) method
     
  3. Offline

    chikenlitle99

  4. Offline

    Wizehh

    Break is a Java keyword that, as the name suggests, stops a loop. Why.. why are you getting the hashcode of the String? That makes no sense at all.
    Here's a revised method for you:
    PHP:
    public Color getColor(String c){
    switch (
    c) {
        case 
    "MAROON"//note: this is case-sensitive
            
    return Color.MAROON//or whatever you want to return
        
    default: //if none of the cases have accounted for the string
            
    return null;
        }
    }
    Edit:
    Are you using Bukkit's color enum? Tell me if you are, because I can show you a cool trick.
     
  5. Offline

    chikenlitle99

    @Wizehh
    Teach the trick

    Yout method is like this?
    Code:java
    1. public Color getColor(String c){
    2. String str;
    3. switch ((str = c.toUpperCase()).hashCode()){
    4. case 1:
    5. if(str.equalsIgnoreCase("lime")){
    6. return Color.LIME;
    7. }
    8. case 2:
    9. if(str.equalsIgnoreCase("black")){
    10. return Color.BLACK;
    11. }
    12. case 3:
    13. if(str.equalsIgnoreCase("yellow")){
    14. return Color.YELLOW;
    15. }
    16. case 4:
    17. if(str.equalsIgnoreCase("orange")){
    18. return Color.ORANGE;
    19. }
    20. case 5:
    21. if(str.equalsIgnoreCase("navy")){
    22. return Color.NAVY;
    23. }
    24. case 6:
    25. if(str.equalsIgnoreCase("aqua")){
    26. return Color.AQUA;
    27. }
    28. case 7:
    29. if(str.equalsIgnoreCase("blue")){
    30. return Color.BLUE;
    31. }
    32. case 8:
    33. if(str.equalsIgnoreCase("red")){
    34. return Color.RED;
    35. }
    36. case 9:
    37. if(str.equalsIgnoreCase("fuchsia")){
    38. return Color.FUCHSIA;
    39. }
    40. case 10:
    41. if(str.equalsIgnoreCase("maroon")){
    42. return Color.MAROON;
    43. }
    44. case 11:
    45. if(str.equalsIgnoreCase("purple")){
    46. return Color.PURPLE;
    47. }
    48. case 12:
    49. if(str.equalsIgnoreCase("teal")){
    50. return Color.TEAL;
    51. }
    52. case 13:
    53. if(str.equalsIgnoreCase("white")){
    54. return Color.WHITE;
    55. }
    56. case 14:
    57. if(str.equalsIgnoreCase("gree")){
    58. return Color.GREEN;
    59. }
    60. case 15:
    61. if(str.equalsIgnoreCase("gray")){
    62. return Color.GRAY;
    63. }
    64. }
    65. return Color.SILVER;
    66. }


    NOTE: This is for a firework to put colors from the configuration file

    @AdamQpzm
    @Wizehh
    Now not give me error but the firework not have colour is all white

    The code:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(final PlayerJoinEvent pje) {
    3. Player p = pje.getPlayer();
    4. if(p.hasPermission("fa.join") && getConfig().getBoolean("Player_Join.Player_Join_Firework.Firework")){
    5. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    6.  
    7. public void run(){
    8.  
    9. Firework f = (Firework) pje.getPlayer().getWorld().spawn(pje.getPlayer().getLocation(), Firework.class);
    10.  
    11. boolean flicker = getConfig().getBoolean("Player_Join.Player_Join_Firework.Flicker");
    12. boolean trail = getConfig().getBoolean("Player_Join.Player_Join_Firework.Trail");
    13. FireworkEffect.Type fwType = FireworkEffect.Type.valueOf(getConfig().getString("Player_Join.Player_Join_Firework.Type"));
    14. Color Color1 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Color1"));
    15. Color Color2 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Color2"));
    16. Color Color3 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Color3"));
    17. Color Color4 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Color4"));
    18. Color Fade1 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Fade1"));
    19. Color Fade2 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Fade2"));
    20. Color Fade3 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Fade3"));
    21. Color Fade4 = getColor(getConfig().getString("Player_Join.Player_Join_Firework.Fade4"));
    22. int power = getConfig().getInt("Player_Join.Player_Join_Firework.Power");
    23.  
    24. FireworkMeta fm = f.getFireworkMeta();
    25.  
    26. fm.addEffect(FireworkEffect.builder()
    27. .flicker(flicker)
    28. .trail(trail)
    29. .with(fwType) //BALL_LARGE - BALL - BURST - CREEPER - STAR
    30. .withColor(Color1)
    31. .withColor(Color2)
    32. .withColor(Color3)
    33. .withColor(Color4)
    34. .withFade(Fade1)
    35. .withFade(Fade2)
    36. .withFade(Fade3)
    37. .withFade(Fade4)
    38. .build());
    39. fm.setPower(power); //(int) .6
    40. f.setFireworkMeta(fm);
    41.  
    42. }
    43. }, getConfig().getInt("Player_Join.Player_Join_Firework.Delay"));//5);
    44.  
    45. }
    46. }
    47. public Color getColor(String c){
    48. String str;
    49. switch ((str = c.toUpperCase()).hashCode()){
    50. case 1:
    51. if(str.equalsIgnoreCase("lime")){
    52. return Color.LIME;
    53. }
    54. case 2:
    55. if(str.equalsIgnoreCase("black")){
    56. return Color.BLACK;
    57. }
    58. case 3:
    59. if(str.equalsIgnoreCase("yellow")){
    60. return Color.YELLOW;
    61. }
    62. case 4:
    63. if(str.equalsIgnoreCase("orange")){
    64. return Color.ORANGE;
    65. }
    66. case 5:
    67. if(str.equalsIgnoreCase("navy")){
    68. return Color.NAVY;
    69. }
    70. case 6:
    71. if(str.equalsIgnoreCase("aqua")){
    72. return Color.AQUA;
    73. }
    74. case 7:
    75. if(str.equalsIgnoreCase("blue")){
    76. return Color.BLUE;
    77. }
    78. case 8:
    79. if(str.equalsIgnoreCase("red")){
    80. return Color.RED;
    81. }
    82. case 9:
    83. if(str.equalsIgnoreCase("fuchsia")){
    84. return Color.FUCHSIA;
    85. }
    86. case 10:
    87. if(str.equalsIgnoreCase("maroon")){
    88. return Color.MAROON;
    89. }
    90. case 11:
    91. if(str.equalsIgnoreCase("purple")){
    92. return Color.PURPLE;
    93. }
    94. case 12:
    95. if(str.equalsIgnoreCase("teal")){
    96. return Color.TEAL;
    97. }
    98. case 13:
    99. if(str.equalsIgnoreCase("white")){
    100. return Color.WHITE;
    101. }
    102. case 14:
    103. if(str.equalsIgnoreCase("gree")){
    104. return Color.GREEN;
    105. }
    106. case 15:
    107. if(str.equalsIgnoreCase("gray")){
    108. return Color.GRAY;
    109. }
    110. }
    111. return Color.SILVER;
    112. }
    113.  


    The config File:
    Code:java
    1. Player_Join:
    2. Player_Join_Firework:
    3. Firework: true
    4. Type: BALL_LARGE
    5. Flicker: true
    6. Trail: true
    7. Delay: 10
    8. Power: .6
    9. Color1: GOLD
    10. Color2: WHITE
    11. Color3: YELLOW
    12. Color4: BLUE
    13. Fade1: BLUE
    14. Fade2: GREEN
    15. Fade3: RED
    16. Fade4: YELLOW


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

    Wizehh

    WHAT
    This is my method:
    PHP:
    public Color getColor(String paramString) {
        return 
    Color.valueOf(paramString.toUpperCase());
        return 
    null;
    }
     
  7. Offline

    Garris0n

    I think you need to read up on what switch statements do. And what labels do. And possibly what Java does.
     
    bobacadodl and xTigerRebornx like this.
  8. Offline

    chikenlitle99

    @Wizehh
    Use your method and gave me error
    how do I use?
     
  9. Offline

    Wizehh

    WHAT
     
  10. Offline

    chikenlitle99


    [​IMG]
     
  11. Offline

    Wizehh

    What Color class are you using? Is it an enum?
     
  12. Offline

    chikenlitle99

    @Wizehh


    This?
    Code:java
    1. public Color getColor(String c){
    2. String str;
    3. switch ((str = c.toUpperCase()).hashCode()){
    4. case 1:
    5. if(str.equalsIgnoreCase("lime")){
    6. return Color.LIME;
    7. }
    8. case 2:
    9. if(str.equalsIgnoreCase("black")){
    10. return Color.BLACK;
    11. }
    12. case 3:
    13. if(str.equalsIgnoreCase("yellow")){
    14. return Color.YELLOW;
    15. }
    16. case 4:
    17. if(str.equalsIgnoreCase("orange")){
    18. return Color.ORANGE;
    19. }
    20. case 5:
    21. if(str.equalsIgnoreCase("navy")){
    22. return Color.NAVY;
    23. }
    24. case 6:
    25. if(str.equalsIgnoreCase("aqua")){
    26. return Color.AQUA;
    27. }
    28. case 7:
    29. if(str.equalsIgnoreCase("blue")){
    30. return Color.BLUE;
    31. }
    32. case 8:
    33. if(str.equalsIgnoreCase("red")){
    34. return Color.RED;
    35. }
    36. case 9:
    37. if(str.equalsIgnoreCase("fuchsia")){
    38. return Color.FUCHSIA;
    39. }
    40. case 10:
    41. if(str.equalsIgnoreCase("maroon")){
    42. return Color.MAROON;
    43. }
    44. case 11:
    45. if(str.equalsIgnoreCase("purple")){
    46. return Color.PURPLE;
    47. }
    48. case 12:
    49. if(str.equalsIgnoreCase("teal")){
    50. return Color.TEAL;
    51. }
    52. case 13:
    53. if(str.equalsIgnoreCase("white")){
    54. return Color.WHITE;
    55. }
    56. case 14:
    57. if(str.equalsIgnoreCase("gree")){
    58. return Color.GREEN;
    59. }
    60. case 15:
    61. if(str.equalsIgnoreCase("gray")){
    62. return Color.GRAY;
    63. }
    64. }
    65. return Color.SILVER;
    66. }


    And this?
    Code:java
    1. Color Color4 = getColor(getConfig().getString("First_Player_Join.Player_First_Join_Firework.Color4"));
     
  13. Offline

    Garris0n

    http://jd.bukkit.org/rb/doxygen/d8/d67/classorg_1_1bukkit_1_1Color.html

    It's not an enum, it represents RGB colors. It has a bunch of built-in constants so some might assume it's an enum.

    @OP Where on earth did you get that code? None of it makes any sense. As I said,
     
  14. Offline

    chikenlitle99

    @Garris0n
    @Wizehh
    All I want is to get a color from config file to use in a firework, yes? || Todo lo que quiero es conseguir un color en el archivo de configuración para utilizarlo en los fuegos artificiales, ¿si?

    This is my config file:

    Code:java
    1. Player_Join:
    2. Player_Join_Firework:
    3. Firework: true
    4. Type: BALL_LARGE
    5. Flicker: true
    6. Trail: true
    7. Delay: 10
    8. Power: .6
    9. Color1: GOLD
    10. Color2: WHITE
    11. Color3: YELLOW
    12. Color4: BLUE
    13. Fade1: BLUE
    14. Fade2: GREEN
    15. Fade3: RED
    16. Fade4: YELLOW


    This is the old method that I used but does not work:
    Code:java
    1. Color Color1 = getConfig().getColor("First_Player_Join.Player_First_Join_Firework.Color1");


    This is the method that I used, but the colors do not work:
    Code:java
    1. Color Color1 = getColor(getConfig().getString("First_Player_Join.Player_First_Join_Firework.Color1"));
     
  15. Offline

    BillyGalbreath

    I posted in your other thread how to get the Color from the config...
     
  16. Offline

    chikenlitle99

    @BillyGalbreath
    Code:java
    1. Color Color1 = Color.fromRGB (getConfig().getString(" First_Player_Join.Player_First_Join_Firework.Color1"));

    not working
     
  17. Offline

    Wizehh

    Here:
    PHP:
    public Color getColor(String paramString) {
        
    String temp paramString;
        if (
    temp.equalsIgnoreCase("lime") return Color.LIME;
        if (
    temp.equalsIgnoreCase("red") return Color.RED;
        
    //etcetera
        
    return null;
    }
    But in all honesty, I think you should take a more than cursory glance at the Java tutorials, because you should at least know the basics of the language. I mean no offense, but it's annoying when these type of questions are asked.
     
  18. Offline

    BillyGalbreath

    I said some like that. I didnt test it, but I posted it to get your mind away from all these switches and to understand the sifference between a Color and a String. With that knowledge you should be able to change your question to something that is easier to answer. So easy that you should be able to answer it yourself woth a little searching on google.
     
  19. Offline

    chikenlitle99

    @BillyGalbreath
    @Wizehh
    Thanks for all and sorry , I'm a little new in java only I have 3 months in JAVA and Bukkit, I still have much to learn

    @Wizehh
    Your method works, thanks for everything, I need to improve my English a lot to make better questions XD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  20. Offline

    RawCode

    break may be used as goto statement inside switch, but this is not expected usage of break.
     
Thread Status:
Not open for further replies.

Share This Page