Broadcast random string from config file

Discussion in 'Plugin Development' started by ARavinMadMonkey, Apr 29, 2014.

Thread Status:
Not open for further replies.
  1. I'm trying to make some variation of a broadcasting plugin (Yes I know they're over done, but this is just practice).

    Could someone give me advice on how to create a config file that has strings in it, then have the plugin choose a random string and broadcast that string?

    I think I have an idea of how to do the random thing, use an array to give each string a number, then broadcast the string that number relates to. But I dont know how to make a config that users can add to.

    Any help would be greatly appreciated :)
     
  2. Offline

    1Rogue

    I personally always felt this system worked better by using a plain text file in an "announcements" directory, with each file being an announcement. Then it's just basic I/O
     

  3. Any chance you could give some example code?
    Not asking you to write it for me obviously but I dont really know where to start.
     
  4. To create a configuration file, you would have a file called config.yml where your plugin.yml file is located.
    In the config.yml file, you can put something such as:
    Code:
    messages:
      - This is a broadcast
      - This is another broadcast!
    Then, in the plugin, use
    Code:java
    1. saveDefaultConfig();
    in your onEnable() method to save the file into the /plugins/yourPlugin folder.

    As for broadcasting it, you need to use something along the lines of:
    Code:java
    1. ArrayList<String> messages = plugin.getConfig().getStringList("messages");
    2. Random random = new Random();
    3. int index = random.nextInt(messages.size());
    4. Bukkit.broadcastMessage(messages.get(index));
     
  5. Offline

    mythbusterma

    Using the built-in Bukkit config system you can create a config.yml file with almost no effort.
    See: http://wiki.bukkit.org/Configuration_API_Reference

    Then retrieve all the elements of a node with getConfig().getStringList()
    Then you can choose a random one with:

    Code:java
    1. List<String> sl = plugin.getServer().getConfig().getStringList("things");
    2. int l = rand()%sl.length();
    3. String temp = sl.root();
    4.  
    5. for(int i = 0; i<l; i++){
    6. temp = temp.getNext();
    7. }
    8.  


    Or something similar to that.
     
  6. Offline

    1Rogue

Thread Status:
Not open for further replies.

Share This Page