-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathVelocityCommandManager.java
More file actions
186 lines (167 loc) · 7.63 KB
/
VelocityCommandManager.java
File metadata and controls
186 lines (167 loc) · 7.63 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// 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 org.incendo.cloud.velocity;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Singleton;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.player.ServerPreConnectEvent;
import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.SenderMapperHolder;
import org.incendo.cloud.brigadier.BrigadierManagerHolder;
import org.incendo.cloud.brigadier.CloudBrigadierManager;
import org.incendo.cloud.brigadier.suggestion.TooltipSuggestion;
import org.incendo.cloud.caption.CaptionProvider;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.suggestion.SuggestionFactory;
import org.incendo.cloud.velocity.parser.PlayerParser;
import org.incendo.cloud.velocity.parser.ServerParser;
/**
* {@link CommandManager} implementation for Velocity.
* <p>
* This can be injected if {@link CloudInjectionModule} is registered in the
* injector. This can be achieved by using {@link com.google.inject.Injector#createChildInjector(Module...)}
* <p>
* {@link #suggestionFactory()} has been overridden to map suggestions to {@link TooltipSuggestion}.
* You may use {@link TooltipSuggestion} to display tooltips for your suggestions.
* {@link com.velocitypowered.api.command.VelocityBrigadierMessage} can be used to make use of Adventure
* {@link net.kyori.adventure.text.Component components} in the tooltips.
*
* @param <C> Command sender type
*/
@Singleton
public class VelocityCommandManager<C> extends CommandManager<C>
implements BrigadierManagerHolder<C, CommandSource>, SenderMapperHolder<CommandSource, C> {
/**
* Default caption for {@link VelocityCaptionKeys#ARGUMENT_PARSE_FAILURE_PLAYER}
*/
public static final String ARGUMENT_PARSE_FAILURE_PLAYER = "'<input>' is not a valid player";
/**
* Default caption for {@link VelocityCaptionKeys#ARGUMENT_PARSE_FAILURE_SERVER}
*/
public static final String ARGUMENT_PARSE_FAILURE_SERVER = "'<input>' is not a valid server";
private final ProxyServer proxyServer;
private final SenderMapper<CommandSource, C> senderMapper;
private final SuggestionFactory<C, ? extends TooltipSuggestion> suggestionFactory;
/**
* Create a new command manager instance
*
* @param plugin Container for the owning plugin
* @param proxyServer ProxyServer instance
* @param commandExecutionCoordinator Coordinator provider
* @param senderMapper Function that maps {@link CommandSource} to the command sender type
*/
@Inject
@SuppressWarnings({"unchecked", "this-escape"})
public VelocityCommandManager(
final @NonNull PluginContainer plugin,
final @NonNull ProxyServer proxyServer,
final @NonNull ExecutionCoordinator<C> commandExecutionCoordinator,
final @NonNull SenderMapper<CommandSource, C> senderMapper
) {
super(commandExecutionCoordinator, new VelocityPluginRegistrationHandler<>());
this.proxyServer = proxyServer;
this.senderMapper = senderMapper;
this.suggestionFactory = super.suggestionFactory().mapped(TooltipSuggestion::tooltipSuggestion);
((VelocityPluginRegistrationHandler<C>) this.commandRegistrationHandler()).initialize(this);
/* Register Velocity Preprocessor */
this.registerCommandPreProcessor(new VelocityCommandPreprocessor<>(this));
/* Register Velocity Parsers */
this.parserRegistry()
.registerParser(PlayerParser.playerParser())
.registerParser(ServerParser.serverParser());
/* Register default captions */
this.captionRegistry()
.registerProvider(CaptionProvider.<C>constantProvider()
.putCaption(VelocityCaptionKeys.ARGUMENT_PARSE_FAILURE_PLAYER, ARGUMENT_PARSE_FAILURE_PLAYER)
.putCaption(VelocityCaptionKeys.ARGUMENT_PARSE_FAILURE_SERVER, ARGUMENT_PARSE_FAILURE_SERVER)
.build());
this.proxyServer.getEventManager().register(plugin, ServerPreConnectEvent.class, ev -> {
this.lockRegistration();
});
this.parameterInjectorRegistry().registerInjector(
CommandSource.class,
(context, annotations) -> this.senderMapper.reverse(context.sender())
);
this.registerDefaultExceptionHandlers();
}
@Override
public final boolean hasPermission(final @NonNull C sender, final @NonNull String permission) {
return this.senderMapper.reverse(sender).hasPermission(permission);
}
/**
* {@inheritDoc}
*
* <p>This will always return true for {@link VelocityCommandManager}s.</p>
*
* @return {@code true}
* @since 2.0.0
*/
@API(status = API.Status.STABLE, since = "2.0.0")
@Override
public final boolean hasBrigadierManager() {
return true;
}
/**
* {@inheritDoc}
*
* <p>{@link VelocityCommandManager}s always use Brigadier for registration, so the aforementioned check is not needed.</p>
*
* @return {@inheritDoc}
* @since 1.2.0
*/
@API(status = API.Status.STABLE, since = "2.0.0")
@Override
public final @NonNull CloudBrigadierManager<C, CommandSource> brigadierManager() {
return ((VelocityPluginRegistrationHandler<C>) this.commandRegistrationHandler()).brigadierManager();
}
@Override
public final @NonNull SuggestionFactory<C, ? extends TooltipSuggestion> suggestionFactory() {
return this.suggestionFactory;
}
final @NonNull ProxyServer proxyServer() {
return this.proxyServer;
}
private void registerDefaultExceptionHandlers() {
this.registerDefaultExceptionHandlers(
triplet -> {
final CommandSource source = this.senderMapper().reverse(triplet.first().sender());
final String message = triplet.first().formatCaption(triplet.second(), triplet.third());
source.sendMessage(Component.text(message, NamedTextColor.RED));
},
pair -> pair.second().printStackTrace()
);
}
@Override
public final @NonNull SenderMapper<CommandSource, C> senderMapper() {
return this.senderMapper;
}
}