/*
 * This file is part of GenericsLib for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


package com.jcwhatever.bukkit.generic.scripting.api;

import com.jcwhatever.bukkit.generic.messaging.Messenger;
import com.jcwhatever.bukkit.generic.messaging.Messenger.LineWrapping;
import com.jcwhatever.bukkit.generic.utils.PlayerUtils;
import com.jcwhatever.bukkit.generic.scripting.IEvaluatedScript;
import com.jcwhatever.bukkit.generic.scripting.ScriptApiInfo;
import com.jcwhatever.bukkit.generic.utils.PreCon;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

/**
 * Provide scripts with api access to generics messenger.
 */
@ScriptApiInfo(
        variableName = "msg",
        description = "Provide scripts with API access to chat messenger.")
public class ScriptApiMsg extends GenericsScriptApi {

    private ApiObject _api;

    /**
     * Constructor. Automatically adds variable to script.
     *
     * @param plugin The owning plugin
     */
    public ScriptApiMsg(Plugin plugin) {
        super(plugin);
    }

    @Override
    public IScriptApiObject getApiObject(IEvaluatedScript script) {
        if (_api == null)
            _api = new ApiObject(getPlugin());

        return _api;
    }

    public static class ApiObject implements IScriptApiObject {

        private final Plugin _plugin;

        ApiObject(Plugin plugin) {
            _plugin = plugin;
        }

        @Override
        public boolean isDisposed() {
            return false;
        }

        @Override
        public void dispose() {
            // do nothing
        }

        /**
         * Tell a player a message.
         *
         * @param player   The player to tell.
         * @param message  The message to send.
         * @param params   Optional message formatting parameters.
         */
        public void tell(Object player, String message, Object... params) {
            PreCon.notNull(player);
            PreCon.notNull(message);

            Player p = PlayerUtils.getPlayer(player);
            PreCon.notNull(p);

            Messenger.tell(LineWrapping.DISABLED, _plugin, p, message, params);
        }

        /**
         * Tell a player a message without a plugin tag in the message.
         *
         * @param player   The player to tell.
         * @param message  The message to send.
         * @param params   Optional message formatting parameters.
         */
        public void tellAnon(Object player, String message, Object... params) {
            PreCon.notNull(player);
            PreCon.notNull(message);

            Player p = PlayerUtils.getPlayer(player);
            PreCon.notNull(p);

            Messenger.tell(LineWrapping.DISABLED, null, p, message, params);
        }

        /**
         * Tell a player a message and prevent spamming of the same message.
         *
         * @param player   The player to tell.
         * @param timeout  The spam timeout, the amount of time to wait before the message can be seen again.
         * @param message  The message to send.
         * @param params   Optional message formatting parameters.
         */
        public void tellNoSpam(Object player, int timeout, String message, Object... params) {
            PreCon.notNull(player);
            PreCon.greaterThanZero(timeout);
            PreCon.notNull(message);

            Player p = PlayerUtils.getPlayer(player);
            PreCon.notNull(p);

            Messenger.tellNoSpam(LineWrapping.DISABLED, _plugin, p, timeout, message, params);
        }

        /**
         * Tell a player a message and prevent spamming of the same message
         * without a plugin tag in the message.
         *
         * @param player        The player to tell.
         * @param timeout  The spam timeout, the amount of time to wait before the message can be seen again.
         * @param message  The message to send.
         * @param params   Optional message formatting parameters.
         */
        public void tellNoSpamAnon(Object player, int timeout, String message, Object... params) {
            PreCon.notNull(player);
            PreCon.greaterThanZero(timeout);
            PreCon.notNull(message);

            Player p = PlayerUtils.getPlayer(player);
            PreCon.notNull(p);

            Messenger.tellNoSpam(LineWrapping.DISABLED, null, p, timeout, message, params);
        }

        /**
         * Send scripting debug message to console.
         *
         * @param message  The message to send.
         * @param params   Optional message formatting parameters.
         */
        public void debug(String message, Object... params) {
            PreCon.notNull(message);

            Messenger.warning(_plugin, "[SCRIPT DEBUG] " + message, params);
        }
    }
}

@@ -1402,64 +1402,8 @@
 ng;%0A
-import com.jcwhatever.bukkit.generic.utils.PlayerUtils;%0A
 impo
@@ -1525,16 +1525,72 @@
 piInfo;%0A
+import com.jcwhatever.bukkit.generic.utils.PlayerUtils;%0A
 import c
@@ -1632,16 +1632,58 @@
 PreCon;%0A
+import com.sun.istack.internal.Nullable;%0A%0A
 import o
@@ -1988,37 +1988,8 @@
  %7B%0A%0A
-    private ApiObject _api;%0A%0A
     
@@ -2270,93 +2270,30 @@
     
-if (_api == null)%0A            _api = new ApiObject(getPlugin());%0A%0A        return _api
+return new ApiObject()
 ;%0A  
@@ -2382,105 +2382,64 @@
 ate 
-final Plugin _plugin;%0A%0A        ApiObject(Plugin plugin) %7B%0A            _plugin = plugin;%0A        %7D
+Object _chatPrefix;%0A        private boolean _isDisposed;
 %0A%0A  
@@ -2515,13 +2515,19 @@
 urn 
-fal
+_isDispo
 se
+d
 ;%0A  
@@ -2601,21 +2601,277 @@
     
-// do nothing
+_chatPrefix = null;%0A            _isDisposed = true;%0A        %7D%0A%0A        /**%0A         * Set the chat prefix to use.%0A         *%0A         * @param prefix  The prefix.%0A         */%0A        public void setChatPrefix(@Nullable Object prefix) %7B%0A            _chatPrefix = prefix;
 %0A   
@@ -3394,30 +3394,34 @@
 .DISABLED, _
-plugin
+chatPrefix
 , p, message
@@ -4780,22 +4780,26 @@
 ABLED, _
-plugin
+chatPrefix
 , p, tim
@@ -6013,17 +6013,22 @@
 ng(_
-plugin
+chatPrefix
 , %22
+ 
 %5BSCR

allow setting message prefixes