Replacement of Two Strings

Discussion in 'Plugin Development' started by THXMINECRAFT, Jul 21, 2014.

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

    THXMINECRAFT

    While fixing a few problems and cleaned my plugin Sword Asker a bit, I ran into a little problem.

    Code:java
    1. if(target_1.contains("%SENDER%") && (!target_1.contains("%WEAPON%"))){
    2. targetPlayer.sendMessage(Prefix + (target_1.replace("%SENDER%", sender.getName())));
    3. }else if(target_1.contains("%WEAPON%") && (!target_1.contains("%SENDER%"))){
    4. targetPlayer.sendMessage(Prefix + (target_1.replace("%WEAPON%", plugin.getConfig().getString("sword"))));
    5. }else if(target_1.contains("%SENDER%") && (target_1.contains("%WEAPON%"))){
    6.  
    7. targetPlayer.sendMessage(Prefix + (target_1.replace("%SENDER%", sender.getName())));
    8. targetPlayer.sendMessage(Prefix + (target_1.replace("%WEAPON%", plugin.getConfig().getString("sword"))));
    9.  
    10. }else{
    11. targetPlayer.sendMessage(Prefix + target_1);
    12. }


    (The Prefix is "&c[Sword Asker]")

    What I'm trying to do is when the player types the command /sword it prints a message that replaces %SENDER% with the executor's name and replaces %WEAPON% determining with what command was used and so on. But when I added these two lines (as you can see what I've divided) it prints the message twice in two different forms.

    According to the configuration file the default message is this:

    Code:java
    1. target_1: '&a--- &9%SENDER% &arequests your &9%WEAPON%. &a---'
    2.  


    In game when I type "/sword Rascal32" (My player name) I get these two messages:

    "[Sword Asker] --- Rascal32 requests your %WEAPON%. ---"
    and...
    "[Sword Asker] --- %SENDER% requests your Sword. ---"

    So is there a way to replace both strings without writing almost the same line of text? Since in result to that it prints the same message twice. This may be one of those questions where it's so obvious, I just can't find it.

    Edit: I forgot to mention that "target_1" is actually this:

    Code:java
    1. String target_1 = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("target_1"));
     
  2. Offline

    Saladoc

    THXMINECRAFT

    since you don't need any features only regular expressions offer, there is nothing wrong with using String.replace() twice.

    Code:java
    1. String foo = "%SENDER% can haz %WEAPON%";
    2. foo = foo.replace("%SENDER%", sender.getName());
    3. foo = foo.replace("%WEAPON%", weapon.getName());


    Anything more would be overkill.

    Edit: If you like it more, you can even chain the calls

    Code:java
    1. String foo = "%SENDER% can haz %WEAPON%";
    2. foo = foo.replace("%SENDER%", sender.getName()).replace("%WEAPON%", weapon.getName());
     
  3. Offline

    daavko

    THXMINECRAFT Try this:
    Remove entire if-else if-else if-else statement and replace it with this:
    Code:java
    1. targetPlayer.sendMessage(Prefix + (target_1.replace("%SENDER%", sender.getName()).replace("%WEAPON%", plugin.getConfig().getString("sword"))));

    This will work even when there is no %WEAPON% or no %SENDER% in message, because it simply doesn't have anything to replace when it's not there.

    EDIT: Ninja'd :eek:
     
    Saladoc likes this.
  4. Offline

    THXMINECRAFT

    Saladoc

    What'd you know. It actually worked this time.
    Thanks, I knew it was something obvious. :p

    daavko

    Thank you for clearing that up. <3
     
    Zarkopafilis likes this.
Thread Status:
Not open for further replies.

Share This Page