Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public record EventValueInfo<E extends Event, T>(
}

public static <E extends Event, T> EventValueInfo<E, T> fromModern(EventValue<E, T> eventValue) {
//noinspection unchecked
return new EventValueInfo<>(
eventValue.eventClass(),
eventValue.valueClass(),
Expand All @@ -314,7 +315,7 @@ public void set(E event, @Nullable T value) {
})
.orElse(eventValue.converter()),
eventValue.excludedErrorMessage(),
eventValue.excludedEvents(),
eventValue.excludedEvents().toArray(new Class[0]),
eventValue.time().value()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import ch.njol.skript.classes.Changer.ChangeMode;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import org.skriptlang.skript.lang.converter.Converter;
import org.skriptlang.skript.lang.converter.Converters;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.SequencedCollection;

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

@Override
public String @Nullable [] patterns() {
public @Unmodifiable SequencedCollection<String> patterns() {
return source.patterns();
}

Expand Down Expand Up @@ -163,10 +165,6 @@ public Optional<Changer<ConvertedEvent, ConvertedValue>> changer(ChangeMode mode
return source.changer(mode).map(changer -> (event, value) -> {
if (!source.eventClass().isAssignableFrom(event.getClass()))
return;
if (changer instanceof EventValue.NoValueChanger) {
changer.change(source.eventClass().cast(event), null);
return;
}
if (reverseConverter == null)
return;
SourceValue sourceValue = reverseConverter.convert(value);
Expand All @@ -181,21 +179,23 @@ public Time time() {
}

@Override
public Class<? extends ConvertedEvent> @Nullable [] excludedEvents() {
Class<? extends SourceEvent>[] excludedEvents = source.excludedEvents();
if (excludedEvents == null)
return null;
//noinspection unchecked
return Arrays.stream(excludedEvents)
public @Unmodifiable Collection<Class<? extends ConvertedEvent>> excludedEvents() {
//noinspection unchecked,rawtypes
return (Collection) source.excludedEvents().stream()
.filter(eventClass::isAssignableFrom)
.toArray(Class[]::new);
.toList();
}

@Override
public @Nullable String excludedErrorMessage() {
return source.excludedErrorMessage();
}

@Override
public boolean matches(EventValue<?, ?> eventValue) {
return matches(eventValue.eventClass(), eventValue.valueClass(), eventValue.patterns());
}

@Override
public @Nullable <NewEvent extends Event, NewValue> EventValue<NewEvent, NewValue> getConverted(
Class<NewEvent> newEventClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import org.bukkit.event.Event;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import org.skriptlang.skript.lang.converter.Converter;

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.SequencedCollection;
import java.util.function.BiPredicate;
import java.util.function.Function;

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

/**
* The type of the value produced by this event value.
*
* @return the value type
*/
@Contract(pure = true)
Class<V> valueClass();

/**
* Patterns used to identify this event value from user input.
*
* @return the patterns
*/
String @Nullable [] patterns();
@Contract(pure = true)
@Unmodifiable SequencedCollection<String> patterns();

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

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

/**
* The converter used to obtain the value from the event.
*
* @return the converter
*/
@Contract(pure = true)
Converter<E, V> converter();

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

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

/**
* The time state this event value is registered for.
*
* @return the time state
*/
@Contract(pure = true)
Time time();

/**
* Event types explicitly excluded from using this event value.
*
* @return an array of excluded event classes or {@code null} if none
* @return a list of excluded event classes
*/
Class<? extends E> @Nullable [] excludedEvents();
@Contract(pure = true)
@Unmodifiable Collection<Class<? extends E>> excludedEvents();

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

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

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

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

}

/**
* A changer that does not require a value to be passed (e.g. for {@link ChangeMode#DELETE} or {@link ChangeMode#RESET}).
*
* @param <E> the event type
* @param <V> the value type
*/
@FunctionalInterface
interface NoValueChanger<E extends Event, V> extends Changer<E, V> {

/**
* Applies a change to the given event instance without a value.
*
* @param event the event instance
*/
void change(E event);

/**
* {@inheritDoc}
* <p>
* This implementation ignores the provided value and calls {@link #change(Event)}.
*
* @param event the event instance
* @param value the value (ignored)
*/
@Override
default void change(E event, V value) {
change(event);
}

}

/**
* A builder for creating {@link EventValue} instances.
*
Expand Down
Loading
Loading