/*
 * This file is part of DropletAlert.
 *
 * Copyright (c) 2012, SpoutDev <http://www.spout.org/>
 * DropletAlert is licensed under the SpoutDev License Version 1.
 *
 * DropletAlert is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * In addition, 180 days after any changes are published, you can use the
 * software, incorporating those changes, under the terms of the MIT license,
 * as described in the SpoutDev License Version 1.
 *
 * DropletAlert is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License,
 * the MIT license and the SpoutDev License Version 1 along with this program.
 * If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public
 * License and see <http://www.spout.org/SpoutDevLicenseV1.txt> for the full license,
 * including the MIT license.
 */
package org.spout.droplet;

import java.util.logging.Level;

import org.spout.api.Engine;
import org.spout.api.Spout;
import org.spout.api.command.CommandRegistrationsFactory;
import org.spout.api.command.annotated.AnnotatedCommandRegistrationFactory;
import org.spout.api.command.annotated.SimpleAnnotatedCommandExecutorFactory;
import org.spout.api.command.annotated.SimpleInjector;
import org.spout.api.exception.ConfigurationException;
import org.spout.api.player.Player;
import org.spout.api.plugin.CommonPlugin;
import org.spout.api.scheduler.TaskPriority;

import org.spout.droplet.command.DropletCommand;
import org.spout.droplet.config.DropletConfig;

public class DropletAlertPlugin extends CommonPlugin {
	private DropletConfig config;
	private Engine engine;

	@Override
	public void onDisable() {
		try {
			config.save();
		} catch (ConfigurationException e) {
			getLogger().log(Level.WARNING, "Error saving DropletAlert configuration: ", e);
		}
		Spout.getEngine().getScheduler().cancelTasks(this);
		getLogger().log(Level.INFO, "disabled");
	}

	@Override
	public void onEnable() {
		engine = getEngine();

		try {
			config.load();
		} catch (ConfigurationException e) {
			getLogger().log(Level.WARNING, "Error loading DropletAlert configuration: ", e);
		}

		//Register commands
		CommandRegistrationsFactory<Class<?>> commandRegFactory = new AnnotatedCommandRegistrationFactory(new SimpleInjector(this), new SimpleAnnotatedCommandExecutorFactory());
		engine.getRootCommand().addSubCommands(this, DropletCommand.class, commandRegFactory);

		engine.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
			@Override
			public void run() {
				//Do not run the task if no players are online or they are no messages.
				if (DropletConfig.MESSAGES.getStringList().size() == 0 || engine.getOnlinePlayers().length == 0) {
					return;
				}
				for (Player plr : engine.getOnlinePlayers()) {
					for (String str : DropletConfig.MESSAGES.getStringList()) {
						plr.sendMessage(str);
					}
				}
			}
		}, DropletConfig.DELAY.getLong() * 20, DropletConfig.REPEAT.getLong() * 20, TaskPriority.LOW);

		getLogger().log(Level.INFO, "b" + getDescription().getVersion() + " enabled");
	}

	@Override
	public void onLoad() {
		config = new DropletConfig(getDataFolder());
		getLogger().log(Level.INFO, "loaded");
	}

	public DropletConfig getConfig() {
		return config;
	}
}

@@ -1307,65 +1307,8 @@
 l;%0A%0A
-import org.spout.api.Engine;%0Aimport org.spout.api.Spout;%0A
 impo
@@ -1570,16 +1570,52 @@
 jector;%0A
+import org.spout.api.entity.Player;%0A
 import o
@@ -1665,44 +1665,8 @@
 on;%0A
-import org.spout.api.player.Player;%0A
 impo
@@ -1935,32 +1935,8 @@
 fig;
-%0A%09private Engine engine;
 %0A%0A%09@
@@ -1977,166 +1977,8 @@
 %7B%0A%09%09
-try %7B%0A%09%09%09config.save();%0A%09%09%7D catch (ConfigurationException e) %7B%0A%09%09%09getLogger().log(Level.WARNING, %22Error saving DropletAlert configuration: %22, e);%0A%09%09%7D%0A%09%09Spout.
 getE
@@ -2107,33 +2107,8 @@
 ) %7B%0A
-%09%09engine = getEngine();%0A%0A
 %09%09tr
@@ -2453,22 +2453,27 @@
 y());%0A%09%09
-e
+getE
 ngine
+()
 .getRoot
@@ -2548,22 +2548,27 @@
 ry);%0A%0A%09%09
-e
+getE
 ngine
+()
 .getSche
@@ -2656,24 +2656,85 @@
 oid run() %7B%0A
+%09%09%09%09Player%5B%5D onlinePlayers = getEngine().getOnlinePlayers();%0A
 %09%09%09%09//Do not
@@ -2859,22 +2859,27 @@
 == 0 %7C%7C 
-e
+getE
 ngine
+()
 .getOnli
@@ -2950,19 +2950,9 @@
 r : 
-engine.getO
+o
 nlin
@@ -2959,18 +2959,16 @@
 ePlayers
-()
 ) %7B%0A%09%09%09%09
@@ -3343,49 +3343,8 @@
 ));%0A
-%09%09getLogger().log(Level.INFO, %22loaded%22);%0A
 %09%7D%0A%0A

Update for SpoutAPI changes. Signed-off-by: Chris Sanders <zidane@outlook.com>