Another WIP of 0.9.15 + vChat stuff

This commit is contained in:
nossr50
2011-04-01 12:34:00 -07:00
parent db79f9a33f
commit 7782c3c03c
26 changed files with 4491 additions and 1286 deletions

View File

@ -0,0 +1,37 @@
package com.gmail.nossr50.vChat;
import java.io.File;
import java.util.HashMap;
import org.bukkit.entity.Player;
import org.bukkit.Server;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
/**
* vChat for Bukkit
*
* @author nossr50
* @author cerevisae
*/
public class vChat extends JavaPlugin {
private final vPlayerListener playerListener = new vPlayerListener(this);
private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
public void onEnable() {
PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
PluginDescriptionFile pdfFile = this.getDescription();
System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
//Load the users file
vUsers.getInstance().loadUsers();
}
public void onDisable() {
System.out.println("vChat Disabled!");
}
}

View File

@ -0,0 +1,707 @@
package com.gmail.nossr50.vChat;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.plugin.Plugin;
import org.bukkit.ChatColor;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.gmail.nossr50.mcConfig;
import com.gmail.nossr50.mcMMO;
/**
* Handle events for all Player related events
* @author nossr50
*/
public class vPlayerListener extends PlayerListener {
private final vChat plugin;
protected static final Logger log = Logger.getLogger("Minecraft");
//The length of a text box line in pixels
protected static final int lineLength = 312;
//Characters we will split the line at
protected static final String lineSplit = "/- ";
public vPlayerListener(vChat instance) {
plugin = instance;
}
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
vUsers.addUser(player);
player.sendMessage(ChatColor.YELLOW+"This server is running vChat");
player.sendMessage(ChatColor.YELLOW+"Type /color or /prefix to do some thangs");
player.sendMessage(ChatColor.DARK_AQUA+"Currently running in Linux");
player.sendMessage(ChatColor.DARK_AQUA+"Steam community: vminecraft");
}
public Boolean isPlayer(String playername){
for(Player derp : plugin.getServer().getOnlinePlayers()){
if(derp.getName().toLowerCase().equals(playername.toLowerCase())){
return true;
}
}
return false;
}
//Special Color Codes
protected static final String[] rainbow = new String[] {
ChatColor.DARK_RED.toString(),
ChatColor.RED.toString(),
ChatColor.GOLD.toString(),
ChatColor.YELLOW.toString(),
ChatColor.GREEN.toString(),
ChatColor.DARK_GREEN.toString(),
ChatColor.BLUE.toString(),
ChatColor.DARK_BLUE.toString(),
ChatColor.AQUA.toString(),
ChatColor.DARK_AQUA.toString(),
ChatColor.DARK_PURPLE.toString(),
ChatColor.LIGHT_PURPLE.toString()
};
protected static final String[] xmas = new String[] {
ChatColor.DARK_RED.toString(),
ChatColor.DARK_RED.toString(),
ChatColor.WHITE.toString(),
ChatColor.WHITE.toString(),
ChatColor.DARK_GREEN.toString(),
ChatColor.DARK_GREEN.toString(),
};
public void onPlayerChat(PlayerChatEvent event) {
Player player = event.getPlayer();
String message = event.getMessage();
String split[] = event.getMessage().split(" ");
Player[] players = plugin.getServer().getOnlinePlayers();
Plugin tester = plugin.getServer().getPluginManager().getPlugin("mcMMO");
if (tester == null) {
} else {
try {
mcMMO plugin = (mcMMO)tester;
if (plugin.isPartyChatToggled(player) || plugin.isAdminChatToggled(player)) {
return;
} else {
if(split[0].startsWith(">"))
quote(player, message, players);
else{
quakeColors(player, message, players);
}
}
} catch (ClassCastException ex) {
player.sendMessage("There's a plugin disguised as mcMMO! It's not the one I was expecting!");
}
}
event.setCancelled(true);
}
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
String message = event.getMessage();
String split[] = event.getMessage().split(" ");
/*
* COLORS
*/
if(split[0].equalsIgnoreCase("/color")){
event.setCancelled(true);
if(split.length > 1)
{
vUsers.getProfile(player).setColor(split[1]);
player.sendMessage(ChatColor.RED
+ "Default chat color set.");
} else {
player.sendMessage(ChatColor.RED + "You use these color codes like in quake or MW2.");
player.sendMessage(ChatColor.RED + "^4 would make text " + ChatColor.DARK_RED
+ "red" + ChatColor.RED + ", ^a would make it " + ChatColor.GREEN
+ "light green" + ChatColor.RED + ".");
player.sendMessage(
ChatColor.BLACK + "0"
+ ChatColor.DARK_BLUE + "1"
+ ChatColor.DARK_GREEN + "2"
+ ChatColor.DARK_AQUA + "3"
+ ChatColor.DARK_RED + "4"
+ ChatColor.DARK_PURPLE + "5"
+ ChatColor.GOLD + "6"
+ ChatColor.GRAY + "7"
+ ChatColor.DARK_GRAY + "8"
+ ChatColor.BLUE + "9"
+ ChatColor.GREEN + "A"
+ ChatColor.AQUA + "B"
+ ChatColor.RED + "C"
+ ChatColor.LIGHT_PURPLE + "D"
+ ChatColor.YELLOW + "E"
+ ChatColor.WHITE + "F");
//+ "^r" + "[R]ainbow")
}
event.setCancelled(true);
}
/*
* PREFIX
*/
if(split[0].equalsIgnoreCase("/prefix")){
event.setCancelled(true);
if(split.length < 3 && player.isOp()){
player.sendMessage( ChatColor.RED + "Usage is /prefix [Player] [Color Code] <Tag>");
player.sendMessage(ChatColor.RED + "Example: /prefix " + player.getName() + " e ^0[^a<3^0]");
player.sendMessage( ChatColor.RED + "This would produce a name like... " + ChatColor.BLACK + "[" + ChatColor.GREEN + "<3" +ChatColor.BLACK + "]" + ChatColor.YELLOW + player.getName());
return;
}
if(player.isOp()){
//Check if the player exists
Player other = plugin.getServer().getPlayer(split[1]);
if(other == null)
{
player.sendMessage( ChatColor.RED
+ "The player you specified could not be found");
return;
}
if(split.length >= 3 && split[2] != null)
{
vUsers.getProfile(other).setPrefix(split[2]);
player.sendMessage(ChatColor.RED + "Name color changed");
}
if(split.length >= 4 && msgLength(split[3]) > 60)
{
player.sendMessage( ChatColor.RED
+ "The prefix you entered was too long.");
return;
}
if(split.length >= 4 && split[3] != null)
{
vUsers.players.findProfile(other).setTag(split[3]);
player.sendMessage(ChatColor.GREEN + "Prefix changed");
log.log(Level.INFO, player + " changed their prefix to " + split[3]);
}
return;
}
if(split.length < 2){
player.sendMessage( ChatColor.RED + "Usage is /prefix [Color Code] <Tag>");
player.sendMessage(ChatColor.RED + "Example: /prefix " + player.getName() + " e ^0[^a<3^0]");
player.sendMessage( ChatColor.RED + "This would produce a name like... " + ChatColor.BLACK + "[" + ChatColor.GREEN + "<3" + ChatColor.BLACK + "]" + ChatColor.YELLOW + player.getName());
return;
}
//Name color
if(split.length >= 2 && split[1] != null){
vUsers.getProfile(player).setPrefix(split[1]);
player.sendMessage(ChatColor.RED + "Name color changed");
}
//Prefix
if(split.length >= 3 && split[2] != null){
//Check if the prefix is too long
if(msgLength(split[1]) > 60)
{
player.sendMessage( ChatColor.RED
+ "The prefix you entered was too long.");
return;
}
vUsers.players.findProfile(player).setTag(split[2]);
player.sendMessage(ChatColor.GREEN + "Prefix changed");
}
}
/*
* SUFFIX
*/
if(split[0].equalsIgnoreCase("/suffix")){
event.setCancelled(true);
}
if(split[0].equalsIgnoreCase("/rprefix")){
if(!player.isOp()){
player.sendMessage("Op Only");
}
if(split.length < 2){
player.sendMessage("Usage is /rprefix <name>");
return;
}
if(isPlayer(split[1])){
Player target = plugin.getServer().getPlayer(split[1]);
vUsers.getProfile(target).setPrefix("");
vUsers.getProfile(target).setTag("");
}
}
}
//=====================================================================
//Function: quakeColors
//Input: Player player: The player talking
// String message: The message to apply the effect to
//Output: boolean: If this feature is enabled
//Use: Displays a message in red
//=====================================================================
public static void quakeColors(Player player, String message, Player[] players)
{
//Format the name
String playerName = "<"
//Insert their tag
+ vUsers.getProfile(player).getTag()
//Color their name
+ colorChange(vUsers.getProfile(player).getPrefix().charAt(0))
//Insert their name
+ player.getName() +ChatColor.WHITE+ "> ";
String color = vUsers.getProfile(player).getColor();
//Log the chat
log.log(Level.INFO, "<"+player.getName()+"> " + message);
//Output the message
gmsg(player, playerName + color + message, players);
//Loop through the string finding the color codes and inserting them
}
//=====================================================================
//Function: gmsg
//Input: Player sender: The player sending the message
// String msg: The message to be broadcast to all players
//Output: None
//Use: Outputs a message to everybody
//=====================================================================
public static void gmsg(Player sender, String msg, Player[] players){
/* Disabled for now
if(sender != null && sender.isMuted())
sender.sendMessage(ChatColor.DARK_RED + "You have been muted.");
*/
for (Player receiver : players) {
if (receiver == null) return;
//if(vUsers.getProfile(receiver) == null) return;
//Check if the person has the sender ignored
/* Disabled for now
if(sender != null)
if(vUsers.getProfile(receiver).isIgnored(sender))
return;
*/
String[] message = applyColors(wordWrap(msg));
for(String out : message)
receiver.sendMessage(out);
}
}
//=====================================================================
//Function: gmsg
//Input: String msg: The message to be broadcast to all players
//Output: None
//Use: Outputs a message to everybody
//=====================================================================
public static void gmsg(String msg){gmsg(null, msg, null);}
public static void gmsg(Player player, String msg){gmsg(player, msg, null);}
//=====================================================================
//Function: wordWrap
//Input: String msg: The message to be wrapped
//Output: String[]: The array of substrings
//Use: Cuts the message apart into whole words short enough to fit
// on one line
//=====================================================================
public static String[] wordWrap(String msg){
//Split each word apart
ArrayList<String> split = new ArrayList<String>();
for(String in : msg.split(" "))
split.add(in);
//Create an arraylist for the output
ArrayList<String> out = new ArrayList<String>();
//While i is less than the length of the array of words
while(!split.isEmpty()){
int len = 0;
//Create an arraylist to hold individual words
ArrayList<String> words = new ArrayList<String>();
//Loop through the words finding their length and increasing
//j, the end point for the sub string
while(!split.isEmpty() && split.get(0) != null && len <= lineLength)
{
int wordLength = msgLength(split.get(0)) + 4;
//If a word is too long for a line
if(wordLength > lineLength)
{
String[] tempArray = wordCut(len, split.remove(0));
words.add(tempArray[0]);
split.add(tempArray[1]);
}
//If the word is not too long to fit
len += wordLength;
if( len < lineLength)
words.add(split.remove(0));
}
//Merge them and add them to the output array.
out.add(combineSplit(words.toArray(new String[words.size()]), " ") + " " );
}
//Convert to an array and return
return out.toArray(new String[out.size()]);
}
//CombineSplit
public static String combineSplit(String[] array, String merge) {
String out = "";
for(String word : array)
out += word + merge;
return out;
}
//=====================================================================
//Function: msgLength
//Input: String str: The string to find the length of
//Output: int: The length on the screen of a string
//Use: Finds the length on the screen of a string. Ignores colors.
//=====================================================================
public static int msgLength(String str){
int length = 0;
//Loop through all the characters, skipping any color characters
//and their following color codes
for(int x = 0; x<str.length(); x++)
{
if((x+1 <= str.length()) && (str.charAt(x) == '^' || str.charAt(x) == ChatColor.WHITE.toString().charAt(0)))
{
if(colorChange(str.charAt(x + 1)) != null)
{
x++;
continue;
}
}
int len = charLength(str.charAt(x));
length += len;
}
return length;
}
//=====================================================================
//Function: colorChange
//Input: char colour: The color code to find the color for
//Output: String: The color that the code identified
//Use: Finds a color giving a color code
//=====================================================================
public static String colorChange(char colour)
{
String color;
switch(colour)
{
case '0':
color = ChatColor.BLACK.toString();
break;
case '1':
color = ChatColor.DARK_BLUE.toString();
break;
case '2':
color = ChatColor.DARK_GREEN.toString();
break;
case '3':
color = ChatColor.DARK_AQUA.toString();
break;
case '4':
color = ChatColor.DARK_RED.toString();
break;
case '5':
color = ChatColor.DARK_PURPLE.toString();
break;
case '6':
color = ChatColor.GOLD.toString();
break;
case '7':
color = ChatColor.GRAY.toString();
break;
case '8':
color = ChatColor.DARK_GRAY.toString();
break;
case '9':
color = ChatColor.BLUE.toString();
break;
case 'a':
color = ChatColor.GREEN.toString();
break;
case 'b':
color = ChatColor.AQUA.toString();
break;
case 'c':
color = ChatColor.RED.toString();
break;
case 'd':
color = ChatColor.LIGHT_PURPLE.toString();
break;
case 'e':
color = ChatColor.YELLOW.toString();
break;
case 'f':
color = ChatColor.WHITE.toString();
break;
case 'A':
color = ChatColor.GREEN.toString();
break;
case 'B':
color = ChatColor.AQUA.toString();
break;
case 'C':
color = ChatColor.RED.toString();
break;
case 'D':
color = ChatColor.LIGHT_PURPLE.toString();
break;
case 'E':
color = ChatColor.YELLOW.toString();
break;
case 'F':
color = ChatColor.WHITE.toString();
break;
case 'R':
color = "^r";
break;
case 'r':
color = "^r";
break;
case 'x':
color = "^x";
break;
case 'X':
color = "^x";
break;
default:
color = null;
break;
}
return color;
}
private static String[] wordCut(int lengthBefore, String str){
int length = lengthBefore;
//Loop through all the characters, skipping any color characters
//and their following color codes
String[] output = new String[2];
int x = 0;
while(length < lineLength && x < str.length())
{
int len = charLength(str.charAt(x));
if( len > 0)
length += len;
else
x++;
x++;
}
if(x > str.length())
x = str.length();
//Add the substring to the output after cutting it
output[0] = str.substring(0, x);
//Add the last of the string to the output.
output[1] = str.substring(x);
return output;
}
private static int charLength(char x)
{
if("i.:,;|!".indexOf(x) != -1)
return 2;
else if("l'".indexOf(x) != -1)
return 3;
else if("tI[]".indexOf(x) != -1)
return 4;
else if("fk{}<>\"*()".indexOf(x) != -1)
return 5;
else if("abcdeghjmnopqrsuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890\\/#?$%-=_+&^".indexOf(x) != -1)
return 6;
else if("@~".indexOf(x) != -1)
return 7;
else if(x==' ')
return 4;
else
return -1;
}
//=====================================================================
//Function: rainbow
//Input: String msg: The string to colorify
//Output: String: The rainbowed result
//Use: Rainbowifies a string;
//=====================================================================
public static String rainbow(String msg){
String temp = "";
int counter=0;
//Loop through the message applying the colors
for(int x=0; x<msg.length(); x++)
{
temp += rainbow[counter]+msg.charAt(x);
if(msg.charAt(x)!=' ') counter++;
if(counter==rainbow.length) counter = 0;
}
return temp;
}
//=====================================================================
//Function: xmas
//Input: String msg: The string to colorify
//Output: String: The xmas colored result
//Use: Makes a string more festive
//=====================================================================
public static String xmas(String msg){
String temp = "";
int counter=0;
//Loop through the message applying the colors
for(int x=0; x<msg.length(); x++)
{
temp += xmas[counter]+msg.charAt(x);
if(msg.charAt(x)!=' ') counter++;
if(counter==xmas.length) counter = 0;
}
return temp;
}
//=====================================================================
//Function: quote
//Input: Player player: The player talking
// String message: The message to apply the effect to
//Output: boolean: If this feature is enabled
//Use: Displays a message as a quote
//=====================================================================
public void quote(Player player, String message, Player[] players)
{
//Format the name
//Format the name
String playerName = "<"+
//Insert their tag
vUsers.getProfile(player).getTag()
//Color their name
+ colorChange(vUsers.getProfile(player).getPrefix().charAt(0))
//Insert their name
+ player.getName() +ChatColor.WHITE+ "> ";
//Log the chat
log.log(Level.INFO, "<"+player.getName()+"> " + message);
//Output the message
gmsg(player, playerName + ChatColor.GREEN + message, players);
}
//=====================================================================
//Function: applyColors
//Input: String[] message: The lines to be colored
//Output: String[]: The lines, but colorful
//Use: Colors each line
//=====================================================================
public static String[] applyColors(String[] message)
{
if(message != null && message[0] != null && !message[0].isEmpty()){
//The color to start the line with
String recentColor = ChatColor.WHITE.toString();
//Go through each line
int counter = 0;
int i = 0;
boolean taste = false;
boolean xmasparty = false;
for(String msg: message)
{
//Start the line with the most recent color
String temp = "";
if(!recentColor.equals("^r") && !recentColor.equals("^x") && recentColor != null)
temp += recentColor;
//Loop through looking for a color code
for(int x = 0; x< msg.length(); x++)
{
//If the char is a ^ or
if(taste || msg.charAt(x) == '^'
|| msg.charAt(x) == ChatColor.DARK_RED.toString().charAt(0))
{
if(x != msg.length() - 1)
{
//If the following character is a color code
if(colorChange(msg.charAt(x+1)) != null)
{
//Set the most recent color to the new color
recentColor = colorChange(msg.charAt(x+1));
//If the color specified is rainbow
if(taste || recentColor.equals("^r"))
{
/*
//Skip the quake code for rainbow
if(recentColor.equals("^r"))
{
x += 2;
}
//Taste keeps it going with rainbow if there
//are more lines
taste = true;
//Loop through the message applying the colors
while(x < msg.length() && msg.charAt(x) != '^'
&& msg.charAt(x) != ChatColor.DARK_RED.toString().charAt(0))
{
temp += rainbow[i] + msg.charAt(x);
if(msg.charAt(x) != ' ') i++;
if(i == rainbow.length) i = 0;
x++;
}
//If it reached another color instead of the end
if(x < msg.length() && msg.charAt(x) == '^')
{
taste = false;
i = 0;
x--;
}
*/
}
if(xmasparty || recentColor.equals("^x"))
{
/*
//Skip the quake code for xmas
if(recentColor.equals("^x"))
{
x += 2;
}
//xmasparty keeps it going with xmas if there
//are more lines
xmasparty = true;
//Loop through the message applying the colors
while(x < msg.length() && msg.charAt(x) != '^'
&& msg.charAt(x) != ChatColor.DARK_RED.toString().charAt(0))
{
temp += xmas[i] + msg.charAt(x);
if(msg.charAt(x) != ' ') i++;
if(i == xmas.length) i = 0;
x++;
}
//If it reached another color instead of the end
if(x < msg.length() && msg.charAt(x) == '^'
|| x < msg.length()
&& msg.charAt(x) == ChatColor.DARK_RED.toString().charAt(0) )
{
xmasparty = false;
i = 0;
x--;
}
*/
}
else
{
//Add the color
temp += recentColor;
//Skip these chars
x++;
}
//Otherwise ignore it.
} else {
temp += msg.charAt(x);
}
//Insert the character
} else {
temp += msg.charAt(x);
}
} else {
temp += msg.charAt(x);
}
}
//Replace the message with the colorful message
message[counter] = temp;
counter++;
}
}
return message;
}
}

View File

@ -0,0 +1,570 @@
package com.gmail.nossr50.vChat;
import java.io.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.entity.*;
public class vUsers {
private static volatile vUsers instance;
protected static final Logger log = Logger.getLogger("Minecraft");
String location = "vChat.users";
public static PlayerList players = new PlayerList();
private Properties properties = new Properties();
//To load
public void load() throws IOException {
properties.load(new FileInputStream(location));
}
//To save
public void save() {
try {
properties.store(new FileOutputStream(location), null);
}catch(IOException ex) {
}
}
public void loadUsers(){
File theDir = new File(location);
if(!theDir.exists()){
//properties = new PropertiesFile(location);
FileWriter writer = null;
try {
writer = new FileWriter(location);
writer.write("#Storage place for user information\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 {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
}
}
} else {
//properties = new PropertiesFile(location);
try {
load();
} catch (IOException e) {
log.log(Level.SEVERE, "Exception while loading " + location, e);
}
}
}
//=====================================================================
//Function: addUser
//Input: Player player: The player to create a profile for
//Output: none
//Use: Loads the profile for the specified player
//=====================================================================
public static void addUser(Player player){
players.addPlayer(player);
}
//=====================================================================
//Function: removeUser
//Input: Player player: The player to stop following
//Output: none
//Use: Creates the player profile
//=====================================================================
public static void removeUser(Player player){
players.removePlayer(player);
}
//=====================================================================
//Function: getProfile
//Input: Player player: The player to find the profile for
//Output: PlayerList.PlayerProfile: The profile
//Use: Gets the player profile
//=====================================================================
public static PlayerList.PlayerProfile getProfile(Player player){
return players.findProfile(player);
}
public static vUsers getInstance() {
if (instance == null) {
instance = new vUsers();
}
return instance;
}
public static void getRow(){
}
}
class PlayerList
{
protected static final Logger log = Logger.getLogger("Minecraft");
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
//=====================================================================
public PlayerProfile findProfile(Player player)
{
for(PlayerProfile ply : players)
{
if(ply.isPlayer(player))
return ply;
}
return null;
}
//=====================================================================
//Class: PlayerProfile
//Use: Encapsulates all commands for player options
//Author: cerevisiae
//=====================================================================
class PlayerProfile
{
protected final Logger log = Logger.getLogger("Minecraft");
private String playerName,
lastMessage,
nickName,
tag,
suffix,
prefix,
party;
private boolean dead,
silent;
char defaultColor;
String location = "vChat.users";
private ArrayList<String> ignoreList;
//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)
{
//Declare things
playerName = player.getName();
tag = new String();
nickName = new String();
suffix = new String();
prefix = new String();
party = new String();
party = null;
defaultColor = 'f';
ignoreList = new ArrayList<String>();
dead = false;
//Try to load the player and if they aren't found, append them
if(!load())
addPlayer();
}
public boolean load()
{
try {
//Open the user file
FileReader file = new FileReader(location);
BufferedReader in = new BufferedReader(file);
String line = "";
while((line = in.readLine()) != null)
{
//Find if the line contains the player we want.
String[] character = line.split(":");
if(!character[0].equals(playerName)){continue;}
//Get the tag
if(character.length > 1)
tag = character[1];
//Get the nickname
if(character.length > 2)
nickName = character[2];
//Get the suffix
if(character.length > 3)
suffix = character[3];
//Get the color
if(character.length > 4)
defaultColor = character[4].charAt(0);
//Ignore previously ignored players
if(character.length > 5)
{
String[] ignores = character[5].split(",");
if(ignores.length > 0)
{
for(String ignore : ignores)
ignoreList.add(ignore);
}
}
in.close();
return true;
}
in.close();
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while reading "
+ location + " (Are you sure you formatted it correctly?)", e);
}
return false;
}
//=====================================================================
// Function: save
// Input: none
// Output: None
// Use: Writes current values of PlayerProfile to disk
// Call this function to save current values
//=====================================================================
public void save()
{
try {
//Open the file
FileReader file = new FileReader(location);
BufferedReader in = new BufferedReader(file);
StringBuilder writer = new StringBuilder();
String line = "";
//While not at the end of the file
while((line = in.readLine()) != null)
{
//Read the line in and copy it to the output it's not the player
//we want to edit
if(!line.split(":")[0].equalsIgnoreCase(playerName))
{
writer.append(line).append("\r\n");
//Otherwise write the new player information
} else {
writer.append(playerName + ":");
writer.append(tag + ":");
writer.append(nickName + ":");
writer.append(suffix + ":");
writer.append(defaultColor + ":");
writer.append(prefix + ":");
int i = 0;
for(String ignore : ignoreList)
{
writer.append(ignore);
if(i < ignoreList.size() - 1)
writer.append(",");
}
writer.append("\r\n");
}
}
in.close();
//Write the new file
FileWriter out = new FileWriter(location);
out.write(writer.toString());
out.close();
} catch (Exception 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, true);
BufferedWriter out = new BufferedWriter(file);
//Add the player to the end
out.append(playerName + ":");
out.append("" + ":");
out.append(nickName + ":");
out.append(suffix + ":");
out.append("f" + ":");
out.append("f" + ":");
int i = 0;
for(String ignore : ignoreList)
{
out.append(ignore);
if(i < ignoreList.size() - 1)
out.append(",");
}
out.newLine();
out.close();
} catch (Exception e) {
log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
}
}
//=====================================================================
//Function: isPlayer
//Input: None
//Output: Player: The player this profile belongs to
//Use: Finds if this profile belongs to a specified player
//=====================================================================
public boolean isPlayer(Player player)
{
return player.getName().equals(playerName);
}
//=====================================================================
//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
//=====================================================================
public boolean isIgnored(Player player){
return ignoreList.contains(player.getName());
}
//=====================================================================
//Function: addIgnore
//Input: Player name: The player to ignore
//Output: boolean: If the player was successfully ignored
//Use: Ignores a player.
//=====================================================================
public boolean addIgnore(Player name)
{
if(!ignoreList.contains(name))
{
ignoreList.add(name.getName());
save();
return true;
}
return false;
}
//=====================================================================
//Function: removeIgnore
//Input: Player name: The player to unignore
//Output: boolean: If the player was successfully unignored
//Use: Stops ignoring a player.
//=====================================================================
public boolean removeIgnore(Player name)
{
if(ignoreList.contains(name.getName()))
{
ignoreList.remove(name.getName());
save();
return true;
}
return false;
}
//=====================================================================
//Function: removeIgnore
//Input: Player name: The player to unignore
//Output: boolean: If the player was successfully unignored
//Use: Stops ignoring a player.
//=====================================================================
public String[] listIgnore()
{
return ignoreList.toArray(new String[ignoreList.size()]);
}
//=====================================================================
//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;
save();
}
//=====================================================================
//Function: getTag
//Input: None
//Output: String: The player tag
//Use: Gets a player tag
//=====================================================================
public String getTag() { return tag; }
//=====================================================================
//Function: setNick
//Input: String newTag: The nickname to set for the player
//Output: None
//Use: Sets a player nickname
//=====================================================================
public void setNick(String newNick)
{
nickName = newNick;
save();
}
public void setSilent(){
silent = true;
}
public void disableSilent(){
silent = false;
}
public boolean isSilent(){
return silent;
}
//Store the player's party
public void setParty(String newParty)
{
party = newParty;
save();
}
//Retrieve the player's party
public String getParty() {return party;}
//Remove party
public void removeParty() {
party = null;
save();
}
//Retrieve whether or not the player is in a party
public boolean inParty() {
if(party != null){
return true;
} else {
return false;
}
}
//=====================================================================
//Function: getNick
//Input: None
//Output: String: The player nickname
//Use: Gets a player nickname
//=====================================================================
public String getNick() { return nickName; }
//=====================================================================
//Function: setSuffix
//Input: String newTag: The suffix to set for the player
//Output: None
//Use: Sets a player suffix
//=====================================================================
public void setSuffix(String newSuffix)
{
suffix = newSuffix;
save();
}
//=====================================================================
//Function: getSuffix
//Input: None
//Output: String: The player suffix
//Use: Gets a player suffix
//=====================================================================
public String getSuffix() { return suffix; }
public void setPrefix(String newPrefix)
{
prefix = newPrefix;
save();
}
public String getPrefix() {
if(prefix != null && !prefix.equals("") && !prefix.equals("null")){
return prefix;
} else {
return "f";
}
}
//=====================================================================
//Function: setColor
//Input: String newTag: The color to set for the player
//Output: None
//Use: Sets a player color
//=====================================================================
public void setColor(String newColor)
{
defaultColor = newColor.charAt(0);
save();
}
//=====================================================================
//Function: getColor
//Input: None
//Output: String: The player color
//Use: Gets a player color
//=====================================================================
public String getColor() {return vPlayerListener.colorChange(defaultColor);}
//=====================================================================
//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()
{
//if(lastMessage != null)
//We need the bukkit equivalent of this
//return matchPlayer(lastMessage);
return null;
}
//=====================================================================
//Function: isDead
//Input: None
//Output: boolean: If the player is dead or not
//Use: Gets the player is dead or not.
//=====================================================================
public boolean isDead() {return dead;}
//=====================================================================
//Function: isDead
//Input: boolean isded: if the player is dead or not.
//Output: None
//Use: Sets if the player is dead or not
//=====================================================================
public void isDead(boolean isded){dead = isded;}
}
}

View File

@ -1,3 +1,3 @@
name: vChat
main: com.bukkit.nossr50.vChat.vChat
name: vChat
main: com.gmail.nossr50.vChat.vChat
version: 1.0