ArrayList<String>, get all and if it contains a string do this and that..

Discussion in 'Plugin Development' started by adde, Aug 9, 2013.

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

    adde

    Hello once again bukkit forum.
    I am in need of your help again in a plugin that is not too much complicated for me but still somewhat complicated...

    I want to have Strings in my StringList(ArrayList) "Words:".
    Each string will have words that are seperated with ':'.
    I already fixed that, it works with one string, but when it comes to more than just one string, if I use StringList, it won't work.

    Can you please tell me how I would get it to work.

    Let's say, if e.getMessage() contains hello, replace it with split[1] (the word behind the ':' seperator).
    I got that setup, the thing is, if I change it to StringList, it won't pick it per row, it will do something else that is messed up.. :S

    I tried using "for" method but never really got it?
    If you are gentle, please give me an example with AsyncPlayerChatEvent and then replace the message with the word behind ':' in a StringList. :)
     
  2. Offline

    adam753

    I'm not completely sure what it is you want to use the ArrayList for?
    If you just want a list of strings and to call the splitting function on each one, then it's as simple as this:
    Code:java
    1.  
    2. ArrayList<String> stringlist; //Assuming you already have this line
    3.  
    4. for(String s: stringlist){
    5. doSomethingWith(s);
    6. }
    7.  
     
  3. Offline

    adde

    What I want is, if e.getMessage() contains a string from the config, the word before split.
    Let's say I have hello:nothello, and if the e.getMessage() contains hello, it will replace hello with nothello. I got it setup if I do it with one string but when I try to use more than just one string with StringList(ArrayList) it won't work.
     
  4. Offline

    Ivan

    You can split Strings by using string.split(regex);
    The regex is the char where the string will split, the "." in this case :)
     
  5. Offline

    adde

    I said I already got it setup, but it only works per string, not per line in stringlist.
    EDIT by Iroh: Removed cursing at another user.
     
  6. Offline

    foodyling

    adde Have fun getting help. You're being rude and not that specific :x
     
  7. Offline

    adde

    Lol it was a joke saying no shit.

    And it's maybe my problem that I can't explain it as good so you can understand what I want help with, but I tried my best. Well fuck it then
     
  8. Offline

    Ivan

    Whoa, just trying to help dude. I misread a line there.
     
  9. Offline

    adde

    And I was just saying "No shit". I didn't know people saw it as an harassment.

    Also, thanks for trying to help me.
     
  10. Offline

    TheBoy3

    You're probably not going to read this, but the way I join Arrays and Iterables is by using this:

    Code:java
    1. Joiner.on(" : ").join(iterablearraylist)
     
  11. Offline

    adde

    I don't need to split the words in anyway, I already got the fixed.. The thing is when someone chats and the message contains the split[0] string, it will replace the string with the string behind the ':' (split[1]).
    In a StringList

    TheBoy3 , Ivan , foodyling aaand adam753 (Nice name, Adam :D)

    Sorry for being rude if someone saw it like that..
    Now, I got it working but my loop will repeat and go on forever until the server crashes.. :p
    This is how it looks like:
    [​IMG]
    Code:
    Code:java
    1.  
    2. @EventHandler
    3. public void replace(AsyncPlayerChatEvent e)
    4. {
    5. Player player = e.getPlayer();
    6. List<String> reg = plugin.getConfig().getStringList("Words");
    7. String message = "";
    8.  
    9. for(String regex : reg)
    10. {
    11. String[] split = regex.substring(2).split(":");
    12. String word = split[0];
    13. String replace = split[1];
    14.  
    15. if(e.getMessage().contains(word))
    16. {
    17. message = e.getMessage().replaceAll(word, replace);
    18. player.chat(message);
    19. break;
    20. }
    21. }
    22. }
    23.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  12. Offline

    WoodMKII

    Does:
    Code:java
    1. player.chat(message);

    Fire a chat event? If so, then you have an infinite loop... well infinite until a crash xD

    Perhaps instead of making the player chat something new, it might be easier just to set the message in the chat event try replacing:

    Code:java
    1. player.chat(message);


    with:

    Code:java
    1. e.setMessage(message);


    Also, it will only do the first word in the list, because it breaks after it finds the first word, if you remove the break, or replace it with a continue, it should continue down the list.

    EDIT: Woah, sorry didn't realise how old this post was. xD
     
Thread Status:
Not open for further replies.

Share This Page