summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/chat/AbstractChatController.java
blob: d88e85219ab07a227f65b143cb53fb3c04bc949d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package io.dico.dicore.command.chat;

import io.dico.dicore.command.CommandException;
import io.dico.dicore.command.EMessageType;
import io.dico.dicore.command.ExecutionContext;
import io.dico.dicore.command.ICommandAddress;
import io.dico.dicore.command.chat.help.HelpPages;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

public class AbstractChatController implements IChatController {
    private @NotNull HelpPages helpPages;

    public AbstractChatController(@NotNull HelpPages helpPages) {
        this.helpPages = helpPages;
    }

    public AbstractChatController() {
        this(HelpPages.newDefaultHelpPages());
    }

    @NotNull
    public HelpPages getHelpPages() {
        return helpPages;
    }

    public void setHelpPages(@NotNull HelpPages helpPages) {
        this.helpPages = helpPages;
    }

    @Override
    public void sendMessage(ExecutionContext context, EMessageType type, String message) {
        sendMessage(context.getSender(), type, message);
    }

    @Override
    public void sendMessage(CommandSender sender, EMessageType type, String message) {
        if (message != null && !message.isEmpty()) {
            sender.sendMessage(filterMessage(getMessagePrefixForType(type) + getChatFormatForType(type) + message));
        }
    }

    @Override
    public void handleCommandException(CommandSender sender, ExecutionContext context, CommandException exception) {
        sendMessage(sender, EMessageType.EXCEPTION, exception.getMessage());
    }

    @Override
    public void handleException(CommandSender sender, ExecutionContext context, Throwable exception) {
        if (exception instanceof CommandException) {
            handleCommandException(sender, context, (CommandException) exception);
        } else {
            sendMessage(sender, EMessageType.EXCEPTION, "An internal error occurred whilst executing this command");
            exception.printStackTrace();
        }
    }

    @Override
    public void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
        sender.sendMessage(helpPages.getHelpPage(sender, context, address, page));
    }

    @Override
    public void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
        sender.sendMessage(helpPages.getSyntax(sender, context, address));
    }

    @Override
    public Formatting getChatFormatForType(EMessageType type) {
        switch (type) {
            case EXCEPTION:
            case BAD_NEWS:
                return Formatting.RED;
            case INSTRUCTION:
            case NEUTRAL:
                return Formatting.GRAY;
            case CUSTOM:
                return Formatting.WHITE;
            case INFORMATIVE:
                return Formatting.AQUA;
            case RESULT:
            default:
            case GOOD_NEWS:
                return Formatting.GREEN;
            case WARNING:
                return Formatting.YELLOW;

            case DESCRIPTION:
                return Formatting.GREEN;
            case SYNTAX:
                return Formatting.BLUE;
            case HIGHLIGHT:
                return Formatting.RED;
            case SUBCOMMAND:
                return Formatting.GRAY;
            case NUMBER:
                return Formatting.YELLOW;
        }
    }

    @Override
    public String getMessagePrefixForType(EMessageType type) {
        return "";
    }

    @Override
    public String filterMessage(String message) {
        return message;
    }

}