mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-28 05:54:16 +02:00
91 lines
3.6 KiB
Java
91 lines
3.6 KiB
Java
package de.shiewk.smoderation.paper.command;
|
|
|
|
import com.mojang.brigadier.Command;
|
|
import com.mojang.brigadier.arguments.StringArgumentType;
|
|
import com.mojang.brigadier.context.CommandContext;
|
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
|
import com.mojang.brigadier.tree.LiteralCommandNode;
|
|
import de.shiewk.smoderation.paper.SModerationPaper;
|
|
import de.shiewk.smoderation.paper.punishments.Kick;
|
|
import de.shiewk.smoderation.paper.punishments.Punishment;
|
|
import de.shiewk.smoderation.paper.punishments.PunishmentManager;
|
|
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 java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
import static io.papermc.paper.command.brigadier.Commands.argument;
|
|
import static io.papermc.paper.command.brigadier.Commands.literal;
|
|
|
|
@SuppressWarnings("UnstableApiUsage") // Paper Brigadier API
|
|
public final class KickCommand implements CommandProvider {
|
|
|
|
private final PunishmentManager punishmentManager;
|
|
|
|
public KickCommand(PunishmentManager punishmentManager) {
|
|
this.punishmentManager = punishmentManager;
|
|
}
|
|
|
|
@Override
|
|
public LiteralCommandNode<CommandSourceStack> getCommandNode() {
|
|
return literal("kick")
|
|
.requires(CommandUtil.requirePermission("smod.kick"))
|
|
.then(argument("player", ArgumentTypes.player())
|
|
.executes(this::kickWithoutReason)
|
|
.then(argument("reason", StringArgumentType.greedyString())
|
|
.executes(this::kickWithReason)
|
|
)
|
|
)
|
|
.build();
|
|
}
|
|
|
|
private int kickWithReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
|
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
|
Player target = CommandUtil.getPlayerSingle(context, "player");
|
|
String reason = StringArgumentType.getString(context, "reason");
|
|
executeKick(punishmentManager, sender, target, reason);
|
|
return Command.SINGLE_SUCCESS;
|
|
}
|
|
|
|
private int kickWithoutReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
|
if (SModerationPaper.config().getBoolean("force-reason", false)){
|
|
CommandUtil.errorTranslatable("smod.command.kick.fail.forceReason");
|
|
}
|
|
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
|
Player target = CommandUtil.getPlayerSingle(context, "player");
|
|
executeKick(punishmentManager, sender, target, SModerationPaper.config().getString("default-reason", "No reason provided."));
|
|
return Command.SINGLE_SUCCESS;
|
|
}
|
|
|
|
public static void executeKick(PunishmentManager manager, UUID sender, Player target, String reason) throws CommandSyntaxException {
|
|
UUID targetId = target.getUniqueId();
|
|
if (sender.equals(targetId)) {
|
|
CommandUtil.errorTranslatable("smod.command.kick.fail.self");
|
|
} else if (target.hasPermission("smod.preventkick")){
|
|
CommandUtil.errorTranslatable("smod.command.kick.fail.protect");
|
|
}
|
|
Punishment punishment = new Kick(
|
|
Punishment.generateUUID(),
|
|
System.currentTimeMillis(),
|
|
sender,
|
|
targetId,
|
|
reason
|
|
);
|
|
manager.tryIssue(punishment);
|
|
}
|
|
|
|
@Override
|
|
public String getCommandDescription() {
|
|
return "Kicks a player";
|
|
}
|
|
|
|
@Override
|
|
public Collection<String> getAliases() {
|
|
return List.of("smodkick");
|
|
}
|
|
}
|