Solved Rainbow name?

Discussion in 'Plugin Development' started by BeefySticks, Oct 15, 2014.

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

    BeefySticks

    I was wondering, how would I go about making someones name rainbow. Would I get each letter and color then set there name?
     
  2. Offline

    MeRPG

    You should use player.setDisplayName(colorfulname);
     
  3. Offline

    BeefySticks

    Thanks for trying to help but..... How would you define colorfulname?
     
  4. Offline

    MeRPG

    It is a string. I already coded what you asked for. Your choice now; I can show you pieces to help you do it yourself, or if you learn by seeing code I can just give it to you.


    Below is the code... I'll help you get there yourself if you want, otherwise just read this and let me know. It should work.
    Show Spoiler

    Code:java
    1.  
    2. public String colorfulize(String in) {
    3. ArrayList<String> chars = new ArrayList<String>(Arrays.asList(in.split("(?!^)")));
    4. ArrayList<ChatColor> colors = new ArrayList<ChatColor>();
    5. colors.add(ChatColor.AQUA);
    6. colors.add(ChatColor.BLACK);
    7. colors.add(ChatColor.BLUE);
    8. colors.add(ChatColor.DARK_AQUA);
    9. colors.add(ChatColor.DARK_BLUE);
    10. colors.add(ChatColor.DARK_GRAY);
    11. colors.add(ChatColor.DARK_GREEN);
    12. colors.add(ChatColor.DARK_PURPLE);
    13. colors.add(ChatColor.DARK_RED);
    14. colors.add(ChatColor.GOLD);
    15. colors.add(ChatColor.GRAY);
    16. colors.add(ChatColor.GREEN);
    17. colors.add(ChatColor.LIGHT_PURPLE);
    18. colors.add(ChatColor.RED);
    19. int c = 0;
    20. String out = "";
    21. for(String s : chars) {
    22. out+=colors.get(c)+s;
    23. c++;
    24. if(c>=colors.size()) {
    25. c=0;
    26. Collections.shuffle(colors);
    27. }
    28. }
    29. return out;
    30. }
    31.  

    Implemented:
    Code:java
    1.  
    2. player.setDisplayName(colorfulize(player.getName));
    3.  


    Hope you learn something! If you don't understand something I did, don't be afraid to ask.


    Ok tested. It works fine.
     
    BeefySticks likes this.
  5. Offline

    BeefySticks

    Thanks your awesome! +1
     
    MeRPG likes this.
  6. Offline

    MeRPG

    Thanks! Glad I could help! :D
     
  7. Offline

    fireblast709

    MeRPG Please don't spoonfeed code (and not so efficient code at that)
     
    Garris0n likes this.
  8. Offline

    MeRPG

    fireblast709
    You see, I put it in the spoiler to give the option. Personally I learn from reading code (I learned configurations from your Pyromania), so simpler tasks such as that can be learned by reading it. If someone wants to code but doesn't want to learn, it is a shame. But no reason to make absolute sure someone won't copy/paste it. And honestly, it's efficiency isn't that bad. Only 1299452 nanoseconds, or ~1.3 milliseconds (for a 16 character input).
     
  9. Offline

    fireblast709

    MeRPG that means that with about 50 players messaging in one tick, you are guaranteed to have TPS drop :p.
     
  10. Offline

    MeRPG

    Hrm. That assumes that you are making the name colorful every time a player sends the message. I sure hope that is not the case. If it is, yes, the code is definitely too inefficient for that :p

    Wait... Even if that was the case... 0.025989 ticks to handle that... So a 0.129945% drop in TPS.
     
  11. Offline

    GeorgeeeHD

    instead of an ArrayList and shuffling it use a ChatColor[] and use Random.nextInt(array.length) to get a random ChatColr from the array and to .toCharArray() for the input string and iterate with that
     
  12. Offline

    MeRPG

    that way would increase the chances of the same colors being adjacent. significantly. though you are right that an array should be used (less memory and more efficiency)
     
  13. Offline

    fireblast709

    MeRPG there is a very simple way to prevent the same colour being used twice in a row.
    Code:java
    1. function random(n, previous)
    2. {
    3. next = random.nextInt(n - 1);
    4. return next >= previous ? next + 1 : next;
    5. }
    This pseudo code basically takes a random number between 0 and n - 1, while excluding the previous number.
     
  14. Offline

    MeRPG

    Yea I know what it does... That could work fine.
     
Thread Status:
Not open for further replies.

Share This Page