diff --git a/src/com/redstoner/misc/Utils.java b/src/com/redstoner/misc/Utils.java index 732c813..cf78bb8 100644 --- a/src/com/redstoner/misc/Utils.java +++ b/src/com/redstoner/misc/Utils.java @@ -1,5 +1,8 @@ package com.redstoner.misc; +import java.text.SimpleDateFormat; +import java.util.Date; + import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -13,9 +16,12 @@ import net.md_5.bungee.api.ChatColor; /** The utils class containing utility functions. Those include but are not limited to sending formatted messages, broadcasts and more. * * @author Pepich */ -@Version(major = 1, minor = 2, revision = 10, compatible = 1) +@Version(major = 1, minor = 2, revision = 11, compatible = 1) public final class Utils { + /** The SimpleDateFormat used for getting the current date. */ + public static SimpleDateFormat dateFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]"); + /** Hidden constructor. Do not instantiate UTILS classes! :) */ private Utils() {} @@ -216,4 +222,13 @@ public final class Utils { recipient.sendMessage("ยง2--=[ " + getCaller() + " ]=--"); } + + /** Provides a uniform way of getting the date for all modules. + * + * @return The current date in the format "[dd-mm-yyyy hh:mm:ss]" */ + public static String getDate() + { + Date date = new Date(System.currentTimeMillis()); + return dateFormat.format(date); + } } diff --git a/src/com/redstoner/modules/essentials/Essentials.java b/src/com/redstoner/modules/essentials/Essentials.java index 98f6ce5..cd048a7 100644 --- a/src/com/redstoner/modules/essentials/Essentials.java +++ b/src/com/redstoner/modules/essentials/Essentials.java @@ -1,8 +1,121 @@ package com.redstoner.modules.essentials; +import java.io.File; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.json.simple.JSONObject; + +import com.nemez.cmdmgr.Command; +import com.nemez.cmdmgr.CommandManager; +import com.redstoner.annotations.AutoRegisterListener; import com.redstoner.annotations.Version; +import com.redstoner.exceptions.PlayerNotFoundException; +import com.redstoner.misc.JsonManager; +import com.redstoner.misc.Main; +import com.redstoner.misc.Utils; import com.redstoner.modules.Module; +@AutoRegisterListener @Version(major = 3, minor = 0, revision = 0, compatible = 3) public class Essentials implements Module -{} +{ + static HashMap players; + private final File saveFile = new File(Main.plugin.getDataFolder(), "knownPlayers.json"); + private static JSONObject knownNames; + + @Override + public boolean onEnable() + { + knownNames = JsonManager.getObject(saveFile); + if (knownNames == null) + { + Utils.warn("Couldn't find existing"); + } + players = new HashMap(); + for (Player p : Bukkit.getOnlinePlayers()) + { + players.put(p.getUniqueId(), Essentials.getPlayer(p)); + } + return true; + } + + @Override + public void postEnable() + { + Utils.info("Creating command links for essentials. This may take a while..."); + CommandManager.registerCommand(this.getClass().getResourceAsStream("Essentials.cmd"), this, Main.plugin); + } + + @Command(hook = "kickDef") + public void kick(CommandSender sender, String name) + { + kick(sender, name, EssentialsDefaults.kickMessage); + } + + @Command(hook = "kick") + public void kick(CommandSender sender, String name, String reason) + { + try + { + getPlayer(name).kick(getName(sender), reason); + } + catch (PlayerNotFoundException e) + { + Utils.sendErrorMessage(sender, null, e.getMessage()); + } + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent e) + { + players.put(e.getPlayer().getUniqueId(), getPlayer(e.getPlayer())); + } + + @EventHandler + public void onPlayerLeave(PlayerQuitEvent e) + { + UUID uuid = e.getPlayer().getUniqueId(); + players.get(uuid).onLeave(); + players.remove(uuid); + } + + public static EssentialsPlayer getPlayer(UUID uuid) throws PlayerNotFoundException + { + Player player = Bukkit.getPlayer(uuid); + if (player == null) + throw new PlayerNotFoundException(); + return getPlayer(player); + } + + public static EssentialsPlayer getPlayer(Player player) + { + if (players.containsKey(player.getUniqueId())) + return players.get(player.getUniqueId()); + return new EssentialsPlayer(player); + } + + public static EssentialsPlayer getPlayer(String name) throws PlayerNotFoundException + { + Player player = Bukkit.getPlayer(name); + if (player == null) + throw new PlayerNotFoundException(); + return getPlayer(player); + } + + public static String getName(CommandSender sender) + { + String name = "&9"; + if (sender instanceof Player) + name += ((Player) sender).getDisplayName(); + else + name += sender.getName(); + return name; + } +}