Skip to content

Commit ce75dc9

Browse files
UnderscoreTudAPickledWalrussovdeeth
authored
Make event value registration check more lenient (#8530)
* Make event value registration check more lenient * Switch from storing arrays to unmodifiable lists * Replace usages of List with SequencedCollection and return empty collections instead of null * Use Collection instead of SequencedCollection for excluded events and fix tests * Replace null checks with isEmpty checks --------- Co-authored-by: Patrick Miller <apickledwalrus@icloud.com> Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
1 parent 0d0999c commit ce75dc9

File tree

5 files changed

+87
-109
lines changed

5 files changed

+87
-109
lines changed

src/main/java/ch/njol/skript/registrations/EventValues.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ public record EventValueInfo<E extends Event, T>(
297297
}
298298

299299
public static <E extends Event, T> EventValueInfo<E, T> fromModern(EventValue<E, T> eventValue) {
300+
//noinspection unchecked
300301
return new EventValueInfo<>(
301302
eventValue.eventClass(),
302303
eventValue.valueClass(),
@@ -314,7 +315,7 @@ public void set(E event, @Nullable T value) {
314315
})
315316
.orElse(eventValue.converter()),
316317
eventValue.excludedErrorMessage(),
317-
eventValue.excludedEvents(),
318+
eventValue.excludedEvents().toArray(new Class[0]),
318319
eventValue.time().value()
319320
);
320321
}

src/main/java/org/skriptlang/skript/bukkit/lang/eventvalue/ConvertedEventValue.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import ch.njol.skript.classes.Changer.ChangeMode;
44
import org.bukkit.event.Event;
55
import org.jetbrains.annotations.Nullable;
6+
import org.jetbrains.annotations.Unmodifiable;
67
import org.skriptlang.skript.lang.converter.Converter;
78
import org.skriptlang.skript.lang.converter.Converters;
89

910
import java.lang.reflect.Array;
10-
import java.util.Arrays;
11+
import java.util.Collection;
1112
import java.util.Optional;
13+
import java.util.SequencedCollection;
1214

1315
/**
1416
* An {@link EventValue} that is a converted version of another event value.
@@ -124,7 +126,7 @@ public Class<ConvertedValue> valueClass() {
124126
}
125127

126128
@Override
127-
public String @Nullable [] patterns() {
129+
public @Unmodifiable SequencedCollection<String> patterns() {
128130
return source.patterns();
129131
}
130132

@@ -163,10 +165,6 @@ public Optional<Changer<ConvertedEvent, ConvertedValue>> changer(ChangeMode mode
163165
return source.changer(mode).map(changer -> (event, value) -> {
164166
if (!source.eventClass().isAssignableFrom(event.getClass()))
165167
return;
166-
if (changer instanceof EventValue.NoValueChanger) {
167-
changer.change(source.eventClass().cast(event), null);
168-
return;
169-
}
170168
if (reverseConverter == null)
171169
return;
172170
SourceValue sourceValue = reverseConverter.convert(value);
@@ -181,21 +179,23 @@ public Time time() {
181179
}
182180

183181
@Override
184-
public Class<? extends ConvertedEvent> @Nullable [] excludedEvents() {
185-
Class<? extends SourceEvent>[] excludedEvents = source.excludedEvents();
186-
if (excludedEvents == null)
187-
return null;
188-
//noinspection unchecked
189-
return Arrays.stream(excludedEvents)
182+
public @Unmodifiable Collection<Class<? extends ConvertedEvent>> excludedEvents() {
183+
//noinspection unchecked,rawtypes
184+
return (Collection) source.excludedEvents().stream()
190185
.filter(eventClass::isAssignableFrom)
191-
.toArray(Class[]::new);
186+
.toList();
192187
}
193188

194189
@Override
195190
public @Nullable String excludedErrorMessage() {
196191
return source.excludedErrorMessage();
197192
}
198193

194+
@Override
195+
public boolean matches(EventValue<?, ?> eventValue) {
196+
return matches(eventValue.eventClass(), eventValue.valueClass(), eventValue.patterns());
197+
}
198+
199199
@Override
200200
public @Nullable <NewEvent extends Event, NewValue> EventValue<NewEvent, NewValue> getConverted(
201201
Class<NewEvent> newEventClass,

src/main/java/org/skriptlang/skript/bukkit/lang/eventvalue/EventValue.java

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import org.bukkit.event.Event;
77
import org.jetbrains.annotations.Contract;
88
import org.jetbrains.annotations.Nullable;
9+
import org.jetbrains.annotations.Unmodifiable;
910
import org.skriptlang.skript.lang.converter.Converter;
1011

11-
import java.util.Arrays;
12+
import java.util.Collection;
1213
import java.util.Optional;
14+
import java.util.SequencedCollection;
1315
import java.util.function.BiPredicate;
1416
import java.util.function.Function;
1517

@@ -57,21 +59,24 @@ static <E extends Event, V> EventValue<E, V> simple(Class<E> eventClass, Class<V
5759
*
5860
* @return the event type this event value is defined for
5961
*/
62+
@Contract(pure = true)
6063
Class<E> eventClass();
6164

6265
/**
6366
* The type of the value produced by this event value.
6467
*
6568
* @return the value type
6669
*/
70+
@Contract(pure = true)
6771
Class<V> valueClass();
6872

6973
/**
7074
* Patterns used to identify this event value from user input.
7175
*
7276
* @return the patterns
7377
*/
74-
String @Nullable [] patterns();
78+
@Contract(pure = true)
79+
@Unmodifiable SequencedCollection<String> patterns();
7580

7681
/**
7782
* Validates that this event value can be used in the provided event context.
@@ -88,6 +93,7 @@ static <E extends Event, V> EventValue<E, V> simple(Class<E> eventClass, Class<V
8893
* @param input the identifier provided by the user
8994
* @return {@code true} if the validation succeeds
9095
*/
96+
@Contract(pure = true)
9197
boolean matchesInput(String input);
9298

9399
/**
@@ -96,13 +102,15 @@ static <E extends Event, V> EventValue<E, V> simple(Class<E> eventClass, Class<V
96102
* @param event the event instance
97103
* @return the value obtained from the event, which may be {@code null}
98104
*/
105+
@Contract(pure = true)
99106
V get(E event);
100107

101108
/**
102109
* The converter used to obtain the value from the event.
103110
*
104111
* @return the converter
105112
*/
113+
@Contract(pure = true)
106114
Converter<E, V> converter();
107115

108116
/**
@@ -111,6 +119,7 @@ static <E extends Event, V> EventValue<E, V> simple(Class<E> eventClass, Class<V
111119
* @param mode the change mode
112120
* @return {@code true} if a changer is supported
113121
*/
122+
@Contract(pure = true)
114123
boolean hasChanger(ChangeMode mode);
115124

116125
/**
@@ -119,27 +128,31 @@ static <E extends Event, V> EventValue<E, V> simple(Class<E> eventClass, Class<V
119128
* @param mode the change mode
120129
* @return an {@link Optional} containing the changer if available
121130
*/
131+
@Contract(pure = true)
122132
Optional<Changer<E, V>> changer(ChangeMode mode);
123133

124134
/**
125135
* The time state this event value is registered for.
126136
*
127137
* @return the time state
128138
*/
139+
@Contract(pure = true)
129140
Time time();
130141

131142
/**
132143
* Event types explicitly excluded from using this event value.
133144
*
134-
* @return an array of excluded event classes or {@code null} if none
145+
* @return a list of excluded event classes
135146
*/
136-
Class<? extends E> @Nullable [] excludedEvents();
147+
@Contract(pure = true)
148+
@Unmodifiable Collection<Class<? extends E>> excludedEvents();
137149

138150
/**
139151
* An optional error message shown when this value is excluded for a matching event.
140152
*
141153
* @return the exclusion error message or {@code null}
142154
*/
155+
@Contract(pure = true)
143156
@Nullable String excludedErrorMessage();
144157

145158
/**
@@ -149,9 +162,8 @@ static <E extends Event, V> EventValue<E, V> simple(Class<E> eventClass, Class<V
149162
* @param eventValue the event value to compare against
150163
* @return {@code true} if they match
151164
*/
152-
default boolean matches(EventValue<?, ?> eventValue) {
153-
return matches(eventValue.eventClass(), eventValue.valueClass(), eventValue.patterns());
154-
}
165+
@Contract(pure = true)
166+
boolean matches(EventValue<?, ?> eventValue);
155167

156168
/**
157169
* Checks whether this event value matches the provided event class, value class,
@@ -162,8 +174,9 @@ default boolean matches(EventValue<?, ?> eventValue) {
162174
* @param patterns the patterns to compare against
163175
* @return {@code true} if they match
164176
*/
165-
default boolean matches(Class<? extends Event> eventClass, Class<?> valueClass, String[] patterns) {
166-
return matches(eventClass, valueClass) && Arrays.equals(patterns(), patterns);
177+
@Contract(pure = true)
178+
default boolean matches(Class<? extends Event> eventClass, Class<?> valueClass, SequencedCollection<String> patterns) {
179+
return matches(eventClass, valueClass) && patterns().equals(patterns);
167180
}
168181

169182
/**
@@ -173,6 +186,7 @@ default boolean matches(Class<? extends Event> eventClass, Class<?> valueClass,
173186
* @param valueClass the value class to compare against
174187
* @return {@code true} if they match
175188
*/
189+
@Contract(pure = true)
176190
default boolean matches(Class<? extends Event> eventClass, Class<?> valueClass) {
177191
return eventClass().equals(eventClass) && valueClass().equals(valueClass);
178192
}
@@ -319,37 +333,6 @@ interface Changer<E extends Event, V> {
319333

320334
}
321335

322-
/**
323-
* A changer that does not require a value to be passed (e.g. for {@link ChangeMode#DELETE} or {@link ChangeMode#RESET}).
324-
*
325-
* @param <E> the event type
326-
* @param <V> the value type
327-
*/
328-
@FunctionalInterface
329-
interface NoValueChanger<E extends Event, V> extends Changer<E, V> {
330-
331-
/**
332-
* Applies a change to the given event instance without a value.
333-
*
334-
* @param event the event instance
335-
*/
336-
void change(E event);
337-
338-
/**
339-
* {@inheritDoc}
340-
* <p>
341-
* This implementation ignores the provided value and calls {@link #change(Event)}.
342-
*
343-
* @param event the event instance
344-
* @param value the value (ignored)
345-
*/
346-
@Override
347-
default void change(E event, V value) {
348-
change(event);
349-
}
350-
351-
}
352-
353336
/**
354337
* A builder for creating {@link EventValue} instances.
355338
*

0 commit comments

Comments
 (0)