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

Fix for exact event values in the event value expression #5579

Merged
merged 5 commits into from
May 20, 2023
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
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/expressions/ExprEgg.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ExprEgg extends EventValueExpression<Egg> {
}

public ExprEgg() {
super(Egg.class);
super(Egg.class, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
* }
* </pre>
*
* @author Peter Güttinger
* @see Classes#registerClass(ClassInfo)
* @see ClassInfo#defaultExpression(DefaultExpression)
* @see DefaultExpression
Expand All @@ -70,14 +69,30 @@ public class EventValueExpression<T> extends SimpleExpression<T> implements Defa
private Changer<? super T> changer;
private final Map<Class<? extends Event>, Getter<? extends T, ?>> getters = new HashMap<>();
private final boolean single;
private final boolean exact;

public EventValueExpression(Class<? extends T> c) {
this(c, null);
}

/**
* Construct an event value expression.
*
* @param c The class that this event value represents.
* @param exact If false, the event value can be a subclass or a converted event value.
*/
public EventValueExpression(Class<? extends T> c, boolean exact) {
this(c, null, exact);
}

public EventValueExpression(Class<? extends T> c, @Nullable Changer<? super T> changer) {
this(c, changer, false);
}

public EventValueExpression(Class<? extends T> c, @Nullable Changer<? super T> changer, boolean exact) {
assert c != null;
this.c = c;
this.exact = exact;
this.changer = changer;
single = !c.isArray();
componentType = single ? c : c.getComponentType();
Expand Down Expand Up @@ -143,7 +158,12 @@ public boolean init() {
hasValue = getters.get(event) != null;
continue;
}
Getter<? extends T, ?> getter = EventValues.getEventValueGetter(event, c, getTime());
Getter<? extends T, ?> getter;
if (exact) {
getter = EventValues.getExactEventValueGetter(event, c, getTime());
} else {
getter = EventValues.getEventValueGetter(event, c, getTime());
}
if (getter != null) {
getters.put(event, getter);
hasValue = true;
Expand Down Expand Up @@ -203,7 +223,13 @@ public boolean setTime(int time) {
}
for (Class<? extends Event> event : events) {
assert event != null;
if (EventValues.doesEventValueHaveTimeStates(event, c)) {
boolean has;
if (exact) {
has = EventValues.doesExactEventValueHaveTimeStates(event, c);
} else {
has = EventValues.doesEventValueHaveTimeStates(event, c);
}
if (has) {
super.setTime(time);
// Since the time was changed, we now need to re-initalize the getters we already got. START
getters.clear();
Expand Down
51 changes: 38 additions & 13 deletions src/main/java/ch/njol/skript/registrations/EventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,9 @@ public static <T, E extends Event> T getEventValue(E e, Class<T> c, int time) {
return null;
return getter.get(e);
}

/**
* Returns a getter to get a value from in an event.
* <p>
* Can print an error if the event value is blocked for the given event.
* Checks that a getter exists for the exact type. No converting or subclass checking.
*
* @param event the event class the getter will be getting from
* @param c type of getter
Expand All @@ -212,13 +210,8 @@ public static <T, E extends Event> T getEventValue(E e, Class<T> c, int time) {
* @see EventValueExpression#EventValueExpression(Class)
*/
@Nullable
public static <T, E extends Event> Getter<? extends T, ? super E> getEventValueGetter(Class<E> event, Class<T> c, int time) {
return getEventValueGetter(event, c, time, true);
}

@SuppressWarnings("unchecked")
@Nullable
private static <T, E extends Event> Getter<? extends T, ? super E> getEventValueGetter(Class<E> event, Class<T> c, int time, boolean allowDefault) {
public static <T, E extends Event> Getter<? extends T, ? super E> getExactEventValueGetter(Class<E> event, Class<T> c, int time) {
List<EventValueInfo<?, ?>> eventValues = getEventValuesList(time);
// First check for exact classes matching the parameters.
for (EventValueInfo<?, ?> eventValueInfo : eventValues) {
Expand All @@ -229,6 +222,34 @@ public static <T, E extends Event> T getEventValue(E e, Class<T> c, int time) {
if (eventValueInfo.event.isAssignableFrom(event))
return (Getter<? extends T, ? super E>) eventValueInfo.getter;
}
return null;
}

/**
* Returns a getter to get a value from in an event.
* <p>
* Can print an error if the event value is blocked for the given event.
*
* @param event the event class the getter will be getting from
* @param c type of getter
* @param time the event-value's time
* @return A getter to get values for a given type of events
* @see #registerEventValue(Class, Class, Getter, int)
* @see EventValueExpression#EventValueExpression(Class)
*/
@Nullable
public static <T, E extends Event> Getter<? extends T, ? super E> getEventValueGetter(Class<E> event, Class<T> c, int time) {
return getEventValueGetter(event, c, time, true);
}

@Nullable
@SuppressWarnings("unchecked")
private static <T, E extends Event> Getter<? extends T, ? super E> getEventValueGetter(Class<E> event, Class<T> c, int time, boolean allowDefault) {
List<EventValueInfo<?, ?>> eventValues = getEventValuesList(time);
// First check for exact classes matching the parameters.
Getter<? extends T, ? super E> exact = (Getter<? extends T, ? super E>) getExactEventValueGetter(event, c, time);
if (exact != null)
return exact;
// Second check for assignable subclasses.
for (EventValueInfo<?, ?> eventValueInfo : eventValues) {
if (!c.isAssignableFrom(eventValueInfo.c))
Expand Down Expand Up @@ -351,9 +372,13 @@ public T get(E e) {
}
};
}

public static boolean doesEventValueHaveTimeStates(Class<? extends Event> e, Class<?> c) {
return getEventValueGetter(e, c, -1, false) != null || getEventValueGetter(e, c, 1, false) != null;

public static boolean doesExactEventValueHaveTimeStates(Class<? extends Event> event, Class<?> c) {
return getExactEventValueGetter(event, c, TIME_PAST) != null || getExactEventValueGetter(event, c, TIME_FUTURE) != null;
}

public static boolean doesEventValueHaveTimeStates(Class<? extends Event> event, Class<?> c) {
return getEventValueGetter(event, c, TIME_PAST, false) != null || getEventValueGetter(event, c, TIME_FUTURE, false) != null;
}

}