Skip to content

Commit

Permalink
Fix placeholder replacements in click events
Browse files Browse the repository at this point in the history
  • Loading branch information
Brikster committed Dec 11, 2023
1 parent 59c7ab6 commit 1f722f2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class LinkParserComponentTransformer implements ComponentTransforme
} catch (MalformedURLException | URISyntaxException e) {
return null;
}
});
}, __ -> null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public abstract class AbstractPlaceholderApiComponentTransformer implements Plac
} else {
return componentStringConverter.stringToComponent(matchedWithPlaceholders + " ");
}
}, matchedString -> {
String matchedTransformed = matchedStringTransformFunction.apply(matchedString);
String matchedWithPlaceholders = PlaceholderAPI.setPlaceholders(context.getPlayer(), matchedTransformed);
if (matchedWithPlaceholders.equals(matchedString)) {
return null;
} else {
return matchedWithPlaceholders;
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public final class PlaceholderApiRelationalComponentTransformer extends Relation
} else {
return componentStringConverter.stringToComponent(matchedWithPlaceholders + " ");
}
}, matchedString -> {
String matchedWithPlaceholders = PlaceholderAPI.setRelationalPlaceholders(context.getOne(), context.getTwo(), matchedString);
if (matchedWithPlaceholders.equals(matchedString)) {
return null;
} else {
return matchedWithPlaceholders;
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public AbstractPrefixComponentTransformer(PrefixProvider prefixProvider,
} else {
throw new IllegalStateException("Illegal string matched: " + matchedString);
}
}, matchedString -> {
if (matchedString.equals(prefixPlaceholder)) {
return ObjectUtil.requireNonNullElse(prefix, "");
} else if (matchedString.equals(suffixPlaceholder)) {
return ObjectUtil.requireNonNullElse(suffix, "");
} else {
throw new IllegalStateException("Illegal string matched: " + matchedString);
}
});
}

Expand Down
40 changes: 34 additions & 6 deletions spigot/src/main/java/ru/brikster/chatty/util/AdventureUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,20 @@ private List<ComponentPart> parts(Component component) {
* > inserted components uses previous click/hover events, fonts and insertion, if they don't have own <br>
* > inserted components do NOT share their click/hover events, fonts and insertions with the following components <br>
* <br>
* If replaceFunction returns null, matched string won't be replaced.
* If componentReplaceFunction returns null, matched string won't be replaced.
* Processed component should have ending space as a trick for keeping ending styles (for example, component from
* legacy string "&6&lPrefix &e" will lose yellow color without ending space)
* legacy string "&6&lPrefix &e" will lose yellow color without ending space).
* stringReplaceFunction used for non-styled text sections in click event and shouldn't be end-spaced
* @author Brikster
* @param componentWithEndingSpace the component with ending space
* @param pattern the pattern to match
* @param replaceFunction the function to process a matched string
* @param componentReplaceFunction the function to process a matched string into a component
* @param stringReplaceFunction the function to process a matched string into a string
* @return the processed component
*/
public Component replaceWithEndingSpace(Component componentWithEndingSpace, Pattern pattern, Function<String, @Nullable Component> replaceFunction) {
public Component replaceWithEndingSpace(Component componentWithEndingSpace, Pattern pattern,
Function<String, @Nullable Component> componentReplaceFunction,
Function<String, @Nullable String> stringReplaceFunction) {
List<ComponentPart> originalParts = parts(componentWithEndingSpace);
Component resultComponent = Component.empty();

Expand All @@ -274,17 +278,41 @@ public Component replaceWithEndingSpace(Component componentWithEndingSpace, Patt
hoverEvent = hoverEvent.value(replaceWithEndingSpace(
hoverEvent.value(),
pattern,
replaceFunction));
componentReplaceFunction,
stringReplaceFunction));
part = part.hoverEvent(hoverEvent);
}

if (part.clickEvent != null) {
StringBuilder builder = new StringBuilder();

String clickEventValue = part.clickEvent.value();

int beginIndex = 0;
Matcher matcher = pattern.matcher(clickEventValue);
while (matcher.find()) {
builder.append(clickEventValue, beginIndex, matcher.start());
String group = matcher.group();
String replacement = stringReplaceFunction.apply(group);
builder.append(replacement == null ? group : replacement);
beginIndex = matcher.end();
}

if (beginIndex != clickEventValue.length()) {
builder.append(clickEventValue.substring(beginIndex));
}

part = part.clickEvent(ClickEvent
.clickEvent(part.clickEvent.action(), builder.toString()));
}

state.apply(part);

int beginIndex = 0;
Matcher matcher = pattern.matcher(part.text);
while (matcher.find()) {
String group = matcher.group();
Component replaced = replaceFunction.apply(group);
Component replaced = componentReplaceFunction.apply(group);
if (replaced != null) {
String previousText = part.text.substring(beginIndex, matcher.start());
beginIndex = matcher.end();
Expand Down

0 comments on commit 1f722f2

Please sign in to comment.