add some argument types
This commit is contained in:
parent
f946566048
commit
62151736bd
7 changed files with 193 additions and 1 deletions
|
|
@ -0,0 +1,24 @@
|
|||
package suggestionprovider;
|
||||
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import site.lab0x13.scrow.commands.model.CommandContext;
|
||||
import site.lab0x13.scrow.commands.model.SuggestionProvider;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class PlayerSuggestionProvider<C extends CommandContext<C>> implements SuggestionProvider<C> {
|
||||
|
||||
private final ProxyServer proxyServer;
|
||||
|
||||
public PlayerSuggestionProvider(ProxyServer proxyServer) {
|
||||
this.proxyServer = proxyServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> suggest(C __, String ___) {
|
||||
return proxyServer.getAllPlayers().stream()
|
||||
.map(Player::getUsername)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package type;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
import com.leakyabstractions.result.core.Results;
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import site.lab0x13.scrow.commands.model.CommandContext;
|
||||
import site.lab0x13.scrow.commands.model.SuggestionProvider;
|
||||
import site.lab0x13.scrow.commands.model.argument.ArgumentType;
|
||||
import suggestionprovider.PlayerSuggestionProvider;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerArgumentType<C extends CommandSource & CommandContext<C>> implements ArgumentType<C, Player> {
|
||||
|
||||
private final ProxyServer server;
|
||||
|
||||
public PlayerArgumentType(ProxyServer server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Player, String> parseInput(String input) {
|
||||
Player player;
|
||||
if (input.length() == 36) {
|
||||
try {
|
||||
player = server.getPlayer(UUID.fromString(input)).orElse(null);
|
||||
} catch (IllegalArgumentException _) {
|
||||
return Results.failure("Invalid UUID.");
|
||||
}
|
||||
} else {
|
||||
player = server.getPlayer(input).orElse(null);
|
||||
}
|
||||
|
||||
return Results.ofNullable(player, "Player not online.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SuggestionProvider<C> defaultSuggestionProvider() {
|
||||
return new PlayerSuggestionProvider<>(server);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue