1
0

impl tab completion on player color command

This commit is contained in:
Lee
2024-04-01 13:15:47 +01:00
parent ab3d82047a
commit 04e31da952
4 changed files with 55 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package cc.fascinated.account;
import cc.fascinated.command.Command;
import cc.fascinated.utils.Manager;
import lombok.Getter;
import org.bukkit.configuration.ConfigurationSection;
@ -12,6 +13,11 @@ public abstract class Profile extends Manager {
*/
private final Account account;
/**
* The configuration section for this profile.
*/
private final ConfigurationSection section;
/**
* This constructor is used for loading a profile from file.
*
@ -20,6 +26,7 @@ public abstract class Profile extends Manager {
*/
public Profile(Account account, ConfigurationSection section) {
this.account = account;
this.section = section;
}
/**

View File

@ -5,11 +5,15 @@ import cc.fascinated.account.AccountManager;
import lombok.Getter;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@Getter
public abstract class Command implements CommandExecutor {
public abstract class Command implements CommandExecutor, TabCompleter {
/**
* The command name
@ -30,6 +34,25 @@ public abstract class Command implements CommandExecutor {
this.command = command;
}
/**
* Executes the command.
*
* @param account The account executing the command.
* @param args The arguments of the command.
*/
public abstract void execute(Account account, String[] args);
/**
* Tab completes the command.
*
* @param account The account executing the command.
* @param args The arguments of the command.
* @return The tab completions.
*/
public List<String> tabComplete(Account account, String[] args) {
return null;
}
@Override
public boolean onCommand(@NotNull CommandSender commandSender, org.bukkit.command.@NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (this.permission != null && !commandSender.hasPermission(permission)) {
@ -43,11 +66,14 @@ public abstract class Command implements CommandExecutor {
return true;
}
/**
* Executes the command.
*
* @param account The account executing the command.
* @param args The arguments of the command.
*/
public abstract void execute(Account account, String[] args);
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, org.bukkit.command.@NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (this.permission != null && !commandSender.hasPermission(permission)) {
return null;
}
Player player = (Player) commandSender;
Account account = AccountManager.getAccount(player.getUniqueId());
return this.tabComplete(account, strings);
}
}

View File

@ -24,5 +24,6 @@ public class CommandManager {
throw new IllegalArgumentException("Command " + command.getCommand() + " does not exist.");
}
pluginCommand.setExecutor(command);
pluginCommand.setTabCompleter(command);
}
}

View File

@ -7,6 +7,7 @@ import cc.fascinated.playercolor.PlayerColorManager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList;
import java.util.List;
public class PlayerColorCommand extends Command {
@ -46,4 +47,16 @@ public class PlayerColorCommand extends Command {
playerColorProfile.setColor(color);
account.sendMessage(Component.text("§b§lPLAYERCOLOR §7» §fYour color has been set to: ").append(Component.text(playerColorProfile.getColor().toString()).color(playerColorProfile.getColor())));
}
@Override
public List<String> tabComplete(Account account, String[] args) {
List<NamedTextColor> colors = PlayerColorManager.getValidColors();
if (args.length == 1) {
List<String> list = new ArrayList<>(colors.stream().map(NamedTextColor::toString).toList());
list.add("random");
return list;
}
return null;
}
}