Question

I'm writing a Bukkit (for those that don't know: "Mod and plugins that simplify running and managing Minecraft servers. Includes forums and documentation.") plugin that stores user's commands into their own respective files. I feel like the code I've written is extremely inefficient.

Every time the PlayerCommandPreprocessEvent fires (every time a message with "/" at the beginning is inputted), I create a variable like so...

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fM.retrievePlayerFile(e.getPlayer().getName()), true)))

Then I write to that specific player's file, and then close the output.

Inefficient? Yeah, I think so....

Is there a simpler, more efficient way to do this?

Thanks in advance.

Edit: fM.retrievePlayerFile(Player player) is this:

public File retrievePlayerFile(String player) {
    player = player.toLowerCase();
    return (new File(plugin.getDataFolder(), "/users/" + player));
}
Was it helpful?

Solution

If you could not open and close the file each time, that could speed things up.
Maybe you can keep it open all the time (not sure of the your exact scenario).

If not, you can try other optimization approaches.

1) You can close it when the user logs out. 2) You can use some heuristics and close it if the user has no activity for N minutes. 2) A possible idea is the keep only those open which are really most active. You can track somehow the users' activity over the last M minutes, take the top K users, and keep only their files open.

The code itself is not that inefficient, at least I don't see how you can improve the writing to the file that much. I would advise you change a bit your overall algorithm/design and try to improve there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top