-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GlobalParser for Argumento.class
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...br/com/finalcraft/evernifecore/commands/finalcmd/argument/parsers/ArgParserArgumento.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package br.com.finalcraft.evernifecore.commands.finalcmd.argument.parsers; | ||
|
||
import br.com.finalcraft.evernifecore.argumento.Argumento; | ||
import br.com.finalcraft.evernifecore.commands.finalcmd.argument.ArgInfo; | ||
import br.com.finalcraft.evernifecore.commands.finalcmd.argument.ArgParser; | ||
import br.com.finalcraft.evernifecore.commands.finalcmd.argument.exception.ArgParseException; | ||
import br.com.finalcraft.evernifecore.commands.finalcmd.argument.parsers.util.ArgsParserUtil; | ||
import br.com.finalcraft.evernifecore.util.FCMessageUtil; | ||
import com.google.common.collect.ImmutableList; | ||
import org.apache.commons.lang3.Validate; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.util.StringUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ArgParserArgumento extends ArgParser<Argumento> { | ||
|
||
private final List<String> possibilities; | ||
|
||
public ArgParserArgumento(ArgInfo argInfo) { | ||
super(argInfo); | ||
|
||
//If context is empty, take the name for it | ||
String context = argInfo.getArgData().getContext().isEmpty() | ||
? argInfo.getArgData().getName() | ||
: argInfo.getArgData().getContext(); | ||
|
||
possibilities = ImmutableList.copyOf(ArgsParserUtil.parseStringContextSelectional(context)); | ||
|
||
Validate.isTrue(possibilities.size() > 0, "Can't create a ArgParserString without at least one option! [context=='" + context + "']"); | ||
} | ||
|
||
@Override | ||
public Argumento parserArgument(@NotNull CommandSender sender, @NotNull Argumento argumento) throws ArgParseException { | ||
|
||
if (possibilities.size() == 1){ | ||
return argumento.isEmpty() ? null : argumento; | ||
} | ||
|
||
for (String option : possibilities) { | ||
if (option.equalsIgnoreCase(argumento.toString())){ | ||
return new Argumento(option); | ||
} | ||
} | ||
|
||
if (argInfo.isRequired()){ | ||
FCMessageUtil.notWithinPossibilities(sender, argumento.toString(), possibilities); | ||
throw new ArgParseException(); | ||
} | ||
|
||
return Argumento.EMPTY_ARG; | ||
} | ||
|
||
@Override | ||
public @NotNull List<String> tabComplete(TabContext tabContext) { | ||
|
||
return possibilities.stream() | ||
.filter(s -> StringUtil.startsWithIgnoreCase(s, tabContext.getLastWord())) | ||
.sorted(String.CASE_INSENSITIVE_ORDER) | ||
.collect(Collectors.toList()); | ||
|
||
} | ||
} |