Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: feat: restore legacy parser #431

Draft
wants to merge 10 commits into
base: v4
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface MessageParser {
* @return The result of the translation
* @since 4.0.0
*/
@NotNull TranslationResult<String> translateString(String text, Localized language, FeatureSyntax syntax);
@NotNull TranslationResult<String> translateString(@NotNull String text, @NotNull Localized language, @NotNull FeatureSyntax syntax);

/**
* Find and replace Triton placeholders in a Component.
Expand All @@ -46,6 +46,6 @@ public interface MessageParser {
* @return The result of the translation
* @since 4.0.0
*/
@NotNull TranslationResult<Component> translateComponent(Component component, Localized language, FeatureSyntax syntax);
@NotNull TranslationResult<Component> translateComponent(@NotNull Component component, @NotNull Localized language, @NotNull FeatureSyntax syntax);

}
16 changes: 8 additions & 8 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ dependencies {
compileOnly project(":api")

// Adventure / MiniMessage
compileOnly 'net.kyori:adventure-text-serializer-gson:4.17.0'
compileOnly 'net.kyori:adventure-text-serializer-legacy:4.17.0'
compileOnly 'net.kyori:adventure-text-serializer-plain:4.17.0'
compileOnly('net.kyori:adventure-text-minimessage:4.17.0') {
compileOnly 'net.kyori:adventure-text-serializer-gson:4.18.0'
compileOnly 'net.kyori:adventure-text-serializer-legacy:4.18.0'
compileOnly 'net.kyori:adventure-text-serializer-plain:4.18.0'
compileOnly('net.kyori:adventure-text-minimessage:4.18.0') {
exclude group: 'net.kyori', module: 'adventure-api'
exclude group: 'net.kyori', module: 'adventure-key'
exclude group: 'net.kyori', module: 'adventure-text-serialize-gson'
Expand Down Expand Up @@ -48,10 +48,10 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'

testImplementation 'net.kyori:adventure-api:4.17.0'
testImplementation 'net.kyori:adventure-text-serializer-gson:4.17.0'
testImplementation 'net.kyori:adventure-text-serializer-legacy:4.17.0'
testImplementation 'net.kyori:adventure-text-serializer-plain:4.17.0'
testImplementation 'net.kyori:adventure-api:4.18.0'
testImplementation 'net.kyori:adventure-text-serializer-gson:4.18.0'
testImplementation 'net.kyori:adventure-text-serializer-legacy:4.18.0'
testImplementation 'net.kyori:adventure-text-serializer-plain:4.18.0'
}

shadowJar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public enum Dependency {
ADVENTURE(
"net{}kyori",
"adventure-api",
"4.17.0",
"FcjC6xpp2LG8kU9VQ1PajufPB0wFyAdNqYmK7lxw0Ng=",
"4.18.0",
"B5+PYDDzIMNxWau11V0Qaxc2tEyQ6fekKIaoEYPaw/w=",
relocate("net{}kyori{}adventure", "adventure"),
relocate("net{}kyori{}examination", "kyori{}examination")
),
ADVENTURE_TEXT_SERIALIZER_GSON(
"net{}kyori",
"adventure-text-serializer-gson",
"4.17.0",
"YifWv9LWrm0SHT3NpZDTP/g3POpliUhd8/e3Wwnn0hA=",
"4.18.0",
"zPaBB6/mWl9vjYImmLAC5G4F4QrKb7WAGn+GoO1x+T0=",
relocate("net{}kyori{}option", "kyori{}option"),
relocateIf("net{}kyori{}adventure", "adventure", LoaderFlag.RELOCATE_ADVENTURE),
relocateIf("net{}kyori{}examination", "kyori{}examination", LoaderFlag.RELOCATE_ADVENTURE),
Expand All @@ -35,17 +35,17 @@ public enum Dependency {
ADVENTURE_TEXT_SERIALIZER_LEGACY(
"net{}kyori",
"adventure-text-serializer-legacy",
"4.17.0",
"nUlVVC5A61tB2+0XKygmrPHL40BeAC8zYOsupXjVNro=",
"4.18.0",
"NuCPIYknxH3KHJAwavsx8fC7v8cePqWLD7/h1wIMaWU=",
relocateIf("net{}kyori{}adventure", "adventure", LoaderFlag.RELOCATE_ADVENTURE),
relocateIf("net{}kyori{}examination", "kyori{}examination", LoaderFlag.RELOCATE_ADVENTURE),
relocateIfNot("net{}kyori{}adventure{}text{}serializer{}legacy", "adventure{}text{}serializer{}legacy", LoaderFlag.RELOCATE_ADVENTURE)
),
ADVENTURE_TEXT_SERIALIZER_PLAIN(
"net{}kyori",
"adventure-text-serializer-plain",
"4.17.0",
"Im1KPSxCt3J98RoCppjGxkR1OEcF3RK/Asf2l0R5nC4=",
"4.18.0",
"Ff+eI31b8gDsrOtX6DBJ0jdlaQu4zjpTIKo31LXLo9o=",
relocateIf("net{}kyori{}adventure", "adventure", LoaderFlag.RELOCATE_ADVENTURE),
relocateIf("net{}kyori{}examination", "kyori{}examination", LoaderFlag.RELOCATE_ADVENTURE),
relocateIfNot("net{}kyori{}adventure{}text{}serializer{}plain", "adventure{}text{}serializer{}plain", LoaderFlag.RELOCATE_ADVENTURE)
Expand All @@ -66,8 +66,8 @@ public enum Dependency {
ADVENTURE_MINI_MESSAGE(
"net{}kyori",
"adventure-text-minimessage",
"4.17.0",
"g3Kp6bwMCuIdYObObihKgcbJbR7DmPgIglAVxvYCUvU=",
"4.18.0",
"RE5xCOKwMNAtEtNkOVYzUMz88s9qj1DbYFf8dqFtCr8=",
relocate("net{}kyori{}option", "kyori{}option"),
relocateIf("net{}kyori{}adventure", "adventure", LoaderFlag.RELOCATE_ADVENTURE),
relocateIf("net{}kyori{}examination", "kyori{}examination", LoaderFlag.RELOCATE_ADVENTURE),
Expand All @@ -81,16 +81,16 @@ public enum Dependency {
ADVENTURE_KEY(
"net{}kyori",
"adventure-key",
"4.17.0",
"jlz1cGEtbM7flDrBcWpEneLdWpAge33m9zwCNpNbdm4=",
"4.18.0",
"KzLdH5mHr43NC/Wrjpb8Pe6sA/hdUym1iNmLgGIw5S0=",
relocate("net{}kyori{}adventure", "adventure"),
relocateIf("net{}kyori{}examination", "kyori{}examination", LoaderFlag.RELOCATE_ADVENTURE)
),
ADVENTURE_TEXT_SERIALIZER_JSON(
"net{}kyori",
"adventure-text-serializer-json",
"4.17.0",
"bJHT4jsGTXarfBVOvpQPVSIyrheLldIbUN/c/sKSXZY=",
"4.18.0",
"ZFBB/KVQs78by4UItylCzBXqGDd7Uc65bQc+30Mutm8=",
relocate("net{}kyori{}option", "kyori{}option"),
relocateIf("net{}kyori{}adventure", "adventure", LoaderFlag.RELOCATE_ADVENTURE),
relocateIf("net{}kyori{}examination", "kyori{}examination", LoaderFlag.RELOCATE_ADVENTURE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
@RequiredArgsConstructor
public class TranslationManager implements com.rexcantor64.triton.api.language.TranslationManager {

private static final String MINIMESSAGE_TYPE_TAG = "[minimsg]";
private static final String JSON_TYPE_TAG = "[triton_json]";
public static final String MINIMESSAGE_TYPE_TAG = "[minimsg]";
public static final String JSON_TYPE_TAG = "[triton_json]";

private final Triton<?, ?> triton;

Expand All @@ -59,6 +59,7 @@ public class TranslationManager implements com.rexcantor64.triton.api.language.T
@Getter
private int signTranslationCount = 0;

@Getter
private Component translationNotFoundComponent = Component.empty();

private final Map<Language, MiniMessage> miniMessageInstances = new HashMap<>();
Expand Down Expand Up @@ -204,7 +205,7 @@ private synchronized void setupMiniMessage() {
* @return The instance for the given language.
* @since 4.0.0
*/
private @NotNull MiniMessage getMiniMessageInstanceForLanguage(@NotNull Language language) {
public @NotNull MiniMessage getMiniMessageInstanceForLanguage(@NotNull Language language) {
return Streams.concat(
Stream.of(language),
language.getFallbackLanguages()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.rexcantor64.triton.api.language.Localized;
import com.rexcantor64.triton.api.language.MessageParser;
import com.rexcantor64.triton.utils.ComponentUtils;
import com.rexcantor64.triton.utils.ParserUtils;
import com.rexcantor64.triton.utils.StringUtils;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -55,7 +56,7 @@ public class AdventureParser implements MessageParser {
* @see MessageParser#translateString(String, Localized, FeatureSyntax)
*/
@Override
public @NotNull TranslationResult<String> translateString(String text, Localized language, FeatureSyntax syntax) {
public @NotNull TranslationResult<String> translateString(@NotNull String text, @NotNull Localized language, @NotNull FeatureSyntax syntax) {
return translateComponent(
ComponentUtils.deserializeFromLegacy(text),
language,
Expand All @@ -81,8 +82,8 @@ public class AdventureParser implements MessageParser {
* @since 4.0.0
*/
@Override
public @NotNull TranslationResult<Component> translateComponent(Component component, Localized language, FeatureSyntax syntax) {
TranslationConfiguration configuration = new TranslationConfiguration(
public @NotNull TranslationResult<Component> translateComponent(@NotNull Component component, @NotNull Localized language, @NotNull FeatureSyntax syntax) {
val configuration = new TranslationConfiguration<Component>(
syntax,
Triton.get().getConfig().getDisabledLine(),
// TODO properly integrate this
Expand All @@ -102,17 +103,17 @@ public class AdventureParser implements MessageParser {
* @since 4.0.0
*/
@VisibleForTesting
TranslationResult<Component> translateComponent(Component component, TranslationConfiguration configuration) {
TranslationResult<Component> translateComponent(@NotNull Component component, @NotNull TranslationConfiguration<Component> configuration) {
String plainText = componentToString(component);

if (ComponentUtils.hasLegacyFormatting(plainText)) {
component = ComponentUtils.unflattenLegacyFormatting(component);
plainText = componentToString(component);
}

val indexes = this.getPatternIndexArray(plainText, configuration.getFeatureSyntax().getLang());
val indexes = ParserUtils.getPatternIndexArray(plainText, configuration.getFeatureSyntax().getLang());

if (indexes.size() == 0) {
if (indexes.isEmpty()) {
return handleNonContentText(component, configuration);
}

Expand Down Expand Up @@ -165,12 +166,12 @@ TranslationResult<Component> translateComponent(Component component, Translation
* @return The translation of this placeholder. Empty optional if the translation is "disabled line".
* @since 4.0.0
*/
private Optional<Component> handlePlaceholder(Component placeholder, TranslationConfiguration configuration) {
private Optional<Component> handlePlaceholder(Component placeholder, TranslationConfiguration<Component> configuration) {
Style defaultStyle = getStyleOfFirstCharacterOrEmpty(placeholder);
placeholder = stripStyleOfFirstCharacter(placeholder);

String placeholderStr = componentToString(placeholder);
val indexes = this.getPatternIndexArray(placeholderStr, configuration.getFeatureSyntax().getArg());
val indexes = ParserUtils.getPatternIndexArray(placeholderStr, configuration.getFeatureSyntax().getArg());
Queue<Integer> indexesToSplitAt = indexes.stream()
.flatMap(Arrays::stream)
.sorted()
Expand All @@ -186,10 +187,7 @@ private Optional<Component> handlePlaceholder(Component placeholder, Translation
Component part = splitComponents.get(i);
if (i == 0) {
key = PlainTextComponentSerializer.plainText().serialize(part);
// The [args] tag is optional since v4.0.0, so strip it if it's present
if (key.endsWith("[" + configuration.getFeatureSyntax().getArgs() + "]")) {
key = key.substring(0, key.length() - configuration.getFeatureSyntax().getArgs().length() - 2);
}
key = ParserUtils.normalizeTranslationKey(key, configuration);
if (!StringUtils.isEmptyOrNull(configuration.getDisabledLine()) && configuration.getDisabledLine()
.equals(key)) {
return Optional.empty();
Expand Down Expand Up @@ -228,7 +226,7 @@ private Optional<Component> handlePlaceholder(Component placeholder, Translation
* @since 4.0.0
*/
@SuppressWarnings("unchecked")
private TranslationResult<Component> handleNonContentText(Component component, TranslationConfiguration configuration) {
private TranslationResult<Component> handleNonContentText(Component component, TranslationConfiguration<Component> configuration) {
boolean changed = false;
HoverEvent<?> hoverEvent = component.hoverEvent();
if (hoverEvent != null) {
Expand Down Expand Up @@ -352,7 +350,8 @@ private Style getStyleOfFirstCharacterOrEmpty(Component component) {
*/
@VisibleForTesting
@Contract("_ -> new")
@NotNull Component stripStyleOfFirstCharacter(@NotNull Component component) {
@NotNull
Component stripStyleOfFirstCharacter(@NotNull Component component) {
if (component instanceof TextComponent) {
TextComponent textComponent = (TextComponent) component;
if (!textComponent.content().isEmpty()) {
Expand Down Expand Up @@ -536,7 +535,7 @@ private List<Component> splitComponent(List<Component> comps, SplitState state)
* @since 4.0.0
*/
private List<Component> flushAccumulator(List<Component> accumulator, List<Component> splits) {
if (accumulator.size() == 0) {
if (accumulator.isEmpty()) {
return accumulator;
}

Expand Down Expand Up @@ -584,51 +583,6 @@ private List<Component> handleChildren(Component parent, List<Component> childre
return accumulator;
}

/**
* Find the indexes of all root "[pattern][/pattern]" tags in the given string.
* <p>
* Only the root tags are included, that is, nested tags are ignored.
* For example, <code>[pattern][pattern][/pattern][/pattern]</code> would only
* return the indexes for the outer tags.
* <p>
* Each array in the returned list corresponds to a different set of opening and closing tags,
* and has size 4.
* Indexes have the following meaning:
* <ul>
* <li>0: the first character of the opening tag</li>
* <li>1: the character after the last character of the closing tag</li>
* <li>2: the character after the last character of the opening tag</li>
* <li>3: the first character of the closing tag</li>
* </ul>
*
* @param input The string to search for opening and closing tags.
* @param pattern The tags to search for (i.e. "lang" will search for "[lang]" and "[/lang]").
* @return A list of indexes of all the found tags, as specified by the method description.
*/
public List<Integer[]> getPatternIndexArray(String input, String pattern) {
List<Integer[]> result = new ArrayList<>();
int start = -1;
int openedAmount = 0;

for (int i = 0; i < input.length(); i++) {
char currentChar = input.charAt(i);
if (currentChar == '[' && input.length() > i + pattern.length() + 1 && input.substring(i + 1,
i + 2 + pattern.length()).equals(pattern + "]")) {
if (start == -1) start = i;
openedAmount++;
i += 1 + pattern.length();
} else if (currentChar == '[' && input.length() > i + pattern.length() + 2 && input.substring(i + 1,
i + 3 + pattern.length()).equals("/" + pattern + "]")) {
openedAmount--;
if (openedAmount == 0) {
result.add(new Integer[]{start, i + 3 + pattern.length(), start + pattern.length() + 2, i});
start = -1;
}
}
}
return result;
}

/**
* Adventure has an issue where some components might not become empty
* components, even though they should be. This is a fix for that, while
Expand Down
Loading
Loading