Plex/server/src/main/java/dev/plex/command/impl/TempbanCMD.java

87 lines
3.0 KiB
Java
Raw Normal View History

2022-02-26 06:24:11 +00:00
package dev.plex.command.impl;
import com.google.common.collect.ImmutableList;
import dev.plex.cache.DataUtils;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.PlayerNotFoundException;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.player.PlexPlayer;
import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.util.BungeeUtil;
2022-02-26 06:24:11 +00:00
import dev.plex.util.PlexUtils;
2022-04-19 20:19:55 +00:00
import dev.plex.util.TimeUtils;
2022-02-26 06:24:11 +00:00
import net.kyori.adventure.text.Component;
2022-06-08 20:09:42 +00:00
import org.apache.commons.lang3.StringUtils;
2022-02-26 06:24:11 +00:00
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2023-03-08 20:26:10 +00:00
import java.util.List;
2022-02-26 06:24:11 +00:00
@CommandParameters(name = "tempban", usage = "/<command> <player> <time> [reason]", description = "Temporarily ban a player")
@CommandPermissions(permission = "plex.tempban", source = RequiredCommandSource.ANY)
2022-02-26 06:24:11 +00:00
public class TempbanCMD extends PlexCommand
{
@Override
public Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
{
if (args.length <= 1)
{
return usage();
}
PlexPlayer target = DataUtils.getPlayer(args[0]);
2022-02-26 06:24:11 +00:00
String reason;
if (target == null)
2022-02-26 06:24:11 +00:00
{
throw new PlayerNotFoundException();
}
Player player = Bukkit.getPlayer(target.getUuid());
2022-02-26 06:24:11 +00:00
if (plugin.getPunishmentManager().isBanned(target.getUuid()))
2022-02-26 06:24:11 +00:00
{
return messageComponent("playerBanned");
}
Punishment punishment = new Punishment(target.getUuid(), getUUID(sender));
2022-04-19 20:20:39 +00:00
punishment.setType(PunishmentType.TEMPBAN);
2022-02-26 06:24:11 +00:00
if (args.length > 2)
{
reason = StringUtils.join(args, " ", 2, args.length);
punishment.setReason(reason);
2022-05-10 05:00:48 +00:00
}
else
2022-02-26 06:24:11 +00:00
{
punishment.setReason("No reason provided.");
}
punishment.setPunishedUsername(target.getName());
2022-04-19 20:19:55 +00:00
punishment.setEndDate(TimeUtils.createDate(args[1]));
2022-02-26 06:24:11 +00:00
punishment.setCustomTime(false);
punishment.setActive(true);
2022-04-02 23:03:58 +00:00
if (player != null)
{
punishment.setIp(player.getAddress().getAddress().getHostAddress().trim());
}
plugin.getPunishmentManager().punish(target, punishment);
PlexUtils.broadcast(messageComponent("banningPlayer", sender.getName(), target.getName()));
2022-02-26 06:24:11 +00:00
if (player != null)
{
2022-05-10 05:00:48 +00:00
BungeeUtil.kickPlayer(player, Punishment.generateBanMessage(punishment));
2022-02-26 06:24:11 +00:00
}
return null;
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
return args.length == 1 && silentCheckPermission(sender,"plex.tempban") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
2022-02-26 06:24:11 +00:00
}
}