Fixed promote/demote

This commit is contained in:
Teriuihi 2022-02-13 18:02:05 +01:00
parent 708c2ea185
commit 3bcec7c482

View File

@ -7,10 +7,12 @@ import com.alttd.vboosters.VelocityBoosters;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
@ -20,48 +22,91 @@ import net.luckperms.api.node.Node;
import net.luckperms.api.node.NodeType;
import net.luckperms.api.node.types.InheritanceNode;
import java.util.ArrayList;
import java.util.Collection;
public class DonorRankCommand {
private final MiniMessage miniMessage;
public DonorRankCommand(ProxyServer proxyServer) {
miniMessage = MiniMessage.get();
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
.<CommandSource>literal("donorrank")
.requires(ctx -> ctx.hasPermission("command.proxy.donorrank"))
.then(RequiredArgumentBuilder.argument("username", StringArgumentType.word()))
.then(RequiredArgumentBuilder.argument("action", StringArgumentType.word()))
.then(RequiredArgumentBuilder.argument("rank", StringArgumentType.word()))
.executes(context -> {
.then(RequiredArgumentBuilder.<CommandSource, String>argument("username", StringArgumentType.string())
.suggests((context, builder) -> {
Collection<String> possibleValues = new ArrayList<>();
for (Player player : proxyServer.getAllPlayers()) {
possibleValues.add(player.getGameProfile().getName());
}
if(possibleValues.isEmpty()) return Suggestions.empty();
String remaining = builder.getRemaining().toLowerCase();
for (String str : possibleValues) {
if (str.toLowerCase().startsWith(remaining)) {
builder.suggest(StringArgumentType.escapeIfRequired(str));
}
}
return builder.buildFuture();
})
.then(RequiredArgumentBuilder.<CommandSource, String>argument("action", StringArgumentType.string())
.suggests((context, builder) -> {
Collection<String> possibleValues = new ArrayList<>();
possibleValues.add("promote");
possibleValues.add("demote");
String remaining = builder.getRemaining().toLowerCase();
for (String str : possibleValues) {
if (str.toLowerCase().startsWith(remaining)) {
builder.suggest(StringArgumentType.escapeIfRequired(str));
}
}
return builder.buildFuture();
})
.then(RequiredArgumentBuilder.<CommandSource, String>argument("rank", StringArgumentType.string())
.suggests((context, builder) -> {
Collection<String> possibleValues = new ArrayList<>(Config.donorRanks);
String remaining = builder.getRemaining().toLowerCase();
for (String str : possibleValues) {
if (str.toLowerCase().startsWith(remaining)) {
builder.suggest(StringArgumentType.escapeIfRequired(str));
}
}
return builder.buildFuture();
})
.executes(context -> {
CommandSource commandSource = context.getSource();
String username = context.getArgument("username", String.class);
String action = context.getArgument("action", String.class);
String rank = context.getArgument("rank", String.class).toLowerCase();
LuckPerms luckPerms = BoosterAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(username); //TODO test if this works with username
String username = context.getArgument("username", String.class);
String action = context.getArgument("context", String.class);
String rank = context.getArgument("rank", String.class);
if (user == null) {
commandSource.sendMessage(miniMessage.parse(
Config.INVALID_USER,
Template.of("player", username)));
return 1;
}
LuckPerms luckPerms = BoosterAPI.get().getLuckPerms();
User user = luckPerms.getUserManager().getUser(username); //TODO test if this works with username
if (!Config.donorRanks.contains(rank)) {
commandSource.sendMessage(miniMessage.parse(
Config.INVALID_DONOR_RANK,
Template.of("rank", rank)));
return 1;
}
if (user == null) {
context.getSource().sendMessage(miniMessage.parse(
Config.INVALID_USER,
Template.of("player", username)));
return 1;
}
if (!Config.donorRanks.contains(context.getInput())) {
context.getSource().sendMessage(miniMessage.parse(
Config.INVALID_DONOR_RANK,
Template.of("rank", rank)));
return 1;
}
switch (action) {
case "promote" -> promote(user, rank);
case "demote" -> demote(user, rank);
default -> context.getSource().sendMessage(miniMessage.parse(Config.INVALID_ACTION));
}
return 1;
})
switch (action) {
case "promote" -> promote(user, rank);
case "demote" -> demote(user, rank);
default -> commandSource.sendMessage(miniMessage.parse(Config.INVALID_ACTION));
}
return 1;
})
)
)
)
.executes(context -> 1)
.build();
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
@ -74,10 +119,11 @@ public class DonorRankCommand {
}
private void promote(User user, String rank) {
LuckPerms luckPerms = BoosterAPI.get().getLuckPerms();
user.getNodes(NodeType.INHERITANCE).stream()
.filter(Node::getValue)
.forEach(node -> {
if (Config.donorRanks.contains(node.getKey()))
if (Config.donorRanks.contains(node.getGroupName()))
user.data().remove(node);
});
user.data().add(InheritanceNode.builder(rank).build());
@ -88,13 +134,15 @@ public class DonorRankCommand {
Template.of("player", player.getUsername())));
}
});
luckPerms.getUserManager().saveUser(user);
}
private void demote(User user, String rank) {
LuckPerms luckPerms = BoosterAPI.get().getLuckPerms();
user.getNodes(NodeType.INHERITANCE).stream()
.filter(Node::getValue)
.forEach(node -> {
if (Config.donorRanks.contains(node.getKey()))
if (Config.donorRanks.contains(node.getGroupName()))
user.data().remove(node);
});
VelocityBoosters.getPlugin().getProxy().getPlayer(user.getUniqueId()).ifPresent(player -> {
@ -104,5 +152,6 @@ public class DonorRankCommand {
Template.of("player", player.getUsername())));
}
});
luckPerms.getUserManager().saveUser(user);
}
}