[Tutorial] Animated Text

Discussion in 'Resources' started by Niknea, Jan 24, 2014.

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

    Niknea

    Hello everyone, and welcome to my first ever tutorial! In this thread I will show you guys how to make animated text!

    1. You would first need to decide where you want to use this awesome feature, for a event? Or a command? In this tutorial I will use it for a command.

    2. How fast do you want the text to show up? In this tutorial I put the text at 10 ticks, so every second two letters will show up.

    Now let's get to the real stuff

    1. You will first need to make two integers, one for the starting point of your phrase, word, or letter, this integer's value is usually set to 1. We'll then need to make another integer that will be for the amount of spaces between each message, this will give the animated feature.
    In this example I will set the starting point to 1, and the spaces to 20
    Code:java
    1. int start = 1; // starting point in your phrase, word, or letter
    2. int spaces = 20; // for spaces

    2. Here is where we use our start variable . You will now need to create a for loop, this will loop the same code until your happy with the message, here is an example loop
    Code:java
    1. for(; start <= 10;)

    replace "10" with the number of slides you want, for example, if you want to spell the word "hello" and add one letter in every slide you will replace "10" to "5" because there are five letter, and one will be added in each slide, therefore you need 5 slides.

    3. We will now need to create an if-else-if ladder for every slide, so if you want to use the previous example that spells "hello" you will need to create one if statement and four else if statements.
    Here is an example
    Code:java
    1.  
    2. if ( start == 1)
    3. {
    4. // Code Here
    5. }
    6. else if ( start == 2)
    7. {
    8. // Code Here
    9. }
    10. else if ( start == 3)
    11. {
    12. // Code Here
    13. }
    14. else if ( start == 4)
    15. {
    16. // Code Here
    17. }
    18. else if ( start == 5)
    19. {
    20. // Code Here
    21. }
    22.  


    4. Here is where we will use our spaces variable, you will need to create a simple for loop in each if-else-if statement that will send the player a message that contains just spaces. Here is an example of the for loop
    Code:java
    1. for(; spaces <= 0; --spaces)
    2. {
    3. player.sendMessage("");
    4. }

    and here is the entire if-else-if statement now
    Code:java
    1. if ( start == 1)
    2. {
    3. for(; spaces <= 0; --spaces)
    4. {
    5. player.sendMessage("");
    6. }
    7. }
    8. else if ( start == 2)
    9. {
    10. for(; spaces <= 0; --spaces)
    11. {
    12. player.sendMessage("");
    13. }
    14. }
    15. else if ( start == 3)
    16. {
    17. for(; spaces <= 0; --spaces)
    18. {
    19. player.sendMessage("");
    20. }
    21. }
    22. else if ( start == 4)
    23. {
    24. for(; spaces <= 0; --spaces)
    25. {
    26. player.sendMessage("");
    27. }
    28. }
    29. else if ( start == 5)
    30. {
    31. for(; spaces <= 0; --spaces)
    32. {
    33. player.sendMessage("");
    34. }
    35. }

    5. We will now add the characters to our slides. Please keep in mind that every slide you will need to add the previous slide characters and the new characters. Here is the new if-else-if statement.
    Code:java
    1. if ( start == 1)
    2. {
    3. for(; spaces <= 0; --spaces)
    4. {
    5. player.sendMessage("");
    6. }
    7. player.sendMessage(ChatColor.GOLD + "H");
    8. }
    9. else if ( start == 2)
    10. {
    11. for(; spaces <= 0; --spaces)
    12. {
    13. player.sendMessage("");
    14. }
    15. player.sendMessage(ChatColor.GOLD + "He");
    16. }
    17. else if ( start == 3)
    18. {
    19. for(; spaces <= 0; --spaces)
    20. {
    21. player.sendMessage("");
    22. }
    23. player.sendMessage(ChatColor.GOLD + "Hel");
    24. }
    25. else if ( start == 4)
    26. {
    27. for(; spaces <= 0; --spaces)
    28. {
    29. player.sendMessage("");
    30. }
    31. player.sendMessage(ChatColor.GOLD + "Hell");
    32. }
    33. else if ( start == 5)
    34. {
    35. for(; spaces <= 0; --spaces)
    36. {
    37. player.sendMessage("");
    38. }
    39. player.sendMessage(ChatColor.GOLD + "Hello");
    40. }

    6. Finally we will need to create a Bukkit Runnable to add a delay, which gives the animation feature. Here is an example of a Bukkit Runnable
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    2. {
    3. public void run()
    4. {
    5. // Code Here
    6. }
    7. }, /* Time until code executes here */);

    Inside the Bukkit Runnable you will add one to the value of the start variable..

    7. Congratulate yourself! You have made an awesome feature in your plugin!


    Example Use


    I hope you enjoyed reading my tutorial, and good luck on your plugins!

    - Niknea
     
  2. Could you post a picture of it in action?
     
  3. Mother of god...
     
    DogeDev, Cirno, hawkfalcon and 3 others like this.
  4. Offline

    Niknea

    Techno I've added an "Example Use" section in the main post.


    Assist Oh god... did I do something wrong?
     
  5. Niknea
    Well yes. You're using 10 different runnables for this, which all of them contain 19 lines of empty sendMessage().
     
  6. Offline

    Niknea

    Assist I did add a tip in the post saying you can use a for loop.
     
  7. Niknea
    Sorry, I didn't see that, I was just admiring your code. You should just create a method that does all the message sending for you. Also, I'd use a repeating task instead of 10 delayed tasks.
     
  8. Offline

    Niknea

    Assist Thanks, I'll definitely check out repeating tasks.
     
  9. Offline

    filoghost

    Another thing, don't send more chat lines than needed. I think that ~12 lines should be enough to clear the chat. Please, use a for loop and provide a method for every possible String.
     
    Skyost likes this.
  10. filoghost
    I'm not sure about this, as I haven't messed with the multiplayer chat settings, but isn't it possible to make the chat window larger, therefore allowing more text to be seen, or does the text scale with the size?
     
    Niknea likes this.
  11. Offline

    filoghost

    Assist You're right, you can change the size. Niknea ignore my previous comment about lines.
     
  12. Offline

    DevRosemberg

    Speechless...
     
    Skyost and bobacadodl like this.
  13. Offline

    Niknea

    DevRosemberg I'm pretty sure that comment was more towards the negative side, and I apologize, it is my first tutorial so it's probably poorly written, and probably isn't efficient code either. My apologize.
     
  14. Offline

    TryB4

    Niknea
    Code:java
    1. public void sendMessage(final Player p, final String s)
    2. {
    3. final int i = s.length();
    4. new BukkitRunnable() {
    5. int c = 0;
    6. public void run() {
    7. if(c != i){
    8. for (int i = 0; i < 10; i++)
    9. {
    10. p.sendMessage("");
    11. }
    12. p.sendMessage(s.substring(0, c+1));
    13. c++;
    14. }else
    15. {
    16. for (int i = 0; i < 10; i++)
    17. {
    18. p.sendMessage("");
    19. }
    20. p.sendMessage(s);
    21. this.cancel();
    22. }
    23. }
    24. }.runTaskTimer(this, 6, 6);
    25. }



    I think this is a bit easier to understand.
     
    ThunderWaffeMC and Skyost like this.
  15. Offline

    DevRosemberg

    TryB4 Offtopic but, that signature lol
     
    coaster3000, bobacadodl and Skyost like this.
  16. Offline

    TryB4

  17. Offline

    Ultimate_n00b

    A better idea is to use ProtocolLib to grab the messages sent to the player, then update his list of messages
     
  18. Offline

    mattrick

    Niknea
    That was actually a really smart idea :p Nice work!
     
    Niknea likes this.
  19. Offline

    Niknea

    mattrick Thanks man, really appreciate it!
     
  20. Offline

    ZeusAllMighty11

    This is neat, however you are doing this very inefficiently.

    You can use a simple for loop to repeat the blank messages. In addition, you can schedule a runnable timer that does what you need, instead f a bunch of delayed tasks.
     
  21. Offline

    Garris0n

    Correct me if I'm wrong, but would the for loop be any more efficient than just spamming the method? Obviously it's better to use the loop, but in the case of efficiency...
     
  22. Offline

    michael566

    Wow Cool.
     
    Niknea likes this.
  23. Offline

    Wizehh

    Someone make a method for this?
     
  24. Offline

    mydeblob

    Wizehh TryB4 did make one, as seen in his post.
     
    TryB4 likes this.
  25. Offline

    Wizehh

    mydeblob Oops, must have skipped over that :p
     
  26. Offline

    The_Doctor_123

    I don't believe so, except when loading the jar from the disk, as it would be a larger file.

    Niknea
    Please edit that code in your topic..
     
    Garris0n likes this.
  27. Offline

    Niknea

    Last edited by a moderator: Jun 6, 2016
  28. Offline

    Desle

    Niknea
    Not.. really animated.. isn't it just clearing the chat, then add a message?..

    If so.. not really nice that you don't show that in the video.
     
  29. Offline

    Niknea

    Desle It put's it up enough spaces that it will take a while for the player to scroll to the message. It's at that point where it's off the recent message screen automatically.
     
  30. Offline

    bobacadodl

    Its just repeatedly sending messages, that flow off of the screen to make it look like an animation
     
Thread Status:
Not open for further replies.

Share This Page