Fix using code blocks and code strings, reorganize and rename some stuff

This commit is contained in:
Vankka 2024-07-20 16:47:19 +03:00
parent c225f92032
commit 7f3c4c958b
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
15 changed files with 62 additions and 59 deletions

View File

@ -28,7 +28,7 @@ import com.discordsrv.api.discord.DiscordAPI;
import com.discordsrv.api.discord.connection.details.DiscordConnectionDetails;
import com.discordsrv.api.event.bus.EventBus;
import com.discordsrv.api.placeholder.PlaceholderService;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import com.discordsrv.api.player.DiscordSRVPlayer;
import com.discordsrv.api.player.IPlayerProvider;
import com.discordsrv.api.profile.IProfileManager;
@ -110,7 +110,7 @@ public interface DiscordSRVApi {
* @return the {@link PlainPlaceholderFormat} instance
*/
@NotNull
PlainPlaceholderFormat discordPlaceholders();
PlainPlaceholderFormat discordMarkdownFormat();
/**
* A provider for {@link com.discordsrv.api.component.MinecraftComponent}s.

View File

@ -29,9 +29,9 @@ import com.discordsrv.api.discord.entity.message.AllowedMention;
import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.api.discord.util.DiscordFormattingUtil;
import com.discordsrv.api.placeholder.FormattedText;
import com.discordsrv.api.placeholder.format.FormattedText;
import com.discordsrv.api.placeholder.PlaceholderService;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.util.Placeholders;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.jetbrains.annotations.NotNull;
@ -360,7 +360,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
}
this.replacements.put(
PlaceholderService.PATTERN,
wrapFunction(matcher -> api.placeholderService().getResultAsPlain(matcher, context))
wrapFunction(matcher -> api.placeholderService().getResultAsCharSequence(matcher, context))
);
return this;
}
@ -373,8 +373,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
return result.toString();
} else if (result instanceof CharSequence) {
// Escape content
return DiscordFormattingUtil.escapeContent(
result.toString());
return DiscordFormattingUtil.escapeContent(result.toString());
}
// Use default behaviour for everything else
@ -401,13 +400,13 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
String output = placeholderUtil.toString();
return output.isEmpty() ? null : output;
};
Function<String, String> discordPlaceholders = input -> {
Function<String, String> markdownPlaceholders = input -> {
if (input == null) {
return null;
}
// Empty string -> null (so we don't provide empty strings to random fields)
String output = api.discordPlaceholders().map(input, in -> {
String output = api.discordMarkdownFormat().map(input, in -> {
// Since this will be processed in parts, we don't want parts to return null, only the full output
String out = placeholders.apply(in);
return out == null ? "" : out;
@ -418,7 +417,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
PlainPlaceholderFormat.with(
PlainPlaceholderFormat.Formatting.DISCORD,
() -> builder.setContent(discordPlaceholders.apply(builder.getContent()))
() -> builder.setContent(markdownPlaceholders.apply(builder.getContent()))
);
List<DiscordMessageEmbed> embeds = new ArrayList<>(builder.getEmbeds());
@ -461,7 +460,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
PlainPlaceholderFormat.with(PlainPlaceholderFormat.Formatting.DISCORD, () -> embedBuilder.setDescription(
cutToLength(
discordPlaceholders.apply(embedBuilder.getDescription()),
markdownPlaceholders.apply(embedBuilder.getDescription()),
MessageEmbed.DESCRIPTION_MAX_LENGTH
)
));

View File

@ -54,8 +54,8 @@ public interface PlaceholderService {
Object getResult(@NotNull Matcher matcher, @NotNull Set<Object> context);
@NotNull
CharSequence getResultAsPlain(@NotNull Matcher matcher, @NotNull Set<Object> context);
CharSequence getResultAsCharSequence(@NotNull Matcher matcher, @NotNull Set<Object> context);
@NotNull
CharSequence getResultAsPlain(@Nullable Object result);
CharSequence getResultAsCharSequence(@Nullable Object result);
}

View File

@ -21,7 +21,7 @@
* SOFTWARE.
*/
package com.discordsrv.api.placeholder;
package com.discordsrv.api.placeholder.format;
import org.jetbrains.annotations.NotNull;

View File

@ -21,7 +21,7 @@
* SOFTWARE.
*/
package com.discordsrv.api.placeholder;
package com.discordsrv.api.placeholder.format;
import java.util.function.Function;
import java.util.function.Supplier;

View File

@ -21,7 +21,7 @@ package com.discordsrv.bukkit.integration;
import com.discordsrv.api.event.bus.Subscribe;
import com.discordsrv.api.event.events.placeholder.PlaceholderLookupEvent;
import com.discordsrv.api.placeholder.PlaceholderLookupResult;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import com.discordsrv.api.player.DiscordSRVPlayer;
import com.discordsrv.api.profile.IProfile;
import com.discordsrv.bukkit.BukkitDiscordSRV;

View File

@ -74,7 +74,7 @@ import com.discordsrv.common.messageforwarding.game.StopMessageModule;
import com.discordsrv.common.messageforwarding.game.minecrafttodiscord.MentionCachingModule;
import com.discordsrv.common.module.ModuleManager;
import com.discordsrv.common.module.type.AbstractModule;
import com.discordsrv.common.placeholder.DiscordPlaceholdersImpl;
import com.discordsrv.common.placeholder.format.DiscordMarkdownFormatImpl;
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
import com.discordsrv.common.placeholder.context.GlobalDateFormattingContext;
import com.discordsrv.common.placeholder.context.GlobalTextHandlingContext;
@ -136,7 +136,7 @@ public abstract class AbstractDiscordSRV<
private EventBusImpl eventBus;
private ProfileManager profileManager;
private PlaceholderServiceImpl placeholderService;
private DiscordPlaceholdersImpl discordPlaceholders;
private DiscordMarkdownFormatImpl discordMarkdownFormat;
private ComponentFactory componentFactory;
private DiscordAPIImpl discordAPI;
private DiscordConnectionDetailsImpl discordConnectionDetails;
@ -181,7 +181,7 @@ public abstract class AbstractDiscordSRV<
this.moduleManager = new ModuleManager(this);
this.profileManager = new ProfileManager(this);
this.placeholderService = new PlaceholderServiceImpl(this);
this.discordPlaceholders = new DiscordPlaceholdersImpl();
this.discordMarkdownFormat = new DiscordMarkdownFormatImpl();
this.componentFactory = new ComponentFactory(this);
this.discordAPI = new DiscordAPIImpl(this);
this.discordConnectionDetails = new DiscordConnectionDetailsImpl(this);
@ -301,8 +301,8 @@ public abstract class AbstractDiscordSRV<
}
@Override
public final @NotNull DiscordPlaceholdersImpl discordPlaceholders() {
return discordPlaceholders;
public final @NotNull DiscordMarkdownFormatImpl discordMarkdownFormat() {
return discordMarkdownFormat;
}
@Override

View File

@ -20,7 +20,7 @@ package com.discordsrv.common;
import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.module.Module;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import com.discordsrv.common.bootstrap.IBootstrap;
import com.discordsrv.common.channel.ChannelConfigHelper;
import com.discordsrv.common.command.game.GameCommandExecutionHelper;
@ -98,7 +98,7 @@ public interface DiscordSRV extends DiscordSRVApi {
@Override
@NotNull
PlainPlaceholderFormat discordPlaceholders();
PlainPlaceholderFormat discordMarkdownFormat();
@Override
@NotNull

View File

@ -28,7 +28,7 @@ import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessage;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.api.discord.util.DiscordFormattingUtil;
import com.discordsrv.api.event.events.discord.message.DiscordMessageReceiveEvent;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.provider.SinglePlaceholder;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.command.game.GameCommandExecutionHelper;

View File

@ -22,7 +22,7 @@ import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.api.discord.connection.details.DiscordGatewayIntent;
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
import com.discordsrv.api.event.bus.Subscribe;
import com.discordsrv.api.placeholder.FormattedText;
import com.discordsrv.api.placeholder.format.FormattedText;
import com.discordsrv.api.placeholder.annotation.Placeholder;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.main.DiscordInviteConfig;

View File

@ -34,7 +34,7 @@ import com.discordsrv.api.event.events.discord.message.DiscordMessageDeleteEvent
import com.discordsrv.api.event.events.discord.message.DiscordMessageUpdateEvent;
import com.discordsrv.api.event.events.message.forward.game.AbstractGameMessageForwardedEvent;
import com.discordsrv.api.event.events.message.receive.discord.DiscordChatMessageReceiveEvent;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.provider.SinglePlaceholder;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.main.channels.MirroringConfig;

View File

@ -30,7 +30,7 @@ import com.discordsrv.api.event.bus.EventPriority;
import com.discordsrv.api.event.bus.Subscribe;
import com.discordsrv.api.event.events.message.forward.game.GameChatMessageForwardedEvent;
import com.discordsrv.api.event.events.message.receive.game.GameChatMessageReceiveEvent;
import com.discordsrv.api.placeholder.FormattedText;
import com.discordsrv.api.placeholder.format.FormattedText;
import com.discordsrv.api.placeholder.util.Placeholders;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.util.ComponentUtil;
@ -225,7 +225,7 @@ public class MinecraftToDiscordChatModule extends AbstractGameMessageModule<Mine
}
public String convertComponent(MinecraftToDiscordChatConfig config, Component component) {
String content = discordSRV.placeholderService().getResultAsPlain(component).toString();
String content = discordSRV.placeholderService().getResultAsCharSequence(component).toString();
Placeholders messagePlaceholders = new Placeholders(content);
config.contentRegexFilters.forEach(messagePlaceholders::replaceAll);

View File

@ -177,13 +177,13 @@ public class PlaceholderServiceImpl implements PlaceholderService {
}
@Override
public @NotNull CharSequence getResultAsPlain(@NotNull Matcher matcher, @NotNull Set<Object> context) {
public @NotNull CharSequence getResultAsCharSequence(@NotNull Matcher matcher, @NotNull Set<Object> context) {
Object result = getResult(matcher, context);
return getResultAsPlain(result);
return getResultAsCharSequence(result);
}
@Override
public @NotNull CharSequence getResultAsPlain(@Nullable Object result) {
public @NotNull CharSequence getResultAsCharSequence(@Nullable Object result) {
if (result == null) {
return "";
} else if (result instanceof CharSequence) {
@ -216,7 +216,7 @@ public class PlaceholderServiceImpl implements PlaceholderService {
private String updateContent(List<PlaceholderLookupResult> results, String placeholder, Matcher matcher, String input) {
Object representation = getResultRepresentation(results, placeholder, matcher);
CharSequence output = getResultAsPlain(representation);
CharSequence output = getResultAsCharSequence(representation);
return Pattern.compile(
matcher.group(1) + placeholder + matcher.group(3),
Pattern.LITERAL
@ -242,9 +242,9 @@ public class PlaceholderServiceImpl implements PlaceholderService {
case SUCCESS:
replacement = result.getValue();
if (replacement == null) {
replacement = getResultAsPlain(null);
replacement = getResultAsCharSequence(null);
}
if (StringUtils.isNotBlank(getResultAsPlain(replacement))) {
if (StringUtils.isNotBlank(getResultAsCharSequence(replacement))) {
return replacement;
}
break;

View File

@ -16,13 +16,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.discordsrv.common.placeholder;
package com.discordsrv.common.placeholder.format;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import dev.vankka.mcdiscordreserializer.rules.DiscordMarkdownRules;
import dev.vankka.simpleast.core.TextStyle;
import dev.vankka.mcdiscordreserializer.rules.StyleNode;
import dev.vankka.simpleast.core.node.Node;
import dev.vankka.simpleast.core.node.StyleNode;
import dev.vankka.simpleast.core.node.TextNode;
import dev.vankka.simpleast.core.parser.Parser;
import dev.vankka.simpleast.core.parser.Rule;
@ -32,11 +31,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class DiscordPlaceholdersImpl implements PlainPlaceholderFormat {
public class DiscordMarkdownFormatImpl implements PlainPlaceholderFormat {
private final Parser<Object, Node<Object>, Object> parser;
public DiscordPlaceholdersImpl() {
public DiscordMarkdownFormatImpl() {
List<Rule<Object, Node<Object>, Object>> rules = new ArrayList<>();
rules.add(SimpleMarkdownRules.createEscapeRule());
rules.add(SimpleMarkdownRules.createNewlineRule());
@ -61,31 +60,36 @@ public class DiscordPlaceholdersImpl implements PlainPlaceholderFormat {
PlainPlaceholderFormat.with(Formatting.DISCORD, () -> finalText.append(placeholders.apply(content)));
for (Object style : ((StyleNode<?, ?>) node).getStyles()) {
if (!(style instanceof TextStyle)) {
if (!(style instanceof StyleNode.Style)) {
continue;
}
TextStyle textStyle = (TextStyle) style;
StyleNode.Style textStyle = (StyleNode.Style) style;
String childText = ((TextNode<?>) node.getChildren().get(0)).getContent();
if (textStyle.getType() == TextStyle.Type.CODE_STRING) {
finalText.append("`").append(placeholders.apply(childText)).append("`");
} else if (textStyle.getType() == TextStyle.Type.CODE_BLOCK) {
String language = textStyle.getExtra().get("language");
if (textStyle == StyleNode.Styles.CODE_STRING) {
String blockContent = placeholders.apply(childText);
if (blockContent != null && !blockContent.isEmpty()) {
finalText.append("`").append(blockContent).append("`");
}
} else if (textStyle instanceof StyleNode.CodeBlockStyle) {
String language = ((StyleNode.CodeBlockStyle) textStyle).getLanguage();
if (language != null && language.equals("ansi")) {
PlainPlaceholderFormat.with(Formatting.ANSI, () -> finalText
.append("```ansi\n")
.append(placeholders.apply(childText))
.append("```")
);
String blockContent = PlainPlaceholderFormat.supplyWith(Formatting.ANSI, () -> placeholders.apply(childText));
if (blockContent != null && !blockContent.isEmpty()) {
finalText.append("```ansi\n").append(blockContent).append("```");
}
} else {
finalText
.append("```")
.append(language != null ? language : "")
.append("\n")
.append(placeholders.apply(childText))
.append("```");
String blockContent = PlainPlaceholderFormat.supplyWith(Formatting.PLAIN, () -> placeholders.apply(childText));
if (blockContent != null && !blockContent.isEmpty()) {
finalText
.append("```")
.append(language != null ? language : "")
.append("\n")
.append(blockContent)
.append("```");
}
}
}
}

View File

@ -19,8 +19,8 @@
package com.discordsrv.common.placeholder.result;
import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.api.placeholder.FormattedText;
import com.discordsrv.api.placeholder.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.format.FormattedText;
import com.discordsrv.api.placeholder.format.PlainPlaceholderFormat;
import com.discordsrv.api.placeholder.mapper.PlaceholderResultMapper;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.util.ComponentUtil;