/*
 * This file is part of LuckPerms, licensed under the MIT License.
 *
 *  Copyright (c) lucko (Luck) <luck@lucko.me>
 *  Copyright (c) contributors
 *
 *  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 me.lucko.luckperms.common.commands.sender;

import lombok.EqualsAndHashCode;
import lombok.Getter;

import me.lucko.luckperms.common.constants.Constants;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;

import io.github.mkremins.fanciful.FancyMessage;

import java.lang.ref.WeakReference;
import java.util.UUID;

/**
 * Simple implementation of {@link Sender} using a {@link SenderFactory}
 *
 * @param <T> the command sender type
 */
@Getter
@EqualsAndHashCode(of = "uuid")
public final class AbstractSender<T> implements Sender {
    private final LuckPermsPlugin platform;
    private final SenderFactory<T> factory;
    private final WeakReference<T> ref;
    private final String name;
    private final UUID uuid;

    AbstractSender(LuckPermsPlugin platform, SenderFactory<T> factory, T t) {
        this.platform = platform;
        this.factory = factory;
        this.ref = new WeakReference<>(t);
        this.name = factory.getName(t);
        this.uuid = factory.getUuid(t);
    }

    @Override
    public void sendMessage(String s) {
        final T t = ref.get();
        if (t != null) {
            factory.sendMessage(t, s);
        }
    }

    @Override
    public void sendMessage(FancyMessage message) {
        final T t = ref.get();
        if (t != null) {
            factory.sendMessage(t, message);
        }
    }

    @Override
    public boolean hasPermission(Permission permission) {
        if (isConsole()) return true;

        T t = ref.get();
        if (t != null) {
            for (String s : permission.getNodes()) {
                if (factory.hasPermission(t, s)) {
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    public boolean isConsole() {
        return this.uuid.equals(Constants.CONSOLE_UUID) || this.uuid.equals(Constants.IMPORT_UUID);
    }

    @Override
    public boolean isImport() {
        return this.uuid.equals(Constants.IMPORT_UUID);
    }

}

@@ -1343,24 +1343,65 @@
 ok.Getter;%0A%0A
+import com.google.common.base.Splitter;%0A%0A
 import me.lu
@@ -1884,16 +1884,90 @@
 ender %7B%0A
+    private static final Splitter NEW_LINE_SPLITTER = Splitter.on(%22%5Cn%22);%0A%0A
     priv
@@ -2523,32 +2523,69 @@
 f (t != null) %7B%0A
+%0A            if (!isConsole()) %7B%0A    
             fact
@@ -2619,82 +2619,430 @@
     
-%7D%0A    %7D%0A%0A    @Override%0A    public void sendMessage(FancyMessage message) %7B
+        return;%0A            %7D%0A%0A            // if it is console, split up the lines and send individually.%0A            for (String line : NEW_LINE_SPLITTER.split(s)) %7B%0A                factory.sendMessage(t, line);%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void sendMessage(FancyMessage message) %7B%0A        if (isConsole()) %7B%0A            sendMessage(message.toOldMessageFormat());%0A            return;%0A        %7D%0A
 %0A   

Split up chat messages sent to console containing new lines