feat: plot-title flag value should allow single values (#3410)

This commit is contained in:
Yannick Lamprecht
2022-01-22 11:02:08 +01:00
committed by GitHub
parent 42bf413528
commit a003836dbc
4 changed files with 180 additions and 9 deletions

View File

@ -29,6 +29,7 @@ import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.plot.PlotTitle;
import com.plotsquared.core.plot.flag.FlagParseException;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.util.StringMan;
import org.checkerframework.checker.nullness.qual.NonNull;
public class PlotTitleFlag extends PlotFlag<PlotTitle, PlotTitleFlag> {
@ -56,17 +57,18 @@ public class PlotTitleFlag extends PlotFlag<PlotTitle, PlotTitleFlag> {
if (!input.contains("\"")) {
return new PlotTitleFlag(new PlotTitle(input, ""));
}
input = input.substring(input.indexOf("\""));
input = input.substring(0, input.lastIndexOf("\"") + 1);
String[] inputs = input.split("\"");
PlotTitle value;
if (inputs.length == 2) {
value = new PlotTitle(inputs[1], "");
} else if (inputs.length > 3) {
value = new PlotTitle(inputs[1], inputs[3]);
} else {
var split = StringMan.splitMessage(input);
if (split.isEmpty() || split.size() > 2) {
throw new FlagParseException(this, input, TranslatableCaption.of("flags.flag_error_title"));
}
PlotTitle value;
if (split.size() == 1) {
value = new PlotTitle(split.get(0), "");
} else {
value = new PlotTitle(split.get(0), split.get(1));
}
return new PlotTitleFlag(value);
}

View File

@ -30,16 +30,21 @@ import com.plotsquared.core.configuration.caption.Caption;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
public class StringMan {
private static final Pattern STRING_SPLIT_PATTERN = Pattern.compile("(?<quoted>\"[\\w ]+\")|(?<single>\\w+)");
public static String replaceFromMap(String string, Map<String, String> replacements) {
StringBuilder sb = new StringBuilder(string);
int size = string.length();
@ -320,4 +325,46 @@ public class StringMan {
return col;
}
/**
* @param message an input string
* @return a list of strings
* @since TODO
*
* <table border="1">
* <caption>Converts multiple quoted and single strings into a list of strings</caption>
* <thead>
* <tr>
* <th>Input</th>
* <th>Output</th>
* </tr>
* </thead>
* <tbody>
* <tr>
* <td>title "sub title"</td>
* <td>["title", "sub title"]</td>
* </tr>
* <tr>
* <td>"a title" subtitle</td>
* <td>["a title", "subtitle"]</td>
* </tr>
* <tr>
* <td>"title" "subtitle"</td>
* <td>["title", "subtitle"]</td>
* </tr>
* <tr>
* <td>"PlotSquared is going well" the authors "and many contributors"</td>
* <td>["PlotSquared is going well", "the", "authors", "and many contributors"]</td>
* </tr>
* </tbody>
* </table>
*/
public static @NonNull List<String> splitMessage(@NonNull String message) {
var matcher = StringMan.STRING_SPLIT_PATTERN.matcher(message);
List<String> splitMessages = new ArrayList<>();
while (matcher.find()) {
splitMessages.add(matcher.group(0).replaceAll("\"", ""));
}
return splitMessages;
}
}