Skip to content

Commit

Permalink
Improve replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
Brikster committed Jul 8, 2024
1 parent 3d0c710 commit 19028c2
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.brikster.chatty.chat.component.impl;

import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

public final class InternalPlaceholdersReplacementsStringTransformer implements ReplacementsStringTransformer {

@Override
public String transform(@NotNull OfflinePlayer sender, @NotNull String message) {
return message.replace("{player}", sender instanceof Player
? ((Player) sender).getDisplayName()
: Objects.requireNonNull(sender.getName(), "Player name cannot be null"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class ReplacementsComponentTransformer implements PlaceholdersCompo

private final ReplacementsConfig replacementsConfig;
private final ComponentStringConverter componentStringConverter;
private final ReplacementsStringTransformer replacementsStringTransformer;
private final Set<String> cycledReplacements;

@Override
Expand All @@ -28,15 +29,15 @@ public final class ReplacementsComponentTransformer implements PlaceholdersCompo
do {
matches[0] = 0;
componentWithReplacements = AdventureUtil.replaceWithEndingSpace(componentWithReplacements, Constants.REPLACEMENTS_PATTERN, matchedString -> {
String result = replace(matchedString);
String result = replace(matchedString, context);
Component component = componentStringConverter.stringToComponent(result + " ");
if (result != null) {
matches[0]++;
return component;
}
return null;
}, matchedString -> {
String result = replace(matchedString);
String result = replace(matchedString, context);
if (result != null) {
matches[0]++;
return result;
Expand All @@ -48,10 +49,11 @@ public final class ReplacementsComponentTransformer implements PlaceholdersCompo
return componentWithReplacements;
}

private String replace(String matchedString) {
private String replace(String matchedString, SinglePlayerTransformContext context) {
String replacementKey = matchedString.substring(3, matchedString.length() - 1);
if (cycledReplacements.contains(replacementKey)) return null;
return replacementsConfig.getReplacements().get(replacementKey);
String replacementText = replacementsConfig.getReplacements().get(replacementKey);
return replacementsStringTransformer.transform(context.getPlayer(), replacementText);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.brikster.chatty.chat.component.impl;

import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;

public interface ReplacementsStringTransformer {

String transform(@NotNull OfflinePlayer sender, @NotNull String message);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.brikster.chatty.chat.component.impl.papi;

import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import ru.brikster.chatty.chat.component.impl.ReplacementsStringTransformer;

public final class PlaceholderApiReplacementsStringTransformer implements ReplacementsStringTransformer {

@Override
public String transform(@NotNull OfflinePlayer sender, @NotNull String message) {
return PlaceholderAPI.setPlaceholders(sender, message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ public void handleFinishedEarlyContextEvent(AsyncPlayerChatEvent event) {
if (groupIndex == 0) {
String stringFormat = LegacyComponentSerializer.legacySection().serialize(lateContext.getFormat());
String stringMessage = LegacyComponentSerializer.legacySection().serialize(lateContext.getMessage());
stringFormat = stringFormat.replaceFirst(Pattern.quote("{player}"), Matcher.quoteReplacement(lateContext.getSender().getDisplayName()));
stringFormat = stringFormat.replaceFirst(Pattern.quote("{message}"), Matcher.quoteReplacement(stringMessage));
stringFormat = stringFormat.replace("%", "%%");
stringFormat = stringFormat.replaceFirst(Pattern.quote("{player}"), Matcher.quoteReplacement("%1$s"));
stringFormat = stringFormat.replaceFirst(Pattern.quote("{message}"), Matcher.quoteReplacement("%2$s"));
event.setFormat(stringFormat);
event.setMessage(stringMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;
import ru.brikster.chatty.api.chat.message.strategy.MessageTransformStrategy;
import ru.brikster.chatty.chat.component.impl.ChainPlaceholdersComponentTransformer;
import ru.brikster.chatty.chat.component.impl.PlaceholdersComponentTransformer;
import ru.brikster.chatty.chat.component.impl.RelationalPlaceholdersComponentTransformer;
import ru.brikster.chatty.chat.component.impl.ReplacementsComponentTransformer;
import ru.brikster.chatty.chat.component.impl.*;
import ru.brikster.chatty.chat.component.impl.dummy.DummyPlaceholdersComponentTransformer;
import ru.brikster.chatty.chat.component.impl.dummy.DummyRelationalPlaceholdersComponentTransformer;
import ru.brikster.chatty.chat.component.impl.papi.CommonChatPlaceholderApiComponentTransformer;
import ru.brikster.chatty.chat.component.impl.papi.PlaceholderApiRelationalComponentTransformer;
import ru.brikster.chatty.chat.component.impl.papi.PlaceholderApiReplacementsStringTransformer;
import ru.brikster.chatty.chat.component.impl.pm.placeholders.PmFromPlaceholderApiComponentTransformer;
import ru.brikster.chatty.chat.component.impl.pm.placeholders.PmFromPlaceholdersComponentTransformer;
import ru.brikster.chatty.chat.component.impl.pm.placeholders.PmToPlaceholderApiComponentTransformer;
Expand Down Expand Up @@ -282,10 +280,31 @@ private boolean isVaultChatRegistered() {
}
}

@Provides
@Singleton
public ReplacementsStringTransformer replacementsStringTransformer(ProxyConfig proxyConfig) {
List<ReplacementsStringTransformer> transformerList = new LinkedList<>();

if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
transformerList.add(new PlaceholderApiReplacementsStringTransformer());
}

transformerList.add(new InternalPlaceholdersReplacementsStringTransformer());

return (sender, message) -> {
String result = message;
for (ReplacementsStringTransformer transformer : transformerList) {
result = transformer.transform(sender, result);
}
return result;
};
}

@Provides
@Singleton
public PlaceholdersComponentTransformer placeholdersComponentTransformer(ReplacementsConfig replacementsConfig,
ComponentStringConverter componentStringConverter,
ReplacementsStringTransformer replacementsStringTransformer,
Logger logger) {
List<PlaceholdersComponentTransformer> transformerList = new LinkedList<>();

Expand All @@ -297,7 +316,7 @@ public PlaceholdersComponentTransformer placeholdersComponentTransformer(Replace
}
}

transformerList.add(new ReplacementsComponentTransformer(replacementsConfig, componentStringConverter, cycleAnalysisResult.getKeysWithCycles()));
transformerList.add(new ReplacementsComponentTransformer(replacementsConfig, componentStringConverter, replacementsStringTransformer, cycleAnalysisResult.getKeysWithCycles()));

if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
transformerList.add(new CommonChatPlaceholderApiComponentTransformer(componentStringConverter));
Expand Down

0 comments on commit 19028c2

Please sign in to comment.