summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/ICommandDispatcher.java
blob: 055171da917bcb8b2523d5fa457322eca6730d9a (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package io.dico.dicore.command;

import io.dico.dicore.command.parameter.ArgumentBuffer;
import io.dico.dicore.command.registration.CommandMap;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;

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

public interface ICommandDispatcher {

    /**
     * Get a potentially indirect child of the root of this dispatcher
     *
     * @param buffer the argument buffer with the subsequent keys to traverse. Any keys beyond the first that isn't found are ignored.
     * @return the child, or this same instance of no child is found.
     */
    ICommandAddress getDeepChild(ArgumentBuffer buffer);

    /**
     * Similar to {@link #getDeepChild(ArgumentBuffer)},
     * but this method incorporates checks on the command of traversed children:
     * {@link Command#isVisibleTo(CommandSender)}
     * and {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)}
     * <p>
     * The target of a command is never null, however, the same instance might be returned, and the returned address might not hold a command.
     *
     * @param sender the sender of the command
     * @param buffer the command itself as a buffer.
     * @return the address that is the target of the command.
     */
    @Deprecated
    ICommandAddress getCommandTarget(CommandSender sender, ArgumentBuffer buffer);

    /**
     * Similar to {@link #getDeepChild(ArgumentBuffer)},
     * but this method incorporates checks on the command of traversed children:
     * {@link Command#isVisibleTo(CommandSender)}
     * and {@link Command#takePrecedenceOverSubcommand(String, ArgumentBuffer)}
     * <p>
     * The target of a command is never null, however, the same instance might be returned, and the returned address might not hold a command.
     *
     * @param context the context of the command. The context must not have its address set.
     * @param buffer the command itself as a buffer.
     * @return the address that is the target of the command.
     */
    ICommandAddress getCommandTarget(ExecutionContext context, ArgumentBuffer buffer) throws CommandException;

    /**
     * dispatch the command
     *
     * @param sender  the sender
     * @param command the command
     * @return true if a command has executed
     */
    boolean dispatchCommand(CommandSender sender, String[] command);

    /**
     * dispatch the command
     *
     * @param sender    the sender
     * @param usedLabel the label (word after the /)
     * @param args      the arguments
     * @return true if a command has executed
     */
    boolean dispatchCommand(CommandSender sender, String usedLabel, String[] args);

    /**
     * dispatch the command
     *
     * @param sender the sender
     * @param buffer the command
     * @return true if a command has executed
     */
    boolean dispatchCommand(CommandSender sender, ArgumentBuffer buffer);

    /**
     * suggest tab completions
     *
     * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @param args the arguments as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     *             args must be sanitized such that it contains no empty elements, particularly at the last index.
     * @return tab completions
     */
    List<String> getTabCompletions(CommandSender sender, Location location, String[] args);

    /**
     * suggest tab completions
     *
     * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @param usedLabel the label as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @param args the arguments as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @return tab completions
     */
    List<String> getTabCompletions(CommandSender sender, String usedLabel, Location location, String[] args);

    /**
     * suggest tab completions
     *
     * @param sender the sender as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @param location the location as passed to {@link org.bukkit.command.Command#tabComplete(CommandSender, String, String[], Location)}
     * @param buffer the arguments as a buffer
     * @return tab completions
     */
    List<String> getTabCompletions(CommandSender sender, Location location, ArgumentBuffer buffer);

    /**
     * Register this dispatcher's commands to the command map
     *
     * @throws UnsupportedOperationException if this dispatcher is not the root of the tree
     */
    default void registerToCommandMap() {
        registerToCommandMap(null, CommandMap.getCommandMap(), EOverridePolicy.OVERRIDE_ALL);
    }

    /**
     * Register this dispatcher's commands to the command map
     *
     * @param fallbackPrefix the fallback prefix to use, null if none
     * @param overridePolicy the override policy
     * @throws UnsupportedOperationException if this dispatcher is not the root of the tree
     */
    default void registerToCommandMap(String fallbackPrefix, EOverridePolicy overridePolicy) {
        registerToCommandMap(fallbackPrefix, CommandMap.getCommandMap(), overridePolicy);
    }

    /**
     * Register this dispatcher's commands to the command map
     *
     * @param fallbackPrefix the fallback prefix to use, null if none
     * @param map            the command map
     * @param overridePolicy the override policy
     * @throws UnsupportedOperationException if this dispatcher is not the root of the tree
     */
    void registerToCommandMap(String fallbackPrefix, Map<String, org.bukkit.command.Command> map, EOverridePolicy overridePolicy);

    default void unregisterFromCommandMap() {
        unregisterFromCommandMap(CommandMap.getCommandMap());
    }

    void unregisterFromCommandMap(Map<String, org.bukkit.command.Command> map);

}