2010-12-01 19:59:51 +01:00
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
2010-12-02 09:06:03 +01:00
|
|
|
|
import java.util.Scanner;
|
2010-12-01 22:15:37 +01:00
|
|
|
|
|
2010-12-01 19:59:51 +01:00
|
|
|
|
public class vMinecraftUsers {
|
|
|
|
|
private static volatile vMinecraftUsers instance;
|
|
|
|
|
protected static final Logger log = Logger.getLogger("Minecraft");
|
|
|
|
|
String file = "vminecraftusers.txt";
|
|
|
|
|
private PropertiesFile properties;
|
|
|
|
|
String location = "vminecraftusers.txt";
|
2010-12-02 21:45:24 +01:00
|
|
|
|
|
2010-12-08 05:00:32 +01:00
|
|
|
|
public static PlayerList players = new PlayerList();
|
2010-12-02 21:45:24 +01:00
|
|
|
|
|
|
|
|
|
|
2010-12-01 19:59:51 +01:00
|
|
|
|
public void loadUsers(){
|
|
|
|
|
File theDir = new File("vminecraftusers.txt");
|
|
|
|
|
if(!theDir.exists()){
|
|
|
|
|
properties = new PropertiesFile("vminecraftusers.txt");
|
|
|
|
|
FileWriter writer = null;
|
|
|
|
|
try {
|
|
|
|
|
writer = new FileWriter(location);
|
|
|
|
|
writer.write("#Storage place for user information\r\n");
|
2010-12-02 09:06:03 +01:00
|
|
|
|
writer.write("#username:nickname:suffix:tag:ignore,list,names:alias,commands,here\r\n");
|
2010-12-01 19:59:51 +01:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.log(Level.SEVERE, "Exception while creating " + location, e);
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (writer != null) {
|
|
|
|
|
writer.close();
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
properties = new PropertiesFile("vminecraftusers.txt");
|
|
|
|
|
try {
|
|
|
|
|
properties.load();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.log(Level.SEVERE, "Exception while loading vminecraftusers.txt", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-02 09:06:03 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2010-12-08 05:53:51 +01:00
|
|
|
|
|
2010-12-01 19:59:51 +01:00
|
|
|
|
public static void addUser(Player player){
|
2010-12-08 05:53:51 +01:00
|
|
|
|
players.addPlayer(player);
|
2010-12-01 19:59:51 +01:00
|
|
|
|
}
|
2010-12-08 05:53:51 +01:00
|
|
|
|
|
2010-12-01 19:59:51 +01:00
|
|
|
|
public static vMinecraftUsers getInstance() {
|
|
|
|
|
if (instance == null) {
|
|
|
|
|
instance = new vMinecraftUsers();
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2010-12-01 22:15:37 +01:00
|
|
|
|
public static void getRow(){
|
|
|
|
|
|
|
|
|
|
}
|
2010-12-01 19:59:51 +01:00
|
|
|
|
}
|
2010-12-02 07:17:42 +01:00
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Class: PlayerList
|
|
|
|
|
//Use: Encapsulates the player list
|
|
|
|
|
//Author: cerevisiae
|
|
|
|
|
//=====================================================================
|
|
|
|
|
class PlayerList
|
2010-12-02 22:42:59 +01:00
|
|
|
|
{
|
2010-12-08 10:47:42 +01:00
|
|
|
|
protected static final Logger log = Logger.getLogger("Minecraft");
|
2010-12-02 07:17:42 +01:00
|
|
|
|
ArrayList<PlayerProfile> players;
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: PlayerList
|
|
|
|
|
//Input: Player player: The player to create a profile object for
|
|
|
|
|
//Output: none
|
|
|
|
|
//Use: Initializes the ArrayList
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public PlayerList() { players = new ArrayList<PlayerProfile>(); }
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: addPlayer
|
|
|
|
|
//Input: Player player: The player to add
|
|
|
|
|
//Output: None
|
|
|
|
|
//Use: Add a profile of the specified player
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public void addPlayer(Player player)
|
|
|
|
|
{
|
|
|
|
|
players.add(new PlayerProfile(player));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: removePlayer
|
|
|
|
|
//Input: Player player: The player to remove
|
|
|
|
|
//Output: None
|
|
|
|
|
//Use: Remove the profile of the specified player
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public void removePlayer(Player player)
|
|
|
|
|
{
|
|
|
|
|
players.remove(findProfile(player));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: findProfile
|
|
|
|
|
//Input: Player player: The player to find's profile
|
|
|
|
|
//Output: PlayerProfile: The profile of the specified player
|
|
|
|
|
//Use: Get the profile for the specified player
|
|
|
|
|
//=====================================================================
|
2010-12-08 05:00:32 +01:00
|
|
|
|
public PlayerProfile findProfile(Player player)
|
2010-12-02 07:17:42 +01:00
|
|
|
|
{
|
|
|
|
|
for(PlayerProfile ply : players)
|
|
|
|
|
{
|
2010-12-08 05:00:32 +01:00
|
|
|
|
if(ply.isPlayer(player))
|
2010-12-02 07:17:42 +01:00
|
|
|
|
return ply;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Class: PlayerProfile
|
|
|
|
|
//Use: Encapsulates all commands for player options
|
|
|
|
|
//Author: cerevisiae
|
|
|
|
|
//=====================================================================
|
|
|
|
|
class PlayerProfile
|
|
|
|
|
{
|
2010-12-02 21:45:24 +01:00
|
|
|
|
protected final Logger log = Logger.getLogger("Minecraft");
|
2010-12-08 05:00:32 +01:00
|
|
|
|
private String playerName,
|
|
|
|
|
lastMessage,
|
|
|
|
|
nickName,
|
|
|
|
|
tag,
|
|
|
|
|
suffix;
|
2010-12-08 20:29:13 +01:00
|
|
|
|
private ArrayList<String> ignoreList;
|
2010-12-02 07:17:42 +01:00
|
|
|
|
private commandList aliasList;
|
|
|
|
|
|
|
|
|
|
static final int EXIT_FAIL = 0,
|
|
|
|
|
EXIT_SUCCESS = 1,
|
|
|
|
|
EXIT_CONTINUE = 2;
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: PlayerProfile
|
|
|
|
|
//Input: Player player: The player to create a profile object for
|
|
|
|
|
//Output: none
|
|
|
|
|
//Use: Loads settings for the player or creates them if they don't
|
|
|
|
|
// exist.
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public PlayerProfile(Player player)
|
|
|
|
|
{
|
2010-12-08 05:00:32 +01:00
|
|
|
|
//Declare things
|
2010-12-08 10:47:42 +01:00
|
|
|
|
playerName = player.getName();
|
2010-12-02 21:45:24 +01:00
|
|
|
|
nickName = new String();
|
|
|
|
|
tag = new String();
|
|
|
|
|
suffix = new String();
|
2010-12-08 20:29:13 +01:00
|
|
|
|
ignoreList = new ArrayList<String>();
|
2010-12-08 05:00:32 +01:00
|
|
|
|
aliasList = new commandList();
|
2010-12-02 21:45:24 +01:00
|
|
|
|
String location = "vminecraftusers.txt";
|
2010-12-08 05:00:32 +01:00
|
|
|
|
|
2010-12-02 21:45:24 +01:00
|
|
|
|
//Try to apply what we can
|
|
|
|
|
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(":");
|
2010-12-08 05:00:32 +01:00
|
|
|
|
|
|
|
|
|
//If the player name is equal to the name in the list
|
|
|
|
|
if (split.length > 0 && split[0].equalsIgnoreCase(player.getName())) {
|
|
|
|
|
|
|
|
|
|
//Get the tag from the 1st split
|
2010-12-08 20:29:13 +01:00
|
|
|
|
if (split.length >= 2)
|
|
|
|
|
nickName = split[1];
|
2010-12-08 05:00:32 +01:00
|
|
|
|
|
|
|
|
|
//Get the tag from the 2nd split
|
2010-12-08 20:29:13 +01:00
|
|
|
|
if (split.length >= 3)
|
|
|
|
|
suffix = split[2];
|
2010-12-08 05:00:32 +01:00
|
|
|
|
|
|
|
|
|
//Get the tag from the 3rd split
|
2010-12-08 20:29:13 +01:00
|
|
|
|
if (split.length >= 4)
|
2010-12-08 05:00:32 +01:00
|
|
|
|
tag = (split[3]);
|
|
|
|
|
|
|
|
|
|
//Add all the ignored people to the player's ignore list
|
|
|
|
|
if (split.length >= 5) {
|
|
|
|
|
for(String name : split[4].split(","))
|
2010-12-08 20:29:13 +01:00
|
|
|
|
ignoreList.add(name);
|
2010-12-08 05:00:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Get the alias list, from the 5th split
|
|
|
|
|
if (split.length >= 6) {
|
|
|
|
|
//Loop through all the aliases
|
|
|
|
|
for(String alias : split[5].split(","))
|
|
|
|
|
{
|
|
|
|
|
//Break apart the two parts of the alias
|
|
|
|
|
String[] parts = alias.split("@");
|
|
|
|
|
if(parts.length > 1)
|
|
|
|
|
{
|
|
|
|
|
//Register the alias to the player's aliasList
|
2010-12-08 10:59:38 +01:00
|
|
|
|
aliasList.registerAlias(parts[0], parts[2]);
|
2010-12-08 05:00:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2010-12-02 21:45:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
scanner.close();
|
|
|
|
|
} catch (Exception e) {
|
2010-12-08 05:00:32 +01:00
|
|
|
|
log.log(Level.SEVERE, "Exception while reading "
|
|
|
|
|
+ location + " (Are you sure you formatted it correctly?)", e);
|
2010-12-02 21:45:24 +01:00
|
|
|
|
}
|
2010-12-08 20:29:13 +01:00
|
|
|
|
save();
|
2010-12-02 22:46:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
2010-12-02 22:42:59 +01:00
|
|
|
|
// Function: save
|
|
|
|
|
// Input: none
|
2010-12-02 22:46:50 +01:00
|
|
|
|
// Output: None
|
|
|
|
|
// Use: Writes current values of PlayerProfile to disk
|
|
|
|
|
// Call this function to save current values
|
2010-12-02 22:42:59 +01:00
|
|
|
|
//=====================================================================
|
|
|
|
|
public void save(){
|
|
|
|
|
try {
|
|
|
|
|
String location = "vminecraftusers.txt";
|
|
|
|
|
BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
|
|
|
|
|
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(":");
|
2010-12-08 05:00:32 +01:00
|
|
|
|
if (!split[0].equalsIgnoreCase(playerName)) {
|
2010-12-02 22:42:59 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-12-08 20:29:13 +01:00
|
|
|
|
String output =playerName + ":" + nickName + ":" + suffix + ":" + tag + ":";
|
|
|
|
|
for(String player : ignoreList)
|
|
|
|
|
output += player + ",";
|
|
|
|
|
output += ":";
|
|
|
|
|
bw.write(output);
|
2010-12-02 22:42:59 +01:00
|
|
|
|
}
|
|
|
|
|
scanner.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
String location = "vminecraftusers.txt";
|
|
|
|
|
log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
|
|
|
|
|
}
|
2010-12-02 07:17:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
2010-12-08 05:00:32 +01:00
|
|
|
|
//Function: isPlayer
|
2010-12-02 07:17:42 +01:00
|
|
|
|
//Input: None
|
|
|
|
|
//Output: Player: The player this profile belongs to
|
2010-12-08 05:00:32 +01:00
|
|
|
|
//Use: Finds if this profile belongs to a specified player
|
2010-12-02 07:17:42 +01:00
|
|
|
|
//=====================================================================
|
2010-12-08 05:00:32 +01:00
|
|
|
|
public boolean isPlayer(Player player)
|
|
|
|
|
{
|
|
|
|
|
return player.getName().equals(playerName);
|
|
|
|
|
}
|
2010-12-02 07:17:42 +01:00
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: isIgnored
|
|
|
|
|
//Input: Player player: Checks if a player is ignored
|
|
|
|
|
//Output: boolean: If they're ignored
|
|
|
|
|
//Use: Finds if the specified player is in the ignore list
|
|
|
|
|
//=====================================================================
|
2010-12-08 20:29:13 +01:00
|
|
|
|
public boolean isIgnored(Player player){
|
2010-12-09 01:30:26 +01:00
|
|
|
|
return ignoreList.contains(player.getName());
|
|
|
|
|
}
|
2010-12-02 07:17:42 +01:00
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: addIgnore
|
|
|
|
|
//Input: Player name: The player to ignore
|
2010-12-08 20:29:13 +01:00
|
|
|
|
//Output: boolean: If the player was successfully ignored
|
2010-12-02 07:17:42 +01:00
|
|
|
|
//Use: Ignores a player.
|
|
|
|
|
//=====================================================================
|
2010-12-08 20:29:13 +01:00
|
|
|
|
public boolean addIgnore(Player name)
|
2010-12-02 07:17:42 +01:00
|
|
|
|
{
|
|
|
|
|
if(!ignoreList.contains(name))
|
2010-12-08 20:29:13 +01:00
|
|
|
|
{
|
|
|
|
|
ignoreList.add(name.getName());
|
|
|
|
|
save();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2010-12-02 07:17:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: removeIgnore
|
2010-12-08 20:29:13 +01:00
|
|
|
|
//Input: Player name: The player to unignore
|
|
|
|
|
//Output: boolean: If the player was successfully unignored
|
|
|
|
|
//Use: Stops ignoring a player.
|
2010-12-02 07:17:42 +01:00
|
|
|
|
//=====================================================================
|
2010-12-08 20:29:13 +01:00
|
|
|
|
public boolean removeIgnore(Player name)
|
2010-12-02 07:17:42 +01:00
|
|
|
|
{
|
2010-12-08 20:29:13 +01:00
|
|
|
|
if(ignoreList.contains(name.getName()))
|
|
|
|
|
{
|
|
|
|
|
ignoreList.remove(name.getName());
|
|
|
|
|
save();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2010-12-02 07:17:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: addAlias
|
|
|
|
|
//Input: String command: The command to try to call
|
|
|
|
|
// String[] args: The arguments for the command
|
|
|
|
|
//Output: None
|
|
|
|
|
//Use: Adds a command
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public void addAlias(String name, String callCommand)
|
|
|
|
|
{
|
|
|
|
|
aliasList.registerAlias(name, callCommand);
|
2010-12-08 20:29:13 +01:00
|
|
|
|
save();
|
2010-12-02 07:17:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: callAlias
|
|
|
|
|
//Input: String command: The command to try to call
|
|
|
|
|
// Player player: Checks if a player is ignored
|
|
|
|
|
// String[] args: The arguments for the command
|
|
|
|
|
//Output: int: Exit code
|
|
|
|
|
//Use: Attempts to call a command
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public int callAlias(String command, Player player, String[] args)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//Attemt to call the function
|
|
|
|
|
return aliasList.call(command, player, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Throwable e)
|
|
|
|
|
{
|
|
|
|
|
//The function wasn't found, returns fail
|
|
|
|
|
return EXIT_FAIL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: setTag
|
|
|
|
|
//Input: String newTag: The tag to set for the player
|
|
|
|
|
//Output: None
|
|
|
|
|
//Use: Sets a player tag
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public void setTag(String newTag){ tag = newTag; }
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: getTag
|
|
|
|
|
//Input: None
|
|
|
|
|
//Output: String: The player tag
|
|
|
|
|
//Use: Gets a player tag
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public String getTag() { return tag; }
|
2010-12-08 05:00:32 +01:00
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: setMessage
|
|
|
|
|
//Input: String newName: The name of the player they last messaged
|
|
|
|
|
// or recieved a message from.
|
|
|
|
|
//Output: None
|
|
|
|
|
//Use: Sets a player tag
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public void setMessage(Player newName){ lastMessage = newName.getName(); }
|
|
|
|
|
|
|
|
|
|
//=====================================================================
|
|
|
|
|
//Function: getMessage
|
|
|
|
|
//Input: None
|
|
|
|
|
//Output: String: The player name
|
|
|
|
|
//Use: Gets the name of the player they last messaged or recieved
|
|
|
|
|
// a message from.
|
|
|
|
|
//=====================================================================
|
|
|
|
|
public Player getMessage()
|
|
|
|
|
{
|
2010-12-08 10:47:42 +01:00
|
|
|
|
if(lastMessage != null)
|
|
|
|
|
return etc.getServer().matchPlayer(lastMessage);
|
|
|
|
|
return null;
|
2010-12-08 05:00:32 +01:00
|
|
|
|
}
|
2010-12-02 07:17:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-02 23:13:23 +01:00
|
|
|
|
|