From a8f6811d0cef6dad525f713104e35b6b9092324c Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Mon, 3 Apr 2023 14:39:35 -0600 Subject: [PATCH 1/3] Allow support for exact event values in the event value expression --- .../ch/njol/skript/expressions/ExprEgg.java | 2 +- .../base/EventValueExpression.java | 32 +++++++++++-- .../expressions/base/PropertyExpression.java | 2 +- .../skript/registrations/EventValues.java | 47 ++++++++++++++----- 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprEgg.java b/src/main/java/ch/njol/skript/expressions/ExprEgg.java index 180fb1cf5a9..3ca80a02e33 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprEgg.java +++ b/src/main/java/ch/njol/skript/expressions/ExprEgg.java @@ -42,7 +42,7 @@ public class ExprEgg extends EventValueExpression { } public ExprEgg() { - super(Egg.class); + super(Egg.class, true); } @Override diff --git a/src/main/java/ch/njol/skript/expressions/base/EventValueExpression.java b/src/main/java/ch/njol/skript/expressions/base/EventValueExpression.java index a08757d33e2..9781eade3dd 100644 --- a/src/main/java/ch/njol/skript/expressions/base/EventValueExpression.java +++ b/src/main/java/ch/njol/skript/expressions/base/EventValueExpression.java @@ -57,7 +57,6 @@ * } * * - * @author Peter Güttinger * @see Classes#registerClass(ClassInfo) * @see ClassInfo#defaultExpression(DefaultExpression) * @see DefaultExpression @@ -70,14 +69,30 @@ public class EventValueExpression extends SimpleExpression implements Defa private Changer changer; private final Map, Getter> getters = new HashMap<>(); private final boolean single; + private final boolean exact; public EventValueExpression(Class 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 c, boolean exact) { + this(c, null, exact); + } + public EventValueExpression(Class c, @Nullable Changer changer) { + this(c, changer, false); + } + + public EventValueExpression(Class c, @Nullable Changer changer, boolean exact) { assert c != null; this.c = c; + this.exact = exact; this.changer = changer; single = !c.isArray(); componentType = single ? c : c.getComponentType(); @@ -143,7 +158,12 @@ public boolean init() { hasValue = getters.get(event) != null; continue; } - Getter getter = EventValues.getEventValueGetter(event, c, getTime()); + Getter 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; @@ -203,7 +223,13 @@ public boolean setTime(int time) { } for (Class 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(); diff --git a/src/main/java/ch/njol/skript/expressions/base/PropertyExpression.java b/src/main/java/ch/njol/skript/expressions/base/PropertyExpression.java index 1a41665c3d7..1bc06c9e7ce 100644 --- a/src/main/java/ch/njol/skript/expressions/base/PropertyExpression.java +++ b/src/main/java/ch/njol/skript/expressions/base/PropertyExpression.java @@ -120,7 +120,7 @@ protected T[] get(final F[] source, final ch.njol.skript.classes.Converter T getEventValue(E e, Class c, int time) { return null; return getter.get(e); } - + /** - * Returns a getter to get a value from in an event. - *

- * 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 @@ -212,13 +210,8 @@ public static T getEventValue(E e, Class c, int time) { * @see EventValueExpression#EventValueExpression(Class) */ @Nullable - public static Getter getEventValueGetter(Class event, Class c, int time) { - return getEventValueGetter(event, c, time, true); - } - @SuppressWarnings("unchecked") - @Nullable - private static Getter getEventValueGetter(Class event, Class c, int time, boolean allowDefault) { + public static Getter getExactEventValueGetter(Class event, Class c, int time) { List> eventValues = getEventValuesList(time); // First check for exact classes matching the parameters. for (EventValueInfo eventValueInfo : eventValues) { @@ -229,6 +222,34 @@ public static T getEventValue(E e, Class c, int time) { if (eventValueInfo.event.isAssignableFrom(event)) return (Getter) eventValueInfo.getter; } + return null; + } + + /** + * Returns a getter to get a value from in an event. + *

+ * 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 Getter getEventValueGetter(Class event, Class c, int time) { + return getEventValueGetter(event, c, time, true); + } + + @Nullable + @SuppressWarnings("unchecked") + private static Getter getEventValueGetter(Class event, Class c, int time, boolean allowDefault) { + List> eventValues = getEventValuesList(time); + // First check for exact classes matching the parameters. + Getter exact = (Getter) getExactEventValueGetter(event, c, time); + if (exact != null) + return exact; // Second check for assignable subclasses. for (EventValueInfo eventValueInfo : eventValues) { if (!c.isAssignableFrom(eventValueInfo.c)) @@ -351,7 +372,11 @@ public T get(E e) { } }; } - + + public static boolean doesExactEventValueHaveTimeStates(Class e, Class c) { + return getExactEventValueGetter(e, c, -1) != null || getExactEventValueGetter(e, c, 1) != null; + } + public static boolean doesEventValueHaveTimeStates(Class e, Class c) { return getEventValueGetter(e, c, -1, false) != null || getEventValueGetter(e, c, 1, false) != null; } From ae828a2ea8aafdacea553514108ae7682735e08d Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Tue, 25 Apr 2023 02:42:04 -0600 Subject: [PATCH 2/3] Apply changes --- .../java/ch/njol/skript/registrations/EventValues.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/njol/skript/registrations/EventValues.java b/src/main/java/ch/njol/skript/registrations/EventValues.java index 9cbdfd2ba9b..e6ff1049d44 100644 --- a/src/main/java/ch/njol/skript/registrations/EventValues.java +++ b/src/main/java/ch/njol/skript/registrations/EventValues.java @@ -373,12 +373,12 @@ public T get(E e) { }; } - public static boolean doesExactEventValueHaveTimeStates(Class e, Class c) { - return getExactEventValueGetter(e, c, -1) != null || getExactEventValueGetter(e, c, 1) != null; + public static boolean doesExactEventValueHaveTimeStates(Class event, Class c) { + return getExactEventValueGetter(event, c, -1) != null || getExactEventValueGetter(event, c, 1) != null; } - public static boolean doesEventValueHaveTimeStates(Class e, Class c) { - return getEventValueGetter(e, c, -1, false) != null || getEventValueGetter(e, c, 1, false) != null; + public static boolean doesEventValueHaveTimeStates(Class event, Class c) { + return getEventValueGetter(event, c, -1, false) != null || getEventValueGetter(event, c, 1, false) != null; } } From ed0858a60f2729922405bbd6e05e752889783712 Mon Sep 17 00:00:00 2001 From: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> Date: Tue, 9 May 2023 16:57:33 -0600 Subject: [PATCH 3/3] Update EventValues.java --- src/main/java/ch/njol/skript/registrations/EventValues.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/registrations/EventValues.java b/src/main/java/ch/njol/skript/registrations/EventValues.java index e6ff1049d44..255dbfcb6a8 100644 --- a/src/main/java/ch/njol/skript/registrations/EventValues.java +++ b/src/main/java/ch/njol/skript/registrations/EventValues.java @@ -374,11 +374,11 @@ public T get(E e) { } public static boolean doesExactEventValueHaveTimeStates(Class event, Class c) { - return getExactEventValueGetter(event, c, -1) != null || getExactEventValueGetter(event, c, 1) != null; + return getExactEventValueGetter(event, c, TIME_PAST) != null || getExactEventValueGetter(event, c, TIME_FUTURE) != null; } public static boolean doesEventValueHaveTimeStates(Class event, Class c) { - return getEventValueGetter(event, c, -1, false) != null || getEventValueGetter(event, c, 1, false) != null; + return getEventValueGetter(event, c, TIME_PAST, false) != null || getEventValueGetter(event, c, TIME_FUTURE, false) != null; } }