From c17f42169483003fe52a0e3e53bf1beba6b0f06c Mon Sep 17 00:00:00 2001 From: Shiewk Date: Sat, 8 Jun 2024 11:48:17 +0200 Subject: [PATCH] broadcast punishment to admins when issued --- .../smoderation/punishments/Punishment.java | 48 +++++++++++++++++-- .../storage/PunishmentContainer.java | 21 +++++++- src/main/resources/plugin.yml | 5 +- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/shiewk/smoderation/punishments/Punishment.java b/src/main/java/de/shiewk/smoderation/punishments/Punishment.java index 614893c..cd14717 100644 --- a/src/main/java/de/shiewk/smoderation/punishments/Punishment.java +++ b/src/main/java/de/shiewk/smoderation/punishments/Punishment.java @@ -15,6 +15,9 @@ import java.nio.ByteBuffer; import java.util.UUID; public class Punishment { + + public static final NamedTextColor PRIMARY_COLOR = NamedTextColor.RED; + public static final NamedTextColor SECONDARY_COLOR = NamedTextColor.GOLD; public final PunishmentType type; public final long time; public final long until; @@ -92,11 +95,48 @@ public class Punishment { Bukkit.getPluginManager().callEvent(event); if (!event.isCancelled()){ container.add(punishment); - punishment.firstIssue(); + punishment.firstIssue(container); } } - private void firstIssue(){ + private Component broadcastMessage(){ + final String toName = PlayerUtil.offlinePlayerName(to); + switch (type) { + case MUTE -> { + return Component.text(toName).color(SECONDARY_COLOR).append( + Component.text(" has been muted by ").color(PRIMARY_COLOR) + .append(Component.text(PlayerUtil.offlinePlayerName(this.by)).color(SECONDARY_COLOR)) + .append(Component.text(" for ")) + .append(Component.text(TimeUtil.formatTimeLong(this.until - this.time)).color(SECONDARY_COLOR)) + .append(Component.text("."))); + } + case KICK -> { + return Component.text(toName).color(SECONDARY_COLOR).append( + Component.text(" has been kicked by ").color(PRIMARY_COLOR) + .append(Component.text(PlayerUtil.offlinePlayerName(this.by)).color(SECONDARY_COLOR)) + .append(Component.text(".")) + .color(PRIMARY_COLOR)); + } + case BAN -> { + return Component.text(toName).color(SECONDARY_COLOR).append( + Component.text(" has been kicked by ").color(PRIMARY_COLOR) + .append(Component.text(PlayerUtil.offlinePlayerName(this.by)).color(SECONDARY_COLOR)) + .append(Component.text(" for ")) + .append(Component.text(TimeUtil.formatTimeLong(this.until - this.time)).color(SECONDARY_COLOR)) + .append(Component.text(".")) + .color(PRIMARY_COLOR)); + } + default -> throw new IllegalStateException("Unknown punishment type " + type); + } + } + + private void broadcastIssue(PunishmentContainer container){ + for (CommandSender sender : container.collectBroadcastTargets()) { + sender.sendMessage(broadcastMessage()); + } + } + + private void firstIssue(PunishmentContainer container){ switch (type) { case MUTE, BAN -> { final CommandSender sender = PlayerUtil.senderByUUID(to); @@ -105,13 +145,11 @@ public class Punishment { } } } + broadcastIssue(container); } public Component playerMessage(){ - NamedTextColor PRIMARY_COLOR = NamedTextColor.RED; - NamedTextColor SECONDARY_COLOR = NamedTextColor.GOLD; - switch (type) { case MUTE -> { return Component.text("You have been muted by ") diff --git a/src/main/java/de/shiewk/smoderation/storage/PunishmentContainer.java b/src/main/java/de/shiewk/smoderation/storage/PunishmentContainer.java index 1299b00..d8a7748 100644 --- a/src/main/java/de/shiewk/smoderation/storage/PunishmentContainer.java +++ b/src/main/java/de/shiewk/smoderation/storage/PunishmentContainer.java @@ -1,8 +1,14 @@ package de.shiewk.smoderation.storage; import de.shiewk.smoderation.punishments.Punishment; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Predicate; @@ -20,8 +26,8 @@ public class PunishmentContainer { return punishments.remove(index); } - public boolean remove(Punishment punishment){ - return punishments.remove(punishment); + public void remove(Punishment punishment){ + punishments.remove(punishment); } public @Nullable Punishment find(Predicate predicate){ @@ -33,6 +39,17 @@ public class PunishmentContainer { return null; } + public List collectBroadcastTargets(){ + ArrayList senders = new ArrayList<>(); + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { + if (onlinePlayer.hasPermission("smod.notifications")){ + senders.add(onlinePlayer); + } + } + senders.add(Bukkit.getConsoleSender()); + return Collections.unmodifiableList(senders); + } + public @Nullable Punishment findByTimestamp(long timestamp){ return find(punishment -> punishment.time == timestamp); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index f9f0dad..c304a5f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -44,4 +44,7 @@ permissions: description: Allows the player to kick other players. smod.menu: default: op - description: Allows the player to use the SModeration menu. \ No newline at end of file + description: Allows the player to use the SModeration menu. + smod.notifications: + default: op + description: Allows the player to be notified when a punishment is issued. \ No newline at end of file