Workaround colors on newlines of string flags issue in text lib.

Also fix newline lookbehind replacement, and only replace color macros
in string flags.
This commit is contained in:
wizjany 2019-10-06 11:20:36 -04:00
parent d410e1f84e
commit e792dd6f98
4 changed files with 19 additions and 10 deletions

View File

@ -24,7 +24,9 @@
import com.sk89q.worldedit.util.formatting.text.serializer.legacy.LegacyComponentSerializer;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Command-related utility methods.
@ -76,7 +78,12 @@ public static String replaceColorMacros(String str) {
str = str.replace("`x", "&r");
// MC classic
str = LegacyComponentSerializer.INSTANCE.serialize(LegacyComponentSerializer.INSTANCE.deserialize(str, '&'));
// FIXME: workaround for https://github.com/KyoriPowered/text/issues/50
// remove when fixed upstream and updated in WorldEdit
str = Arrays.stream(str.split("\n")).map(line -> {
TextComponent comp = LegacyComponentSerializer.INSTANCE.deserialize(line, '&');
return LegacyComponentSerializer.INSTANCE.serialize(comp);
}).collect(Collectors.joining("\n"));
return str;
}

View File

@ -500,11 +500,6 @@ public void flag(CommandContext args, Actor sender) throws CommandException {
value = "";
}
// Add color codes
if (value != null) {
value = CommandUtils.replaceColorMacros(value);
}
// Lookup the existing region
RegionManager manager = checkRegionManager(world);
ProtectedRegion existing = checkExistingRegion(manager, args.getString(0), true);
@ -572,7 +567,7 @@ public void flag(CommandContext args, Actor sender) throws CommandException {
if (value != null) {
// Set the flag if [value] was given even if [-g group] was given as well
try {
setFlag(existing, foundFlag, sender, value);
value = setFlag(existing, foundFlag, sender, value).toString();
} catch (InvalidFlagFormat e) {
throw new CommandException(e.getMessage());
}

View File

@ -392,8 +392,10 @@ protected static void setPlayerSelection(LocalPlayer player, ProtectedRegion reg
* @param value the value
* @throws InvalidFlagFormat thrown if the value is invalid
*/
protected static <V> void setFlag(ProtectedRegion region, Flag<V> flag, Actor sender, String value) throws InvalidFlagFormat {
region.setFlag(flag, flag.parseInput(FlagContext.create().setSender(sender).setInput(value).setObject("region", region).build()));
protected static <V> V setFlag(ProtectedRegion region, Flag<V> flag, Actor sender, String value) throws InvalidFlagFormat {
V val = flag.parseInput(FlagContext.create().setSender(sender).setInput(value).setObject("region", region).build());
region.setFlag(flag, val);
return val;
}
}

View File

@ -19,6 +19,8 @@
package com.sk89q.worldguard.protection.flags;
import com.sk89q.worldguard.commands.CommandUtils;
import javax.annotation.Nullable;
/**
@ -56,7 +58,10 @@ public String getDefault() {
@Override
public String parseInput(FlagContext context) throws InvalidFlagFormat {
return context.getUserInput().replaceAll("(?!<\\\\)\\\\n", "\n").replaceAll("\\\\\\\\n", "\\n");
String lines = context.getUserInput().replaceAll("(?<!\\\\)\\\\n", "\n").replaceAll("\\\\\\\\n", "\\\\n");
// Add color codes
lines = CommandUtils.replaceColorMacros(lines);
return lines;
}
@Override