Multimessage config help :S

Discussion in 'Plugin Development' started by NeoSilky, Sep 7, 2011.

  1. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Hello everyone :D

    Yeah, so i'm making a plugin that shows the player a message when they do a certain event. The thing is, the person im making it for wants it to be setup so that there are several messages avaialable and they are on loop so the same message isn't repeated. I got config to work along with colour codes. I use Bukkit Standard configuration .yml stuff. I prefer not to use .bml :S

    So if anyone knows how to set messages for it and make them do one after another when the player does the event, then thanks :)
  2. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Not exactly sure what you want but if I understand correctly:
    Make a string array of all the options, get a random generated number with the array length, send the string with the random array.
  3. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @Randy Schoutenthat's probably what i need, but ill explain more.

    So when a player kills a pig, it will get the message from config, do some cool colour stuff also from config, and then give it to player, but normally, that only means one message is possible, but to save boringness, he wants a range of messages he can configure. So i would want it so it randomly goes one after another, or maybe a looped sequence? :)
  4. Offline

    Randy Schouten

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @NeoSilky
    So you want it to send all the messages in a random order?
  5. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yes, if not, one after another :)
  6. Offline

    killdave

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    From what you have said I get this:

    1. A file of different "sayings"
    2. plugin loads the "sayings"
    3. event occurs it randomly choses a "saying"
    4. then message...??

    Well then what I would do then is make the plug in read a file and have a message or "saying" on each line be put in an arraylist so you have to import an arraylist.

    then when the event calls for a message just calls a message() and that would return a random message from the array
  7. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    alright, thanks :) would this mean that no message would get repeated twice?
  8. Offline

    killdave

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    They can get repeated twice if the random get the same number twice lol

    and the way i'd avoid that is to make a varable of last index chosen and if the random chose that then make it randomiz again
  9. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    alright thanks, would i have to use the Random method? :)
  10. Offline

    killdave

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ya the Math.random
  11. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    alright, thank you :)
  12. Offline

    daemitus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If all your messages are in a single array, you could try using an Iterator and if !hasNext(), just reinitialize it.

    Its not random, but it wont repeat anything either.

    This post has been edited 1 time. It was last edited by daemitus Sep 8, 2011.
  13. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Oh, urmm, help? :p
  14. Offline

    Bush

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  15. Offline

    Bush

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  16. Offline

    daemitus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    So lets see.

    List<String> list = config.getStringList("yourDumbConfig", new ArrayList<String>());

    Iterator<String> iter = list.iterator();
    while(iter.hasNext()) {
    String output = iter.next();
    }
    iter = list.iterator();

    Like bush said, theres the Collections.shuffle(list) method, that does what it says.
  17. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @daemitus can this be applied to other animals that are killed? :)
  18. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    ahh, right, no errors :D so the config, how shall i set it so it read the mutliple messages? :D
  19. Offline

    Bush

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    thnx for that, i just hope my implementation of it works
  20. Offline

    daemitus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You mean how is the YAML config for stringlists?


    Code:
    List<String> list = config.getStringList("messages");
    
    messages:
    - I ate a cookie
    - It was good
    - You get the idea

    This post has been edited 2 times. It was last edited by daemitus Sep 8, 2011.
  21. Offline

    NeoSilky

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yeah, how would i write that in code? :p
  22. Offline

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Put them in a string[], then do
    Code:
    int x;
    Random ran = new Random(array.length())
    
    Or something like that. Can't remember too well, but then pick the one from the array with the random number has pointed to.
  23. Offline

    Bush

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    thanx for the reply, but that part i know how to do, its getting the custom strings that the user changes in the config file to actually go in to the array
  24. Offline

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Oh, that.
    Look 3 posts up.

    This post has been edited 1 time. It was last edited by Lolmewn Sep 8, 2011.
  25. Offline

    Bush

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    i've tried it, but i will look at it again.
  26. Offline

    Darkman2412

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:java
    1. List<String> list = config.getStringList("messages");
    2. int x;
    3. Random generator = new Random(list.length);
    4. String randommessage = list.get(generator.nextInt());
    5. player.sendMessage(randommessage);
  27. Offline

    Bush

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    thnx for that, will try that aswell
  28. Offline

    Darkman2412

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    No problem :)
  29. Offline

    daemitus

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    That is how you would do it in code.
    Code:
    List<String> list = config.getStringList("messages", new ArrayList<String>());
    
    and the YAML would look like
    Code:
    messages:
    - I ate a cookie
    - It was good
    - You get the idea
    
    To iterate though, either use the random method, or a cyclic iterator.
    Code:
    private Iterator<String> iter = list.iterator();
    
    public String getMessage() {
      if(!list.isEmpty()) {
        if(iter.hasNext()) {
          return iter.next();     } else {
          iter = list.iterator();
          return getMessage();
        }
      }
    }
    

    This post has been edited 1 time. It was last edited by daemitus Sep 8, 2011.
  30. Offline

    Darkman2412

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @daemitus isn't this easier?

Share This Page