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..fe8043d7e06 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); + } + + /** + * @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 getter exists for the exact type. No converting or subclass checking. + * 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,78 @@ 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) { + return hasMultipleConverters(event, type, time); + } + + /** + * 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 +286,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 +349,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 +367,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 +414,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 +432,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(