From fc061d64ac8cb63ffe67209f7e7b28cbedf3356e Mon Sep 17 00:00:00 2001 From: Shiewk Date: Sat, 8 Jun 2024 19:14:05 +0200 Subject: [PATCH] Unmute and unban commands --- .../de/shiewk/smoderation/SModeration.java | 10 +++ .../smoderation/command/ModLogsCommand.java | 3 +- .../smoderation/command/UnbanCommand.java | 64 +++++++++++++++++++ .../smoderation/command/UnmuteCommand.java | 64 +++++++++++++++++++ src/main/resources/plugin.yml | 14 ++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/shiewk/smoderation/command/UnbanCommand.java create mode 100644 src/main/java/de/shiewk/smoderation/command/UnmuteCommand.java diff --git a/src/main/java/de/shiewk/smoderation/SModeration.java b/src/main/java/de/shiewk/smoderation/SModeration.java index 8ac5a1b..ac9e50a 100644 --- a/src/main/java/de/shiewk/smoderation/SModeration.java +++ b/src/main/java/de/shiewk/smoderation/SModeration.java @@ -54,5 +54,15 @@ public final class SModeration extends JavaPlugin { assert logs != null; logs.setExecutor(new ModLogsCommand()); logs.setTabCompleter(new ModLogsCommand()); + + final PluginCommand unmute = getCommand("unmute"); + assert unmute != null; + unmute.setExecutor(new UnmuteCommand()); + unmute.setTabCompleter(new UnmuteCommand()); + + final PluginCommand unban = getCommand("unban"); + assert unban != null; + unban.setExecutor(new UnbanCommand()); + unban.setTabCompleter(new UnbanCommand()); } } diff --git a/src/main/java/de/shiewk/smoderation/command/ModLogsCommand.java b/src/main/java/de/shiewk/smoderation/command/ModLogsCommand.java index 7511d36..5a3909e 100644 --- a/src/main/java/de/shiewk/smoderation/command/ModLogsCommand.java +++ b/src/main/java/de/shiewk/smoderation/command/ModLogsCommand.java @@ -6,6 +6,7 @@ import de.shiewk.smoderation.punishments.PunishmentType; import de.shiewk.smoderation.util.PlayerUtil; import de.shiewk.smoderation.util.TimeUtil; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -37,7 +38,7 @@ public class ModLogsCommand implements CommandExecutor, TabCompleter { uuid = PlayerUtil.offlinePlayerUUIDByName(playername); } if (uuid == null){ - sender.sendMessage(Component.text("This player was not found. Try running /%s with an UUID instead.".formatted(label))); + sender.sendMessage(Component.text("This player was not found. Try running /%s with an UUID instead.".formatted(label)).color(NamedTextColor.RED)); return true; } name = PlayerUtil.offlinePlayerName(uuid); diff --git a/src/main/java/de/shiewk/smoderation/command/UnbanCommand.java b/src/main/java/de/shiewk/smoderation/command/UnbanCommand.java new file mode 100644 index 0000000..f3435dc --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/command/UnbanCommand.java @@ -0,0 +1,64 @@ +package de.shiewk.smoderation.command; + +import de.shiewk.smoderation.SModeration; +import de.shiewk.smoderation.punishments.Punishment; +import de.shiewk.smoderation.punishments.PunishmentType; +import de.shiewk.smoderation.util.PlayerUtil; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.*; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.UUID; + +public class UnbanCommand implements CommandExecutor, TabCompleter { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (args.length < 1){ + 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(Component.text("Blocks can't execute this command.").color(NamedTextColor.RED)); + return true; + } else { + sender.sendMessage(Component.text("Your command sender type is unknown (%s).".formatted(sender.getClass().getName())).color(NamedTextColor.RED)); + return true; + } + + String nameArg = args[0]; + UUID uuid; + try { + uuid = UUID.fromString(nameArg); + } catch (IllegalArgumentException ignored){ + uuid = PlayerUtil.offlinePlayerUUIDByName(nameArg); + } + if (uuid == null){ + sender.sendMessage(Component.text("This player was not found. Try running /%s with an UUID instead.".formatted(label)).color(NamedTextColor.RED)); + return true; + } + UUID finalUuid = uuid; + final Punishment punishment = SModeration.container.find(p -> p.to.equals(finalUuid) && p.isActive() && p.type == PunishmentType.BAN); + if (punishment != null) { + punishment.cancel(senderUUID); + punishment.broadcastCancellation(SModeration.container); + } else { + sender.sendMessage(Component.text("This player is not banned.").color(NamedTextColor.RED)); + } + return true; + } + } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + return List.of(); + } +} diff --git a/src/main/java/de/shiewk/smoderation/command/UnmuteCommand.java b/src/main/java/de/shiewk/smoderation/command/UnmuteCommand.java new file mode 100644 index 0000000..7c21c55 --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/command/UnmuteCommand.java @@ -0,0 +1,64 @@ +package de.shiewk.smoderation.command; + +import de.shiewk.smoderation.SModeration; +import de.shiewk.smoderation.punishments.Punishment; +import de.shiewk.smoderation.punishments.PunishmentType; +import de.shiewk.smoderation.util.PlayerUtil; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.*; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.UUID; + +public class UnmuteCommand implements CommandExecutor, TabCompleter { + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (args.length < 1){ + 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(Component.text("Blocks can't execute this command.").color(NamedTextColor.RED)); + return true; + } else { + sender.sendMessage(Component.text("Your command sender type is unknown (%s).".formatted(sender.getClass().getName())).color(NamedTextColor.RED)); + return true; + } + + String nameArg = args[0]; + UUID uuid; + try { + uuid = UUID.fromString(nameArg); + } catch (IllegalArgumentException ignored){ + uuid = PlayerUtil.offlinePlayerUUIDByName(nameArg); + } + if (uuid == null){ + sender.sendMessage(Component.text("This player was not found. Try running /%s with an UUID instead.".formatted(label)).color(NamedTextColor.RED)); + return true; + } + UUID finalUuid = uuid; + final Punishment punishment = SModeration.container.find(p -> p.to.equals(finalUuid) && p.isActive() && p.type == PunishmentType.MUTE); + if (punishment != null) { + punishment.cancel(senderUUID); + punishment.broadcastCancellation(SModeration.container); + } else { + sender.sendMessage(Component.text("This player is not muted.").color(NamedTextColor.RED)); + } + return true; + } + } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + return List.of(); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index cfa7f03..20e0b93 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -40,6 +40,20 @@ commands: - smoderation permission: smod.menu description: Shows the SModeration menu. + unmute: + usage: "§cUsage: /unmute " + aliases: + - sunmute + permission: smod.unmute + description: Unmutes a muted player. + unban: + usage: "§cUsage: /unban " + aliases: + - sunban + - pardon + - spardon + permission: smod.unban + description: Unbans a banned player. permissions: smod.mute: default: op