|
6 | 6 |
|
7 | 7 | import ch.njol.skript.classes.data.DefaultChangers; |
8 | 8 | import ch.njol.skript.lang.Expression; |
| 9 | +import org.skriptlang.skript.lang.arithmetic.Arithmetics; |
| 10 | +import org.skriptlang.skript.lang.arithmetic.OperationInfo; |
| 11 | +import org.skriptlang.skript.lang.arithmetic.Operator; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.function.Predicate; |
9 | 15 |
|
10 | 16 | /** |
11 | 17 | * An interface to declare changeable values. All Expressions implement something similar like this by default, but refuse any change if {@link Expression#acceptChange(ChangeMode)} |
@@ -94,6 +100,29 @@ public static boolean acceptsChangeTypes(Class<?>[] validTypes, Class<?> @NotNul |
94 | 100 | return false; |
95 | 101 | } |
96 | 102 |
|
| 103 | + /** |
| 104 | + * Gets the types that can be added/removed via arithmetic for the given type. |
| 105 | + * This is used to determine accepted change types for add/remove when no changer is present. |
| 106 | + * @param type The type to get arithmetic change types for. |
| 107 | + * @param mode Whether to get addition or subtraction types. |
| 108 | + * @param filter A filter to apply to the available operations. Used for custom constraints on the operations, like |
| 109 | + * ensuring the return type matches the left type. |
| 110 | + * @return The types that can be added/removed via arithmetic for the given type and mode, after applying the filter. |
| 111 | + * @param <T> The type to get arithmetic change types for. |
| 112 | + */ |
| 113 | + public static <T> Class<?>[] getArithmeticChangeTypes(Class<T> type, ChangeMode mode, Predicate<OperationInfo<T, ? ,?>> filter) { |
| 114 | + List<OperationInfo<T, ?, ?>> opInfos = List.of(); |
| 115 | + if (mode == ChangeMode.ADD) { |
| 116 | + opInfos = Arithmetics.getOperations(Operator.ADDITION, type); |
| 117 | + } else if (mode == ChangeMode.REMOVE) { |
| 118 | + opInfos = Arithmetics.getOperations(Operator.SUBTRACTION, type); |
| 119 | + } |
| 120 | + return opInfos.stream() |
| 121 | + .filter(filter) |
| 122 | + .map(OperationInfo::right) |
| 123 | + .toArray(Class[]::new); |
| 124 | + } |
| 125 | + |
97 | 126 | } |
98 | 127 |
|
99 | 128 | } |
0 commit comments