@@ -64,21 +64,34 @@ public static FunctionRegistry getRegistry() {
64
64
65
65
/**
66
66
* Registers a signature.
67
+ * <p>
68
+ * Attempting to register a local signature in the global namespace, or a global signature in
69
+ * a local namespace, will throw an {@link IllegalArgumentException}.
70
+ * </p>
67
71
*
68
72
* @param namespace The namespace to register the signature in.
69
73
* If namespace is null, will register this signature globally.
70
74
* Usually represents the path of the script this signature is registered in.
71
75
* @param signature The signature to register.
72
76
* @throws SkriptAPIException if a signature with the same name and parameters is already registered
73
77
* in this namespace.
78
+ * @throws IllegalArgumentException if the signature is global and namespace is not null, or
79
+ * if the signature is local and namespace is null.
74
80
*/
75
81
public void register (@ Nullable String namespace , @ NotNull Signature <?> signature ) {
76
82
Preconditions .checkNotNull (signature , "signature cannot be null" );
83
+ if (signature .isLocal () && namespace == null ) {
84
+ throw new IllegalArgumentException ("Cannot register a local signature in the global namespace" );
85
+ }
86
+ if (!signature .isLocal () && namespace != null ) {
87
+ throw new IllegalArgumentException ("Cannot register a global signature in a local namespace" );
88
+ }
89
+
77
90
Skript .debug ("Registering signature '%s'" , signature .getName ());
78
91
79
92
// namespace
80
93
NamespaceIdentifier namespaceId ;
81
- if (namespace != null && signature . isLocal () ) {
94
+ if (namespace != null ) {
82
95
namespaceId = new NamespaceIdentifier (namespace );
83
96
} else {
84
97
namespaceId = GLOBAL_NAMESPACE ;
@@ -118,17 +131,29 @@ public void register(@NotNull Function<?> function) {
118
131
119
132
/**
120
133
* Registers a function.
134
+ * <p>
135
+ * Attempting to register a local function in the global namespace, or a global function in
136
+ * a local namespace, will throw an {@link IllegalArgumentException}.
137
+ * </p>
121
138
*
122
139
* @param namespace The namespace to register the function in.
123
- * If namespace is null, will register this function globally.
140
+ * If namespace is null, will register this function globally, only if the function is global .
124
141
* Usually represents the path of the script this function is registered in.
125
142
* @param function The function to register.
126
- * @throws SkriptAPIException if the function name is invalid or if
127
- * a function with the same name and parameters is already registered
128
- * in this namespace.
143
+ * @throws SkriptAPIException if the function name is invalid or if
144
+ * a function with the same name and parameters is already registered
145
+ * in this namespace.
146
+ * @throws IllegalArgumentException if the function is global and namespace is not null, or
147
+ * if the function is local and namespace is null.
129
148
*/
130
149
public void register (@ Nullable String namespace , @ NotNull Function <?> function ) {
131
150
Preconditions .checkNotNull (function , "function cannot be null" );
151
+ if (function .getSignature ().isLocal () && namespace == null ) {
152
+ throw new IllegalArgumentException ("Cannot register a local function in the global namespace" );
153
+ }
154
+ if (!function .getSignature ().isLocal () && namespace != null ) {
155
+ throw new IllegalArgumentException ("Cannot register a global function in a local namespace" );
156
+ }
132
157
Skript .debug ("Registering function '%s'" , function .getName ());
133
158
134
159
String name = function .getName ();
@@ -138,7 +163,7 @@ public void register(@Nullable String namespace, @NotNull Function<?> function)
138
163
139
164
// namespace
140
165
NamespaceIdentifier namespaceId ;
141
- if (namespace != null && function . getSignature (). isLocal () ) {
166
+ if (namespace != null ) {
142
167
namespaceId = new NamespaceIdentifier (namespace );
143
168
} else {
144
169
namespaceId = GLOBAL_NAMESPACE ;
@@ -487,8 +512,13 @@ public void remove(@NotNull Signature<?> signature) {
487
512
String name = signature .getName ();
488
513
FunctionIdentifier identifier = FunctionIdentifier .of (signature );
489
514
490
- Namespace namespace = namespaces .getOrDefault (new NamespaceIdentifier (signature .script ),
491
- namespaces .get (GLOBAL_NAMESPACE ));
515
+ Namespace namespace ;
516
+ if (signature .isLocal ()) {
517
+ namespace = namespaces .get (new NamespaceIdentifier (signature .script ));
518
+ } else {
519
+ namespace = namespaces .get (GLOBAL_NAMESPACE );
520
+ }
521
+
492
522
if (namespace == null ) {
493
523
return ;
494
524
}
@@ -532,6 +562,7 @@ private record NamespaceIdentifier(@Nullable String name) {
532
562
533
563
/**
534
564
* Returns whether this identifier is for local namespaces.
565
+ *
535
566
* @return Whether this identifier is for local namespaces.
536
567
*/
537
568
public boolean local () {
0 commit comments