Chat/velocity/src/main/java/com/alttd/velocitychat/commands/PartyCommand.java

125 lines
4.8 KiB
Java
Raw Normal View History

2022-01-30 01:12:09 +00:00
package com.alttd.velocitychat.commands;
import com.alttd.chat.config.Config;
import com.alttd.chat.util.Utility;
import com.alttd.velocitychat.commands.partysubcommands.*;
2022-01-30 01:12:09 +00:00
import com.mojang.brigadier.arguments.StringArgumentType;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;
2025-06-20 22:53:55 +00:00
import net.kyori.adventure.text.ComponentLike;
2022-02-19 14:14:41 +00:00
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
2022-01-30 01:12:09 +00:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PartyCommand implements SimpleCommand {
2022-01-30 01:12:09 +00:00
private final List<SubCommand> subCommands;
public PartyCommand() {
subCommands = Arrays.asList(
new Help(this),
new Create(),
new Disband(),
new Info(),
2022-01-30 01:12:09 +00:00
new Invite(),
new Join(),
new Leave(),
new Name(),
new Owner(),
new Password(),
new Remove());
2022-01-30 01:12:09 +00:00
}
@Override
public void execute(SimpleCommand.Invocation invocation) {
String[] args = invocation.arguments();
CommandSource source = invocation.source();
if (args.length < 1) {
2025-06-20 22:53:55 +00:00
if (!source.hasPermission("party.use")) {
source.sendMessage(Utility.parseMiniMessage(Config.NO_PERMISSION));
2025-06-20 22:53:55 +00:00
} else if (source instanceof Player) {
source.sendMessage(getHelpMessage(source));
2025-06-20 22:53:55 +00:00
} else {
source.sendMessage(Utility.parseMiniMessage(Config.NO_CONSOLE));
2025-06-20 22:53:55 +00:00
}
2022-01-30 01:12:09 +00:00
return;
}
subCommands.stream()
.filter(subCommand -> subCommand.getName().equalsIgnoreCase(args[0]))
.findFirst()
.ifPresentOrElse(subCommand -> {
2025-06-20 22:53:55 +00:00
if (source.hasPermission(subCommand.getPermission())) {
subCommand.execute(args, source);
2025-06-20 22:53:55 +00:00
} else {
source.sendMessage(Utility.parseMiniMessage(Config.NO_PERMISSION));
2025-06-20 22:53:55 +00:00
}
}, () -> source.sendMessage(getHelpMessage(source)));
2022-01-30 01:12:09 +00:00
}
@Override
public List<String> suggest(SimpleCommand.Invocation invocation) {
String[] args = invocation.arguments();
List<String> suggest = new ArrayList<>();
2025-06-20 22:53:55 +00:00
if (!invocation.source().hasPermission("party.use")) {
return suggest;
2025-06-20 22:53:55 +00:00
} else if (args.length == 0) {
2022-01-30 01:12:09 +00:00
subCommands.stream()
.filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission()))
.forEach(subCommand -> suggest.add(subCommand.getName()));
} else if (args.length == 1) {
2022-01-30 01:12:09 +00:00
subCommands.stream()
.filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission()))
.filter(subCommand -> subCommand.getName().startsWith(args[0].toLowerCase()))
.forEach(subCommand -> suggest.add(subCommand.getName()));
} else {
subCommands.stream()
.filter(subCommand -> invocation.source().hasPermission(subCommand.getPermission()))
.filter(subCommand -> subCommand.getName().equalsIgnoreCase(args[0]))
.findFirst()
.ifPresent(subCommand -> suggest.addAll(subCommand.suggest(args, invocation.source())));
2022-01-30 01:12:09 +00:00
}
2025-06-20 22:53:55 +00:00
if (args.length == 0) {
2022-01-30 01:12:09 +00:00
return suggest;
2025-06-20 22:53:55 +00:00
} else {
2022-01-30 01:12:09 +00:00
return finalizeSuggest(suggest, args[args.length - 1]);
2025-06-20 22:53:55 +00:00
}
2022-01-30 01:12:09 +00:00
}
public List<String> finalizeSuggest(List<String> possibleValues, String remaining) {
List<String> finalValues = new ArrayList<>();
for (String str : possibleValues) {
if (str.toLowerCase().startsWith(remaining.toLowerCase())) {
2022-01-30 01:12:09 +00:00
finalValues.add(StringArgumentType.escapeIfRequired(str));
}
}
return finalValues;
}
2025-06-20 22:53:55 +00:00
public ComponentLike getHelpMessage(CommandSource source) {
2022-01-30 01:12:09 +00:00
StringBuilder stringBuilder = new StringBuilder();
subCommands.stream()
.filter(subCommand -> source.hasPermission(subCommand.getPermission()))
.forEach(subCommand -> stringBuilder.append(subCommand.getHelpMessage()).append("\n"));
2025-06-20 22:53:55 +00:00
if (source.hasPermission("command.chat.p")) {
stringBuilder.append(Config.PARTY_HELP_CHAT).append("\n");
2025-06-20 22:53:55 +00:00
}
if (!stringBuilder.isEmpty()) {
2022-01-30 01:12:09 +00:00
stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "");
2025-06-20 22:53:55 +00:00
}
2022-01-30 01:12:09 +00:00
2022-02-05 20:16:20 +00:00
return Utility.parseMiniMessage(Config.PARTY_HELP_WRAPPER,
2025-06-20 22:53:55 +00:00
Placeholder.component("commands", Utility.parseMiniMessage(stringBuilder.toString()))
);
}
2025-06-20 22:53:55 +00:00
}