Solved Replacing certain sections of a String

Discussion in 'Plugin Development' started by TfT_02, Dec 3, 2012.

Thread Status:
Not open for further replies.
  1. So I'm looking for a way to randomly chance letters in a chat message with ChatColor.MAGIC, so I thought of this to do this.


    Edit: I've figured it out. :) Wasn't that hard with the StringBuilder. Here's how I did it:
    Code:java
    1. public String Scrambled(String msg) {
    2. StringBuilder sb = new StringBuilder(msg);
    3. int halfLength;
    4. int start1;
    5. int end1;
    6. int start2;
    7. int end2;
    8. int scrambleLength1;
    9. int scrambleLength2;
    10.  
    11. int length = msg.length();
    12. if (length == 0){
    13. //This is not possible, but hey! You never know, right? :D
    14. }else if (length == 1) {
    15. int scramble = random.nextInt(100);
    16. if (scramble > 50) {
    17. sb.insert(0, ChatColor.MAGIC);
    18. }
    19. }else if (length <= 3) {
    20. start1 = random.nextInt(length);
    21. end1 = random.nextInt(length + 1);
    22. sb.insert(start1, ChatColor.MAGIC);
    23. sb.insert(end1 + 2, ChatColor.RESET);
    24. }else if (length >= 4) {
    25. halfLength = (int) length / 2;
    26. scrambleLength1 = random.nextInt(5) + 3;
    27. scrambleLength2 = random.nextInt(5) + 3;
    28. start1 = random.nextInt(halfLength);
    29. end1 = scrambleLength1 + start1;
    30. start2 = random.nextInt(halfLength) + end1;
    31. end2 = scrambleLength2 + start2;
    32. if (end2 > length) {
    33. end2 = length;
    34. }
    35. if (start2 > length) {
    36. start2 = length;
    37. }
    38. sb.insert(start1, ChatColor.MAGIC);
    39. sb.insert(end1 + 2, ChatColor.RESET);
    40. sb.insert(start2 + 4, ChatColor.MAGIC);
    41. sb.insert(end2 + 6, ChatColor.RESET);
    42. if (plugin.debug_mode) {
    43. System.out.println("DEBUG INFORMATION:");
    44. System.out.println(start1 + "-" + end1 + " | " + scrambleLength1);
    45. System.out.println(start2 + "-" + end2 + " | " + scrambleLength2);
    46. }
    47. }
    48. String scrambled = sb.toString();
    49. return scrambled;
    50. }
    51.  
     
  2. Offline

    leiger

    It really depends on what you want to do. If you just wanted random letters in the chat message to be changed, not specific letters, you could get a few random numbers between 0 and the chat message's length, then insert your colour code there.

    What you're doing at the moment will lead to approximately 50% of the message having the magic effect. Probably more, if vowels are returned as true for the effect. That would make messages a bit unreadable.

    What is the end goal for this? Will it apply to every message that is posted in chat, or only some of them?
     
  3. I'm trying to make the chat unreadable / barely readable, depending on random odds of how many characters get replaced with the magic effect. So I thought that the easiest way would be to have 50% of each letter to get replaced, but the way that I've got it now is not right. Just replacing random characters might work even better. ;)

    How would I replace random characters of a string? Is there a method that will allow me to replace specific characters? E.g. characters 1-3, 5-6, 7 and 9 of a string?
     
  4. Offline

    leiger

    You could very easily write your own method to do that (e.g. concatenate two substrings and your colour code together).

    The algorithm used to determine which random characters will be replaced depends entirely on what your desired effect is. I'd suggest not tying it to specific letters of the alphabet though - just go through each character in the sentence and randomly decide whether it's to be modified or not.
     
  5. Yeah that seems like a good idea.
     
  6. can you use this, to decrypt the nameplate by keeping the skin?

    thx

    julia :)
     
  7. Heh? That doesn't make any sense.

    I'm using this code to "scramble" chat message; e.g. Insert ChatColor.MAGIC in parts of messages, making them unreadable.
     
  8. o.k. thx TFT_02, i don´t understand your code. But i thought from readeing the thread, it would be possible to make the tagname unreadable by using your code or part of it.
     
  9. Well, just change the color of the nametag to MAGIC
     
  10. yes i don´t programm anything - i can´t. i am just searching for bukkit-plugin, that can change my tagname to magic-format(&kNAME) but leave chatname, welcome-message-name/deathname & playerlistname as the normal Playername. And i found your Thread.

    thx

    julia :)
     
Thread Status:
Not open for further replies.

Share This Page