问题描述:

So right now I'm making a mod in Minecraft where it takes everyones username from a server and adds it to a txt file, it works but the the problem is I don't want to duplicate the names when I use the command again. Nothing has worked so far. How would I check if the txt already contains the username, don't add it again? Thank you. Again, I need it to before writing another name to the list, check the txt file if it already contains the name, if so don't add it.

for (int i = 0; i < minecraft.thePlayer.sendQueue.playerInfoList.size(); i++) {

List playerList = minecraft.thePlayer.sendQueue.playerInfoList;

GuiPlayerInfo playerInfo = (GuiPlayerInfo) playerList.get(i);

String playerName = StringUtils.stripControlCodes(playerInfo.name);

try {

fileWriter = new FileWriter(GameDirectory() + "\\scraped.txt", true);

bufferedReader = new BufferedReader(new FileReader(GameDirectory() + "\\scraped.txt"));

lineNumberReader = new LineNumberReader(new FileReader(GameDirectory() + "\\scraped.txt"));

} catch (IOException e) {

e.printStackTrace();

}

printWriter = new PrintWriter(fileWriter);

try {

fileWriter.write(playerName + "\r\n");

lineNumberReader.skip(Long.MAX_VALUE);

} catch (IOException e) {

e.printStackTrace();

}

printWriter.flush();

}

addMessage("Scraped " + lineNumberReader.getLineNumber() + " usernames!");

EDIT: Really need an answer guys :( Thank you

EDIT: this is what I have now, but it's not even writing it anymore.

List playerList = minecraft.thePlayer.sendQueue.playerInfoList;

for (int i = 0; i < minecraft.thePlayer.sendQueue.playerInfoList.size(); i++) {

GuiPlayerInfo playerInfo = (GuiPlayerInfo) playerList.get(i);

String playerName = StringUtils.stripControlCodes(playerInfo.name);

String lines;

try {

if ((lines = bufferedReader.readLine()) != null) {

if (!lines.contains(playerName)) {

bufferedWriter.write(playerName);

bufferedWriter.newLine();

bufferedWriter.flush();

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

int linenumber = 0;

try {

while (lineNumberReader.readLine() != null) {

linenumber++;

}

} catch (IOException e) {

e.printStackTrace();

}

网友答案:

The logic of your second piece of code is wrong. If you write out the pseudo-code of it, it's easy to see why:

Open a File Reader at the start of the file

For every Player on the server
    Save the player name
    Read the next line of the file
        If we have not reached the end of the file
            If the player name is not on this line of the file
                Write the name of the player to the file

You need to read the entire file outside of the loop, and then check if the player exists anywhere in the file, not just if it happens to be on the line which is the same position as the player on the server.

The easiest way to do this is to keep the players in a list while you're processing, and read/write them to file, like this:

public static List<String> loadPlayerList() throws FileNotFoundException
{
    final Scanner scanner = new Scanner(new File(GameDirectory() + "\\scraped.txt"));
    final List<String> players = new ArrayList<>();
    while(scanner.hasNextLine())
        players.add(scanner.nextLine());
    return players;
}

public static void writePlayersList(final List<String> players) throws IOException
{
    try(final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream((GameDirectory() + "\\scraped.txt")))))
    {
        for(final String player : players)
        {
            writer.write(player);
            writer.newLine();
        }
    }
}

public static void main(String[] args) throws IOException
{
    final List<String> players = loadPlayerList();
    for(final GuiPlayerInfo player : minecraft.thePlayer.sendQueue.playerInfoList)
    {
        final String playerName = StringUtils.stripControlCodes(player.name);
        if(!players.contains(playerName))
            players.add(playerName);
    }
    writePlayersList(players);
}
相关阅读:
Top