1
|
|
|
/* |
2
|
|
|
* This file is part of Araknemu. |
3
|
|
|
* |
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.game.admin; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.common.account.Permission; |
23
|
|
|
import fr.quatrevieux.araknemu.game.admin.exception.CommandException; |
24
|
|
|
import fr.quatrevieux.araknemu.game.admin.help.CommandHelp; |
25
|
|
|
|
26
|
|
|
import java.util.Arrays; |
27
|
|
|
import java.util.EnumSet; |
28
|
|
|
import java.util.Set; |
29
|
|
|
import java.util.function.Consumer; |
30
|
|
|
import java.util.function.Supplier; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Base command class |
34
|
1 |
|
*/ |
35
|
1 |
|
public abstract class AbstractCommand<A> implements Command<A> { |
36
|
1 |
|
private CommandHelp help = new CommandHelp(this); |
37
|
|
|
private final EnumSet<Permission> permissions = EnumSet.of(Permission.ACCESS); |
38
|
1 |
|
private Supplier<A> argumentsFactory; |
39
|
|
|
private boolean initialized = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Build the command |
43
|
|
|
*/ |
44
|
|
|
protected abstract void build(Builder builder); |
45
|
|
|
|
46
|
|
|
@Override |
47
|
1 |
|
public CommandHelp help() { |
48
|
|
|
initialize(); |
49
|
1 |
|
|
50
|
|
|
return help; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
@Override |
54
|
1 |
|
public final Set<Permission> permissions() { |
55
|
|
|
initialize(); |
56
|
1 |
|
|
57
|
|
|
return permissions; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
@Override |
61
|
1 |
|
public A createArguments() { |
62
|
|
|
initialize(); |
63
|
1 |
|
|
64
|
1 |
|
if (argumentsFactory != null) { |
65
|
|
|
return argumentsFactory.get(); |
66
|
|
|
} |
67
|
1 |
|
|
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
/** |
72
|
1 |
|
* Raise a command error and stop execution of the command |
73
|
1 |
|
* |
74
|
|
|
* @param message The error message |
75
|
1 |
|
*/ |
76
|
|
|
protected final void error(String message) throws CommandException { |
77
|
1 |
|
throw new CommandException(name(), message); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private void initialize() { |
81
|
|
|
if (!initialized) { |
82
|
|
|
build(new Builder()); |
83
|
|
|
initialized = true; |
84
|
|
|
} |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
protected final class Builder { |
88
|
|
|
/** |
89
|
|
|
* Define the command arguments factory |
90
|
|
|
* |
91
|
|
|
* Note: this method should be used only for annotated object argument. |
92
|
|
|
* For other arguments type, prefer overrides the method {@link Command#createArguments()} |
93
|
|
|
*/ |
94
|
|
|
public Builder arguments(Supplier<A> constructor) { |
95
|
|
|
AbstractCommand.this.argumentsFactory = constructor; |
96
|
|
|
|
97
|
|
|
return this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Configure the help page |
102
|
|
|
* |
103
|
1 |
|
* <code> |
104
|
|
|
* builder.help( |
105
|
1 |
|
* formatter -> formatter |
106
|
|
|
* .synopsis("my_command [option]") |
107
|
|
|
* .options("--opt", "opt description") |
108
|
|
|
* .example("my_command --opt", "example description") |
109
|
|
|
* ); |
110
|
|
|
* </code> |
111
|
|
|
*/ |
112
|
1 |
|
public Builder help(Consumer<CommandHelp.Builder> configurator) { |
113
|
|
|
help = help.modify(configurator); |
114
|
1 |
|
|
115
|
|
|
return this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add required permissions |
120
|
|
|
*/ |
121
|
|
|
public Builder requires(Permission... permissions) { |
122
|
|
|
AbstractCommand.this.permissions.addAll(Arrays.asList(permissions)); |
123
|
|
|
|
124
|
|
|
return this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|