Skip to content

Commit

Permalink
Implements #41
Browse files Browse the repository at this point in the history
  • Loading branch information
JorelAli committed Jan 8, 2019
1 parent 6a15430 commit bb50a94
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -851,10 +851,20 @@ private <T> RequiredArgumentBuilder<?, T> getRequiredArgumentBuilder(String argu
//Gets a RequiredArgumentBuilder for a DynamicSuggestedStringArgument
private <T> RequiredArgumentBuilder<?, T> getRequiredArgumentBuilder(String argumentName, DynamicSuggestedStringArgument type, CommandPermission permission) {

//Use NMS ICompletionProvider.a() on DynSuggestions
SuggestionProvider provider = (context, builder) -> {
return getSuggestionsBuilder(builder, type.getDynamicSuggestions().getSuggestions());
};
SuggestionProvider provider = null;

if(type.getDynamicSuggestions() == null) {
//withCS
provider = (context, builder) -> {
return getSuggestionsBuilder(builder, type.getDynamicSuggestionsWithCommandSender().getSuggestions(getCommandSender(context.getSource())));
};
} else if(type.getDynamicSuggestionsWithCommandSender() == null) {
provider = (context, builder) -> {
return getSuggestionsBuilder(builder, type.getDynamicSuggestions().getSuggestions());
};
} else {
throw new RuntimeException("Invalid DynamicSuggestedStringArgument found!");
}

return getRequiredArgumentBuilder(argumentName, type.getRawType(), permission, provider);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package io.github.jorelali.commandapi.api.arguments;

import org.bukkit.command.CommandSender;

import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;

import io.github.jorelali.commandapi.api.CommandPermission;

@SuppressWarnings("unchecked")
public class DynamicSuggestedStringArgument implements Argument {


@FunctionalInterface
public interface DynamicSuggestionsWithCommandSender {
String[] getSuggestions(CommandSender sender);
}

@FunctionalInterface
public interface DynamicSuggestions {
String[] getSuggestions();
}
}

ArgumentType<?> rawType;
private DynamicSuggestions suggestions;
private DynamicSuggestionsWithCommandSender suggestionsWithCS;

/**
* A string argument which has suggestions which are determined at runtime
*/
public DynamicSuggestedStringArgument(DynamicSuggestions suggestions) {
rawType = StringArgumentType.word();
this.suggestions = suggestions;
this.suggestionsWithCS = null;
}

/**
* A string argument which has suggestions which are determined at runtime
*/
public DynamicSuggestedStringArgument(DynamicSuggestionsWithCommandSender suggestions) {
rawType = StringArgumentType.word();
this.suggestionsWithCS = suggestions;
this.suggestions = null;
}

@Override
Expand All @@ -43,6 +61,10 @@ public DynamicSuggestions getDynamicSuggestions() {
return suggestions;
}

public DynamicSuggestionsWithCommandSender getDynamicSuggestionsWithCommandSender() {
return suggestionsWithCS;
}

private CommandPermission permission = CommandPermission.NONE;

@Override
Expand Down

0 comments on commit bb50a94

Please sign in to comment.