Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/ch/njol/skript/patterns/ChoicePatternElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -55,4 +57,12 @@ public String toString() {
.map(PatternElement::toFullString)
.collect(Collectors.joining("|"));
}

@Override
public Set<String> getCombinations(boolean clean) {
Set<String> combinations = new HashSet<>();
patternElements.forEach(patternElement -> combinations.addAll(patternElement.getAllCombinations(clean)));
return combinations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.jetbrains.annotations.Nullable;

import java.util.Set;

/**
* A {@link PatternElement} that represents a group, for example {@code (test)}.
*/
Expand Down Expand Up @@ -34,4 +36,9 @@ public String toString() {
return "(" + patternElement + ")";
}

@Override
public Set<String> getCombinations(boolean clean) {
return patternElement.getAllCombinations(clean);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
* A {@link PatternElement} that contains a literal string to be matched, for example {@code hello world}.
Expand Down Expand Up @@ -52,4 +54,9 @@ public String toString() {
return new String(literal);
}

@Override
public Set<String> getCombinations(boolean clean) {
return new HashSet<>(Set.of(toString()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.jetbrains.annotations.Nullable;

import java.util.Set;

/**
* A {@link PatternElement} that contains an optional part, for example {@code [hello world]}.
*/
Expand Down Expand Up @@ -37,4 +39,11 @@ public String toString() {
return "[" + patternElement.toFullString() + "]";
}

@Override
public Set<String> getCombinations(boolean clean) {
Set<String> combinations = patternElement.getAllCombinations(clean);
combinations.add("");
return combinations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* A {@link PatternElement} that applies a parse mark when matched.
Expand Down Expand Up @@ -82,4 +84,16 @@ public String toString() {
}
}

/**
* {@inheritDoc}
* @param clean Whether the parse mark/tag should be excluded.
*/
@Override
public Set<String> getCombinations(boolean clean) {
Set<String> combinations = new HashSet<>();
if (!clean)
combinations.add(toString());
return combinations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static SkriptPattern compile(String pattern) throws MalformedPatternExcep
* {@link TypePatternElement} should be initiated with.
* @return The first link of the {@link PatternElement} chain
*/
private static PatternElement compile(String pattern, AtomicInteger expressionOffset) {
static PatternElement compile(String pattern, AtomicInteger expressionOffset) {
StringBuilder literalBuilder = new StringBuilder();
PatternElement first = null;

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/ch/njol/skript/patterns/PatternElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Set;

public abstract class PatternElement {

@Nullable
Expand Down Expand Up @@ -48,4 +51,51 @@ public String toFullString() {
return stringBuilder.toString();
}

/**
* Gets the combinations available to this {@link PatternElement}.
* @param clean Whether unnecessary data, determined by each implementation, should be excluded from the combinations.
* @return The combinations.
*/
public abstract Set<String> getCombinations(boolean clean);

/**
* Gets all combinations available to this {@link PatternElement} and linked {@link PatternElement}s.
* @param clean Whether unnecessary data, determined by each implementation, should be excluded from the combinations.
* @return The combinations.
*/
public final Set<String> getAllCombinations(boolean clean) {
Set<String> combinations = getCombinations(clean);
if (combinations.isEmpty())
combinations.add("");
PatternElement next = this;
while ((next = next.originalNext) != null) {
Set<String> newCombinations = new HashSet<>();
Set<String> nextCombinations = next.getCombinations(clean);
if (nextCombinations.isEmpty())
continue;
for (String base : combinations) {
for (String add : nextCombinations) {
newCombinations.add(combineCombination(base, add));
}
}
combinations = newCombinations;
}
return combinations;
}

/**
* Helper method for appropriately combining two strings together.
* @return The resulting string.
*/
private static String combineCombination(String first, String second) {
if (first.isBlank()) {
return second.stripLeading();
} else if (second.isEmpty()) {
return first.stripTrailing();
} else if (first.endsWith(" ") && second.startsWith(" ")) {
return first + second.stripLeading();
}
return first + second;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import ch.njol.skript.log.SkriptLogger;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -58,4 +60,9 @@ public String toString() {
return "<" + pattern + ">";
}

@Override
public Set<String> getCombinations(boolean clean) {
return new HashSet<>(Set.of(toString()));
}

}
18 changes: 18 additions & 0 deletions src/main/java/ch/njol/skript/patterns/TypePatternElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import ch.njol.util.NonNullPair;
import org.jetbrains.annotations.Nullable;

import java.util.HashSet;
import java.util.Set;

/**
* A {@link PatternElement} that contains a type to be matched with an expressions, for example {@code %number%}.
*/
Expand Down Expand Up @@ -263,4 +266,19 @@ public ExprInfo getExprInfo() {
return exprInfo;
}

/**
* {@inheritDoc}
* @param clean Whether this type should be replaced with {@code %*%} if it's not literal.
*/
@Override
public Set<String> getCombinations(boolean clean) {
Set<String> combinations = new HashSet<>();
if (!clean || flagMask == 2) {
combinations.add(toString());
} else {
combinations.add("%*%");
}
return combinations;
}

}
Loading