Easier / cleaner way to do this?

Discussion in 'Plugin Development' started by joeygallegos, May 18, 2014.

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

    joeygallegos

    Is there a way to write this code a little more efficient or neater or easier? Maybe using a case switch statement? If so, could you provide example code? Kinda not sure how to tackle this.
    Code:java
    1. public class BlockEvents implements Listener {
    2.  
    3. public void changeBlockColor(final Block b) {
    4. new BukkitRunnable() {
    5.  
    6. int i = 6;
    7.  
    8. @SuppressWarnings("deprecation")
    9. @Override
    10. public void run() {
    11. if (i == 6) {
    12. b.setType(Material.STAINED_CLAY);
    13. b.setData((byte) 14);
    14. } else if (i == 5) {
    15. b.setData((byte) 13);
    16. } else if (i == 4) {
    17. b.setData((byte) 12);
    18. } else if (i == 3) {
    19. b.setData((byte) 11);
    20. } else if (i == 2) {
    21. b.setData((byte) 10);
    22. } else if(i == 1) {
    23. b.setData((byte) 9);
    24. } else if (i == 0) {
    25.  
    26. // change block back
    27. b.setType(Material.QUARTZ_BLOCK);
    28.  
    29. // cancel event
    30. this.cancel();
    31. }
    32. i--;
    33. }
    34. }.runTaskTimer((Plugin) this, 0L, 20L);
    35.  
    36. }
    37. }
     
  2. Offline

    Glumpz

    joeygallegos If you are trying to change the color of Stained Clay you can always use DyeColor.
     
    Garris0n likes this.
  3. Offline

    joeygallegos

    Glumpz Thank you! But I meant all the "else if" statements
     
  4. Offline

    Glumpz

  5. Offline

    ZeusAllMighty11

    joeygallegos

    You can use a switch statement.

    Code:
     
    int awesome = 15;
     
    switch(awesome)
    {
        default:
            // this fires if criteria not met
            break;
        case 15:
            // this fires because the variable in question is equal to 15
            break;
        case 5:
            // if awesome was 5 it would fire, but nope..
            break;
    }
     
    [/CODE}
     
  6. Offline

    gkovalecyn

    Since the value you are setting is always i + 8, you could do:
    Code:java
    1. if (i > 0) {
    2. b.setType(Material.STAINED_CLAY);
    3. b.setData((byte) i + 8);
    4. } else {
    5. // change block back
    6. b.setType(Material.QUARTZ_BLOCK);
    7. }
    8.  
     
    ZeusAllMighty11 likes this.
  7. Offline

    Garris0n

    You shouldn't really use setData(), but whatever.
     
Thread Status:
Not open for further replies.

Share This Page