[Help] with readLine

Discussion in 'Plugin Development' started by mason1370, May 18, 2012.

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

    mason1370

    so what im trying to do is when the player logs in it will run there username through a file called test.txt
    here is what i have so far but im getting an error
    public void onPlayerJoin(PlayerJoinEvent event) {
    Player test1 = event.getPlayer();

    String strLine;
    FileInputStream fstream = new FileInputStream("test.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while ((strLine = br.readLine()) != (event.getPlayer()));

    }
    catch (Exception e){

    }

    }

    my error is on the file with the while it says "incompatible operated types String and Player
     
  2. Offline

    Technius

    mason1370
    String != Player
    You're trying to convert a player into a string. Not going to work. You need to check the string against the player's name.

    You don't need to use DataInputStream and InputStreamReader, instead do this:
    Code:java
    1.  
    2. try
    3. {
    4. Player player = //your player
    5. File file = new File("path");//default directory is the craftbukkit folder
    6. String l;
    7. while((l=br.readLine()) != null)
    8. {
    9. if(l.equalsIgnoreCase(player.getName())//if the player's name is the same as the string...
    10. {
    11. //do something
    12. }
    13. }
    14. }
    15. catch(IOException ioe)
    16. {
    17. //error
    18. }
     
  3. Offline

    mason1370

    Technius
    OK its giving me errors on like 5, and line 6 with file it says "File can not be resolved to a varible"
     
  4. Offline

    r0306

  5. Offline

    Technius

    You need to import the File class...
    Code:java
    1. import java.io.File;
     
  6. Offline

    mason1370

    package com.gmail.mason1224;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.lang.reflect.Array;
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;




    public class turnon extends JavaPlugin {
    boolean test = false;

    Logger log;

    public void onEnable(){
    log = this.getLogger();
    log.info("Rule Accepter is now running!");
    }

    Logger log1;

    public void onPlayerJoin(PlayerJoinEvent event) {
    Player test1 = event.getPlayer();

    try
    {
    Player player =
    //your player
    File File file = new File("path")
    //default directory is the craftbukkit folder
    BufferedReader br = new BufferedReader(new FileReader(File));
    String l;
    while((l=br.readLine()) != null)
    {
    if(l.equalsIgnoreCase(player.getName())//if the player's name is the same as the string...
    {
    //do something
    }
    }

    }
    catch(IOException ioe)
    {
    //error
    }
    }


    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    {
    Player player = null;
    if (sender instanceof Player)
    {
    player = (Player) sender;
    }
    try{
    BufferedWriter out = new BufferedWriter(new FileWriter("test.txt", true));
    out.write(sender.getName() + System.getProperty( "line.separator" ));
    out.close();
    }
    catch (IOException e)
    {
    log.info("There seems to be an issue");
    }

    log = this.getLogger();

    return false;
    }



    public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[] args)
    {
    Player player = null;
    if (sender instanceof Player)
    {
    player = (Player) sender;
    }
    log = this.getLogger();
    return false;
    }


    public void onDisable(){
    log.info("Rule Accepter is now off.");
    }





    }
     
  7. Offline

    r0306

    mason1370
    You have two File's. Remove one of them.
    Code:
    File File file = new File("path")
     
  8. Offline

    mason1370

    ok there is only 2 files so it says
    File file = new File("path");
    But it is still saying File cannot be resolved to variable
    Code:
    package com.gmail.mason1224;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.lang.reflect.Array;
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
     
    public class turnon extends JavaPlugin {
        boolean test = false;
     
        Logger log;
     
        public void onEnable(){
            log = this.getLogger();
            log.info("Rule Accepter is now running!");
        }
     
        Logger log1;
       
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player test1 = event.getPlayer();
         
            try
            {
                Player player =
                //your player
                File file = new File("path");
                //default directory is the craftbukkit folder
                BufferedReader br = new BufferedReader(new FileReader(File));
                String l;
                while((l=br.readLine()) != null)
                {
                    if(l.equalsIgnoreCase(player.getName()))//if the player's name is the same as the string...
                    {
                        //do something
                    }
                }
           
            }
            catch(IOException ioe)
            {
                //error
            }
    }
     
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
        Player player = null;
        if (sender instanceof Player)
        {
        player = (Player) sender;
        }
        try{
        BufferedWriter out = new BufferedWriter(new FileWriter("test.txt", true));
        out.write(sender.getName() + System.getProperty( "line.separator" ));
        out.close();
        }
        catch (IOException e)
        {
        log.info("There seems to be an issue");
        }
       
        log = this.getLogger();
       
        return false;
        }
     
     
       
        public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {       
        Player player = null;
        if (sender instanceof Player)
            {
            player = (Player) sender;
            }   
        log = this.getLogger();             
        return false;
        }
       
       
    public void onDisable(){
        log.info("Rule Accepter is now off.");
    }
     
     
     
     
     
    }
     
        //sender.sendMessage
        //
    
     
  9. At the line where it says: File file = new File("path"); you have to fill in your own path to the file, not just "path", because that is most likely not a file in your main directory of your test server.
    greetz blackwolf12333

    btw don't you have to implement the Listener class?
     
  10. Offline

    Technius

    When you initialize your BufferedReader...
    Code:java
    1. BufferedReader br = new BufferedReader(new FileReader(File));//<- You put "File"..... It's "file"...... *facepalm*

    You provided "File", not your variable, "file".
     
  11. Offline

    mason1370

    @Technius

    @blackwolf12333
    So all I am needing to do is paste this
    C:\Users\Masons\Desktop\CraftBukkit
    On those 2 places? Because when i do that it gives me even more fun errors that are probably my fault again :)
    Code:
        public class LoginListener implements Listener {
            @EventHandler
            public void normalLogin(PlayerLoginEvent event) {
            Player test1 = event.getPlayer();
     
                try
                    {
                        Player player =
                                    //your player
                                    File file = new File(C:\Users\Masons\Desktop\CraftBukkit);
                            //default directory is the craftbukkit folder
                            BufferedReader br = new BufferedReader(new FileReader(C:\Users\Masons\Desktop\CraftBukkit));
                                String l;
                                    while((l=br.readLine()) != null)
                                        {
                                            if(l.equalsIgnoreCase(player.getName()))
                    {
                                                    //do something
                                        }
                }
     
            }
            catch(IOException ioe)
            {
                //error
            }
            }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page