Read and say a file

Discussion in 'Plugin Development' started by Joshua Neicho, Mar 5, 2011.

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

    Joshua Neicho

    Hi again,
    I was wondering how I would make a Plugin say something off of a .txt or .properties file when a user says /test or something

    thanks anyway
     
  2. Offline

    gnftoxic

    An easy way would be to use a Scanner.

    I've been using onPlayerCommandPreprocess (until I work out how to use onCommand() properly, been moving for the past few days) so, this will simply be based off of onPlayerCommandPreprocess() in PlayerListener.

    Code:
    import java.util.Scanner;
    // bukkit imports go here
    
    public class TestPlayerListener extends PlayerListener
    {
         // constructor and all that here ...
    
        @Override
        public void onPlayerCommandPreprocess(PlayerChatEvent event)
        {
            // parse command, blah blah blah.
    
            if(command.equalsIgnoreCase("/test"))
            {
                Scanner fileread = new Scanner(new File("test.txt"));
                while(fileread.hasNextLine())
                {
                    event.getPlayer().sendMessage(ChatColor.YELLOW + fileread.nextLine());
                }
                fileread.close(); // unsure if this exists, but it's best to close it if it does exist.
            }
        }
    }
    
    Basically, the "Scanner" class allows you to read through a file without any complications, and makes life easier (and lazier).
     
  3. Offline

    Joshua Neicho

    thanks and do you also know how i would let the colour be chosen in the .txt file?
     
  4. Offline

    muCkk

    what you could also use is java.util.Prperties
    wikipedia
    java api

    us it like this:
    Code:
    Properties config = new Properties();
    FileWriter writer = new FileWriter("plugins/ColorPlugin/ColorPlugin.properties");
    FileReader reader = new RileReader("plugins/ColorPlugin/ColorPlugin.properties");
    
    config.setProperty("Color", "red");
    config.store(writer, "put your comments here");
    ...
    config.load(reader)           // only use this once (when the plugin is loaded for example)
    config.getProperty("Color");   // will return red as a String
    
     
  5. Offline

    Joshua Neicho

    where would i put this?
    <font color="rgb(204,204,204)">--- merged: Mar 5, 2011 7:49 PM ---</font>
    can anyone else help as muCkk's one when i use the command it just comes up with what i just used
    <font color="rgb(204,204,204)">--- merged: Mar 5, 2011 8:41 PM ---</font>
    anyone?
    <font color="rgb(204,204,204)">--- merged: Mar 5, 2011 10:10 PM ---</font>
    please when i type /apply since that is what i have set it at the text /apply just comes up in plain white
    <font color="rgb(204,204,204)">--- merged: Mar 6, 2011 4:58 AM ---</font>
    if anyone could help please do here is the source <Edit by Moderator: Redacted mediafire url>
    <font color="rgb(204,204,204)">--- merged: Mar 6, 2011 4:49 PM ---</font>
    come on people i still need help
     
    Last edited by a moderator: Dec 15, 2016
Thread Status:
Not open for further replies.

Share This Page