summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/chat/help/defaults/SubcommandsHelpTopic.java
blob: 1e9922f4a3447978132754d9d088dd4419d3c58c (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
package io.dico.dicore.command.chat.help.defaults;

import io.dico.dicore.command.EMessageType;
import io.dico.dicore.command.ExecutionContext;
import io.dico.dicore.command.ICommandAddress;
import io.dico.dicore.command.chat.Formatting;
import io.dico.dicore.command.chat.help.IHelpComponent;
import io.dico.dicore.command.chat.help.IHelpTopic;
import io.dico.dicore.command.chat.help.SimpleHelpComponent;
import io.dico.dicore.command.predef.PredefinedCommand;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permissible;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SubcommandsHelpTopic implements IHelpTopic {

    @Override
    public List<IHelpComponent> getComponents(ICommandAddress target, Permissible viewer, ExecutionContext context) {
        List<IHelpComponent> out = new ArrayList<>();
        Map<String, ? extends ICommandAddress> children = target.getChildren();
        if (children.isEmpty()) {
            //System.out.println("No subcommands");
            return out;
        }

        CommandSender sender = viewer instanceof CommandSender ? (CommandSender) viewer : context.getSender();
        children.values().stream().distinct().forEach(child -> {
            if ((!child.hasCommand() || child.getCommand().isVisibleTo(sender)) && !(child instanceof PredefinedCommand)) {
                out.add(getComponent(child, viewer, context));
            }
        });

        return out;
    }

    public IHelpComponent getComponent(ICommandAddress child, Permissible viewer, ExecutionContext context) {
        Formatting subcommand = colorOf(context, EMessageType.SUBCOMMAND);
        Formatting highlight = colorOf(context, EMessageType.HIGHLIGHT);

        String address = subcommand + "/" + child.getParent().getAddress() + ' ' + highlight + child.getMainKey();

        String description = child.hasCommand() ? child.getCommand().getShortDescription() : null;
        if (description != null) {
            Formatting descriptionFormat = colorOf(context, EMessageType.DESCRIPTION);
            return new SimpleHelpComponent(address, descriptionFormat + description);
        }

        return new SimpleHelpComponent(address);
    }

    private static Formatting colorOf(ExecutionContext context, EMessageType type) {
        return context.getAddress().getChatController().getChatFormatForType(type);
    }

}