From 2670595c59dd8546f5cc668246c9c9dacdd83508 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Fri, 3 Jan 2025 15:41:37 +0100 Subject: [PATCH 1/3] init commit --- .../base/EventValueExpression.java | 6 +- .../skript/registrations/EventValues.java | 134 +++++++++++++----- 2 files changed, 99 insertions(+), 41 deletions(-) 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 a421691ddf5..49b1a307639 100644 --- a/src/main/java/ch/njol/skript/expressions/base/EventValueExpression.java +++ b/src/main/java/ch/njol/skript/expressions/base/EventValueExpression.java @@ -153,7 +153,7 @@ public boolean init() { hasValue = converters.get(event) != null; continue; } - if (EventValues.hasMultipleGetters(event, type, getTime()) == Kleenean.TRUE) { + if (EventValues.hasMultipleConverters(event, type, getTime()) == Kleenean.TRUE) { Noun typeName = Classes.getExactClassInfo(componentType).getName(); log.printError("There are multiple " + typeName.toString(true) + " in " + Utils.a(getParser().getCurrentEventName()) + " event. " + "You must define which " + typeName + " to use."); @@ -161,9 +161,9 @@ public boolean init() { } Converter converter; if (exact) { - converter = EventValues.getExactEventValueGetter(event, type, getTime()); + converter = EventValues.getExactEventValueConverter(event, type, getTime()); } else { - converter = EventValues.getEventValueGetter(event, type, getTime()); + converter = EventValues.getEventValueConverter(event, type, getTime()); } if (converter != null) { converters.put(event, converter); diff --git a/src/main/java/ch/njol/skript/registrations/EventValues.java b/src/main/java/ch/njol/skript/registrations/EventValues.java index a9d4d0beeec..905cbd9a809 100644 --- a/src/main/java/ch/njol/skript/registrations/EventValues.java +++ b/src/main/java/ch/njol/skript/registrations/EventValues.java @@ -166,28 +166,38 @@ public static void registerEventValue( * @return The event's value * @see #registerEventValue(Class, Class, Converter, int) */ - @Nullable - public static T getEventValue(E e, Class c, int time) { - @SuppressWarnings("unchecked") - Converter getter = getEventValueGetter((Class) e.getClass(), c, time); - if (getter == null) + public static @Nullable T getEventValue(E e, Class c, int time) { + //noinspection unchecked + Converter converter = getEventValueConverter((Class) e.getClass(), c, time); + if (converter == null) return null; - return getter.convert(e); + return converter.convert(e); } /** - * Checks that a getter exists for the exact type. No converting or subclass checking. + * @deprecated Use {@link #getExactEventValueConverter(Class, Class, int)} instead. + */ + @Nullable + @Deprecated(forRemoval = true) + @SuppressWarnings({"removal"}) + public static Getter getExactEventValueGetter(Class event, Class c, int time) { + return toGetter(getExactEventValueConverter(event, c, time)); + } + + /** + * Checks that a {@link Converter} 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 + * @param c type of {@link Converter} * @param time the event-value's time * @return A getter to get values for a given type of events * @see #registerEventValue(Class, Class, Converter, int) * @see EventValueExpression#EventValueExpression(Class) */ @Nullable - @SuppressWarnings("unchecked") - public static Converter getExactEventValueGetter(Class event, Class c, int time) { + public static Converter getExactEventValueConverter( + Class event, Class c, int time + ) { List> eventValues = getEventValuesList(time); // First check for exact classes matching the parameters. for (EventValueInfo eventValueInfo : eventValues) { @@ -196,54 +206,81 @@ public static T getEventValue(E e, Class c, int time) { if (!checkExcludes(eventValueInfo, event)) return null; if (eventValueInfo.event.isAssignableFrom(event)) + //noinspection unchecked return (Converter) eventValueInfo.converter; } return null; } /** - * Checks if an event has multiple getters, including default ones. + * @deprecated Use {@link #hasMultipleConverters(Class, Class, int)} instead. + */ + @Deprecated(forRemoval = true) + public static Kleenean hasMultipleGetters(Class event, Class type, int time) { + List> getters = getEventValueConverters(event, type, time, true, false); + if (getters == null) + return Kleenean.UNKNOWN; + return Kleenean.get(getters.size() > 1); + } + + /** + * Checks if an event has multiple {@link Converter}s, including default ones. * - * @param event the event class the getter will be getting from. - * @param type type of getter. + * @param event the event class the {@link Converter} will be getting from. + * @param type type of {@link Converter}. * @param time the event-value's time. - * @return true or false if the event and type have multiple getters. + * @return true or false if the event and type have multiple {@link Converter}s. */ - public static Kleenean hasMultipleGetters(Class event, Class type, int time) { - List> getters = getEventValueGetters(event, type, time, true, false); + public static Kleenean hasMultipleConverters(Class event, Class type, int time) { + List> getters = getEventValueConverters(event, type, time, true, false); if (getters == null) return Kleenean.UNKNOWN; return Kleenean.get(getters.size() > 1); } /** - * Returns a getter to get a value from in an event. + * @deprecated Use {@link #getEventValueConverter(Class, Class, int)} instead. + */ + @Nullable + @Deprecated(forRemoval = true) + @SuppressWarnings({"removal"}) + public static Getter getEventValueGetter(Class event, Class type, int time) { + return toGetter(getEventValueConverter(event, type, time, true)); + } + + /** + * Returns a {@link Converter} 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 type type of getter. + * @param event the event class the {@link Converter} will be getting from. + * @param type type of {@link Converter}. * @param time the event-value's time. * @return A getter to get values for a given type of events. * @see #registerEventValue(Class, Class, Converter, int) * @see EventValueExpression#EventValueExpression(Class) */ - @Nullable - public static Converter getEventValueGetter(Class event, Class type, int time) { - return getEventValueGetter(event, type, time, true); + public static @Nullable Converter getEventValueConverter( + Class event, Class type, int time + ) { + return getEventValueConverter(event, type, time, true); } @Nullable - private static Converter getEventValueGetter(Class event, Class type, int time, boolean allowDefault) { - List> list = getEventValueGetters(event, type, time, allowDefault); + private static Converter getEventValueConverter( + Class event, Class type, int time, boolean allowDefault + ) { + List> list = getEventValueConverters(event, type, time, allowDefault); if (list == null || list.isEmpty()) return null; return list.get(0); } @Nullable - private static List> getEventValueGetters(Class event, Class type, int time, boolean allowDefault) { - return getEventValueGetters(event, type, time, allowDefault, true); + private static List> getEventValueConverters( + Class event, Class type, int time, boolean allowDefault + ) { + return getEventValueConverters(event, type, time, allowDefault, true); } /* @@ -252,11 +289,14 @@ public static Kleenean hasMultipleGetters(Class event, C */ @Nullable @SuppressWarnings("unchecked") - private static List> getEventValueGetters(Class event, Class type, int time, boolean allowDefault, boolean allowConverting) { + private static List> getEventValueConverters( + Class event, Class type, int time, + boolean allowDefault, boolean allowConverting + ) { List> eventValues = getEventValuesList(time); List> list = new ArrayList<>(); // First check for exact classes matching the parameters. - Converter exact = getExactEventValueGetter(event, type, time); + Converter exact = getExactEventValueConverter(event, type, time); if (exact != null) { list.add(exact); return list; @@ -312,13 +352,14 @@ public static Kleenean hasMultipleGetters(Class event, C if (!event.equals(eventValueInfo.event)) continue; - Converter getter = (Converter) getConvertedGetter(eventValueInfo, type, false); - if (getter == null) + Converter converter = (Converter) + getConvertedConverter(eventValueInfo, type, false); + if (converter == null) continue; if (!checkExcludes(eventValueInfo, event)) return null; - list.add(getter); + list.add(converter); continue; } if (!list.isEmpty()) @@ -329,20 +370,21 @@ public static Kleenean hasMultipleGetters(Class event, C if (!event.isAssignableFrom(eventValueInfo.event)) continue; - Converter getter = (Converter) getConvertedGetter(eventValueInfo, type, true); - if (getter == null) + Converter converter = (Converter) + getConvertedConverter(eventValueInfo, type, true); + if (converter == null) continue; if (!checkExcludes(eventValueInfo, event)) return null; - list.add(getter); + list.add(converter); continue; } if (!list.isEmpty()) return list; // If the check should try again matching event values with a 0 time (most event values). if (allowDefault && time != 0) - return getEventValueGetters(event, type, 0, false); + return getEventValueConverters(event, type, 0, false); return null; } @@ -375,7 +417,9 @@ private static boolean checkExcludes(EventValueInfo info, Class Converter getConvertedGetter(EventValueInfo info, Class to, boolean checkInstanceOf) { + private static Converter getConvertedConverter( + EventValueInfo info, Class to, boolean checkInstanceOf + ) { Converter converter = Converters.getConverter(info.c, to); if (converter == null) @@ -391,12 +435,26 @@ private static boolean checkExcludes(EventValueInfo info, Class Getter toGetter(Converter converter) { + if (converter == null) + return null; + + return new Getter<>() { + @Override + public @Nullable B get(A arg) { + return converter.convert(arg); + } + }; + } + public static boolean doesExactEventValueHaveTimeStates(Class event, Class c) { - return getExactEventValueGetter(event, c, TIME_PAST) != null || getExactEventValueGetter(event, c, TIME_FUTURE) != null; + return getExactEventValueConverter(event, c, TIME_PAST) != null || getExactEventValueConverter(event, c, TIME_FUTURE) != null; } public static boolean doesEventValueHaveTimeStates(Class event, Class c) { - return getEventValueGetter(event, c, TIME_PAST, false) != null || getEventValueGetter(event, c, TIME_FUTURE, false) != null; + return getEventValueConverter(event, c, TIME_PAST, false) != null || getEventValueConverter(event, c, TIME_FUTURE, false) != null; } private record EventValueInfo( From 89ac1f795aa13a78e82f957a542b0f2a765d7340 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Fri, 3 Jan 2025 20:35:19 +0100 Subject: [PATCH 2/3] fix incorrect order of typed args in getter --- src/main/java/ch/njol/skript/registrations/EventValues.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/registrations/EventValues.java b/src/main/java/ch/njol/skript/registrations/EventValues.java index 905cbd9a809..23e928858ff 100644 --- a/src/main/java/ch/njol/skript/registrations/EventValues.java +++ b/src/main/java/ch/njol/skript/registrations/EventValues.java @@ -180,7 +180,7 @@ public static void registerEventValue( @Nullable @Deprecated(forRemoval = true) @SuppressWarnings({"removal"}) - public static Getter getExactEventValueGetter(Class event, Class c, int time) { + public static Getter getExactEventValueGetter(Class event, Class c, int time) { return toGetter(getExactEventValueConverter(event, c, time)); } @@ -195,7 +195,7 @@ public static void registerEventValue( * @see EventValueExpression#EventValueExpression(Class) */ @Nullable - public static Converter getExactEventValueConverter( + public static Converter getExactEventValueConverter( Class event, Class c, int time ) { List> eventValues = getEventValuesList(time); @@ -244,7 +244,7 @@ public static Kleenean hasMultipleConverters(Class event @Nullable @Deprecated(forRemoval = true) @SuppressWarnings({"removal"}) - public static Getter getEventValueGetter(Class event, Class type, int time) { + public static Getter getEventValueGetter(Class event, Class type, int time) { return toGetter(getEventValueConverter(event, type, time, true)); } From 97eb39b3d614aff6edb0306db6b3ef89f73dc9fc Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Sat, 4 Jan 2025 01:04:27 +0100 Subject: [PATCH 3/3] oops! --- src/main/java/ch/njol/skript/registrations/EventValues.java | 5 +---- 1 file changed, 1 insertion(+), 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 23e928858ff..fe8043d7e06 100644 --- a/src/main/java/ch/njol/skript/registrations/EventValues.java +++ b/src/main/java/ch/njol/skript/registrations/EventValues.java @@ -217,10 +217,7 @@ public static void registerEventValue( */ @Deprecated(forRemoval = true) public static Kleenean hasMultipleGetters(Class event, Class type, int time) { - List> getters = getEventValueConverters(event, type, time, true, false); - if (getters == null) - return Kleenean.UNKNOWN; - return Kleenean.get(getters.size() > 1); + return hasMultipleConverters(event, type, time); } /**