Fixed the flatfile system.

This commit is contained in:
cerevisiae 2010-12-12 13:40:39 -06:00
parent 263a5701d7
commit ebdda3afc0
4 changed files with 285 additions and 137 deletions

2
TODO
View File

@ -1,5 +1,4 @@
Vminecraft b8 Todo: Vminecraft b8 Todo:
+ Finish work on the flat file system
+ Antigriefs <Nos> Working on this + Antigriefs <Nos> Working on this
? Time manipulation <Cere> Working on this ? Time manipulation <Cere> Working on this
* Have time changes not be instant but move the sky faster * Have time changes not be instant but move the sky faster
@ -47,5 +46,6 @@ DONE
+ Allow players to nickname themselves or others + Allow players to nickname themselves or others
+ Allow players to set suffixes + Allow players to set suffixes
+ ^r for rainbow color code + ^r for rainbow color code
+ Finish work on the flat file system
Notes: Let's try to to finish as much of this list as possible tomorrow and push for a b8 release soon :P Notes: Let's try to to finish as much of this list as possible tomorrow and push for a b8 release soon :P

View File

@ -11,7 +11,7 @@ public class vMinecraft extends Plugin {
public void enable() { public void enable() {
vMinecraftSettings.getInstance().loadSettings(); vMinecraftSettings.getInstance().loadSettings();
vMinecraftUsers.getInstance().loadUsers(); vMinecraftUsers.getInstance().loadUsers();
vMinecraftCommands.loadCommands(); vMinecraftCommands.loadCommands();
} }

View File

@ -65,7 +65,7 @@ public class vMinecraftCommands{
cl.register("/slay", "slay", "Kill target player"); cl.register("/slay", "slay", "Kill target player");
//Social //Social
cl.register("/colors", "colors"); cl.register("/colors", "colors", "Set your default chat color: /colors <Color Char>");
cl.register("/me", "me"); cl.register("/me", "me");
cl.register("/fabulous", "fabulous", "makes text SUUUPER"); cl.register("/fabulous", "fabulous", "makes text SUUUPER");
cl.register("/msg", "message", "Send a message to a player /msg [Player] [Message]"); cl.register("/msg", "message", "Send a message to a player /msg [Player] [Message]");
@ -521,28 +521,36 @@ public class vMinecraftCommands{
//Use: Displays a list of all colors and color codes //Use: Displays a list of all colors and color codes
//===================================================================== //=====================================================================
public static int colors(Player player, String[] args){ public static int colors(Player player, String[] args){
player.sendMessage(Colors.Rose + "You use these color codes like in quake or MW2."); if(args.length > 0)
player.sendMessage(Colors.Rose + "^4 would make text " + Colors.Red {
+ "red" + Colors.Rose + ", ^a would make it " + Colors.LightGreen vMinecraftUsers.getProfile(player).setColor(args[0]);
+ "light green" + Colors.Rose + "."); vMinecraftChat.sendMessage(player, player,
vMinecraftChat.sendMessage(player, player, vMinecraftChat.colorChange(args[0].charAt(0))
Colors.Black + "0" + "Default chat color set.");
+ Colors.Navy + "1" } else {
+ Colors.Green + "2" player.sendMessage(Colors.Rose + "You use these color codes like in quake or MW2.");
+ Colors.Blue + "3" player.sendMessage(Colors.Rose + "^4 would make text " + Colors.Red
+ Colors.Red + "4" + "red" + Colors.Rose + ", ^a would make it " + Colors.LightGreen
+ Colors.Purple + "5" + "light green" + Colors.Rose + ".");
+ Colors.Gold + "6" vMinecraftChat.sendMessage(player, player,
+ Colors.LightGray + "7" Colors.Black + "0"
+ Colors.Gray + "8" + Colors.Navy + "1"
+ Colors.DarkPurple + "9" + Colors.Green + "2"
+ Colors.LightGreen + "a" + Colors.Blue + "3"
+ Colors.LightBlue + "b" + Colors.Red + "4"
+ Colors.Rose + "c" + Colors.Purple + "5"
+ Colors.LightPurple + "d" + Colors.Gold + "6"
+ Colors.Yellow + "e" + Colors.LightGray + "7"
+ Colors.White + "f" + Colors.Gray + "8"
+ "^r" + "rrrrrrrrrrr"); + Colors.DarkPurple + "9"
+ Colors.LightGreen + "a"
+ Colors.LightBlue + "b"
+ Colors.Rose + "c"
+ Colors.LightPurple + "d"
+ Colors.Yellow + "e"
+ Colors.White + "f"
+ "^r" + "rrrrrrrrrrr");
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -1292,22 +1300,41 @@ public class vMinecraftCommands{
//===================================================================== //=====================================================================
public static int modify(Player player, String[] args) public static int modify(Player player, String[] args)
{ {
if(player.canUseCommand("/prefix")) if(player.canUseCommand("/prefixother"))
vMinecraftChat.sendMessage(player, player, "/prefix [Player]" +
" [Color] (Tag) - Set a players prefix and tag.");
else if(player.canUseCommand("/prefix"))
vMinecraftChat.sendMessage(player, player, "/prefix [Color]" + vMinecraftChat.sendMessage(player, player, "/prefix [Color]" +
" (Tag) - Set your prefix and tag."); " (Tag) - Set your prefix and tag.");
return EXIT_SUCCESS;
} if(player.canUseCommand("/nickother"))
vMinecraftChat.sendMessage(player, player, "/nick [Player]" +
//===================================================================== " [Nickname] - Set a players nickname.");
//Function: Time Reverse else if(player.canUseCommand("/nick"))
//Input: long time: The time to reverse to. vMinecraftChat.sendMessage(player, player, "/nick [Nick]" +
//Output: int: Exit Code " - Set your nickname.");
//Use: List all invulnerable players
//===================================================================== if(player.canUseCommand("/suffixother"))
public static int timeReverse(long tarTime) vMinecraftChat.sendMessage(player, player, "/suffix [Player]" +
{ " [Suffix] - Set a players suffix.");
long curTime = etc.getServer().getRelativeTime(); else if(player.canUseCommand("/suffix"))
//if(cur) vMinecraftChat.sendMessage(player, player, "/suffix [Suffix]" +
" - Set your suffix.");
if(player.canUseCommand("/suffixother"))
vMinecraftChat.sendMessage(player, player, "/suffix [Player]" +
" [Suffix] - Set a players suffix.");
else if(player.canUseCommand("/suffix"))
vMinecraftChat.sendMessage(player, player, "/suffix [Suffix]" +
" - Set your suffix.");
if(player.canUseCommand("/vranks"))
{
vMinecraftChat.sendMessage(player, player, "/promote [Player]" +
" - Promotes a player");
vMinecraftChat.sendMessage(player, player, "/demote [Player]" +
" - Demotes a player");
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
} }
@ -1417,6 +1444,8 @@ class commandList {
//===================================================================== //=====================================================================
//Function: call //Function: call
//Input: String name: The name of the command to be run //Input: String name: The name of the command to be run
// Player player: The player calling the command
// String[] arg: The arguments being input for the command
//Output: boolean: If the command was called successfully //Output: boolean: If the command was called successfully
//Use: Attempts to call a command //Use: Attempts to call a command
//===================================================================== //=====================================================================
@ -1442,6 +1471,25 @@ class commandList {
//Something went wrong //Something went wrong
return EXIT_FAIL; return EXIT_FAIL;
} }
//=====================================================================
//Function: toString
//Input: None
//Output: String: A string representation of the aliases in the list
//Use: Displays all the aliases in thel ist
//=====================================================================
public String toString()
{
String temp = "";
int i = 0;
for(command comm : commands)
{
temp += comm.toString();
if(i < commands.size() - 1)
temp +=",";
}
return temp;
}
@ -1503,6 +1551,14 @@ class commandList {
} }
return 1; return 1;
} }
//=====================================================================
//Function: toString
//Input: None
//Output: String: null
//Use: Returns null
//=====================================================================
public String toString() { return null; }
} }
//===================================================================== //=====================================================================
@ -1586,6 +1642,22 @@ class commandList {
player.command(reference); player.command(reference);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
//=====================================================================
//Function: toString
//Input: None
//Output: String: A string representation of this command.
// command@referencedcommand arg1 arg2 argn
//Use: Returns the string representation of the alias
//=====================================================================
public String toString()
{
String temp = getName();
temp += '@';
temp += reference;
temp += etc.combineSplit(0, args, " ");
return temp;
}
} }
//===================================================================== //=====================================================================
@ -1684,5 +1756,13 @@ class commandList {
} }
return EXIT_FAIL; return EXIT_FAIL;
} }
//=====================================================================
//Function: toString
//Input: None
//Output: String: null
//Use: Returns null
//=====================================================================
public String toString() { return null; }
} }
} }

View File

@ -2,7 +2,6 @@ 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;
@ -21,7 +20,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:tag: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 {
@ -43,26 +42,6 @@ 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;
}
//===================================================================== //=====================================================================
//Function: addUser //Function: addUser
@ -163,8 +142,9 @@ class PlayerList
lastMessage, lastMessage,
nickName, nickName,
tag, tag,
suffix, suffix;
defaultColor;
char defaultColor;
String location = "vminecraft.users"; String location = "vminecraft.users";
@ -186,66 +166,78 @@ class PlayerList
{ {
//Declare things //Declare things
playerName = player.getName(); playerName = player.getName();
nickName = new String();
tag = new String(); tag = new String();
nickName = new String();
suffix = new String(); suffix = new String();
defaultColor = 'f';
ignoreList = new ArrayList<String>(); ignoreList = new ArrayList<String>();
aliasList = new commandList(); aliasList = new commandList();
//Try to apply what we can //Try to load the player and if they aren't found, append them
try { if(!load())
Scanner scanner = new Scanner(new File(location)); addPlayer();
while (scanner.hasNextLine()) { }
String line = scanner.nextLine();
if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
continue;
}
String[] split = line.split(":");
//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
if (split.length >= 2)
nickName = split[1];
//Get the tag from the 2nd split
if (split.length >= 3)
suffix = split[2];
//Get the tag from the 3rd split public boolean load()
if (split.length >= 4) {
tag = (split[3]); try {
//Open the user file
//Add all the ignored people to the player's ignore list FileReader file = new FileReader(location);
if (split.length >= 5) { BufferedReader in = new BufferedReader(file);
for(String name : split[4].split(",")) String line = "";
ignoreList.add(name); while((line = in.readLine()) != null)
} {
//Find if the line contains the player we want.
//Get the alias list, from the 5th split String[] character = line.split(":");
if (split.length >= 6) { if(!character[0].equals(playerName)){continue;}
//Loop through all the aliases
for(String alias : split[5].split(",")) //Get the tag
{ if(character.length > 1)
//Break apart the two parts of the alias tag = character[1];
String[] parts = alias.split("@"); //Get the nickname
if(parts.length > 1) if(character.length > 2)
{ nickName = character[2];
//Register the alias to the player's aliasList //Get the suffix
aliasList.registerAlias(parts[0], parts[2]); if(character.length > 3)
} suffix = character[3];
} //Get the color
} if(character.length > 4)
break; defaultColor = character[4].charAt(0);
} //Ignore previously ignored players
} if(character.length > 5)
scanner.close(); {
String[] ignores = character[5].split(",");
if(ignores.length > 0)
{
for(String ignore : ignores)
ignoreList.add(ignore);
}
}
//Register the aliases
if(character.length > 6)
{
String[] allAliases = character[6].split(",");
if(allAliases.length > 0)
{
for(String singleAlias : allAliases)
{
String[] parts = singleAlias.split("@");
if(parts.length > 1)
{
aliasList.registerAlias(parts[0], parts[1]);
}
}
}
}
in.close();
return true;
}
in.close();
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading " log.log(Level.SEVERE, "Exception while reading "
+ location + " (Are you sure you formatted it correctly?)", e); + location + " (Are you sure you formatted it correctly?)", e);
} }
save(); return false;
} }
//===================================================================== //=====================================================================
@ -255,30 +247,90 @@ class PlayerList
// Use: Writes current values of PlayerProfile to disk // Use: Writes current values of PlayerProfile to disk
// Call this function to save current values // Call this function to save current values
//===================================================================== //=====================================================================
public void save(){ public void save()
{
try { try {
BufferedWriter bw = new BufferedWriter(new FileWriter(location, true)); //Open the file
Scanner scanner = new Scanner(new File(location)); FileReader file = new FileReader(location);
while (scanner.hasNextLine()) { BufferedReader in = new BufferedReader(file);
String line = scanner.nextLine(); StringBuilder writer = new StringBuilder();
if (line.startsWith("#") || line.equals("") || line.startsWith("")) { String line = "";
continue;
} //While not at the end of the file
String[] split = line.split(":"); while((line = in.readLine()) != null)
if (!split[0].equalsIgnoreCase(playerName)) { {
continue; //Read the line in and copy it to the output it's not the player
} //we want to edit
String output =playerName + ":" + nickName + ":" + suffix + ":" + tag + ":"; if(!line.split(":")[0].equalsIgnoreCase(playerName))
for(String player : ignoreList) {
output += player + ","; writer.append(line).append("\r\n");
output += ":";
bw.write(output); //Otherwise write the new player information
} } else {
scanner.close(); writer.append(playerName + ":");
writer.append(tag + ":");
writer.append(nickName + ":");
writer.append(suffix + ":");
writer.append(defaultColor + ":");
int i = 0;
for(String ignore : ignoreList)
{
writer.append(ignore);
if(i < ignoreList.size() - 1)
writer.append(",");
}
writer.append(":");
writer.append(aliasList.toString());
writer.append("\r\n");
}
}
in.close();
//Write the new file
FileWriter out = new FileWriter(location);
out.write(writer.toString());
out.close();
} catch (Exception e) { } catch (Exception e) {
log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e); log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
} }
} }
public void addPlayer()
{
try {
//Open the file to write the player
FileWriter file = new FileWriter(location);
BufferedWriter out = new BufferedWriter(file);
//Add the player to the end
out.append(playerName + ":");
out.append(tag + ":");
out.append(nickName + ":");
out.append(suffix + ":");
out.append(defaultColor + ":");
int i = 0;
for(String ignore : ignoreList)
{
out.append(ignore);
if(i < ignoreList.size() - 1)
out.append(",");
}
out.append(":");
out.append(aliasList.toString());
out.append("\r\n");
out.close();
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
}
}
//===================================================================== //=====================================================================
//Function: isPlayer //Function: isPlayer
@ -387,7 +439,11 @@ class PlayerList
//Output: None //Output: None
//Use: Sets a player tag //Use: Sets a player tag
//===================================================================== //=====================================================================
public void setTag(String newTag){ tag = newTag; } public void setTag(String newTag)
{
tag = newTag;
save();
}
//===================================================================== //=====================================================================
//Function: getTag //Function: getTag
@ -403,7 +459,11 @@ class PlayerList
//Output: None //Output: None
//Use: Sets a player nickname //Use: Sets a player nickname
//===================================================================== //=====================================================================
public void setNick(String newNick){ nickName = newNick; } public void setNick(String newNick)
{
nickName = newNick;
save();
}
//===================================================================== //=====================================================================
//Function: getNick //Function: getNick
@ -419,7 +479,11 @@ class PlayerList
//Output: None //Output: None
//Use: Sets a player suffix //Use: Sets a player suffix
//===================================================================== //=====================================================================
public void setSuffix(String newSuffix){ suffix = newSuffix; } public void setSuffix(String newSuffix)
{
suffix = newSuffix;
save();
}
//===================================================================== //=====================================================================
//Function: getSuffix //Function: getSuffix
@ -435,7 +499,11 @@ class PlayerList
//Output: None //Output: None
//Use: Sets a player color //Use: Sets a player color
//===================================================================== //=====================================================================
public void setColor(String newColor){ defaultColor = newColor; } public void setColor(String newColor)
{
defaultColor = newColor.charAt(0);
save();
}
//===================================================================== //=====================================================================
//Function: getColor //Function: getColor
@ -443,7 +511,7 @@ class PlayerList
//Output: String: The player color //Output: String: The player color
//Use: Gets a player color //Use: Gets a player color
//===================================================================== //=====================================================================
public String getColor() { return defaultColor; } public String getColor() {return vMinecraftChat.colorChange(defaultColor);}
//===================================================================== //=====================================================================
//Function: setMessage //Function: setMessage