|
| 1 | +package ch.njol.skript.expressions; |
| 2 | + |
| 3 | +import ch.njol.skript.Skript; |
| 4 | +import ch.njol.skript.doc.*; |
| 5 | +import ch.njol.skript.lang.*; |
| 6 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 7 | +import ch.njol.skript.lang.parser.ParserInstance; |
| 8 | +import ch.njol.skript.lang.util.SimpleExpression; |
| 9 | +import ch.njol.skript.util.LiteralUtils; |
| 10 | +import ch.njol.util.Kleenean; |
| 11 | +import org.bukkit.event.Event; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | +import org.jetbrains.annotations.UnknownNullability; |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +@Name("Reduce") |
| 18 | +@Description({ |
| 19 | + "Reduces lists to single values by repeatedly applying an operation.", |
| 20 | + "The reduce expression takes each element and combines it with an accumulator value.", |
| 21 | + "Use 'reduced value' to access the current accumulated value and 'input' for the current element.", |
| 22 | +}) |
| 23 | +@Example("set {_sum} to {_numbers::*} reduced with [reduced value + input]") |
| 24 | +@Example("set {_product} to {_values::*} reduced with [reduced value * input]") |
| 25 | +@Example("set {_concatenated} to {_strings::*} reduced with [\"%reduced value%%input%\"]") |
| 26 | +@Since("INSERT VERSION") |
| 27 | +@Keywords({"input", "reduced value", "accumulator"}) |
| 28 | +public class ExprReduce extends SimpleExpression<Object> implements InputSource { |
| 29 | + |
| 30 | + static { |
| 31 | + Skript.registerExpression(ExprReduce.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, |
| 32 | + "%objects% (reduced|folded) (to|with|by) \\[<.+>\\]", |
| 33 | + "%objects% (reduced|folded) (to|with|by) \\(<.+>\\)" |
| 34 | + ); |
| 35 | + if (!ParserInstance.isRegistered(InputData.class)) |
| 36 | + ParserInstance.registerData(InputData.class, InputData::new); |
| 37 | + } |
| 38 | + |
| 39 | + private boolean keyed; |
| 40 | + private Expression<?> reduceExpr; |
| 41 | + private Expression<?> unreducedObjects; |
| 42 | + |
| 43 | + private final Set<ExprInput<?>> dependentInputs = new HashSet<>(); |
| 44 | + |
| 45 | + private @Nullable Object currentValue; |
| 46 | + private @Nullable Object reducedValue; |
| 47 | + private @UnknownNullability String currentIndex; |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
| 51 | + unreducedObjects = LiteralUtils.defendExpression(expressions[0]); |
| 52 | + if (unreducedObjects.isSingle()) { |
| 53 | + Skript.error("A single value cannot be reduced. Only lists can be reduced."); |
| 54 | + return false; |
| 55 | + } |
| 56 | + if ((!LiteralUtils.canInitSafely(unreducedObjects)) || parseResult.regexes.isEmpty()) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + keyed = KeyProviderExpression.canReturnKeys(unreducedObjects); |
| 61 | + |
| 62 | + @Nullable String unparsedExpression = parseResult.regexes.getFirst().group(); |
| 63 | + assert unparsedExpression != null; |
| 64 | + reduceExpr = parseExpression(unparsedExpression, getParser(), SkriptParser.ALL_FLAGS); |
| 65 | + |
| 66 | + return reduceExpr != null; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected Object @Nullable [] get(Event event) { |
| 71 | + try { |
| 72 | + boolean hadNullResult = false; |
| 73 | + |
| 74 | + if (keyed) { |
| 75 | + Iterator<? extends KeyedValue<?>> keyedIterator = ((KeyProviderExpression<?>) unreducedObjects).keyedIterator(event); |
| 76 | + if (keyedIterator == null || !keyedIterator.hasNext()) |
| 77 | + return new Object[0]; |
| 78 | + |
| 79 | + KeyedValue<?> first = keyedIterator.next(); |
| 80 | + reducedValue = first.value(); |
| 81 | + currentIndex = first.key(); |
| 82 | + |
| 83 | + while (keyedIterator.hasNext()) { |
| 84 | + KeyedValue<?> next = keyedIterator.next(); |
| 85 | + currentValue = next.value(); |
| 86 | + currentIndex = next.key(); |
| 87 | + |
| 88 | + Object result = reduceExpr.getSingle(event); |
| 89 | + if (result != null) { |
| 90 | + reducedValue = result; |
| 91 | + } else { |
| 92 | + hadNullResult = true; |
| 93 | + } |
| 94 | + } |
| 95 | + } else { |
| 96 | + currentIndex = null; |
| 97 | + |
| 98 | + Iterator<?> iterator = unreducedObjects.iterator(event); |
| 99 | + if (iterator == null || !iterator.hasNext()) |
| 100 | + return null; |
| 101 | + |
| 102 | + reducedValue = iterator.next(); |
| 103 | + |
| 104 | + int index = 1; |
| 105 | + while (iterator.hasNext()) { |
| 106 | + currentValue = iterator.next(); |
| 107 | + currentIndex = String.valueOf(index); |
| 108 | + |
| 109 | + Object result = reduceExpr.getSingle(event); |
| 110 | + if (result != null) { |
| 111 | + reducedValue = result; |
| 112 | + } else { |
| 113 | + hadNullResult = true; |
| 114 | + } |
| 115 | + |
| 116 | + index++; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (hadNullResult) { |
| 121 | + warning("The reduce expression returned null for one or more elements, which were skipped."); |
| 122 | + } |
| 123 | + |
| 124 | + return new Object[] { reducedValue }; |
| 125 | + } finally { |
| 126 | + currentValue = null; |
| 127 | + reducedValue = null; |
| 128 | + currentIndex = null; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean isSingle() { |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public Class<?> getReturnType() { |
| 139 | + return reduceExpr.getReturnType(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public Class<?>[] possibleReturnTypes() { |
| 144 | + return reduceExpr.possibleReturnTypes(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public boolean canReturn(Class<?> returnType) { |
| 149 | + return reduceExpr.canReturn(returnType); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public Set<ExprInput<?>> getDependentInputs() { |
| 154 | + return dependentInputs; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public @Nullable Object getCurrentValue() { |
| 159 | + return currentValue; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Gets the current reduced/accumulated value. |
| 164 | + * This is accessible via ExprReducedValue. |
| 165 | + */ |
| 166 | + public @Nullable Object getReducedValue() { |
| 167 | + return reducedValue; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public boolean hasIndices() { |
| 172 | + return keyed; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public @UnknownNullability String getCurrentIndex() { |
| 177 | + return currentIndex; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public String toString(@Nullable Event event, boolean debug) { |
| 182 | + return unreducedObjects.toString(event, debug) + " reduced with [" + reduceExpr.toString(event, debug) + "]"; |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments