dev.vankka.mcdiscordreserializer.minecraft.MinecraftSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of MCDiscordReserializer Show documentation
Show all versions of MCDiscordReserializer Show documentation
A library for transcoding between Minecraft and Discord.
The newest version!
/*
* MCDiscordReserializer: A library for transcoding between Minecraft and Discord.
* Copyright (C) 2018-2021 Vankka
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package dev.vankka.mcdiscordreserializer.minecraft;
import dev.vankka.mcdiscordreserializer.renderer.MinecraftRenderer;
import dev.vankka.mcdiscordreserializer.renderer.NodeRenderer;
import dev.vankka.mcdiscordreserializer.renderer.implementation.DefaultDiscordEscapingRenderer;
import dev.vankka.mcdiscordreserializer.renderer.implementation.DefaultMinecraftRenderer;
import dev.vankka.simpleast.core.node.Node;
import dev.vankka.simpleast.core.node.TextNode;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import net.kyori.adventure.text.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
/**
* MinecraftSerializer, for serializing from Discord messages to Minecraft {@link Component}s.
*
* @author Vankka
*
* @see MinecraftSerializerOptions
* @see MinecraftRenderer
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class MinecraftSerializer {
/**
* Default instance of the MinecraftSerializer, incase that's all you need.
* Using {@link MinecraftSerializer#setDefaultOptions(MinecraftSerializerOptions)} and
* {@link MinecraftSerializer#setMarkdownDefaultOptions(MinecraftSerializerOptions)} are not allowed.
*/
public static final MinecraftSerializer INSTANCE = new MinecraftSerializer() {
@Override
public void setDefaultOptions(MinecraftSerializerOptions defaultOptions) {
throw new UnsupportedOperationException("Cannot modify public instance");
}
@Override
public void setMarkdownDefaultOptions(MinecraftSerializerOptions markdownDefaultOptions) {
throw new UnsupportedOperationException("Cannot modify public instance");
}
};
/**
* The default {@link dev.vankka.mcdiscordreserializer.minecraft.MinecraftSerializerOptions}
* to use for this serializer.
* @see #serialize(String)
*/
@Getter
@Setter
private MinecraftSerializerOptions defaultOptions;
/**
* The default {@link dev.vankka.mcdiscordreserializer.minecraft.MinecraftSerializerOptions}
* to use for escaping markdown.
* @see #escapeMarkdown(String, MinecraftSerializerOptions)
*/
@Getter
@Setter
private MinecraftSerializerOptions markdownDefaultOptions;
/**
* Constructor for creating a serializer, with {@link MinecraftSerializerOptions#defaults()}
* and {@link MinecraftSerializerOptions#escapeDefaults()} as defaults.
*/
public MinecraftSerializer() {
this(MinecraftSerializerOptions.defaults(), MinecraftSerializerOptions.escapeDefaults());
}
/**
* Constructor for creating a serializer, with the specified {@link MinecraftSerializerOptions} as defaults.
*
* @param defaultOptions the default serializer options (can be overridden on serialize)
* @see MinecraftSerializerOptions#defaults()
* @see MinecraftSerializerOptions#MinecraftSerializerOptions(dev.vankka.simpleast.core.parser.Parser, List, List, boolean)
*/
public MinecraftSerializer(@NonNull MinecraftSerializerOptions defaultOptions,
@NonNull MinecraftSerializerOptions markdownDefaultOptions) {
this.defaultOptions = defaultOptions;
this.markdownDefaultOptions = markdownDefaultOptions;
}
/**
* Serializes Discord formatting (markdown) to a Minecraft {@link Component} using this serializer's
* {@link MinecraftSerializer#getDefaultOptions() default options}.
* Use {@link MinecraftSerializer#serialize(String, MinecraftSerializerOptions)} to fine tune the serialization options.
*
* @param discordMessage a Discord markdown message
* @return the Discord message formatted to a Minecraft TextComponent
*/
public Component serialize(@NonNull final String discordMessage) {
MinecraftSerializerOptions options = getDefaultOptions();
return serialize(discordMessage, options);
}
/**
* Serializes Discord formatting (markdown) to a Minecraft {@link Component}.
*
* @param discordMessage a Discord markdown message
* @param serializerOptions The options to use for this serialization
* @return the Discord message formatted to a Minecraft TextComponent
* @see MinecraftSerializerOptions#defaults()
* @see MinecraftSerializerOptions#MinecraftSerializerOptions(dev.vankka.simpleast.core.parser.Parser, List, List, boolean)
*/
public Component serialize(@NonNull final String discordMessage, @NonNull final MinecraftSerializerOptions serializerOptions) {
List components = new ArrayList<>();
List> nodes = serializerOptions.getParser().parse(discordMessage, null, serializerOptions.getRules(), serializerOptions.isDebuggingEnabled());
nodes = flattenTextNodes(nodes); // reduce the amount of single character nodes caused by special characters
for (Node