From 423550a38c4e1e4d444637c986428044b959738a Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Fri, 3 Jan 2025 17:17:29 +0800 Subject: [PATCH] misc: Tweak ValueMapArgumentType --- .../arguments/ValueMapArgumentType.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/xyz/nifeather/morph/commands/brigadier/arguments/ValueMapArgumentType.java b/src/main/java/xyz/nifeather/morph/commands/brigadier/arguments/ValueMapArgumentType.java index f1d3c8f7..aa018e9e 100644 --- a/src/main/java/xyz/nifeather/morph/commands/brigadier/arguments/ValueMapArgumentType.java +++ b/src/main/java/xyz/nifeather/morph/commands/brigadier/arguments/ValueMapArgumentType.java @@ -358,21 +358,34 @@ public KeyValuePair parseOnce(StringReader reader, char terminator, char endOfSt if (next == terminator) break; - char current = next; - reader.skip(); + char current = next; + //log.info("Current: '%s'".formatted(next)); //region 识别Key var builder = isKey ? keyStringBuilder : valueStringBuilder; - // 遇到等于号,切换至Value - if (next == '=' && isKey) + // 遇到等于号,切换至Value模式 + if (current == '=' && isKey) { isKey = false; - valueCursor = reader.getCursor(); + + // 判断我们值的cursor要从哪里开始 + if (reader.canRead()) + { + // 如果等于号后面是引号,那么从那里开始,否则就从这里开始 + if (StringReader.isQuotedStringStart(reader.peek())) + valueCursor = reader.getCursor() + 1; + else + valueCursor = reader.getCursor(); + } + else // 以等于号结尾,从这里开始 + { + valueCursor = reader.getCursor(); + } continue; } @@ -382,7 +395,11 @@ public KeyValuePair parseOnce(StringReader reader, char terminator, char endOfSt //endregion 识别Key - // 遇到引号了 + // 忽略开头的空格 + if (builder.isEmpty() && Character.isWhitespace(current)) + continue; + + // 遇到引号了,读取到下个引号结束 if (StringReader.isQuotedStringStart(current)) { var quoteReader = new StringReader(reader); @@ -393,7 +410,7 @@ public KeyValuePair parseOnce(StringReader reader, char terminator, char endOfSt str = quoteReader.readStringUntil(current); reader.setCursor(quoteReader.getCursor()); } - catch (Throwable ignored) + catch (Throwable ignored) // 在补全过程中,我们可能读不到下一个引号,所以 Just in case { str = reader.readUnquotedString(); } @@ -403,10 +420,6 @@ public KeyValuePair parseOnce(StringReader reader, char terminator, char endOfSt continue; } - // 是空格 - if (Character.isWhitespace(current)) - continue; - // 其他情况 //log.info("APPENDING [%s]".formatted(current)); builder.append(current);