summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java
blob: 43403562ff32fb892b70744853ce7c3580ae0e1a (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
package io.dico.dicore.command.predef;

import io.dico.dicore.command.CommandBuilder;
import io.dico.dicore.command.ExtendedCommand;
import io.dico.dicore.command.ICommandAddress;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Marker class for commands that are generated. These commands can be replaced using methods in {@link CommandBuilder}
 */
public abstract class PredefinedCommand<T extends PredefinedCommand<T>> extends ExtendedCommand<T> {
    static final Map<String, Consumer<ICommandAddress>> predefinedCommandGenerators = new HashMap<>();

    /**
     * Get a predefined command
     *
     * @param name the name
     * @return the subscriber
     */
    public static Consumer<ICommandAddress> getPredefinedCommandGenerator(String name) {
        return predefinedCommandGenerators.get(name);
    }

    /**
     * Register a predefined command
     *
     * @param name     the name
     * @param consumer the generator which adds the child to the address
     * @return true if and only if the subscriber was registered (false if the name exists)
     */
    public static boolean registerPredefinedCommandGenerator(String name, Consumer<ICommandAddress> consumer) {
        return predefinedCommandGenerators.putIfAbsent(name, consumer) == null;
    }

    static {
        registerPredefinedCommandGenerator("help", HelpCommand::registerAsChild);
        //noinspection StaticInitializerReferencesSubClass
        registerPredefinedCommandGenerator("syntax", SyntaxCommand::registerAsChild);
    }

    public PredefinedCommand() {
    }

    public PredefinedCommand(boolean modifiable) {
        super(modifiable);
    }
}