package net.acomputerdog.motd;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public class PluginMotd extends JavaPlugin implements Listener {
    private File motdDir;
    private MotdList motds;

    @Override
    public void onEnable() {
        motdDir = new File(getDataFolder(), "/motds/");
        if (!(motdDir.exists() || motdDir.mkdirs())) {
            getLogger().warning("Unable to create MOTD directory!");
        }

        motds = new MotdList(this);
        motds.load(motdDir);

        getServer().getPluginManager().registerEvents(this, this);

        getLogger().info("Startup complete.");
    }

    @Override
    public void onDisable() {
        motdDir = null;
        motds = null;
        getLogger().info("Shutdown complete.");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (command.getName().equalsIgnoreCase("motd")) {
            if (sender.hasPermission("motd.command")) {
                sender.sendMessage(ChatColor.AQUA + "MOTDs for " + sender.getName() + ": ");
                motds.sendMotdsFor(sender);
            } else {
                sender.sendMessage(ChatColor.RED + "You do not have permission!");
            }
        } else {
            sender.sendMessage(ChatColor.RED + "Unknown command!");
        }
        return true;
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onPlayerJoin(PlayerJoinEvent e) {
        //todo threads?
        motds.sendMotdsFor(e.getPlayer());
    }
}

@@ -844,56 +844,8 @@
 is);
-%0A%0A        getLogger().info(%22Startup complete.%22);
 %0A   
@@ -942,56 +942,8 @@
 ll;%0A
-        getLogger().info(%22Shutdown complete.%22);%0A
     
@@ -1066,20 +1066,29 @@
         
-if (
+String cmd = 
 command.
@@ -1101,40 +1101,598 @@
 e().
-equalsIgnoreCase(%22motd%22)) %7B%0A    
+toLowerCase();%0A        switch (cmd) %7B%0A            case %22motd%22:%0A                onCmdMotd(sender);%0A                break;%0A            case %22reload%22:%0A                onCmdReload(sender);%0A            default:%0A                sender.sendMessage(ChatColor.RED + %22Unknown command!%22);%0A                break;%0A        %7D%0A        return true;%0A    %7D%0A%0A    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)%0A    public void onPlayerJoin(PlayerJoinEvent e) %7B%0A        //todo threads?%0A        motds.sendMotdsFor(e.getPlayer());%0A    %7D%0A%0A    private void onCmdMotd(CommandSender sender) %7B%0A
     
@@ -1735,28 +1735,24 @@
 ommand%22)) %7B%0A
-    
             
@@ -1836,28 +1836,24 @@
             
-    
 motds.sendMo
@@ -1872,28 +1872,24 @@
 r);%0A        
-    
 %7D else %7B%0A   
@@ -1889,36 +1889,32 @@
 e %7B%0A            
- 
-   
 sender.sendMessa
@@ -1976,28 +1976,67 @@
     
+%7D%0A
     %7D%0A
+%0A
     
-    %7D else
+private void onCmdReload(CommandSender sender)
  %7B%0A 
@@ -2046,64 +2046,91 @@
     
-    
+if (
 sender.
-sendMessage(ChatColor.RED + %22Unknown command!
+hasPermission(%22motd.reload%22)) %7B%0A            getLogger().info(%22Reloading.
 %22);%0A
@@ -2141,230 +2141,223 @@
     
+  
-%7D%0A        return true
+  onDisable();%0A            onEnable()
 ;%0A    
-%7D%0A%0A
     
-@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)%0A    public void onPlayerJoin(PlayerJoinEvent e) %7B%0A        //todo threads?%0A        motds.sendMotdsFor(e.getPlayer());
+    sender.sendMessage(ChatColor.AQUA + %22Reload complete.%22);%0A        %7D else %7B%0A            sender.sendMessage(ChatColor.RED + %22You do not have permission!%22);%0A        %7D
 %0A   

implement /reload