Added check to see if the user exists before appending file. Also borrowed some code from hMod to tweak into functions for vMinecraftUsers, also put in placeholders for things I'm going to write soon. :P

This commit is contained in:
nossr50 2010-12-02 00:06:03 -08:00
parent 76cadca12f
commit 34612f93d1

View File

@ -2,6 +2,7 @@ import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Scanner;
public class vMinecraftUsers {
private static volatile vMinecraftUsers instance;
@ -17,7 +18,7 @@ public class vMinecraftUsers {
try {
writer = new FileWriter(location);
writer.write("#Storage place for user information\r\n");
writer.write("#username:nickname:suffix:ignore,list,names:alias,commands,here\r\n");
writer.write("#username:nickname:suffix:tag:ignore,list,names:alias,commands,here\r\n");
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while creating " + location, e);
} finally {
@ -39,9 +40,64 @@ public class vMinecraftUsers {
}
}
}
public boolean doesPlayerExist(String player) {
try {
Scanner scanner = new Scanner(new File(location));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
continue;
}
String[] split = line.split(":");
if (!split[0].equalsIgnoreCase(player)) {
continue;
}
return true;
}
scanner.close();
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
}
return false;
}
public Player getPlayer(String name) {
Player player = new Player();
try {
Scanner scanner = new Scanner(new File(location));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
continue;
}
String[] split = line.split(":");
if (!split[0].equalsIgnoreCase(name)) {
continue;
}
if (split.length >= 2) {
//player.setNickname(split[1]
}
if (split.length >= 3) {
//player.setSuffix(split[2]);
}
if (split.length >= 5) {
//player.setIgnoreList(split[4].split(","));
}
if (split.length >= 6) {
//player.setAlias(split[5].split(","));
}
}
scanner.close();
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
}
return player;
}
public static void addUser(Player player){
FileWriter writer = null;
String location = "vminecraftusers.txt";
String playerName = player.getName();
if (vMinecraftUsers.getInstance().doesPlayerExist(playerName)){ //Check to see if the player exists before writing
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
bw.append(player.getName()+"::::\r");
@ -58,7 +114,7 @@ public class vMinecraftUsers {
log.log(Level.SEVERE, "Exception while closing BufferedWriter to " + location, e);
}
}
}
}
public static vMinecraftUsers getInstance() {
if (instance == null) {