Skip to content

Commit

Permalink
Use CommandArguments instead of Object[] when suggesting and parsing …
Browse files Browse the repository at this point in the history
…Arguments
  • Loading branch information
willkroboth committed Feb 3, 2023
1 parent 1aa11dd commit 7ec80bb
Show file tree
Hide file tree
Showing 78 changed files with 188 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public CommandArguments args() {
}

/**
* Converts the List<Argument> into an Object[] for command execution
* Converts the List<Argument> into a {@link CommandArguments} for command execution
*
* @param cmdCtx the command context that will execute this command
* @param args the map of strings to arguments
Expand All @@ -282,7 +282,7 @@ CommandArguments argsToCommandArgs(CommandContext<Source> cmdCtx, Argument[] arg
// Populate array
for (Argument argument : args) {
if (argument.isListed()) {
Object parsedArgument = parseArgument(cmdCtx, argument.getNodeName(), argument, argList.toArray());
Object parsedArgument = parseArgument(cmdCtx, argument.getNodeName(), argument, new CommandArguments(argList.toArray(), argsMap));
argList.add(parsedArgument);
argsMap.put(argument.getNodeName(), parsedArgument);
}
Expand All @@ -300,7 +300,7 @@ CommandArguments argsToCommandArgs(CommandContext<Source> cmdCtx, Argument[] arg
* @return the Argument's corresponding object
* @throws CommandSyntaxException when the input for the argument isn't formatted correctly
*/
Object parseArgument(CommandContext<Source> cmdCtx, String key, Argument value, Object[] previousArgs) throws CommandSyntaxException {
Object parseArgument(CommandContext<Source> cmdCtx, String key, Argument value, CommandArguments previousArgs) throws CommandSyntaxException {
if (value.isListed()) {
return value.parseArgument(cmdCtx, key, previousArgs);
} else {
Expand Down Expand Up @@ -800,19 +800,22 @@ LiteralArgumentBuilder<Source> getLiteralArgumentBuilderArgument(String commandN
argument.getArgumentPermission(), argument.getRequirements())).suggests(newSuggestionsProvider);
}

Object[] generatePreviousArguments(CommandContext<Source> context, Argument[] args, String nodeName)
CommandArguments generatePreviousArguments(CommandContext<Source> context, Argument[] args, String nodeName)
throws CommandSyntaxException {
// Populate Object[], which is our previously filled arguments
List<Object> previousArguments = new ArrayList<>();

// LinkedHashMap for arguments
Map<String, Object> argsMap = new LinkedHashMap<>();

for (Argument arg : args) {
if (arg.getNodeName().equals(nodeName) && !(arg instanceof Literal)) {
break;
}

Object result;
try {
result = parseArgument(context, arg.getNodeName(), arg, previousArguments.toArray());
result = parseArgument(context, arg.getNodeName(), arg, new CommandArguments(previousArguments.toArray(), argsMap));
} catch (IllegalArgumentException e) {
/*
* Redirected commands don't parse previous arguments properly. Simplest way to
Expand All @@ -826,9 +829,10 @@ Object[] generatePreviousArguments(CommandContext<Source> context, Argument[] ar
}
if (arg.isListed()) {
previousArguments.add(result);
argsMap.put(arg.getNodeName(), result);
}
}
return previousArguments.toArray();
return new CommandArguments(previousArguments.toArray(), argsMap);
}

SuggestionProvider<Source> toSuggestions(Argument theArgument, Argument[] args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
*******************************************************************************/
package dev.jorel.commandapi;

import java.util.Arrays;
import java.util.Objects;
import dev.jorel.commandapi.executors.CommandArguments;

/**
* A class that represents information which you can use to generate
* suggestions.
*
* @param sender - the CommandSender typing this command
* @param previousArgs - the list of previously declared (and parsed) arguments
* @param previousArgs - a {@link CommandArguments} object holding previously declared (and parsed) arguments. This can
* be used as if it were arguments in a command executor method
* @param currentInput - a string representing the full current input (including
* /)
* @param currentArg - the current partially typed argument. For example
Expand All @@ -39,9 +39,10 @@ public record SuggestionInfo<CommandSender>(
CommandSender sender,

/**
* @param previousArgs - the list of previously declared (and parsed) arguments
* @param previousArgs - a {@link CommandArguments} object holding previously declared (and parsed) arguments. This can
* be used as if it were arguments in a command executor method
*/
Object[] previousArgs,
CommandArguments previousArgs,

/**
* @param currentInput - a string representing the full current input (including
Expand All @@ -54,33 +55,4 @@ public record SuggestionInfo<CommandSender>(
* tes" will return "tes"
*/
String currentArg) {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.deepHashCode(previousArgs);
result = prime * result + Objects.hash(currentArg, currentInput, sender);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof SuggestionInfo)) {
return false;
}
@SuppressWarnings("rawtypes")
SuggestionInfo other = (SuggestionInfo) obj;
return Objects.equals(currentArg, other.currentArg) && Objects.equals(currentInput, other.currentInput) && Arrays.deepEquals(previousArgs, other.previousArgs)
&& Objects.equals(sender, other.sender);
}

@Override
public String toString() {
return "SuggestionInfo [sender=" + sender + ", previousArgs=" + Arrays.toString(previousArgs) + ", currentInput=" + currentInput + ", currentArg=" + currentArg + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import dev.jorel.commandapi.AbstractArgumentTree;
import dev.jorel.commandapi.CommandPermission;
import dev.jorel.commandapi.executors.CommandArguments;

/**
* The core abstract class for Command API arguments
Expand Down Expand Up @@ -101,11 +102,11 @@ public final String getNodeName() {
* @param <Source> the command source type
* @param cmdCtx the context which ran this command
* @param key the name of the argument node
* @param previousArgs an array of previously declared arguments
* @param previousArgs a {@link CommandArguments} object holding previous parsed arguments
* @return the parsed object represented by this argument
* @throws CommandSyntaxException if parsing fails
*/
public abstract <Source> T parseArgument(CommandContext<Source> cmdCtx, String key, Object[] previousArgs) throws CommandSyntaxException;
public abstract <Source> T parseArgument(CommandContext<Source> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException;

/////////////////
// Suggestions //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import dev.jorel.commandapi.SuggestionInfo;
import dev.jorel.commandapi.executors.CommandArguments;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.*;

/**
* This class represents a branch in the suggestions of an argument. Use {@link SuggestionsBranch#suggest(ArgumentSuggestions...)}
Expand Down Expand Up @@ -81,7 +79,7 @@ private ArgumentSuggestions<CommandSender> getNextSuggestion(CommandSender sende

if (currentSuggestion != null) {
// Validate argument on the path
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, processedArguments.toArray(), currentInput.toString(), "");
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, new CommandArguments(processedArguments.toArray(), new HashMap<>()), currentInput.toString(), "");
SuggestionsBuilder builder = new SuggestionsBuilder(currentInput.toString(), currentInput.length());
currentSuggestion.suggest(info, builder);
if (builder.build().getList().stream().map(Suggestion::getText).noneMatch(currentArgument::equals)) {
Expand Down Expand Up @@ -186,7 +184,7 @@ private EnforceReplacementsResult enforceReplacements(CommandSender sender, Stri

if (currentSuggestion != null) {
// Validate argument on the path
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, processedArguments.toArray(), currentInput.toString(), "");
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, new CommandArguments(processedArguments.toArray(), new HashMap<>()), currentInput.toString(), "");
SuggestionsBuilder builder = new SuggestionsBuilder(currentInput.toString(), currentInput.length());
try {
currentSuggestion.suggest(info, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ void argumentsuggestionsprevious() {
arguments.add(new PlayerArgument("target").replaceSuggestions(ArgumentSuggestions.strings(info -> {

// Cast the first argument (radius, which is an IntegerArgument) to get its value
int radius = (int) info.previousArgs()[0];
int radius = (int) info.previousArgs().get(0);

// Get nearby entities within the provided radius
Player player = (Player) info.sender();
Expand Down Expand Up @@ -1945,7 +1945,7 @@ void safePotionArguments() {
arguments.add(new EntitySelectorArgument.OnePlayer("target"));
arguments.add(new PotionEffectArgument("potioneffect").replaceSafeSuggestions(SafeSuggestions.suggest(
info -> {
Player target = (Player) info.previousArgs()[0];
Player target = (Player) info.previousArgs().get(0);

// Convert PotionEffect[] into PotionEffectType[]
return target.getActivePotionEffects().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.executors.CommandArguments;
import org.bukkit.advancement.Advancement;

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ public SuggestionProviders getSuggestionProvider() {
}

@Override
public <CommandSourceStack> Advancement parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs) throws CommandSyntaxException {
public <CommandSourceStack> Advancement parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
return CommandAPIBukkit.<CommandSourceStack>get().getAdvancement(cmdCtx, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import dev.jorel.commandapi.commandsenders.BukkitPlayer;
import dev.jorel.commandapi.exceptions.PaperAdventureNotFoundException;
import dev.jorel.commandapi.exceptions.WrapperCommandSyntaxException;
import dev.jorel.commandapi.executors.CommandArguments;
import dev.jorel.commandapi.wrappers.PreviewableFunction;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -73,7 +74,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <CommandSourceStack> Component parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs) throws CommandSyntaxException {
public <CommandSourceStack> Component parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
final CommandSender sender = CommandAPIBukkit.<CommandSourceStack>get().getCommandSenderFromCommandSource(cmdCtx.getSource()).getSource();
Component component = CommandAPIBukkit.<CommandSourceStack>get().getAdventureChat(cmdCtx, key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.exceptions.PaperAdventureNotFoundException;
import dev.jorel.commandapi.executors.CommandArguments;
import net.kyori.adventure.text.Component;

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <CommandSourceStack> Component parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs) throws CommandSyntaxException {
public <CommandSourceStack> Component parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
return CommandAPIBukkit.<CommandSourceStack>get().getAdventureChatComponent(cmdCtx, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.executors.CommandArguments;

/**
* An argument that represents a yaw angle, measured in degrees with float
Expand Down Expand Up @@ -57,7 +58,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <CommandSourceStack> Float parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs)
public <CommandSourceStack> Float parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs)
throws CommandSyntaxException {
return CommandAPIBukkit.<CommandSourceStack>get().getAngle(cmdCtx, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.executors.CommandArguments;
import org.bukkit.Axis;

import java.util.EnumSet;
Expand Down Expand Up @@ -60,7 +61,7 @@ public CommandAPIArgumentType getArgumentType() {

@Override
public <CommandSourceStack> EnumSet<Axis> parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key,
Object[] previousArgs)
CommandArguments previousArgs)
throws CommandSyntaxException {
return CommandAPIBukkit.<CommandSourceStack>get().getAxis(cmdCtx, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.executors.CommandArguments;
import org.bukkit.block.Biome;

import java.util.function.Function;
Expand Down Expand Up @@ -62,7 +63,7 @@ public SuggestionProviders getSuggestionProvider() {
}

@Override
public <CommandSourceStack> Biome parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs)
public <CommandSourceStack> Biome parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs)
throws CommandSyntaxException {
return (Biome) CommandAPIBukkit.<CommandSourceStack>get().getBiome(cmdCtx, key, ArgumentSubType.BIOME_BIOME);
}
Expand Down Expand Up @@ -101,7 +102,7 @@ public CommandAPIArgumentType getArgumentType() {

@Override
public <CommandSourceStack> org.bukkit.NamespacedKey parseArgument(CommandContext<CommandSourceStack> cmdCtx,
String key, Object[] previousArgs) throws CommandSyntaxException {
String key, CommandArguments previousArgs) throws CommandSyntaxException {
return (org.bukkit.NamespacedKey) CommandAPIBukkit.<CommandSourceStack>get().getBiome(cmdCtx, key, ArgumentSubType.BIOME_NAMESPACEDKEY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.executors.CommandArguments;
import org.bukkit.block.Block;

import java.util.function.Predicate;
Expand Down Expand Up @@ -58,7 +59,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <CommandSourceStack> Predicate<?> parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs)
public <CommandSourceStack> Predicate<?> parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs)
throws CommandSyntaxException {
return CommandAPIBukkit.<CommandSourceStack>get().getBlockPredicate(cmdCtx, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.executors.CommandArguments;
import org.bukkit.block.data.BlockData;

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <CommandSourceStack> BlockData parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs)
public <CommandSourceStack> BlockData parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs)
throws CommandSyntaxException {
return CommandAPIBukkit.<CommandSourceStack>get().getBlockState(cmdCtx, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.executors.CommandArguments;

/**
* An argument that represents primitive Java booleans
Expand Down Expand Up @@ -52,7 +53,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <Source> Boolean parseArgument(CommandContext<Source> cmdCtx, String key, Object[] previousArgs) throws CommandSyntaxException {
public <Source> Boolean parseArgument(CommandContext<Source> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
return cmdCtx.getArgument(key, getPrimitiveType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.Optional;

import dev.jorel.commandapi.executors.CommandArguments;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -75,7 +76,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <CommandSourceStack> BaseComponent[] parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs) throws CommandSyntaxException {
public <CommandSourceStack> BaseComponent[] parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
final CommandSender sender = CommandAPIBukkit.<CommandSourceStack>get().getCommandSenderFromCommandSource(cmdCtx.getSource()).getSource();
BaseComponent[] component = CommandAPIBukkit.<CommandSourceStack>get().getChat(cmdCtx, key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.executors.CommandArguments;
import org.bukkit.ChatColor;

import java.util.function.Function;
Expand Down Expand Up @@ -57,7 +58,7 @@ public CommandAPIArgumentType getArgumentType() {
}

@Override
public <CommandSourceStack> ChatColor parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, Object[] previousArgs) throws CommandSyntaxException {
public <CommandSourceStack> ChatColor parseArgument(CommandContext<CommandSourceStack> cmdCtx, String key, CommandArguments previousArgs) throws CommandSyntaxException {
return CommandAPIBukkit.<CommandSourceStack>get().getChatColor(cmdCtx, key);
}
}
Loading

0 comments on commit 7ec80bb

Please sign in to comment.