[TUT] Replacing certain text in a string!

Discussion in 'Resources' started by ChipDev, Jun 23, 2014.

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

    ChipDev

    Hey guys, Im Chip. Today we're going to learn how to replace certain text in a string!
    First: Some have been complaining about how non-effecient this is, Well:
    This is only for beginners, Who just want to find out some ways of using bukkit. For example.. this!
    (This may seem long to non-new dev's. If it is, skip to TL;DR!)
    TIP: Because of some old updates, The chat event is NOT PlayerChatEvent anymore. Its AsyncPlayerChatEvent!

    Replacing text!


    First, Get when the player chats.
    Code:java
    1. public void onPlayerChatEvent(AsyncPlayerChatEvent e) {
    2. //Do the stuff!
    3. }

    TIP: See the 'e' after AsyncPlayerChatEvent? That can be changed to anything. E stands for event in this case.

    Next, lets define the message into one huge string (Or small. so what?). We will edit this line of code after I show you the basics! Define this inside the {}'s of AsyncPlayerChatEvent!
    Code:java
    1. String NewMessage = e.getMessage();
    2.  


    Then, edit what we just did by adding a .replace(Thing, thing) after. Heres what I mean:
    Code:java
    1. String NewMessage = e.getMessage().replace("hi", "Hi");

    What will this do?
    This will change "hi" into "Hi" !

    Lastly, we replace the message with the 'NewMessage' string!
    Code:java
    1. e.setMessage(NewMessage);

    Why don't cancel the message and broadcast 'NewMessage'?
    You should not cancel the message and broadcast it again, because that will send it with no name, etc.

    You should Only cancel and broadcast it if you put a name before it, ex: custom names with color etc.



    Final result for AsyncPlayerChatEvent: (TL;DR)

    Code:java
    1. public void onPlayerChatEvent(AsyncPlayerChatEvent e) {
    2. String NewMessage = e.getMessage().replace("hi", "Hi");
    3. e.setMessage(NewMessage);
    4. }


    You made it through this tutorial! Yay! Hopefully this helped. Make sure to leave a like if it helped you :)
    -Chip

    Also, More examples!
    Easy colors, not &'Letter/number'
    Code:java
    1. String ColoredMessage = e.getMessage().replace("Bold", ChatColor.BOLD)
    2. .replace(" [Reset] ", ChatColor.RESET)
    3. .replace(" [Italic] ", ChatColor.ITALIC)
    4. .replace(" [Underline] ", ChatColor.UNDERLINE)
    5. .replace(" [Cross] ", ChatColor.STRIKETHROUGH)
    6. .replace(" [Green] ", ChatColor.GREEN)
    7. .replace(" [Red] ", ChatColor.RED);
    8. //etc. I don't want to go through all the colors :p
    9.  
    10.  

    ^^^
    Some people have been complaining that &1,2,3,etc. is the best way..
    This is an example!
     
    TheArabianCoder likes this.
  2. Offline

    BungeeTheCookie

    In my opinion, using the & symbol is a lot easier. It is shorter than saying "Reset," "Italic," "Underline" when you can simply say &r &o and &m. Although it is some memorization, if you ever want to be good at anything, you need to learn how to memorize. That is why ChatColor.translateAlternateColorCodes is for. And the replace() method is a basic string returner in Java. I don't find this as much as a tutorial but as a pointer to a cool method in Java. Maybe you can create a thread about some useful methods or classes in Java? Yet again, there are Oracle Documents.
     
    AoH_Ruthless likes this.
  3. Offline

    ChipDev

    BungeeTheCookie , I know what that is for. :)
    But I just wanted to show a cool example for how to use it.
     
  4. Offline

    Onlineids

    ChipDev ChatColor.translateAlternateColorCodes('&', String);
    Boom done
     
    ChipDev likes this.
  5. Offline

    ChipDev

    Boom.
     
  6. Offline

    BungeeTheCookie

    ChipDev
    That was not an example.
    That was a lecture on how to use 2 simple methods that take two sentences to explain such as how Onlineids did there.
     
  7. Offline

    ChipDev

    Two simple methods on 'IDEAS' of how do use them. Now people know how to do whatever, Theres no problem expressing our thoughts.
     
  8. Offline

    Skyost

    ChipDev You should put "[EXPERT]" in your title :rolleyes:
     
    97WaterPolo likes this.
  9. Offline

    ChipDev

    Educated guess: Is that good or bad?
     
  10. Offline

    RawCode

    personally i expected here something about memory IO and hacking into interned strings pool in order to alter strings embedded into classfiles...
    (like changing default messages from plugins\cbukkit (like "you dont have permission"))

    well, for developers new to java this tutorial is OK, but it feature nothing beyond standard oracle documentation about String class.
    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char, char)
     
    ZodiacTheories and AoH_Ruthless like this.
  11. Offline

    ChipDev

    permission changing?
    Code:java
    1. getServer().getPluginCommand("Command").setPermissionMessage(ChatColor.BLUE + "Hey, sir you cannot do that!");
    2. //Colored permissions.

    Yeah, could do that.. But this is about beginners for now. Ill get into more advanced stuff :p
     
  12. Offline

    DevRosemberg

    I dont get it, is this a 'Tutorial' or just people messing around with ChipDev making fun of him because this is like the simplest way (and not more efficient) of replacing Strings in a String and a really bad way of chatting like 'BoldGreenHey'.
     
    Skyost likes this.
  13. Offline

    RawCode

  14. Offline

    ChipDev

    No , check again. It has .replace(" Bold ", ChatColor.BOLD); , That means it has to be 'Bold Green Hey',
     
  15. Offline

    ZodiacTheories

    ChipDev

    Explain how Async can cause ThreadExceptions (not sure what the name of the exception is :p).
     
  16. Offline

    DevRosemberg

    ChipDev Still horrible way to do it.


    Is the best way to do it.
     
    Skyost likes this.
  17. Offline

    Phasesaber

    Can everyone just stop? It was a freaking example! He's not saying that "GreenBoldHi" is the best way to do it, he just made an example!
     
    ChipDev likes this.
  18. Offline

    ChipDev

    Correct, thanks for straightening that out.
     
    Phasesaber likes this.
  19. Offline

    AoH_Ruthless

    Phasesaber ChipDev
    That doesn't matter. When you post your code here in the resources section, you immediately are opening it up to criticism. This section is for teaching people or giving easy access to things unseen before or the 'best' or 'better' ways to do specific tasks. This is by far not the best way to approach this task as it is very clunky and hard to manipulate in game. You can't even say the words "blue", "bold", etc.. in normal chat (which are words used pretty often imo) without them disappearing...

    It may be a "freaking example", and if the intent wasn't to say "GreenBoldHi is the best way to do it", then he shouldn't have made this thread to begin with. I honestly, as RawCode mentioned, expected to see something unheard of with the Bukkit API.

    Maybe the various sarcasm was a bit harsh, but hey, don't take it personally: We all deserve to be laughed at in some point of our lives; programming is a good place for this to happen because we all have or will make dumb mistakes or approach something we think is good but ends up being horrible. It's not that big of a deal.

    By the way ... what if I wanted to color the word red, in red? I could never do it :[, and this "tutorial" doesn't check for non-lowercase. So " ReD " doesn't seem like it would be recognized, if I read the code correctly. This tutorial has other improving points:
    - The PlayerChatEvent is not obsolete, but actually many developers use it due to hesitation of Async threads. It is also used in older Bukkit versions that some developers still develop plugins for.
    - You don't even follow code conventions (Strings follow lowerCamelCase, not upper Camel Case).
    - I don't see any application for replacing "hi" to "Hi". You provide no instances where this could be useful or why this is important.
    - As mentioned numerously throughout both this thread and in this essay, your color coding is lightyears behind the standardized & color codes. Especially since you buffer color codes with whitespace .. probably the worst decision. At least make it <blue> instead of " blue ".
     
    Phasesaber likes this.
  20. Offline

    Phasesaber

    AoH_Ruthless I can see your point, but it was just an example. It was posted in the Resources section so other developers can use it. He was giving an example, not "how you should do this".
     
    ChipDev likes this.
Thread Status:
Not open for further replies.

Share This Page