From b740d5854c1fbf62eff167a91ca1f28c2bb3dcef Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Mon, 10 Oct 2022 18:41:35 +0200 Subject: [PATCH] Support MiniMessage in plot-title flag (#3835) --- Core/src/main/java/com/plotsquared/core/util/StringMan.java | 5 +++-- Core/src/test/java/com/plotsquared/core/plot/FlagTest.java | 2 +- .../test/java/com/plotsquared/core/util/StringManTest.java | 4 +++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/util/StringMan.java b/Core/src/main/java/com/plotsquared/core/util/StringMan.java index 8fb2f2940..32a594ea2 100644 --- a/Core/src/main/java/com/plotsquared/core/util/StringMan.java +++ b/Core/src/main/java/com/plotsquared/core/util/StringMan.java @@ -36,7 +36,8 @@ import java.util.regex.Pattern; public class StringMan { - private static final Pattern STRING_SPLIT_PATTERN = Pattern.compile("(?\"[\\w ]+\")|(?\\w+)"); + // Stolen from https://stackoverflow.com/a/366532/12620913 | Debug: https://regex101.com/r/DudJLb/1 + private static final Pattern STRING_SPLIT_PATTERN = Pattern.compile("[^\\s\"]+|\"([^\"]*)\""); public static String replaceFromMap(String string, Map replacements) { StringBuilder sb = new StringBuilder(string); @@ -355,7 +356,7 @@ public class StringMan { var matcher = StringMan.STRING_SPLIT_PATTERN.matcher(message); List splitMessages = new ArrayList<>(); while (matcher.find()) { - splitMessages.add(matcher.group(0).replaceAll("\"", "")); + splitMessages.add(matcher.group(matcher.groupCount() - 1).replaceAll("\"", "")); } return splitMessages; } diff --git a/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java b/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java index d1a7b2797..9b02f34da 100644 --- a/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java +++ b/Core/src/test/java/com/plotsquared/core/plot/FlagTest.java @@ -101,7 +101,7 @@ public class FlagTest { public void shouldSuccessfullyParseTitleFlagWithTitleEmptyAndSubTitleSingleWord() { Assertions.assertDoesNotThrow(() -> { var title = PlotTitleFlag.TITLE_FLAG_DEFAULT.parse("\"\" \"single\"").getValue(); - Assertions.assertEquals(" ", title.title()); + Assertions.assertEquals("", title.title()); Assertions.assertEquals("single", title.subtitle()); }, "Should not throw a FlagParseException"); } diff --git a/Core/src/test/java/com/plotsquared/core/util/StringManTest.java b/Core/src/test/java/com/plotsquared/core/util/StringManTest.java index 8c1eb2aa1..14927ce42 100644 --- a/Core/src/test/java/com/plotsquared/core/util/StringManTest.java +++ b/Core/src/test/java/com/plotsquared/core/util/StringManTest.java @@ -32,7 +32,9 @@ public class StringManTest { new Message("title", List.of("title")), new Message("title \"sub title\"", List.of("title", "sub title")), new Message("\"a title\" subtitle", List.of("a title", "subtitle")), - new Message("\"title\" \"subtitle\"", List.of("title", "subtitle")) + new Message("\"title\" \"subtitle\"", List.of("title", "subtitle")), + new Message("\"How bold of you\" \"to assume I like rainbows\"", + List.of("How bold of you", "to assume I like rainbows")) ); for (Message message : messages) {