diff --git a/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/Config.java b/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/Config.java new file mode 100644 index 000000000..783744ee2 --- /dev/null +++ b/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/Config.java @@ -0,0 +1,78 @@ + +package com.gmail.nossr50.vPlayersOnline; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; + +/** + * + * @author Mark Tolley + */ +class Config +{ + public static String name; + private static final String CONFIG_FILE = "plugins/vPlayersOnline/vplayersonline.properties"; + + public static Properties loadConfig() { + Properties config = defaultConfig(); + + try { + config.load(new FileReader(CONFIG_FILE)); + } + catch (FileNotFoundException e) { + System.out.println(name + ": Creating configuration file..."); + config = defaultConfig(); + writeConfig(); + } + catch (IOException e) { + System.out.println(name + "s: An error occured reading configuration, using defaults."); + config = defaultConfig(); + } + + return config; + } + + private static void writeConfig() { + File f = new File(CONFIG_FILE); + if (f.getParentFile().mkdirs()) { + try { + FileWriter fw = new FileWriter(f); + fw.write("# vPlayersOnline configuration file\r\n"); + fw.write("# \r\n"); + fw.write("# Color codes:\r\n"); + fw.write("# &0 black\r\n"); + fw.write("# &1 dark blue &9 blue\r\n"); + fw.write("# &2 dark green &a green\r\n"); + fw.write("# &3 dark aqua &b aqua\r\n"); + fw.write("# &4 dark red &c red\r\n"); + fw.write("# &5 dark pink &d pink\r\n"); + fw.write("# &6 dark yellow &e yellow\r\n"); + fw.write("# &7 light grey &f white\r\n"); + fw.write("# &8 dark grey\r\n"); + fw.write("\r\n"); + fw.write("PlayersOnline = &aThere are %d players online\r\n"); + fw.write("PlayerList = &cPlayer List &f(%s)\r\n"); + fw.write("TotalPlayers = &cTotal Players: &a%d\r\n"); + fw.write("#1POnline = &aThere is 1 player online.\r\n"); + fw.write("1POnline = &cNo one else is online.\r\n"); + fw.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + } + + private static Properties defaultConfig() { + Properties config = new Properties(); + config.setProperty("PlayersOnline", "&aThere are %d players online"); + config.setProperty("PlayerList", "&cPlayer List &f(%s)"); + config.setProperty("TotalPlayers", "&cTotal Players: &a%d"); + config.setProperty("1POnline", "&cNo one else is online."); + return config; + } +} diff --git a/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayerListener.java b/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayerListener.java index b6bd65365..31e1cedd3 100644 --- a/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayerListener.java +++ b/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayerListener.java @@ -1,12 +1,11 @@ package com.gmail.nossr50.vPlayersOnline; +import java.util.Properties; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerListener; import org.bukkit.ChatColor; -import java.util.logging.Level; -import java.util.logging.Logger; /** * Handle events for all Player related events @@ -14,49 +13,90 @@ import java.util.logging.Logger; */ public class vPlayerListener extends PlayerListener { private final vPlayersOnline plugin; - protected static final Logger log = Logger.getLogger("Minecraft"); + private final String PlayersOnline; + private final String PlayerList; + private final String TotalPlayers; + private final String _1POnline; - public vPlayerListener(vPlayersOnline instance) { + public vPlayerListener(vPlayersOnline instance, Properties config) { plugin = instance; + PlayersOnline = parseColors(config.getProperty("PlayersOnline")); + PlayerList = parseColors(config.getProperty("PlayerList")); + TotalPlayers = parseColors(config.getProperty("TotalPlayers")); + _1POnline = parseColors(config.getProperty("1POnline")); } + //Function to count the players - public int playerCount(){ + private int playerCount(){ Player players[] = plugin.getServer().getOnlinePlayers(); - int x = 0; - for(Player hurrdurr: players){ - x++; + return players.length; + } + + private static String parseColors(String str) { + final StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < str.length(); ++i) { + char c = str.charAt(i); + + if (c == '&') { + char next = str.charAt(i+1); + if (next == '&') { + // literal & + ++i; + } else { + try { + int color = Integer.parseInt(String.valueOf(next), 16); + sb.append(ChatColor.getByCode(color)); + ++i; + continue; + } catch (NumberFormatException e) {} + } + } + + sb.append(c); } - return x; + + return sb.toString(); } //Message to be sent when a player joins + @Override public void onPlayerJoin(PlayerEvent event) { Player player = event.getPlayer(); - //English Version - player.sendMessage(ChatColor.GREEN + "There are " + playerCount() + " players online."); + int count = playerCount(); + + if (count == 1) { + player.sendMessage(_1POnline); + } else { + player.sendMessage(String.format(PlayersOnline, count)); } + } + //Message to be sent when a player uses /list - public void onPlayerCommand(PlayerChatEvent event) { + @Override + public void onPlayerCommandPreprocess(PlayerChatEvent event) { String[] split = event.getMessage().split(" "); Player player = event.getPlayer(); if(split[0].equalsIgnoreCase("/list") || split[0].equalsIgnoreCase("/who")){ - event.setCancelled(true); - String tempList = ""; - int x = 0; + event.setCancelled(true); + + int count = playerCount(); + String tempList = ""; + int x = 0; for(Player p : plugin.getServer().getOnlinePlayers()) { - if(p != null && x+1 >= playerCount()){ + if(p != null && x+1 >= count){ tempList+= p.getName(); x++; } - if(p != null && x < playerCount()){ + if(p != null && x < count){ tempList+= p.getName() +", "; x++; } } //Output the player list - player.sendMessage(ChatColor.RED + "Player List"+ChatColor.WHITE+" ("+ChatColor.WHITE + tempList+")"); - player.sendMessage(ChatColor.RED + "Total Players: " + ChatColor.GREEN + playerCount()); + player.sendMessage(String.format(PlayerList, tempList)); + player.sendMessage(String.format(TotalPlayers, count)); } } diff --git a/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayersOnline.java b/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayersOnline.java index f70ab5406..10b30a69a 100644 --- a/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayersOnline.java +++ b/vPlayersOnline/com/gmail/nossr50/vPlayersOnline/vPlayersOnline.java @@ -1,16 +1,16 @@ package com.gmail.nossr50.vPlayersOnline; import java.io.File; -import java.util.HashMap; -import org.bukkit.event.player.*; -import org.bukkit.Server; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; 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; -import org.bukkit.entity.Player; /** * vPlayersOnline for Bukkit @@ -18,20 +18,31 @@ import org.bukkit.entity.Player; * @author nossr50 */ public class vPlayersOnline extends JavaPlugin { - private final vPlayerListener playerListener = new vPlayerListener(this); - private final String name = "vPlayersOnline"; + private PluginDescriptionFile pdfFile; + + private vPlayerListener playerListener; + + public void onLoad() { + + } public void onEnable() { + pdfFile = this.getDescription(); + Config.name = pdfFile.getName(); + + Properties config = Config.loadConfig(); + playerListener = new vPlayerListener(this, config); + PluginManager pm = getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_DROP_ITEM, playerListener, Priority.Normal, this); pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this); - pm.registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this); + pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Normal, this); + //Displays a message when plugin is loaded - PluginDescriptionFile pdfFile = this.getDescription(); - System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" ); + System.out.println(Config.name + " version " + pdfFile.getVersion() + " is enabled!"); } public void onDisable() { - System.out.println("vPlayersOnline disabled."); + System.out.println(Config.name + " disabled."); } }