Random lore

Discussion in 'Plugin Development' started by JoloCodeBrahs, Aug 7, 2016.

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

    JoloCodeBrahs

    Random Lore, how would i get a list of lore from a text file and randomly add it to an item? (the item is also in the config file, item list)
     
  2. Offline

    timtower Administrator Administrator Moderator

    @JoloCodeBrahs Use a config, getStringList, any method to get a random value within the length of that list. (I am pretty sure that somebody can help with the random part)
    list.get(randomvalue)
     
    JoloCodeBrahs likes this.
  3. Offline

    Marti201

    About the random part:
    Code:
    Random rand = new Random();
    int result = rand.nextInt(list.length() - 1);
    // Returns a random value from 0 to the length of the list -1, as the first item is 0
     
    JoloCodeBrahs likes this.
  4. Offline

    JoloCodeBrahs

    How would I get the list (I'm rusty sorry) or set variable for the list.
     
  5. Offline

    Rixterz

    I've used this code many times and it's very reliable.

    It creates an empty list of strings, then opens the file and reads each line into the list. When there's nothing left to read, it returns the list.

    That means you can get the list and randomly select one from it:

    Code:
    Random rand = new Random();
    
    List<String> lines = readLines("file path here.txt");
    
    int randInt = rand.nextInt(lines.length());
    
    String randLine = lines(randInt);
    Code:
    public List<String> readLines(String filePath)
    {
      
        List<String> lines = new ArrayList<String>();
    
        BufferedReader br = null;
        String line = null;
      
        try
        {
            br = new BufferedReader(new FileReader(new File(filePath)));
          
            while((line = br.readLine()) != null) // while there's a line left to read
            {
                lines.add(line);
            }
    
            return lines;
        }
        catch(Exception e)
        {
            // error reading from file
        }
        finally
        {
            try
            {
                br.close();
            }
            catch(IOException e)
            {
                // ignore
            }       
        }
    
        return null;
    }
     
  6. Offline

    Lordloss

    @Rixterz
    Please read in many other threads why we dont spoonfeed code. Also with your approach he would need to use 2 files, because he wants to have ItemStacks in it too.

    @JoloCodeBrahs Please google for the Configuration API, this is all you need for doing most stuff with configs.
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page