Solved Player name

Discussion in 'Plugin Development' started by ewrs, Jul 10, 2018 at 3:40 PM.

  1. Offline

    ewrs

    Hello. I would like to know how you can decompose the player's name into letters? I can not describe this animation with words and skins the screenshots below. Could you tell me how to do this?

    Sorry for my english, he's not the best.
     

    Attached Files:

  2. @ewrs
    It looks like it is using ChatColors to add the strikethrough effect to the color. Here is what you need to replicate it:
    1. Create a new BukkitRunnable. This will allow us to change the displayname every couple of ticks, allowing us to animate the text.
    2. Inside the runnable, but outside of the run() method, create an int. Set it to 0. This will be the last index of where the strikethrough stops.
    3. Inside the run() method, create a new String. Set it equal to ChatColor.STRIKERHROUGH. Then, add the player's name, but use substring() from index 0 to index (int from #2). This will get the first half of the name that will be striked through. Then, add a chatcolor to remove the strikethrough. Then, add the player's name but substring() from index (int from #2) up to the length of the player's name. This now gets the last part of the player's name, that won't have a strikethrough.
    4. Then, set the player's displayname to be the the String from #3.
     
  3. Offline

    ewrs

    Thank you. I did something similar, but only for 4 letters ... I can not figure out how to find out if the player's name has ended. How to correctly make this animation?

    The code I made:


    String text4 = p.getName();
    String text5 = "§c§l" + text4;
    String text6 = "§4§l§m" + text4.substring(0, 1) + "§c§l" + text4.substring(1, 4); // ewrs
    String text7 = "§4§l§m" + text4.substring(0, 2) + "§c§l" + text4.substring(2, 4);
    String text8 = "§4§l§m" + text4.substring(0, 3) + "§c§l" + text4.substring(3, 4);
    String text9 = "§4§l§m" + text4.substring(0, 4) + "§c";


    After that there is animation in ActionBar
     
    Last edited: Jul 11, 2018 at 7:38 AM
  4. @ewrs
    You can find out how many characters are in a player's name by using getName().length
     
  5. Offline

    ewrs

    You propose to do to me

    if (p.getName (). Length () == 4) {
    }
    if (p.getName (). length () == 5) {
    }

    ?
     
  6. @ewrs
    No. That is hardcoding the length checks. Instead, as I posted above, use a variable index int that will increment up to the name's length, and use that to determine where the substring should start and end.
     
  7. Offline

    ewrs

    I do not understand what you mean. I used my method simply after reading your post. Give an example of what you wrote to me, please.

    I mean it.
    From this all I do not really understand what to add one player's name to the second ??? Give an example, I just can not understand it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jul 11, 2018 at 12:39 PM
  8. @ewrs
    This was your original code. Notice how the number adds +1 to itself for the next frame? Now, lets do an experiment. Lets assume you had the following code:
    What this code will do is broadcast a message with a different index 4 times (corresponding to the 4 times we call run()) What it will broadcast is this:
    Notice how every time the method is called, an extra letter is crossed out? This is because every time index is increased (index++), the sub-string will also have that number increased. Now notice, the code does not have any hard-coded indexes. Instead of needing the 4 lines for each time the message has an extra character with a strikethrough it, it only needed that 1 line that referenced the index value? This is how we are able to animate the message without needing to have code for every name-length.

    Now, you may notice that if you change the message of text4, it will still only cross out the first four letters and would cut out the rest of the message. This is because the old code still hard-coded the 4 as the last index. To allow for any length of a message to have it crossed out, you need to provide the text's length instead of just 4.
    Now you can provide any length for the message and the strikethrough will be added forever.

    Finally, you may notice that even with this, if you call run() too many times, it will throw an error. This is because index could exceed the size of the string. To get around this, we need to set index to 0 if it gets too big. To do that we need to make sure it never gets larger than text4's length. Do that by doing the following:
    Now, once you run the code again, if you call the run() method continuously, you will see that once all the characters have a strike through them, it will clear the strikethrough on the next frame. This is what I assumed you wanted.

    Now, with this information above, you should now be able to make a method to animate these frames.
     
  9. Offline

    ewrs

Share This Page