Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the multiple event-values less strict #6122

Merged
merged 10 commits into from
Jan 1, 2024
28 changes: 17 additions & 11 deletions src/main/java/ch/njol/skript/registrations/EventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public Class<T> getValueClass() {
* @return The classes of the Excludes associated with this event value
*/
@Nullable
@SuppressWarnings("null")
public Class<? extends E>[] getExcludes() {
if (excludes != null)
return Arrays.copyOf(excludes, excludes.length);
Expand All @@ -98,26 +97,26 @@ public String getExcludeErrorMessage() {
return excludeErrorMessage;
}
}

private final static List<EventValueInfo<?, ?>> defaultEventValues = new ArrayList<>(30);
private final static List<EventValueInfo<?, ?>> futureEventValues = new ArrayList<>();
private final static List<EventValueInfo<?, ?>> pastEventValues = new ArrayList<>();

/**
* The past time of an event value. Represented by "past" or "former".
*/
public static final int TIME_PAST = -1;

/**
* The current time of an event value.
*/
public static final int TIME_NOW = 0;

/**
* The future time of an event value.
*/
public static final int TIME_FUTURE = 1;

/**
* Get Event Values list for the specified time
* @param time The time of the event values. One of
Expand All @@ -127,7 +126,7 @@ public String getExcludeErrorMessage() {
public static List<EventValueInfo<?, ?>> getEventValuesListForTime(int time) {
return ImmutableList.copyOf(getEventValuesList(time));
}

private static List<EventValueInfo<?, ?>> getEventValuesList(int time) {
if (time == -1)
return pastEventValues;
Expand Down Expand Up @@ -242,7 +241,7 @@ public static <T, E extends Event> T getEventValue(E e, Class<T> c, int time) {
* @return true or false if the event and type have multiple getters.
*/
public static <T, E extends Event> Kleenean hasMultipleGetters(Class<E> event, Class<T> type, int time) {
List<Getter<? extends T, ? super E>> getters = getEventValueGetters(event, type, time, true);
List<Getter<? extends T, ? super E>> getters = getEventValueGetters(event, type, time, true, false);
if (getters == null)
return Kleenean.UNKNOWN;
return Kleenean.get(getters.size() > 1);
Expand Down Expand Up @@ -273,13 +272,18 @@ public static <T, E extends Event> Kleenean hasMultipleGetters(Class<E> event, C
return list.get(0);
}

@Nullable
private static <T, E extends Event> List<Getter<? extends T, ? super E>> getEventValueGetters(Class<E> event, Class<T> type, int time, boolean allowDefault) {
return getEventValueGetters(event, type, time, allowDefault, true);
}

/*
* We need to be able to collect all possible event-values to a list for determining problematic collisions.
* Always return after the loop check if the list is not empty.
*/
@Nullable
@SuppressWarnings("unchecked")
private static <T, E extends Event> List<Getter<? extends T, ? super E>> getEventValueGetters(Class<E> event, Class<T> type, int time, boolean allowDefault) {
private static <T, E extends Event> List<Getter<? extends T, ? super E>> getEventValueGetters(Class<E> event, Class<T> type, int time, boolean allowDefault, boolean allowConverting) {
List<EventValueInfo<?, ?>> eventValues = getEventValuesList(time);
List<Getter<? extends T, ? super E>> list = new ArrayList<>();
// First check for exact classes matching the parameters.
Expand Down Expand Up @@ -313,6 +317,8 @@ public T get(E event) {
}
if (!list.isEmpty())
return list;
if (!allowConverting)
return null;
// Most checks have returned before this below is called, but Skript will attempt to convert or find an alternative.
// Third check is if the returned object matches the class.
for (EventValueInfo<?, ?> eventValueInfo : eventValues) {
Expand Down Expand Up @@ -361,11 +367,11 @@ public T get(E event) {
// The requesting event must be assignable to the event value's event. Otherwise it'll throw an error.
if (!event.isAssignableFrom(eventValueInfo.event))
continue;

Getter<? extends T, ? super E> getter = (Getter<? extends T, ? super E>) getConvertedGetter(eventValueInfo, type, true);
if (getter == null)
continue;

if (!checkExcludes(eventValueInfo, event))
return null;
list.add(getter);
Expand Down