mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-25 14:46:46 +01:00
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:
parent
76cadca12f
commit
0e171e8ecc
@ -2,6 +2,7 @@ import java.io.*;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class vMinecraftUsers {
|
public class vMinecraftUsers {
|
||||||
private static volatile vMinecraftUsers instance;
|
private static volatile vMinecraftUsers instance;
|
||||||
@ -17,7 +18,7 @@ public class vMinecraftUsers {
|
|||||||
try {
|
try {
|
||||||
writer = new FileWriter(location);
|
writer = new FileWriter(location);
|
||||||
writer.write("#Storage place for user information\r\n");
|
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) {
|
} catch (Exception e) {
|
||||||
log.log(Level.SEVERE, "Exception while creating " + location, e);
|
log.log(Level.SEVERE, "Exception while creating " + location, e);
|
||||||
} finally {
|
} finally {
|
||||||
@ -39,12 +40,67 @@ 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){
|
public static void addUser(Player player){
|
||||||
FileWriter writer = null;
|
FileWriter writer = null;
|
||||||
String location = "vminecraftusers.txt";
|
String location = "vminecraftusers.txt";
|
||||||
|
String playerName = player.getName();
|
||||||
|
if (vMinecraftUsers.getInstance().doesPlayerExist(playerName)){ //Check to see if the player exists before writing
|
||||||
try {
|
try {
|
||||||
BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
|
BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
|
||||||
bw.append(player.getName()+"::::\r");
|
bw.append(player.getName()+":::::\r");
|
||||||
bw.newLine();
|
bw.newLine();
|
||||||
bw.close();
|
bw.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -58,7 +114,7 @@ public class vMinecraftUsers {
|
|||||||
log.log(Level.SEVERE, "Exception while closing BufferedWriter to " + location, e);
|
log.log(Level.SEVERE, "Exception while closing BufferedWriter to " + location, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public static vMinecraftUsers getInstance() {
|
public static vMinecraftUsers getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user