Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
private Collection<String> suggestMVConfigValues(BukkitCommandCompletionContext context) {
return Try.of(() -> context.getContextValue(String.class))
.map(propertyName -> config.getStringPropertyHandle()
.getSuggestedPropertyValue(propertyName, context.getInput(), PropertyModifyAction.SET))
.getSuggestedPropertyValue(propertyName, context.getInput(), PropertyModifyAction.SET, context.getSender()))

Check warning on line 273 in src/main/java/org/mvplugins/multiverse/core/command/MVCommandCompletions.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 132). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/command/MVCommandCompletions.java:273:0: warning: Line is longer than 120 characters (found 132). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.getOrElse(Collections.emptyList());
}

Expand Down Expand Up @@ -316,7 +316,7 @@
MultiverseWorld world = context.getContextValue(MultiverseWorldValue.class).value();
PropertyModifyAction action = context.getContextValue(PropertyModifyAction.class);
String propertyName = context.getContextValue(String.class);
return world.getStringPropertyHandle().getSuggestedPropertyValue(propertyName, context.getInput(), action);
return world.getStringPropertyHandle().getSuggestedPropertyValue(propertyName, context.getInput(), action, context.getSender());

Check warning on line 319 in src/main/java/org/mvplugins/multiverse/core/command/MVCommandCompletions.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 140). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/command/MVCommandCompletions.java:319:0: warning: Line is longer than 120 characters (found 140). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
}).getOrElse(Collections.emptyList());
}

Expand Down Expand Up @@ -357,7 +357,7 @@
return world.getEntitySpawnConfig()
.getSpawnCategoryConfig(spawnCategory)
.getStringPropertyHandle()
.getSuggestedPropertyValue(propertyName, context.getInput(), action);
.getSuggestedPropertyValue(propertyName, context.getInput(), action, context.getSender());
}).getOrElse(Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.vavr.control.Try;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventPriority;
import org.bukkit.plugin.PluginManager;

Expand Down Expand Up @@ -548,13 +548,12 @@ private <N extends Node> N node(N node) {
.build());

// todo: Maybe combine with the similar method in MVCommandCompletion but that has permission checking
private Collection<String> suggestDestinations(String input) {
return destinationsProvider.get().getDestinations().stream()
.flatMap(destination -> destination.suggestDestinations(Bukkit.getConsoleSender(), null)
.stream()
.map(packet -> destination instanceof WorldDestination
? packet.destinationString()
: destination.getIdentifier() + ":" + packet.destinationString()))
private Collection<String> suggestDestinations(CommandSender sender, String input) {
return destinationsProvider.get().suggestDestinations(sender, input)
.stream()
.map(packet -> packet.destination() instanceof WorldDestination
? packet.destinationString()
: packet.destination().getIdentifier() + ":" + packet.destinationString())
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import io.vavr.control.Option;
import io.vavr.control.Try;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -71,20 +74,47 @@ public Try<Class<?>> getPropertyType(@Nullable String name) {
/**
* Suggests property values for command auto-complete based on the input and action type.
*
* @param name The name of the property.
* @param input The input value to suggest based on.
* @param action The modification action being performed.
* @param name The name of the property.
* @param input The input value to suggest based on.
* @param action The modification action being performed.
* @return A collection of suggested values.
*/
public Collection<String> getSuggestedPropertyValue(
@Nullable String name, @Nullable String input, @NotNull PropertyModifyAction action) {
@Nullable String name,
@Nullable String input,
@NotNull PropertyModifyAction action
) {
return getSuggestedPropertyValue(name, input, action, Bukkit.getConsoleSender());
}

/**
* Suggests property values for command auto-complete based on the input and action type.
* <br />
* Providing a sender gives contextual information such as sender name, permissions, or player location
* for better suggestions.
*
* @param name The name of the property.
* @param input The input value to suggest based on.
* @param action The modification action being performed.
* @param sender The sender context to use.
* @return A collection of suggested values
*
* @since 5.1
*/
@ApiStatus.AvailableSince("5.1")
public Collection<String> getSuggestedPropertyValue(
@Nullable String name,
@Nullable String input,
@NotNull PropertyModifyAction action,
@NotNull CommandSender sender
) {
return switch (action) {
case SET -> findNode(name, ValueNode.class)
.map(node -> node.suggest(input))
.map(node -> node.suggest(sender, input))
.getOrElse(Collections.emptyList());

case ADD -> findNode(name, ListValueNode.class)
.map(node -> node.suggestItem(input))
.map(node -> node.suggestItem(sender, input))
.getOrElse(Collections.emptyList());

case REMOVE -> findNode(name, ListValueNode.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

import io.vavr.control.Option;
import io.vavr.control.Try;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import org.mvplugins.multiverse.core.config.node.functions.SenderNodeSuggester;
import org.mvplugins.multiverse.core.config.node.serializer.DefaultSerializerProvider;
import org.mvplugins.multiverse.core.config.node.functions.DefaultStringParserProvider;
import org.mvplugins.multiverse.core.config.node.functions.DefaultSuggesterProvider;
Expand Down Expand Up @@ -115,6 +118,17 @@ protected ConfigNode(
return Collections.emptyList();
}

/**
* {@inheritDoc}
*/
@Override
public @NotNull Collection<String> suggest(@NotNull CommandSender sender, @Nullable String input) {
if (suggester != null && suggester instanceof SenderNodeSuggester senderSuggester) {
return senderSuggester.suggest(sender, input);
}
return suggest(input);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -254,6 +268,20 @@ protected Builder(@NotNull String path, @NotNull Class<T> type) {
return self();
}

/**
* Sets the suggester for this node with sender context.
*
* @param suggester The suggester for this node.
* @return This builder.
*
* @since 5.1
*/
@ApiStatus.AvailableSince("5.1")
public @NotNull B suggester(@NotNull SenderNodeSuggester suggester) {
this.suggester = suggester;
return self();
}

/**
* Sets the string parser for this node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.vavr.Value;
import io.vavr.control.Try;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import org.mvplugins.multiverse.core.config.node.functions.DefaultStringParserProvider;
import org.mvplugins.multiverse.core.config.node.functions.DefaultSuggesterProvider;
import org.mvplugins.multiverse.core.config.node.functions.NodeStringParser;
import org.mvplugins.multiverse.core.config.node.functions.NodeSuggester;
import org.mvplugins.multiverse.core.config.node.functions.SenderNodeSuggester;
import org.mvplugins.multiverse.core.config.node.serializer.DefaultSerializerProvider;
import org.mvplugins.multiverse.core.config.node.serializer.NodeSerializer;
import org.mvplugins.multiverse.core.utils.REPatterns;
Expand Down Expand Up @@ -110,12 +112,12 @@ private void setDefaults() {
}

private void setDefaultSuggester() {
this.suggester = input -> {
if (input == null) {
return itemSuggester.suggest(null);
}
return StringFormatter.addonToCommaSeperated(input, itemSuggester.suggest(input));
};
if (itemSuggester instanceof SenderNodeSuggester senderItemSuggester) {
this.suggester = (SenderNodeSuggester)(sender, input) ->
StringFormatter.addonToCommaSeperated(input, senderItemSuggester.suggest(sender, input));
} else {
this.suggester = input -> StringFormatter.addonToCommaSeperated(input, itemSuggester.suggest(input));
}
}

private void setDefaultStringParser() {
Expand Down Expand Up @@ -194,6 +196,17 @@ private void setDefaultOnSetValue() {
return Collections.emptyList();
}

/**
* {@inheritDoc}
*/
@Override
public @NotNull Collection<String> suggestItem(@NotNull CommandSender sender, @Nullable String input) {
if (itemSuggester != null && itemSuggester instanceof SenderNodeSuggester senderSuggester) {
return senderSuggester.suggest(sender, input);
}
return suggestItem(input);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -267,6 +280,20 @@ protected Builder(@NotNull String path, @NotNull Class<I> itemType) {
return self();
}

/**
* Sets the suggester for an individual item in the list with sender context.
*
* @param itemSuggester The suggester.
* @return This builder.
*
* @since 5.1
*/
@ApiStatus.AvailableSince("5.1")
public @NotNull B itemSuggester(@NotNull SenderNodeSuggester itemSuggester) {
this.itemSuggester = itemSuggester;
return self();
}

/**
* Sets the string parser for an individual item in the list.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;

import io.vavr.control.Try;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -31,6 +33,19 @@ public interface ListValueNode<I> extends ValueNode<List<I>> {
*/
@NotNull Collection<String> suggestItem(@Nullable String input);

/**
* Suggests possible string values for this node. Use contextural information from the sender such as
* sender name, permissions, or player location for better suggestions.
*
* @param sender The sender context.
* @param input The input string.
* @return A collection of possible string values
*
* @since 5.1
*/
@ApiStatus.AvailableSince("5.1")
@NotNull Collection<String> suggestItem(@NotNull CommandSender sender, @Nullable String input);

/**
* Parses the given string into a value of type {@link I}. Used for property set by user input.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import io.vavr.control.Option;
import io.vavr.control.Try;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -40,6 +42,19 @@ public interface ValueNode<T> extends Node {
*/
@NotNull Collection<String> suggest(@Nullable String input);

/**
* Suggests possible string values for this node. Use contextural information from the sender such as
* sender name, permissions, or player location for better suggestions.
*
* @param sender The sender context.
* @param input The input string.
* @return A collection of possible string values
*
* @since 5.1
*/
@ApiStatus.AvailableSince("5.1")
@NotNull Collection<String> suggest(@NotNull CommandSender sender, @Nullable String input);

/**
* Parses the given string into a value of type {@link T}. Used for property set by user input.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.mvplugins.multiverse.core.config.node.functions;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

Check warning on line 9 in src/main/java/org/mvplugins/multiverse/core/config/node/functions/SenderNodeSuggester.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Wrong order for 'java.util.Collection' import. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/node/functions/SenderNodeSuggester.java:9:1: warning: Wrong order for 'java.util.Collection' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

/**
* A function that suggests possible values for a node value with sender contextual information.
*
* @since 5.1
*/
@ApiStatus.AvailableSince("5.1")
@FunctionalInterface
public interface SenderNodeSuggester extends NodeSuggester {

/**
* Suggests possible values for a node value. Generated based on the current user input and sender contextual information.

Check warning on line 21 in src/main/java/org/mvplugins/multiverse/core/config/node/functions/SenderNodeSuggester.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 126). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/config/node/functions/SenderNodeSuggester.java:21:0: warning: Line is longer than 120 characters (found 126). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
*
* @param sender The sender context.
* @param input The current partial user input
* @return The possible values.
*
* @since 5.1
*/
@ApiStatus.AvailableSince("5.1")
@NotNull Collection<String> suggest(@NotNull CommandSender sender, @Nullable String input);

@Override
default @NotNull Collection<String> suggest(@Nullable String input) {
return suggest(Bukkit.getConsoleSender(), input);
}
}
Loading
Loading