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

126 lines
5.1 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;
2024-01-15 23:45:00 +00:00
import dev.plex.util.PlexLog;
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;
2024-01-15 23:45:00 +00:00
import java.util.Collections;
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);
2024-01-15 23:45:00 +00:00
boolean rollBack = true;
2022-02-26 06:24:11 +00:00
if (args.length > 2)
{
reason = StringUtils.join(args, " ", 2, args.length);
2024-01-15 23:45:00 +00:00
String newReason = StringUtils.normalizeSpace(reason.replace("-nrb", ""));
punishment.setReason(newReason.trim().isEmpty() ? "No reason provided." : newReason);
rollBack = !reason.startsWith("-nrb") && !reason.endsWith("-nrb");
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
}
2024-01-15 23:45:00 +00:00
if (rollBack)
{
/*if (plugin.getPrismHook().hasPrism()) {
PrismParameters parameters = plugin.getPrismHook().prismApi().createParameters();
parameters.addActionType("block-place");
parameters.addActionType("block-break");
parameters.addActionType("block-burn");
parameters.addActionType("entity-spawn");
parameters.addActionType("entity-kill");
parameters.addActionType("entity-explode");
parameters.addPlayerName(plexPlayer.getName());
parameters.setBeforeTime(Instant.now().toEpochMilli());
parameters.setProcessType(PrismProcessType.ROLLBACK);
final Future<Result> result = plugin.getPrismHook().prismApi().performLookup(parameters, sender);
Bukkit.getAsyncScheduler().runNow(plugin, scheduledTask -> {
try
{
final Result done = result.get();
} catch (InterruptedException | ExecutionException e)
{
throw new RuntimeException(e);
}
});
}
else */
if (plugin.getCoreProtectHook().hasCoreProtect())
{
PlexLog.debug("Testing coreprotect");
Bukkit.getAsyncScheduler().runNow(plugin, scheduledTask ->
{
plugin.getCoreProtectHook().coreProtectAPI().performRollback(86400, Collections.singletonList(target.getName()), null, null, null, null, 0, null);
});
}
}
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
}
}