Skip to content

Commit

Permalink
fix: prevent http request in tab-completion (fixes #271)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddlama committed Nov 13, 2024
1 parent 2b81adb commit 191072e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 96 deletions.
176 changes: 82 additions & 94 deletions vane-core/src/main/java/org/oddlama/vane/core/command/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,100 +185,89 @@ public default Param choice(Collection<String> choices) {
}

public default <T> ChoiceParam<T> choice(
String argument_type,
Collection<? extends T> choices,
Function1<T, String> to_string
) {
String argument_type,
Collection<? extends T> choices,
Function1<T, String> to_string) {
var p = new ChoiceParam<>(get_command(), argument_type, choices, to_string);
add_param(p);
return p;
}

public default <T> DynamicChoiceParam<T> choice(
String argument_type,
Function1<CommandSender, Collection<? extends T>> choices,
Function2<CommandSender, T, String> to_string,
Function2<CommandSender, String, ? extends T> from_string
) {
String argument_type,
Function1<CommandSender, Collection<? extends T>> choices,
Function2<CommandSender, T, String> to_string,
Function2<CommandSender, String, ? extends T> from_string) {
var p = new DynamicChoiceParam<>(get_command(), argument_type, choices, to_string, from_string);
add_param(p);
return p;
}

public default DynamicChoiceParam<Module<?>> choose_module() {
return choice(
"module",
sender -> get_command().get_module().core.get_modules(),
(sender, m) -> m.get_name(),
(sender, str) ->
get_command()
.get_module()
.core.get_modules()
.stream()
.filter(k -> k.get_name().equalsIgnoreCase(str))
.findFirst()
.orElse(null)
);
"module",
sender -> get_command().get_module().core.get_modules(),
(sender, m) -> m.get_name(),
(sender, str) -> get_command()
.get_module().core.get_modules()
.stream()
.filter(k -> k.get_name().equalsIgnoreCase(str))
.findFirst()
.orElse(null));
}

public default DynamicChoiceParam<World> choose_world() {
return choice(
"world",
sender -> get_command().get_module().getServer().getWorlds(),
(sender, w) -> w.getName().toLowerCase(),
(sender, str) ->
get_command()
.get_module()
.getServer()
.getWorlds()
.stream()
.filter(w -> w.getName().equalsIgnoreCase(str))
.findFirst()
.orElse(null)
);
"world",
sender -> get_command().get_module().getServer().getWorlds(),
(sender, w) -> w.getName().toLowerCase(),
(sender, str) -> get_command()
.get_module()
.getServer()
.getWorlds()
.stream()
.filter(w -> w.getName().equalsIgnoreCase(str))
.findFirst()
.orElse(null));
}

public default DynamicChoiceParam<OfflinePlayer> choose_any_player() {
return choice(
"any_player",
sender -> get_command().get_module().get_offline_players_with_valid_name(),
(sender, p) -> p.getName(),
(sender, str) ->
get_command()
.get_module()
.get_offline_players_with_valid_name()
.stream()
.filter(k -> k.getName().equalsIgnoreCase(str))
.findFirst()
.orElse(null)
);
"any_player",
sender -> get_command().get_module().get_offline_players_with_valid_name(),
(sender, p) -> p.getName(),
(sender, str) -> get_command()
.get_module()
.get_offline_players_with_valid_name()
.stream()
.filter(k -> k.getName().equalsIgnoreCase(str))
.findFirst()
.orElse(null));
}

public default DynamicChoiceParam<Player> choose_online_player() {
return choice(
"online_player",
sender -> get_command().get_module().getServer().getOnlinePlayers(),
(sender, p) -> p.getName(),
(sender, str) ->
get_command()
.get_module()
.getServer()
.getOnlinePlayers()
.stream()
.filter(k -> k.getName().equalsIgnoreCase(str))
.findFirst()
.orElse(null)
);
}

// TODO (minor): Make choose_permission filter results based on the previously specified player.
"online_player",
sender -> get_command().get_module().getServer().getOnlinePlayers(),
(sender, p) -> p.getName(),
(sender, str) -> get_command()
.get_module()
.getServer()
.getOnlinePlayers()
.stream()
.filter(k -> k.getName().equalsIgnoreCase(str))
.findFirst()
.orElse(null));
}

// TODO (minor): Make choose_permission filter results based on the previously
// specified player.
public default DynamicChoiceParam<Permission> choose_permission() {
return choice(
"permission",
sender -> get_command().get_module().getServer().getPluginManager().getPermissions(),
(sender, p) -> p.getName(),
(sender, str) -> get_command().get_module().getServer().getPluginManager().getPermission(str)
);
"permission",
sender -> get_command().get_module().getServer().getPluginManager().getPermissions(),
(sender, p) -> p.getName(),
(sender, str) -> get_command().get_module().getServer().getPluginManager().getPermission(str));
}

public default ChoiceParam<GameMode> choose_gamemode() {
Expand All @@ -290,25 +279,24 @@ public default DynamicChoiceParam<Enchantment> choose_enchantment() {
}

public default DynamicChoiceParam<Enchantment> choose_enchantment(
final Function2<CommandSender, Enchantment, Boolean> filter
) {
final Function2<CommandSender, Enchantment, Boolean> filter) {
return choice(
"enchantment",
sender ->
RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).stream().filter(e -> filter.apply(sender, e)).collect(Collectors.toList()),
(sender, e) -> e.getKey().toString(),
(sender, str) -> {
var parts = str.split(":");
if (parts.length != 2) {
return null;
}
var e = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).get(namespaced_key(parts[0], parts[1]));
if (!filter.apply(sender, e)) {
return null;
}
return e;
}
);
"enchantment",
sender -> RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT).stream()
.filter(e -> filter.apply(sender, e)).collect(Collectors.toList()),
(sender, e) -> e.getKey().toString(),
(sender, str) -> {
var parts = str.split(":");
if (parts.length != 2) {
return null;
}
var e = RegistryAccess.registryAccess().getRegistry(RegistryKey.ENCHANTMENT)
.get(namespaced_key(parts[0], parts[1]));
if (!filter.apply(sender, e)) {
return null;
}
return e;
});
}

public default CheckResult check_accept_delegate(CommandSender sender, String[] args, int offset) {
Expand All @@ -332,10 +320,10 @@ public default CheckResult check_accept_delegate(CommandSender sender, String[]
var max_depth = results.stream().map(r -> r.depth()).reduce(0, Integer::max);

var errors = results
.stream()
.filter(r -> r.depth() == max_depth)
.map(ErrorCheckResult.class::cast)
.collect(Collectors.toList());
.stream()
.filter(r -> r.depth() == max_depth)
.map(ErrorCheckResult.class::cast)
.collect(Collectors.toList());

// If there is only a single max-depth sub-error, propagate it.
// Otherwise, combine multiple errors into new error.
Expand Down Expand Up @@ -363,14 +351,14 @@ public default List<String> build_completions_delegate(CommandSender sender, Str

// Delegate to children
return get_params()
.stream()
.map(p -> p.build_completions(sender, args, offset + 1))
.flatMap(Collection::stream)
.collect(Collectors.toList());
.stream()
.map(p -> p.build_completions(sender, args, offset + 1))
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

public default List<String> build_completions(CommandSender sender, String[] args, int offset) {
if (offset != args.length - 1) {
if (offset < args.length - 1) {
// We are not the last argument.
// Delegate completion to children if the param accepts the given arguments,
// return no completions if it doesn't
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public class OfflinePlayerArgumentType implements CustomArgumentType.Converted<O

@Override
public @NotNull OfflinePlayer convert(@NotNull String nativeType) throws CommandSyntaxException {
return Bukkit.getOfflinePlayer(nativeType);
for (var p : Bukkit.getOfflinePlayers()) {
if (nativeType.equalsIgnoreCase(p.getName())) {
return p;
}
}
return null;
}

@Override
Expand All @@ -43,5 +48,5 @@ public class OfflinePlayerArgumentType implements CustomArgumentType.Converted<O
}
stream.forEach(builder::suggest);
return builder.buildFuture();
}
}
}

0 comments on commit 191072e

Please sign in to comment.