mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-28 05:54:16 +02:00
Move SModeration for Paper to root project
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package de.shiewk.smoderation.paper.command;
|
||||
|
||||
import de.shiewk.smoderation.paper.SModerationPaper;
|
||||
import de.shiewk.smoderation.paper.punishments.Punishment;
|
||||
import de.shiewk.smoderation.paper.util.PlayerUtil;
|
||||
import de.shiewk.smoderation.paper.util.TimeUtil;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.*;
|
||||
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.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
public class MuteCommand implements TabExecutor {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 2){
|
||||
return false;
|
||||
} else {
|
||||
UUID senderUUID;
|
||||
if (sender instanceof ConsoleCommandSender){
|
||||
senderUUID = PlayerUtil.UUID_CONSOLE;
|
||||
} else if (sender instanceof Player pl){
|
||||
senderUUID = pl.getUniqueId();
|
||||
} else if (sender instanceof BlockCommandSender){
|
||||
sender.sendMessage(text("Blocks can't execute this command.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
} else {
|
||||
sender.sendMessage(text("Your command sender type is unknown (%s).".formatted(sender.getClass().getName())).color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
String playerName = args[0];
|
||||
UUID uuid = PlayerUtil.offlinePlayerUUIDByName(playerName);
|
||||
if (senderUUID.equals(uuid)) {
|
||||
sender.sendMessage(text("You can't mute yourself.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
if (uuid == null) {
|
||||
sender.sendMessage(text("This player is either offline or was never on this server.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
final Player toPlayer = Bukkit.getPlayer(uuid);
|
||||
if (toPlayer != null && toPlayer.hasPermission("smod.preventmute")){
|
||||
sender.sendMessage(text().content("This player can't be muted.").color(NamedTextColor.RED));
|
||||
return true;
|
||||
}
|
||||
long duration = 0;
|
||||
int p = 1;
|
||||
for (int i = 1 /* start with index 1 to avoid player name */; i < args.length; i++) {
|
||||
String arg = args[i];
|
||||
long parsedDuration = TimeUtil.parseDurationMillisSafely(arg);
|
||||
if (parsedDuration == -1){
|
||||
p = i;
|
||||
break;
|
||||
} else {
|
||||
duration += parsedDuration;
|
||||
}
|
||||
if (i == args.length - 1){ p = args.length; }
|
||||
}
|
||||
if (duration == 0){
|
||||
sender.sendMessage(text("Please provide a valid duration.").color(NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
if (duration < 0){
|
||||
sender.sendMessage(text("Please provide a duration that's longer than 0ms.").color(NamedTextColor.RED));
|
||||
return false;
|
||||
}
|
||||
StringBuilder reason = new StringBuilder();
|
||||
for (int i = p; i < args.length; i++) {
|
||||
if (!reason.isEmpty()){
|
||||
reason.append(" ");
|
||||
}
|
||||
reason.append(args[i]);
|
||||
}
|
||||
final Punishment punishment = Punishment.mute(System.currentTimeMillis(), System.currentTimeMillis() + duration, senderUUID, uuid, reason.isEmpty() ? Punishment.DEFAULT_REASON : reason.toString());
|
||||
Punishment.issue(punishment, SModerationPaper.container);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 2){
|
||||
String toComplete = args.length > 0 ? args[0] : "";
|
||||
ArrayList<String> names = new ArrayList<>();
|
||||
for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
|
||||
names.add(onlinePlayer.getName());
|
||||
}
|
||||
ArrayList<String> completions = new ArrayList<>();
|
||||
StringUtil.copyPartialMatches(toComplete, names, completions);
|
||||
return completions;
|
||||
} else {
|
||||
for (int i = 1; i < args.length; i++) {
|
||||
if (TimeUtil.parseDurationMillisSafely(args[i]) == -1){
|
||||
try {
|
||||
Long.parseLong(args[i]);
|
||||
} catch (NumberFormatException ignored){
|
||||
if (i != 1){
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return List.of(
|
||||
"100ms",
|
||||
"15s", // some sample completions for duration
|
||||
"30min", // you can input your own ones as well
|
||||
"6h",
|
||||
"1d",
|
||||
"2w",
|
||||
"3mo",
|
||||
"1y"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user