Add sign text tab completion in editsign command (#3497)

This commit is contained in:
Josh Roy 2020-07-28 16:12:07 -04:00 committed by GitHub
parent b7c18d0785
commit 11cd57e8fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 12 deletions

View File

@ -67,6 +67,14 @@ public class Commandeditsign extends EssentialsCommand {
return Lists.newArrayList("set", "clear");
} else if (args.length == 2) {
return Lists.newArrayList("1", "2", "3", "4");
} else if (args.length == 3 && args[0].equalsIgnoreCase("set") && NumberUtil.isPositiveInt(args[1])) {
int line = Integer.parseInt(args[1]);
Block target = user.getBase().getTargetBlock(null, 5);
if (target.getState() instanceof Sign && line <= 4) {
Sign sign = (Sign) target.getState();
return Lists.newArrayList(FormatUtil.unformatString(user, "essentials.editsign", sign.getLine(line - 1)));
}
return Collections.emptyList();
} else {
return Collections.emptyList();
}

View File

@ -18,6 +18,8 @@ public class FormatUtil {
//Vanilla patterns used to strip existing formats
private static final Pattern STRIP_ALL_PATTERN = Pattern.compile("\u00a7+([0-9a-fk-orA-FK-OR])");
//Pattern used to strip md_5 legacy hex hack
private static final Pattern STRIP_RGB_PATTERN = Pattern.compile("\u00a7x((?:\u00a7[0-9a-fA-F]){6})");
//Essentials '&' convention colour codes
private static final Pattern REPLACE_ALL_PATTERN = Pattern.compile("(&)?&([0-9a-fk-orA-FK-OR])");
@ -144,11 +146,63 @@ public class FormatUtil {
return builder.toString();
}
public static String unformatString(final IUser user, final String permBase, String message) {
if (message == null) {
return null;
}
EnumSet<ChatColor> supported = getSupported(user, permBase);
// RGB Codes
StringBuffer rgbBuilder = new StringBuffer();
Matcher rgbMatcher = STRIP_RGB_PATTERN.matcher(message);
boolean rgb = user.isAuthorized(permBase + ".rgb");
while (rgbMatcher.find()) {
String code = rgbMatcher.group(1).replace("\u00a7", "");
if (rgb) {
rgbMatcher.appendReplacement(rgbBuilder, "&#" + code);
continue;
}
rgbMatcher.appendReplacement(rgbBuilder, "");
}
rgbMatcher.appendTail(rgbBuilder);
message = rgbBuilder.toString(); // arreter de parler
// Legacy Colors
StringBuffer builder = new StringBuffer();
Matcher matcher = STRIP_ALL_PATTERN.matcher(message);
searchLoop: while (matcher.find()) {
char code = matcher.group(1).toLowerCase(Locale.ROOT).charAt(0);
for (ChatColor color : supported) {
if (color.getChar() == code) {
matcher.appendReplacement(builder, "&" + code);
continue searchLoop;
}
}
matcher.appendReplacement(builder, "");
}
matcher.appendTail(builder);
return builder.toString();
}
//This is the general permission sensitive message format function, does not touch urls.
public static String formatString(final IUser user, final String permBase, String message) {
if (message == null) {
return null;
}
EnumSet<ChatColor> supported = getSupported(user, permBase);
EnumSet<ChatColor> strip = EnumSet.complementOf(supported);
boolean rgb = user.isAuthorized(permBase + ".rgb");
if (!supported.isEmpty() || rgb) {
message = replaceColor(message, supported, rgb);
}
if (!strip.isEmpty()) {
message = stripColor(message, strip);
}
return message;
}
private static EnumSet<ChatColor> getSupported(IUser user, String permBase) {
EnumSet<ChatColor> supported = EnumSet.noneOf(ChatColor.class);
if (user.isAuthorized(permBase + ".color")) {
supported.addAll(COLORS);
@ -177,16 +231,7 @@ public class FormatUtil {
supported.remove(chatColor);
}
}
EnumSet<ChatColor> strip = EnumSet.complementOf(supported);
boolean rgb = user.isAuthorized(permBase + ".rgb");
if (!supported.isEmpty() || rgb) {
message = replaceColor(message, supported, rgb);
}
if (!strip.isEmpty()) {
message = stripColor(message, strip);
}
return message;
return supported;
}
public static String stripLogColorFormat(final String input) {

View File

@ -90,7 +90,22 @@ public class FormatUtilTest {
checkFormatPerms("This is &&&a message", "This is &&a message", "color");
}
private void checkFormatPerms(String input, String expectedOutput, String... perms) {
@Test
public void testUnformat() {
// Unformatting should only unformat codes which you have perms for
checkUnformatPerms("§bMessage", "Message");
checkUnformatPerms("§bMessage", "&bMessage", "color");
// It should work for rgb color codes too
checkUnformatPerms("§x§b§3§4§2§f§5This is a message", "This is a message");
checkUnformatPerms("§x§b§3§4§2§f§5This is a message", "&#b342f5This is a message", "rgb");
checkUnformatPerms("§x§b§3§4§2§f§5Th§eis is §aa §dmessag§5e", "This is a message");
checkUnformatPerms("§x§b§3§4§2§f§5Th§eis is §aa §dmessag§5e", "&#b342f5This is a message", "rgb");
checkUnformatPerms("§x§b§3§4§2§f§5Th§eis is §aa §dmessag§5e", "&#b342f5Th&eis is a message", "rgb", "yellow");
}
private IUser getMockUser(String... perms) {
IUser user = mock(IUser.class);
for (String perm : perms) {
if (perm.startsWith("-")) {
@ -103,6 +118,14 @@ public class FormatUtilTest {
when(user.isPermissionSet("essentials.chat." + perm)).thenReturn(true);
}
assertEquals(expectedOutput, FormatUtil.formatString(user, "essentials.chat", input));
return user;
}
private void checkFormatPerms(String input, String expectedOutput, String... perms) {
assertEquals(expectedOutput, FormatUtil.formatString(getMockUser(perms), "essentials.chat", input));
}
private void checkUnformatPerms(String input, String expectedOutput, String... perms) {
assertEquals(expectedOutput, FormatUtil.unformatString(getMockUser(perms), "essentials.chat", input));
}
}