Add a /tempmute command (#96)

* Update WhoHasCMD.java

Added functionality that clears all players with a specified item

* Added functionality that clears all players with a specified item

* Altered tab completion to include "clear" argument

* Update WhoHasCMD.java

* Implemented a mob limiter that can be configured via a command

* Fixed some imports

* Refined tab complete

* Implemented reviewed changes

* Added a /tempmute command

* Removed unnecessary imports & simplified the reason assignment

* Fixed the usage for the moblimit command

---------

Co-authored-by: Telesphoreo <me@telesphoreo.me>
This commit is contained in:
james 2024-06-04 15:23:51 +01:00 committed by GitHub
parent 23611e218b
commit 9e974ae737
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 139 additions and 2 deletions

View File

@ -18,7 +18,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@CommandParameters(name = "moblimit", usage = "/<command> [on/off/setmax <limit>]", aliases = "entitylimit", description = "Manages the mob limit per chunk.")
@CommandParameters(name = "moblimit", usage = "/<command> [on | off | setmax <limit>]", aliases = "entitylimit", description = "Manages the mob limit per chunk.")
@CommandPermissions(permission = "plex.moblimit", source = RequiredCommandSource.ANY)
public class MobLimitCMD extends PlexCommand
{

View File

@ -0,0 +1,92 @@
package dev.plex.command.impl;
import com.google.common.collect.ImmutableList;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.player.PlexPlayer;
import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils; // Import your TimeUtils
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
@CommandParameters(name = "tempmute", description = "Temporarily mute a player on the server",
usage = "/<command> <player> <time> [reason]", aliases = "tmute")
@CommandPermissions(permission = "plex.tempmute")
public class TempmuteCMD extends PlexCommand
{
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
{
if (args.length < 2)
{
return usage();
}
Player player = getNonNullPlayer(args[0]);
PlexPlayer punishedPlayer = getOfflinePlexPlayer(player.getUniqueId());
if (punishedPlayer.isMuted())
{
return messageComponent("playerMuted");
}
if (silentCheckPermission(player, "plex.tempmute"))
{
send(sender, messageComponent("higherRankThanYou"));
return null;
}
ZonedDateTime endDate;
try
{
endDate = TimeUtils.createDate(args[1]);
}
catch (NumberFormatException e)
{
return messageComponent("invalidTimeFormat");
}
if (endDate.isBefore(ZonedDateTime.now()))
{
return messageComponent("timeMustBeFuture");
}
ZonedDateTime oneWeekFromNow = ZonedDateTime.now().plusWeeks(1);
if (endDate.isAfter(oneWeekFromNow))
{
return messageComponent("maxTimeExceeded");
}
final String reason = args.length >= 3 ? String.join(" ", Arrays.copyOfRange(args, 2, args.length))
: messageString("noReasonProvided");
Punishment punishment = new Punishment(punishedPlayer.getUuid(), getUUID(sender));
punishment.setCustomTime(true);
punishment.setEndDate(endDate);
punishment.setType(PunishmentType.MUTE);
punishment.setPunishedUsername(player.getName());
punishment.setIp(player.getAddress().getAddress().getHostAddress().trim());
punishment.setReason(reason);
punishment.setActive(true);
plugin.getPunishmentManager().punish(punishedPlayer, punishment);
PlexUtils.broadcast(messageComponent("tempMutedPlayer", sender.getName(), player.getName(), TimeUtils.formatRelativeTime(endDate)));
return null;
}
@Override
public @NotNull List<String> smartTabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
return args.length == 1 && silentCheckPermission(sender, this.getPermission()) ? PlexUtils.getPlayerNameList() : ImmutableList.of();
}
}

View File

@ -7,6 +7,7 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -79,4 +80,41 @@ public class TimeUtils
}
return DATE_FORMAT.withZone(ZoneId.of(TIMEZONE)).format(date);
}
public static String formatRelativeTime(ZonedDateTime date)
{
long seconds = ChronoUnit.SECONDS.between(ZonedDateTime.now(), date);
if (seconds <= 0)
{
return "now";
}
long minute = seconds / 60;
long hour = minute / 60;
long day = hour / 24;
long week = day / 7;
if (week > 0)
{
return week + " week" + (week > 1 ? "s" : "");
}
else if (day > 0)
{
return day + " day" + (day > 1 ? "s" : "");
}
else if (hour > 0)
{
return hour + " hour" + (hour > 1 ? "s" : "");
}
else if (minute > 0)
{
return minute + " minute" + (minute > 1 ? "s" : "");
}
else
{
return seconds + " second" + (seconds > 1 ? "s" : "");
}
}
}

View File

@ -43,6 +43,13 @@ mutedPlayer: "<red>{0} - Muted {1}"
# 0 - The command sender
# 1 - The person who has been unmuted
unmutedPlayer: "<aqua>{0} - Unmuted {1}"
invalidTimeFormat: "<red>Invalid time format. Use s, m, h, d, w, mo, or y (e.g., 1h30m)."
timeMustBeFuture: "<red>The specified time must be in the future."
# 0 - The command sender
# 1 - The person who has been muted
# 2 - The time that the person is muted for
tempMutedPlayer: "<red>{0} - Muted {1} for {2}"
maxTimeExceeded: "<red>The specified time must be under a week."
# 0 - The person who is locking up
# 1 - The person who has been locked up
lockedUpPlayer: "<aqua>{0} - Locking up {1}"