Skip to content

Commit

Permalink
Try to fix Mojang/brigadier#103.
Browse files Browse the repository at this point in the history
Signed-off-by: astro-angelfish <[email protected]>
  • Loading branch information
astro-angelfish committed Jan 4, 2024
1 parent 7c99245 commit 1cb08ed
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ private void registerDefaultArgumentTypes() {
registerCommandArgumentType(BoolArgumentType.bool(), boolean.class);
registerCommandArgumentType(FloatArgumentType.floatArg(), float.class);
registerCommandArgumentType(LongArgumentType.longArg(), long.class);
registerCommandArgumentType(StringArgumentType.string(), String.class);
registerCommandArgumentType(new FishUTFStringArgumentType(), String.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* FishLib, a Bukkit development library
* Copyright (C) Astro angelfish
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package moe.orangemc.fishlib.command.argument;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import moe.orangemc.fishlib.util.StringUtil;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;

public class FishUTFStringArgumentType implements ArgumentType<String> {
@Override
public String parse(StringReader reader) throws CommandSyntaxException {
return readUTFString(reader);
}

public static String readUTFString(StringReader reader, Predicate<Character> matcher) throws CommandSyntaxException {
if (reader.peek() == '"' || reader.peek() == '\'') {
return reader.readQuotedString();
}

int start = reader.getCursor();
while (matcher.test(reader.peek()) && reader.canRead()) {
reader.skip();
}

return reader.getString().substring(start, reader.getCursor());
}

public static String readUTFString(StringReader reader) throws CommandSyntaxException {
return readUTFString(reader, c -> !StringUtil.isBlankChar(c));
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
return ArgumentType.super.listSuggestions(context, builder);
}

@Override
public Collection<String> getExamples() {
return ArgumentType.super.getExamples();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import moe.orangemc.fishlib.command.argument.FishUTFStringArgumentType;
import moe.orangemc.fishlib.command.argument.type.EntityList;
import moe.orangemc.fishlib.command.selector.argument.trait.LimitArgument;
import moe.orangemc.fishlib.command.selector.context.*;
import moe.orangemc.fishlib.command.selector.type.SelectorArgumentTypeAdapter;
import moe.orangemc.fishlib.command.selector.util.CharFilterUtil;
import moe.orangemc.fishlib.command.selector.util.Sorter;
import moe.orangemc.fishlib.command.util.CommandFailException;
import moe.orangemc.fishlib.command.util.CommandSyntaxExceptionBuilder;
Expand Down Expand Up @@ -144,7 +146,7 @@ public ArgumentContext(SelectorArgument argument, Object argumentValue, boolean
List<ArgumentContext> argumentContextList = new ArrayList<>();

do {
String key = promptReader.readString();
String key = FishUTFStringArgumentType.readUTFString(promptReader, CharFilterUtil::charNotSelectorSpecial);
promptReader.expect('=');
boolean invert = prompt.peek() == '!';
if (invert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ private void registerDefaultTypeAdapters() {
registerTypeAdapter(new NBTTypeAdapter());
registerTypeAdapter(new SorterTypeAdapter());
registerTypeAdapter(new StringReaderAdapter());
registerTypeAdapter(new StringTypeAdapter());
registerTypeAdapter(new TeamTypeAdapter());
registerTypeAdapter(new ScoreboardListArgumentTypeAdapter());
registerTypeAdapter(new AdvancementListTypeAdapter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import moe.orangemc.fishlib.command.argument.FishUTFStringArgumentType;
import moe.orangemc.fishlib.command.selector.Selector;
import moe.orangemc.fishlib.command.selector.util.CharFilterUtil;
import moe.orangemc.fishlib.command.util.CommandFailException;
import moe.orangemc.fishlib.command.util.CommandSyntaxExceptionBuilder;

Expand All @@ -41,34 +43,19 @@ public AdvancementListArgumentType fromStringReader(Selector selector, StringRea
prompt.expect('{');
char lastPeek;
do {
String namespace = "";
StringBuilder name = new StringBuilder(prompt.readUnquotedString());
if (prompt.peek() == ':') {
namespace = name.toString();
prompt.skip();
name = new StringBuilder(prompt.readUnquotedString());
}
while (prompt.peek() == '/') {
prompt.skip();
name.append("/").append(prompt.readUnquotedString());
}
if (namespace.isBlank()) {
namespace = NamespacedKey.MINECRAFT;
}

NamespacedKey achievementKey = NamespacedKey.fromString(namespace + ":" + name);
if (achievementKey == null) {
NamespacedKey advancementKey = NamespacedKey.fromString(FishUTFStringArgumentType.readUTFString(prompt, CharFilterUtil::charNotSelectorSpecial));
if (advancementKey == null) {
throw new CommandFailException(selector.getOwner(), "command.notfound", "Target not found.");
}
Advancement advancement = Bukkit.getAdvancement(achievementKey);
Advancement advancement = Bukkit.getAdvancement(advancementKey);
if (advancement == null) {
CommandSyntaxExceptionBuilder.raise(selector.getOwner(), selector.getSender(), "command.advancement.notfound", "Advancement " + namespace + ":" + name + " is not found.", namespace, name);
CommandSyntaxExceptionBuilder.raise(selector.getOwner(), selector.getSender(), "command.advancement.notfound", "Advancement " + advancementKey + " is not found.", advancementKey.getNamespace(), advancementKey.getKey());
}

if (prompt.peek() == '{') {
Map<String, Boolean> criteriaMap = new HashMap<>();
while (prompt.peek() != '}') {
String criteriaName = prompt.readUnquotedString();
String criteriaName = FishUTFStringArgumentType.readUTFString(prompt, CharFilterUtil::charNotSelectorSpecial);
prompt.expect('=');
boolean invertCriteria = !prompt.readBoolean();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,16 @@

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import moe.orangemc.fishlib.command.argument.FishUTFStringArgumentType;
import moe.orangemc.fishlib.command.selector.Selector;
import moe.orangemc.fishlib.command.selector.util.CharFilterUtil;

import org.bukkit.NamespacedKey;

public class NamespacedKeyTypeAdapter implements SelectorArgumentTypeAdapter<NamespacedKey> {
@Override
public NamespacedKey fromStringReader(Selector selector, StringReader prompt) throws CommandSyntaxException {
String namespace = "";
StringBuilder name = new StringBuilder(prompt.readUnquotedString());
if (prompt.peek() == ':') {
namespace = name.toString();
prompt.skip();
name = new StringBuilder(prompt.readUnquotedString());
}
while (prompt.peek() == '/') {
prompt.skip();
name.append("/").append(prompt.readUnquotedString());
}
if (namespace.isBlank()) {
namespace = NamespacedKey.MINECRAFT;
}

return NamespacedKey.fromString(namespace + ":" + name);
return NamespacedKey.fromString(FishUTFStringArgumentType.readUTFString(prompt, CharFilterUtil::charNotSelectorSpecial));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import moe.orangemc.fishlib.command.argument.FishUTFStringArgumentType;
import moe.orangemc.fishlib.command.selector.Selector;
import moe.orangemc.fishlib.command.selector.util.CharFilterUtil;

import org.bukkit.Bukkit;

Expand All @@ -35,7 +37,7 @@ public ScoreboardListArgumentType fromStringReader(Selector selector, StringRead

char lastPeek;
do {
String scoreName = value.readUnquotedString();
String scoreName = FishUTFStringArgumentType.readUTFString(value, CharFilterUtil::charNotSelectorSpecial);
value.expect('=');

scoreboardArgumentTypes.add(new ScoreboardListArgumentType.ScoreboardArgumentType(Bukkit.getScoreboardManager().getMainScoreboard().getObjective(scoreName), new IntegerRangeTypeAdapter().fromStringReader(selector, value)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import moe.orangemc.fishlib.command.argument.FishUTFStringArgumentType;
import moe.orangemc.fishlib.command.selector.Selector;
import moe.orangemc.fishlib.command.selector.SelectorImpl;
import moe.orangemc.fishlib.command.selector.util.CharFilterUtil;
import moe.orangemc.fishlib.command.selector.util.Sorter;

public class SorterTypeAdapter implements SelectorArgumentTypeAdapter<Sorter> {
@Override
public Sorter fromStringReader(Selector selector, StringReader sr) throws CommandSyntaxException {
return ((SelectorImpl) selector).getSorter(sr.readString());
return ((SelectorImpl) selector).getSorter(FishUTFStringArgumentType.readUTFString(sr, CharFilterUtil::charNotSelectorSpecial));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* FishLib, a Bukkit development library
* Copyright (C) Astro angelfish
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package moe.orangemc.fishlib.command.selector.type;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import moe.orangemc.fishlib.command.argument.FishUTFStringArgumentType;
import moe.orangemc.fishlib.command.selector.Selector;

public class StringTypeAdapter implements SelectorArgumentTypeAdapter<String> {
@Override
public String fromStringReader(Selector selector, StringReader sr) throws CommandSyntaxException {
return FishUTFStringArgumentType.readUTFString(sr);
}

@Override
public Class<String> getProvidingClass() {
return String.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import moe.orangemc.fishlib.command.argument.FishUTFStringArgumentType;
import moe.orangemc.fishlib.command.selector.Selector;
import moe.orangemc.fishlib.command.selector.util.CharFilterUtil;

import org.bukkit.Bukkit;
import org.bukkit.scoreboard.Team;

public class TeamTypeAdapter implements SelectorArgumentTypeAdapter<Team> {
@Override
public Team fromStringReader(Selector selector, StringReader sr) throws CommandSyntaxException {
return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(sr.readString());
return Bukkit.getScoreboardManager().getMainScoreboard().getTeam(FishUTFStringArgumentType.readUTFString(sr, CharFilterUtil::charNotSelectorSpecial));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* FishLib, a Bukkit development library
* Copyright (C) Astro angelfish
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package moe.orangemc.fishlib.command.selector.util;

import moe.orangemc.fishlib.util.StringUtil;

public class CharFilterUtil {
public static boolean charNotSelectorSpecial(char v) {
return !StringUtil.isBlankChar(v) && v != '!' && v != '"' && v != '\'' && v != '[' && v != ']' && v != '=' && v != ',' && v != '@';
}
}

0 comments on commit 1cb08ed

Please sign in to comment.