-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
CommodoreBukkitBrigadier.java
118 lines (103 loc) · 4.79 KB
/
CommodoreBukkitBrigadier.java
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
/*
* This file is part of lamp, licensed under the MIT License.
*
* Copyright (c) Revxrsal <reflxction.github@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package revxrsal.commands.bukkit.brigadier;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.bukkit.BukkitBrigadier;
import revxrsal.commands.bukkit.BukkitCommandHandler;
import revxrsal.commands.bukkit.EntitySelector;
import revxrsal.commands.bukkit.core.BukkitActor;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.command.CommandParameter;
import revxrsal.commands.util.ClassMap;
import static revxrsal.commands.bukkit.brigadier.CommodoreProvider.isSupported;
import static revxrsal.commands.bukkit.brigadier.DefaultArgTypeResolvers.*;
import static revxrsal.commands.util.Preconditions.notNull;
public final class CommodoreBukkitBrigadier implements BukkitBrigadier {
private final BukkitCommandHandler handler;
private final Commodore commodore;
private final ClassMap<ArgumentTypeResolver> argumentTypes = new ClassMap<>();
public CommodoreBukkitBrigadier(BukkitCommandHandler handler) {
this.handler = handler;
commodore = CommodoreProvider.getCommodore(handler.getPlugin());
if (isSupported()) {
bind(String.class, STRING);
bind(Number.class, NUMBER);
bind(Boolean.class, BOOLEAN);
bind(Player.class, PLAYER);
bind(EntitySelector.class, ENTITY_SELECTOR);
}
}
@Override public void bind(@NotNull Class<?> type, @NotNull ArgumentTypeResolver resolver) {
notNull(type, "type");
notNull(resolver, "resolver");
argumentTypes.add(type, resolver);
}
@Override public void bind(@NotNull Class<?> type, @NotNull ArgumentType<?> argumentType) {
notNull(type, "type");
notNull(argumentType, "argument type");
argumentTypes.add(type, parameter -> argumentType);
}
@Override public void bind(@NotNull Class<?> type, @NotNull MinecraftArgumentType argumentType) {
notNull(type, "type");
notNull(argumentType, "argument type");
argumentType.getIfPresent().ifPresent(c -> argumentTypes.add(type, parameter -> c));
}
public @NotNull ArgumentType<?> getArgumentType(@NotNull CommandParameter parameter) {
ArgumentTypeResolver resolver = argumentTypes.getFlexible(parameter.getType());
if (resolver != null) {
ArgumentType<?> type = resolver.getArgumentType(parameter);
if (type != null)
return type;
}
return StringArgumentType.string();
}
private void checkSupported() {
if (commodore == null)
throw new IllegalArgumentException("Brigadier is not supported on this version.");
}
@Override public @NotNull CommandActor wrapSource(@NotNull Object commandSource) {
checkSupported();
return new BukkitActor(commodore.getBukkitSender(commandSource), handler);
}
@Override public void register() {
if (!isSupported()) return;
BukkitBrigadierTreeParser.parse(this, handler).forEach(n -> register(n.build()));
}
@Override public @NotNull BukkitCommandHandler getCommandHandler() {
return handler;
}
private void register(@NotNull LiteralCommandNode<?> node) {
Command command = ((JavaPlugin) handler.getPlugin()).getCommand(node.getLiteral());
if (command == null)
commodore.register(node);
else
commodore.register(command, node);
}
}