Skip to content

Commit

Permalink
Adds a new pattern for CommandReplacements: %{<key>} (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoinujNosde authored Feb 5, 2021
1 parent 91e7200 commit f033dd9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions core/src/main/java/co/aikar/commands/ACFPatterns.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ final class ACFPatterns {
public static final Pattern EQUALS = Pattern.compile("=");
public static final Pattern FORMATTER = Pattern.compile("<c(?<color>\\d+)>(?<msg>.*?)</c\\1>", Pattern.CASE_INSENSITIVE);
public static final Pattern I18N_STRING = Pattern.compile("\\{@@(?<key>.+?)}", Pattern.CASE_INSENSITIVE);
public static final Pattern REPLACEMENT_PATTERN = Pattern.compile("%\\{.[^\\s]*}");


private ACFPatterns() {
}

private ACFPatterns() {}
@SuppressWarnings("Convert2MethodRef")
static final Map<String, Pattern> patternCache = ExpiringMap.builder()
.maxSize(200)
Expand All @@ -67,9 +69,7 @@ private ACFPatterns() {}
* <p>
* The {@link #patternCache} does not contain the constant patterns defined in this class.
*
* @param pattern
* The raw pattern in a String.
*
* @param pattern The raw pattern in a String.
* @return The pattern which has been cached.
*/
public static Pattern getPattern(String pattern) {
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/co/aikar/commands/CommandReplacements.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void addReplacements(String... replacements) {
throw new IllegalArgumentException("Must pass a number of arguments divisible by 2.");
}
for (int i = 0; i < replacements.length; i += 2) {
addReplacement(replacements[i], replacements[i+1]);
addReplacement(replacements[i], replacements[i + 1]);
}
}

Expand All @@ -61,7 +61,8 @@ public String addReplacement(String key, String val) {
@Nullable
private String addReplacement0(String key, String val) {
key = ACFPatterns.PERCENTAGE.matcher(key.toLowerCase(Locale.ENGLISH)).replaceAll("");
Pattern pattern = Pattern.compile("%" + Pattern.quote(key) + "\\b", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile("%\\{" + Pattern.quote(key) + "}|%" + Pattern.quote(key) + "\\b",
Pattern.CASE_INSENSITIVE);

Map.Entry<Pattern, String> entry = new AbstractMap.SimpleImmutableEntry<>(pattern, val);
Map.Entry<Pattern, String> replaced = replacements.put(key, entry);
Expand All @@ -83,8 +84,7 @@ public String replace(String text) {
}

// check for unregistered replacements
Pattern pattern = Pattern.compile("%.[^\\s]*");
Matcher matcher = pattern.matcher(text);
Matcher matcher = ACFPatterns.REPLACEMENT_PATTERN.matcher(text);
while (matcher.find()) {
this.manager.log(LogLevel.ERROR, "Found unregistered replacement: " + matcher.group());
}
Expand Down

0 comments on commit f033dd9

Please sign in to comment.