11package org .skriptlang .skript .bukkit .command .brigadier ;
22
33import ch .njol .skript .Skript ;
4- import com .mojang .brigadier .tree .LiteralCommandNode ;
5- import io .papermc .paper .command .brigadier .CommandSourceStack ;
64import io .papermc .paper .command .brigadier .Commands ;
75import io .papermc .paper .plugin .configuration .PluginMeta ;
86import io .papermc .paper .plugin .lifecycle .event .types .LifecycleEvents ;
1614import java .lang .reflect .InvocationHandler ;
1715import java .lang .reflect .Method ;
1816import java .lang .reflect .Proxy ;
19- import java .util .Collection ;
2017import java .util .Map ;
2118import java .util .Set ;
2219import java .util .concurrent .ConcurrentHashMap ;
20+ import java .util .stream .Stream ;
2321
2422/**
2523 * Utility class for registering Brigadier commands at runtime through reflection.
2624 * This avoids a full reload via {@link Bukkit#reloadData()}.
2725 * However, that approach will be used if the reflection-based approach fails to load.
2826 */
29- public final class RuntimeCommandRegistrar {
30-
31- /**
32- *
33- * @param node The command node to register.
34- * @param aliases Aliases used to reference the command. Can be empty.
35- * @param description String describing the command.
36- */
37- public record CommandRegistration (
38- LiteralCommandNode <CommandSourceStack > node ,
39- Collection <String > aliases ,
40- @ Nullable String description ,
41- @ Nullable String namespace
42- ) { }
27+ public final class SkriptCommandRegistrar {
4328
4429 private static JavaPlugin plugin ;
4530
4631 private static Commands commandRegistrar ;
47- private static final Map <CommandRegistration , Set <String >> REGISTERED_COMMANDS = new ConcurrentHashMap <>();
48- private static final Set <CommandRegistration > PENDING_REGISTRATIONS = ConcurrentHashMap .newKeySet ();
32+ private static final Map <SkriptBrigadierCommand , Set <String >> REGISTERED_COMMANDS = new ConcurrentHashMap <>();
33+ private static final Set <SkriptBrigadierCommand > PENDING_REGISTRATIONS = ConcurrentHashMap .newKeySet ();
4934 private static final Set <String > PENDING_UNREGISTRATIONS = ConcurrentHashMap .newKeySet ();
5035
5136 private static @ Nullable MethodHandle SET_VALID ;
@@ -56,13 +41,13 @@ public record CommandRegistration(
5641 private static boolean useSafeReload ;
5742
5843 public static void init (JavaPlugin plugin ) {
59- RuntimeCommandRegistrar .plugin = plugin ;
44+ SkriptCommandRegistrar .plugin = plugin ;
6045 plugin .getLifecycleManager ().registerEventHandler (LifecycleEvents .COMMANDS , commands -> {
6146 commandRegistrar = commands .registrar ();
6247
6348 if (!REGISTERED_COMMANDS .isEmpty () || !PENDING_REGISTRATIONS .isEmpty ()) {
6449 REGISTERED_COMMANDS .replaceAll ((command , ignored ) ->
65- commandRegistrar .register (command .node , command .description , command .aliases ));
50+ commandRegistrar .register (command .node () , command .description () , command .aliases () ));
6651 processRegistrationSet ();
6752 return ;
6853 }
@@ -88,17 +73,17 @@ public static void init(JavaPlugin plugin) {
8873 * @param command The command to register.
8974 * @see #processRegistrations()
9075 */
91- public static void register (CommandRegistration command ) {
76+ public static void register (SkriptBrigadierCommand command ) {
9277 PENDING_REGISTRATIONS .add (command );
9378 }
9479
9580 /**
9681 * Processes all pending registrations, synchronizing them with the server's command dispatcher.
97- * @see #register(CommandRegistration )
82+ * @see #register(SkriptBrigadierCommand )
9883 */
9984 public static void processRegistrations () {
10085 if (!Bukkit .isPrimaryThread ()) {
101- Bukkit .getScheduler ().runTask (plugin , RuntimeCommandRegistrar ::processRegistrations );
86+ Bukkit .getScheduler ().runTask (plugin , SkriptCommandRegistrar ::processRegistrations );
10287 return ;
10388 }
10489
@@ -124,14 +109,14 @@ public static void processRegistrations() {
124109
125110 private static void processRegistrationSet () {
126111 PluginMeta pluginMeta = plugin .getPluginMeta ();
127- for (CommandRegistration command : PENDING_REGISTRATIONS ) {
128- if (command .namespace == null ) {
129- REGISTERED_COMMANDS .put (command , commandRegistrar .register (pluginMeta , command .node , command .description , command .aliases ));
112+ for (SkriptBrigadierCommand command : PENDING_REGISTRATIONS ) {
113+ if (command .namespace () == null ) {
114+ REGISTERED_COMMANDS .put (command , commandRegistrar .register (pluginMeta , command .node () , command .description () , command .aliases () ));
130115 } else {
131- TemporaryNamePluginMeta handler = new TemporaryNamePluginMeta (pluginMeta , command .namespace );
116+ TemporaryNamePluginMeta handler = new TemporaryNamePluginMeta (pluginMeta , command .namespace () );
132117 PluginMeta meta = (PluginMeta ) Proxy .newProxyInstance (pluginMeta .getClass ().getClassLoader (),
133118 new Class <?>[]{PluginMeta .class }, handler );
134- REGISTERED_COMMANDS .put (command , commandRegistrar .register (meta , command .node , command .description , command .aliases ));
119+ REGISTERED_COMMANDS .put (command , commandRegistrar .register (meta , command .node () , command .description () , command .aliases () ));
135120 handler .useAlternativeName = false ;
136121 }
137122 }
@@ -168,17 +153,17 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
168153 * @param command The command to unregister.
169154 * @see #processUnregistrations()
170155 */
171- public static void unregister (CommandRegistration command ) {
156+ public static void unregister (SkriptBrigadierCommand command ) {
172157 PENDING_UNREGISTRATIONS .addAll (REGISTERED_COMMANDS .remove (command ));
173158 }
174159
175160 /**
176161 * Processes all pending unregistrations, synchronizing them with the server's command dispatcher.
177- * @see #unregister(CommandRegistration )
162+ * @see #unregister(SkriptBrigadierCommand )
178163 */
179164 public static void processUnregistrations () {
180165 if (!Bukkit .isPrimaryThread ()) {
181- Bukkit .getScheduler ().runTask (plugin , RuntimeCommandRegistrar ::processUnregistrations );
166+ Bukkit .getScheduler ().runTask (plugin , SkriptCommandRegistrar ::processUnregistrations );
182167 return ;
183168 }
184169
@@ -203,4 +188,16 @@ public static void processUnregistrations() {
203188 }
204189 }
205190
191+ /**
192+ * Obtains a script command by its name.
193+ * @param command The name of the command.
194+ * @return The script command named {@code command}, or null if no script command with that name exists.
195+ */
196+ public static @ Nullable SkriptBrigadierCommand getCommand (String command ) {
197+ return Stream .concat (PENDING_REGISTRATIONS .stream (), REGISTERED_COMMANDS .keySet ().stream ())
198+ .filter (registration -> registration .node ().getLiteral ().equals (command ))
199+ .findFirst ()
200+ .orElse (null );
201+ }
202+
206203}
0 commit comments