Fixed promote/demote
This commit is contained in:
parent
708c2ea185
commit
3bcec7c482
|
|
@ -7,10 +7,12 @@ import com.alttd.vboosters.VelocityBoosters;
|
||||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||||
|
import com.mojang.brigadier.suggestion.Suggestions;
|
||||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||||
import com.velocitypowered.api.command.BrigadierCommand;
|
import com.velocitypowered.api.command.BrigadierCommand;
|
||||||
import com.velocitypowered.api.command.CommandMeta;
|
import com.velocitypowered.api.command.CommandMeta;
|
||||||
import com.velocitypowered.api.command.CommandSource;
|
import com.velocitypowered.api.command.CommandSource;
|
||||||
|
import com.velocitypowered.api.proxy.Player;
|
||||||
import com.velocitypowered.api.proxy.ProxyServer;
|
import com.velocitypowered.api.proxy.ProxyServer;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import net.kyori.adventure.text.minimessage.Template;
|
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.NodeType;
|
||||||
import net.luckperms.api.node.types.InheritanceNode;
|
import net.luckperms.api.node.types.InheritanceNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
public class DonorRankCommand {
|
public class DonorRankCommand {
|
||||||
|
|
||||||
private final MiniMessage miniMessage;
|
private final MiniMessage miniMessage;
|
||||||
|
|
||||||
public DonorRankCommand(ProxyServer proxyServer) {
|
public DonorRankCommand(ProxyServer proxyServer) {
|
||||||
miniMessage = MiniMessage.get();
|
miniMessage = MiniMessage.get();
|
||||||
|
|
||||||
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
|
LiteralCommandNode<CommandSource> command = LiteralArgumentBuilder
|
||||||
.<CommandSource>literal("donorrank")
|
.<CommandSource>literal("donorrank")
|
||||||
.requires(ctx -> ctx.hasPermission("command.proxy.donorrank"))
|
.requires(ctx -> ctx.hasPermission("command.proxy.donorrank"))
|
||||||
.then(RequiredArgumentBuilder.argument("username", StringArgumentType.word()))
|
.then(RequiredArgumentBuilder.<CommandSource, String>argument("username", StringArgumentType.string())
|
||||||
.then(RequiredArgumentBuilder.argument("action", StringArgumentType.word()))
|
.suggests((context, builder) -> {
|
||||||
.then(RequiredArgumentBuilder.argument("rank", StringArgumentType.word()))
|
Collection<String> possibleValues = new ArrayList<>();
|
||||||
.executes(context -> {
|
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);
|
if (user == null) {
|
||||||
String action = context.getArgument("context", String.class);
|
commandSource.sendMessage(miniMessage.parse(
|
||||||
String rank = context.getArgument("rank", String.class);
|
Config.INVALID_USER,
|
||||||
|
Template.of("player", username)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
LuckPerms luckPerms = BoosterAPI.get().getLuckPerms();
|
if (!Config.donorRanks.contains(rank)) {
|
||||||
User user = luckPerms.getUserManager().getUser(username); //TODO test if this works with username
|
commandSource.sendMessage(miniMessage.parse(
|
||||||
|
Config.INVALID_DONOR_RANK,
|
||||||
|
Template.of("rank", rank)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (user == null) {
|
switch (action) {
|
||||||
context.getSource().sendMessage(miniMessage.parse(
|
case "promote" -> promote(user, rank);
|
||||||
Config.INVALID_USER,
|
case "demote" -> demote(user, rank);
|
||||||
Template.of("player", username)));
|
default -> commandSource.sendMessage(miniMessage.parse(Config.INVALID_ACTION));
|
||||||
return 1;
|
}
|
||||||
}
|
return 1;
|
||||||
|
})
|
||||||
if (!Config.donorRanks.contains(context.getInput())) {
|
)
|
||||||
context.getSource().sendMessage(miniMessage.parse(
|
)
|
||||||
Config.INVALID_DONOR_RANK,
|
)
|
||||||
Template.of("rank", rank)));
|
.executes(context -> 1)
|
||||||
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;
|
|
||||||
})
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
|
BrigadierCommand brigadierCommand = new BrigadierCommand(command);
|
||||||
|
|
@ -74,10 +119,11 @@ public class DonorRankCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void promote(User user, String rank) {
|
private void promote(User user, String rank) {
|
||||||
|
LuckPerms luckPerms = BoosterAPI.get().getLuckPerms();
|
||||||
user.getNodes(NodeType.INHERITANCE).stream()
|
user.getNodes(NodeType.INHERITANCE).stream()
|
||||||
.filter(Node::getValue)
|
.filter(Node::getValue)
|
||||||
.forEach(node -> {
|
.forEach(node -> {
|
||||||
if (Config.donorRanks.contains(node.getKey()))
|
if (Config.donorRanks.contains(node.getGroupName()))
|
||||||
user.data().remove(node);
|
user.data().remove(node);
|
||||||
});
|
});
|
||||||
user.data().add(InheritanceNode.builder(rank).build());
|
user.data().add(InheritanceNode.builder(rank).build());
|
||||||
|
|
@ -88,13 +134,15 @@ public class DonorRankCommand {
|
||||||
Template.of("player", player.getUsername())));
|
Template.of("player", player.getUsername())));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
luckPerms.getUserManager().saveUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void demote(User user, String rank) {
|
private void demote(User user, String rank) {
|
||||||
|
LuckPerms luckPerms = BoosterAPI.get().getLuckPerms();
|
||||||
user.getNodes(NodeType.INHERITANCE).stream()
|
user.getNodes(NodeType.INHERITANCE).stream()
|
||||||
.filter(Node::getValue)
|
.filter(Node::getValue)
|
||||||
.forEach(node -> {
|
.forEach(node -> {
|
||||||
if (Config.donorRanks.contains(node.getKey()))
|
if (Config.donorRanks.contains(node.getGroupName()))
|
||||||
user.data().remove(node);
|
user.data().remove(node);
|
||||||
});
|
});
|
||||||
VelocityBoosters.getPlugin().getProxy().getPlayer(user.getUniqueId()).ifPresent(player -> {
|
VelocityBoosters.getPlugin().getProxy().getPlayer(user.getUniqueId()).ifPresent(player -> {
|
||||||
|
|
@ -104,5 +152,6 @@ public class DonorRankCommand {
|
||||||
Template.of("player", player.getUsername())));
|
Template.of("player", player.getUsername())));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
luckPerms.getUserManager().saveUser(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user