Skip to content

Commit ff26ffd

Browse files
authored
Merge pull request #3258 from Multiverse/feat/sender-context
Implement sender context for config node suggestions
2 parents ce263b5 + 058a7c9 commit ff26ffd

File tree

9 files changed

+205
-28
lines changed

9 files changed

+205
-28
lines changed

src/main/java/org/mvplugins/multiverse/core/command/MVCommandCompletions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ private Collection<String> suggestGeneratorPlugins(BukkitCommandCompletionContex
270270
private Collection<String> suggestMVConfigValues(BukkitCommandCompletionContext context) {
271271
return Try.of(() -> context.getContextValue(String.class))
272272
.map(propertyName -> config.getStringPropertyHandle()
273-
.getSuggestedPropertyValue(propertyName, context.getInput(), PropertyModifyAction.SET))
273+
.getSuggestedPropertyValue(propertyName, context.getInput(), PropertyModifyAction.SET, context.getSender()))
274274
.getOrElse(Collections.emptyList());
275275
}
276276

@@ -316,7 +316,7 @@ private Collection<String> suggestMVWorldPropsValue(BukkitCommandCompletionConte
316316
MultiverseWorld world = context.getContextValue(MultiverseWorldValue.class).value();
317317
PropertyModifyAction action = context.getContextValue(PropertyModifyAction.class);
318318
String propertyName = context.getContextValue(String.class);
319-
return world.getStringPropertyHandle().getSuggestedPropertyValue(propertyName, context.getInput(), action);
319+
return world.getStringPropertyHandle().getSuggestedPropertyValue(propertyName, context.getInput(), action, context.getSender());
320320
}).getOrElse(Collections.emptyList());
321321
}
322322

@@ -357,7 +357,7 @@ private Collection<String> suggestSpawnCategoryPropsValue(BukkitCommandCompletio
357357
return world.getEntitySpawnConfig()
358358
.getSpawnCategoryConfig(spawnCategory)
359359
.getStringPropertyHandle()
360-
.getSuggestedPropertyValue(propertyName, context.getInput(), action);
360+
.getSuggestedPropertyValue(propertyName, context.getInput(), action, context.getSender());
361361
}).getOrElse(Collections.emptyList());
362362
}
363363
}

src/main/java/org/mvplugins/multiverse/core/config/CoreConfigNodes.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import io.vavr.control.Try;
55
import jakarta.inject.Inject;
66
import jakarta.inject.Provider;
7-
import org.bukkit.Bukkit;
7+
import org.bukkit.command.CommandSender;
88
import org.bukkit.event.EventPriority;
99
import org.bukkit.plugin.PluginManager;
1010

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

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

src/main/java/org/mvplugins/multiverse/core/config/handle/StringPropertyHandle.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import io.vavr.control.Option;
77
import io.vavr.control.Try;
8+
import org.bukkit.Bukkit;
9+
import org.bukkit.command.CommandSender;
10+
import org.jetbrains.annotations.ApiStatus;
811
import org.jetbrains.annotations.NotNull;
912
import org.jetbrains.annotations.Nullable;
1013

@@ -71,20 +74,47 @@ public Try<Class<?>> getPropertyType(@Nullable String name) {
7174
/**
7275
* Suggests property values for command auto-complete based on the input and action type.
7376
*
74-
* @param name The name of the property.
75-
* @param input The input value to suggest based on.
76-
* @param action The modification action being performed.
77+
* @param name The name of the property.
78+
* @param input The input value to suggest based on.
79+
* @param action The modification action being performed.
7780
* @return A collection of suggested values.
7881
*/
7982
public Collection<String> getSuggestedPropertyValue(
80-
@Nullable String name, @Nullable String input, @NotNull PropertyModifyAction action) {
83+
@Nullable String name,
84+
@Nullable String input,
85+
@NotNull PropertyModifyAction action
86+
) {
87+
return getSuggestedPropertyValue(name, input, action, Bukkit.getConsoleSender());
88+
}
89+
90+
/**
91+
* Suggests property values for command auto-complete based on the input and action type.
92+
* <br />
93+
* Providing a sender gives contextual information such as sender name, permissions, or player location
94+
* for better suggestions.
95+
*
96+
* @param name The name of the property.
97+
* @param input The input value to suggest based on.
98+
* @param action The modification action being performed.
99+
* @param sender The sender context to use.
100+
* @return A collection of suggested values
101+
*
102+
* @since 5.1
103+
*/
104+
@ApiStatus.AvailableSince("5.1")
105+
public Collection<String> getSuggestedPropertyValue(
106+
@Nullable String name,
107+
@Nullable String input,
108+
@NotNull PropertyModifyAction action,
109+
@NotNull CommandSender sender
110+
) {
81111
return switch (action) {
82112
case SET -> findNode(name, ValueNode.class)
83-
.map(node -> node.suggest(input))
113+
.map(node -> node.suggest(sender, input))
84114
.getOrElse(Collections.emptyList());
85115

86116
case ADD -> findNode(name, ListValueNode.class)
87-
.map(node -> node.suggestItem(input))
117+
.map(node -> node.suggestItem(sender, input))
88118
.getOrElse(Collections.emptyList());
89119

90120
case REMOVE -> findNode(name, ListValueNode.class)

src/main/java/org/mvplugins/multiverse/core/config/node/ConfigNode.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
import io.vavr.control.Option;
1010
import io.vavr.control.Try;
11+
import org.bukkit.command.CommandSender;
12+
import org.jetbrains.annotations.ApiStatus;
1113
import org.jetbrains.annotations.NotNull;
1214
import org.jetbrains.annotations.Nullable;
1315

16+
import org.mvplugins.multiverse.core.config.node.functions.SenderNodeSuggester;
1417
import org.mvplugins.multiverse.core.config.node.serializer.DefaultSerializerProvider;
1518
import org.mvplugins.multiverse.core.config.node.functions.DefaultStringParserProvider;
1619
import org.mvplugins.multiverse.core.config.node.functions.DefaultSuggesterProvider;
@@ -115,6 +118,17 @@ protected ConfigNode(
115118
return Collections.emptyList();
116119
}
117120

121+
/**
122+
* {@inheritDoc}
123+
*/
124+
@Override
125+
public @NotNull Collection<String> suggest(@NotNull CommandSender sender, @Nullable String input) {
126+
if (suggester != null && suggester instanceof SenderNodeSuggester senderSuggester) {
127+
return senderSuggester.suggest(sender, input);
128+
}
129+
return suggest(input);
130+
}
131+
118132
/**
119133
* {@inheritDoc}
120134
*/
@@ -254,6 +268,20 @@ protected Builder(@NotNull String path, @NotNull Class<T> type) {
254268
return self();
255269
}
256270

271+
/**
272+
* Sets the suggester for this node with sender context.
273+
*
274+
* @param suggester The suggester for this node.
275+
* @return This builder.
276+
*
277+
* @since 5.1
278+
*/
279+
@ApiStatus.AvailableSince("5.1")
280+
public @NotNull B suggester(@NotNull SenderNodeSuggester suggester) {
281+
this.suggester = suggester;
282+
return self();
283+
}
284+
257285
/**
258286
* Sets the string parser for this node.
259287
*

src/main/java/org/mvplugins/multiverse/core/config/node/ListConfigNode.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@
55
import java.util.Collection;
66
import java.util.Collections;
77
import java.util.List;
8-
import java.util.Set;
98
import java.util.function.BiConsumer;
109
import java.util.function.Function;
1110
import java.util.function.Supplier;
1211
import java.util.stream.Collectors;
1312

1413
import io.vavr.Value;
1514
import io.vavr.control.Try;
15+
import org.bukkit.command.CommandSender;
16+
import org.jetbrains.annotations.ApiStatus;
1617
import org.jetbrains.annotations.NotNull;
1718
import org.jetbrains.annotations.Nullable;
1819

1920
import org.mvplugins.multiverse.core.config.node.functions.DefaultStringParserProvider;
2021
import org.mvplugins.multiverse.core.config.node.functions.DefaultSuggesterProvider;
2122
import org.mvplugins.multiverse.core.config.node.functions.NodeStringParser;
2223
import org.mvplugins.multiverse.core.config.node.functions.NodeSuggester;
24+
import org.mvplugins.multiverse.core.config.node.functions.SenderNodeSuggester;
2325
import org.mvplugins.multiverse.core.config.node.serializer.DefaultSerializerProvider;
2426
import org.mvplugins.multiverse.core.config.node.serializer.NodeSerializer;
2527
import org.mvplugins.multiverse.core.utils.REPatterns;
@@ -110,12 +112,12 @@ private void setDefaults() {
110112
}
111113

112114
private void setDefaultSuggester() {
113-
this.suggester = input -> {
114-
if (input == null) {
115-
return itemSuggester.suggest(null);
116-
}
117-
return StringFormatter.addonToCommaSeperated(input, itemSuggester.suggest(input));
118-
};
115+
if (itemSuggester instanceof SenderNodeSuggester senderItemSuggester) {
116+
this.suggester = (SenderNodeSuggester)(sender, input) ->
117+
StringFormatter.addonToCommaSeperated(input, senderItemSuggester.suggest(sender, input));
118+
} else {
119+
this.suggester = input -> StringFormatter.addonToCommaSeperated(input, itemSuggester.suggest(input));
120+
}
119121
}
120122

121123
private void setDefaultStringParser() {
@@ -194,6 +196,17 @@ private void setDefaultOnSetValue() {
194196
return Collections.emptyList();
195197
}
196198

199+
/**
200+
* {@inheritDoc}
201+
*/
202+
@Override
203+
public @NotNull Collection<String> suggestItem(@NotNull CommandSender sender, @Nullable String input) {
204+
if (itemSuggester != null && itemSuggester instanceof SenderNodeSuggester senderSuggester) {
205+
return senderSuggester.suggest(sender, input);
206+
}
207+
return suggestItem(input);
208+
}
209+
197210
/**
198211
* {@inheritDoc}
199212
*/
@@ -267,6 +280,20 @@ protected Builder(@NotNull String path, @NotNull Class<I> itemType) {
267280
return self();
268281
}
269282

283+
/**
284+
* Sets the suggester for an individual item in the list with sender context.
285+
*
286+
* @param itemSuggester The suggester.
287+
* @return This builder.
288+
*
289+
* @since 5.1
290+
*/
291+
@ApiStatus.AvailableSince("5.1")
292+
public @NotNull B itemSuggester(@NotNull SenderNodeSuggester itemSuggester) {
293+
this.itemSuggester = itemSuggester;
294+
return self();
295+
}
296+
270297
/**
271298
* Sets the string parser for an individual item in the list.
272299
*

src/main/java/org/mvplugins/multiverse/core/config/node/ListValueNode.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.List;
55

66
import io.vavr.control.Try;
7+
import org.bukkit.command.CommandSender;
8+
import org.jetbrains.annotations.ApiStatus;
79
import org.jetbrains.annotations.NotNull;
810
import org.jetbrains.annotations.Nullable;
911

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

36+
/**
37+
* Suggests possible string values for this node. Use contextural information from the sender such as
38+
* sender name, permissions, or player location for better suggestions.
39+
*
40+
* @param sender The sender context.
41+
* @param input The input string.
42+
* @return A collection of possible string values
43+
*
44+
* @since 5.1
45+
*/
46+
@ApiStatus.AvailableSince("5.1")
47+
@NotNull Collection<String> suggestItem(@NotNull CommandSender sender, @Nullable String input);
48+
3449
/**
3550
* Parses the given string into a value of type {@link I}. Used for property set by user input.
3651
*

src/main/java/org/mvplugins/multiverse/core/config/node/ValueNode.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import io.vavr.control.Option;
66
import io.vavr.control.Try;
7+
import org.bukkit.command.CommandSender;
8+
import org.jetbrains.annotations.ApiStatus;
79
import org.jetbrains.annotations.NotNull;
810
import org.jetbrains.annotations.Nullable;
911

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

45+
/**
46+
* Suggests possible string values for this node. Use contextural information from the sender such as
47+
* sender name, permissions, or player location for better suggestions.
48+
*
49+
* @param sender The sender context.
50+
* @param input The input string.
51+
* @return A collection of possible string values
52+
*
53+
* @since 5.1
54+
*/
55+
@ApiStatus.AvailableSince("5.1")
56+
@NotNull Collection<String> suggest(@NotNull CommandSender sender, @Nullable String input);
57+
4358
/**
4459
* Parses the given string into a value of type {@link T}. Used for property set by user input.
4560
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.mvplugins.multiverse.core.config.node.functions;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.command.CommandSender;
5+
import org.jetbrains.annotations.ApiStatus;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.Collection;
10+
11+
/**
12+
* A function that suggests possible values for a node value with sender contextual information.
13+
*
14+
* @since 5.1
15+
*/
16+
@ApiStatus.AvailableSince("5.1")
17+
@FunctionalInterface
18+
public interface SenderNodeSuggester extends NodeSuggester {
19+
20+
/**
21+
* Suggests possible values for a node value. Generated based on the current user input and sender contextual information.
22+
*
23+
* @param sender The sender context.
24+
* @param input The current partial user input
25+
* @return The possible values.
26+
*
27+
* @since 5.1
28+
*/
29+
@ApiStatus.AvailableSince("5.1")
30+
@NotNull Collection<String> suggest(@NotNull CommandSender sender, @Nullable String input);
31+
32+
@Override
33+
default @NotNull Collection<String> suggest(@Nullable String input) {
34+
return suggest(Bukkit.getConsoleSender(), input);
35+
}
36+
}

0 commit comments

Comments
 (0)