1
mirror of https://github.com/Shiewk/SModeration.git synced 2026-04-28 05:54:16 +02:00

Upgrade Paper to 1.21, use Brigadier command system

This commit is contained in:
Shy
2025-05-01 19:55:55 +02:00
parent fbaaa6ec89
commit 361f66f645
19 changed files with 786 additions and 714 deletions
@@ -1,83 +1,81 @@
package de.shiewk.smoderation.paper.command;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.LiteralCommandNode;
import de.shiewk.smoderation.paper.inventory.InvSeeEquipmentInventory;
import de.shiewk.smoderation.paper.util.PlayerUtil;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.HumanEntity;
import de.shiewk.smoderation.paper.util.CommandUtil;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static de.shiewk.smoderation.paper.SModerationPaper.*;
import static io.papermc.paper.command.brigadier.Commands.argument;
import static io.papermc.paper.command.brigadier.Commands.literal;
import static net.kyori.adventure.text.Component.text;
public class InvseeCommand implements TabExecutor {
private enum InvseeType {
INVENTORY,
EQUIPMENT
}
public final class InvseeCommand implements CommandProvider {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length < 1) {
return false;
public LiteralCommandNode<CommandSourceStack> getCommandNode() {
return literal("invsee")
.requires(CommandUtil.requirePermission("smod.invsee"))
.then(argument("player", ArgumentTypes.player())
.executes(this::invseeInventory)
.then(literal("inventory")
.executes(this::invseeInventory)
)
.then(literal("armor")
.executes(this::invseeEquipment)
)
.then(literal("equipment")
.executes(this::invseeEquipment)
)
)
.build();
}
private int invseeInventory(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Player sender = CommandUtil.getExecutingPlayer(context.getSource());
Player target = CommandUtil.getPlayerSingle(context, "player");
if (sender.equals(target)){
CommandUtil.error("You can't open your own inventory.");
}
final InvseeType type;
if (args.length > 1){
switch (args[1].toLowerCase()){
case "armor", "equipment" -> type = InvseeType.EQUIPMENT;
default -> type = InvseeType.INVENTORY;
}
} else {
type = InvseeType.INVENTORY;
sender.sendMessage(CHAT_PREFIX.append(
text("Opening inventory of ").color(PRIMARY_COLOR)
.append(target.teamDisplayName().colorIfAbsent(SECONDARY_COLOR))
.append(text("."))
));
sender.openInventory(target.getInventory());
return Command.SINGLE_SUCCESS;
}
private int invseeEquipment(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
Player sender = CommandUtil.getExecutingPlayer(context.getSource());
Player target = CommandUtil.getPlayerSingle(context, "player");
if (sender.equals(target)){
CommandUtil.error("You can't open your own inventory.");
}
if (sender instanceof HumanEntity human){
final Player player = PlayerUtil.findOnlinePlayer(args[0]);
if (player != null) {
if (human.getUniqueId().equals(player.getUniqueId()) && type != InvseeType.EQUIPMENT){
human.sendMessage(Component.text("You can't open your own inventory.").color(FAIL_COLOR));
} else {
human.sendMessage(CHAT_PREFIX.append(
Component.text("Opening inventory of ").color(PRIMARY_COLOR)
.append(Component.text(player.getName()).color(SECONDARY_COLOR))
.append(Component.text("."))
));
switch (type){
case INVENTORY -> human.openInventory(player.getInventory());
case EQUIPMENT -> new InvSeeEquipmentInventory(human, player).open();
}
}
} else {
human.sendMessage(Component.text("This player is not online.").color(FAIL_COLOR));
}
} else {
sender.sendMessage(Component.text("Only an entity that can open inventories can execute this command!").color(FAIL_COLOR));
}
return true;
sender.sendMessage(CHAT_PREFIX.append(
text("Opening inventory of ").color(PRIMARY_COLOR)
.append(target.teamDisplayName().colorIfAbsent(SECONDARY_COLOR))
.append(text("."))
));
new InvSeeEquipmentInventory(sender, target).open();
return Command.SINGLE_SUCCESS;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length > 2){
return List.of();
} else if (args.length > 1){
return List.of("armor", "equipment", "inventory");
}
List<String> available = new ArrayList<>();
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
available.add(onlinePlayer.getName());
}
List<String> completions = new ArrayList<>();
StringUtil.copyPartialMatches(args.length > 0 ? args[0] : "", available, completions);
return completions;
public String getCommandDescription() {
return "Views the inventory of another player.";
}
@Override
public Collection<String> getAliases() {
return List.of("sinvsee", "smodinvsee", "invs");
}
}