diff --git a/src/main/java/de/shiewk/smoderation/SModeration.java b/src/main/java/de/shiewk/smoderation/SModeration.java index 60338a8..ff79d1d 100644 --- a/src/main/java/de/shiewk/smoderation/SModeration.java +++ b/src/main/java/de/shiewk/smoderation/SModeration.java @@ -1,20 +1,28 @@ package de.shiewk.smoderation; +import de.shiewk.smoderation.listener.PunishmentListener; import de.shiewk.smoderation.storage.PunishmentContainer; import org.bukkit.plugin.java.JavaPlugin; +import static org.bukkit.Bukkit.getPluginManager; + public final class SModeration extends JavaPlugin { public static final PunishmentContainer container = new PunishmentContainer(); + public static SModeration PLUGIN = null; + + @Override + public void onLoad() { + PLUGIN = this; + } @Override public void onEnable() { - // Plugin startup logic - + getPluginManager().registerEvents(new PunishmentListener(), this); } @Override public void onDisable() { - // Plugin shutdown logic + } } diff --git a/src/main/java/de/shiewk/smoderation/event/PunishmentIssueEvent.java b/src/main/java/de/shiewk/smoderation/event/PunishmentIssueEvent.java new file mode 100644 index 0000000..b5a249e --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/event/PunishmentIssueEvent.java @@ -0,0 +1,36 @@ +package de.shiewk.smoderation.event; + +import de.shiewk.smoderation.punishments.Punishment; +import de.shiewk.smoderation.storage.PunishmentContainer; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class PunishmentIssueEvent extends Event { + private static final HandlerList handlerList = new HandlerList(); + + private final Punishment punishment; + private final PunishmentContainer container; + + public PunishmentIssueEvent(Punishment punishment, PunishmentContainer container) { + this.punishment = punishment; + this.container = container; + } + + public Punishment getPunishment() { + return punishment; + } + + public PunishmentContainer getContainer() { + return container; + } + + @Override + public @NotNull HandlerList getHandlers() { + return handlerList; + } + + public static HandlerList getHandlerList() { + return handlerList; + } +} diff --git a/src/main/java/de/shiewk/smoderation/listener/PunishmentListener.java b/src/main/java/de/shiewk/smoderation/listener/PunishmentListener.java new file mode 100644 index 0000000..b2fdc8f --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/listener/PunishmentListener.java @@ -0,0 +1,53 @@ +package de.shiewk.smoderation.listener; + +import de.shiewk.smoderation.SModeration; +import de.shiewk.smoderation.event.PunishmentIssueEvent; +import de.shiewk.smoderation.punishments.Punishment; +import de.shiewk.smoderation.punishments.PunishmentType; +import io.papermc.paper.event.player.AsyncChatEvent; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; + +public class PunishmentListener implements Listener { + + @EventHandler(priority = EventPriority.LOW) + public void onPlayerLogin(PlayerLoginEvent event){ + Punishment punishment = SModeration.container.find(p -> + p.type == PunishmentType.BAN + && p.to.equals(event.getPlayer().getUniqueId()) + && p.until >= System.currentTimeMillis()); + if (punishment != null){ + event.disallow(PlayerLoginEvent.Result.KICK_BANNED, punishment.playerMessage()); + } + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onPlayerChat(AsyncChatEvent event){ + final Player player = event.getPlayer(); + final Punishment punishment = SModeration.container.find(p -> + p.type == PunishmentType.MUTE + && p.to.equals(player.getUniqueId()) + && p.until >= System.currentTimeMillis()); + if (punishment != null) { + event.setCancelled(true); + player.sendMessage(punishment.playerMessage()); + } + } + + @EventHandler(priority = EventPriority.LOW) + public void onPunishmentIssue(PunishmentIssueEvent event){ + final Punishment punishment = event.getPunishment(); + switch (punishment.type){ + case KICK, BAN -> { + final Player player = Bukkit.getPlayer(punishment.to); + if (player != null) { + player.kick(punishment.playerMessage()); + } + } + } + } +} diff --git a/src/main/java/de/shiewk/smoderation/punishments/Punishment.java b/src/main/java/de/shiewk/smoderation/punishments/Punishment.java index c0fa0c4..1971ae3 100644 --- a/src/main/java/de/shiewk/smoderation/punishments/Punishment.java +++ b/src/main/java/de/shiewk/smoderation/punishments/Punishment.java @@ -1,6 +1,13 @@ package de.shiewk.smoderation.punishments; +import de.shiewk.smoderation.event.PunishmentIssueEvent; +import de.shiewk.smoderation.storage.PunishmentContainer; import de.shiewk.smoderation.util.ByteUtil; +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.jetbrains.annotations.NotNull; import java.nio.ByteBuffer; @@ -78,4 +85,41 @@ public class Punishment { ", to=" + to + '}'; } + + public static void issue(Punishment punishment, PunishmentContainer container){ + container.add(punishment); + Bukkit.getPluginManager().callEvent(new PunishmentIssueEvent(punishment, 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 ") + .append(Component.text(PlayerUtil.offlinePlayerName(this.by)).color(SECONDARY_COLOR)) + .append(Component.text(".\nYour mute expires in ")) + .append(Component.text(TimeUtil.formatTimeLong(this.until - System.currentTimeMillis())).color(SECONDARY_COLOR)) + .append(Component.text(".")) + .color(PRIMARY_COLOR); + } + case KICK -> { + return Component.text("You have been kicked by ") + .append(Component.text(PlayerUtil.offlinePlayerName(this.by)).color(SECONDARY_COLOR)) + .append(Component.text(".")) + .color(PRIMARY_COLOR); + } + case BAN -> { + return Component.text("You have been banned from this server by ") + .append(Component.text(PlayerUtil.offlinePlayerName(this.by)).color(SECONDARY_COLOR)) + .append(Component.text(".\nYour ban expires in ")) + .append(Component.text(TimeUtil.formatTimeLong(this.until - System.currentTimeMillis())).color(SECONDARY_COLOR)) + .append(Component.text(".")) + .color(PRIMARY_COLOR); + } + default -> throw new IllegalStateException("Unknown punishment type " + type); + } + } } diff --git a/src/main/java/de/shiewk/smoderation/util/PlayerUtil.java b/src/main/java/de/shiewk/smoderation/util/PlayerUtil.java new file mode 100644 index 0000000..43bb84a --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/util/PlayerUtil.java @@ -0,0 +1,16 @@ +package de.shiewk.smoderation.util; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +import java.util.UUID; + +public abstract class PlayerUtil { + private PlayerUtil(){} + + public static String offlinePlayerName(UUID uuid){ + OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); + return player.getName() == null ? uuid.toString() : player.getName(); + } + +} diff --git a/src/main/java/de/shiewk/smoderation/util/TimeUtil.java b/src/main/java/de/shiewk/smoderation/util/TimeUtil.java new file mode 100644 index 0000000..71cd116 --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/util/TimeUtil.java @@ -0,0 +1,80 @@ +package de.shiewk.smoderation.util; + +public abstract class TimeUtil { + private TimeUtil(){} + + public static String formatTimeLong(long millis){ + long seconds = millis / 1000; + + long minutes = seconds / 60; + seconds -= minutes * 60; + + long hours = minutes / 60; + minutes -= hours * 60; + + long days = hours / 24; + hours -= days * 24; + + long years = days / 365; + days -= years * 365; + + long months = days / 30; + days -= months * 30; + + long weeks = days / 7; + days -= weeks * 7; + + StringBuilder builder = new StringBuilder(); + + if (years > 0){ + if (!builder.isEmpty()){ + builder.append(" "); + } + builder.append("%s years".formatted(years)); + } + + if (months > 0){ + if (!builder.isEmpty()){ + builder.append(" "); + } + builder.append("%s months".formatted(months)); + } + + if (weeks > 0){ + if (!builder.isEmpty()){ + builder.append(" "); + } + builder.append("%s weeks".formatted(weeks)); + } + + if (days > 0){ + if (!builder.isEmpty()){ + builder.append(" "); + } + builder.append("%s days".formatted(days)); + } + + if (hours > 0){ + if (!builder.isEmpty()){ + builder.append(" "); + } + builder.append("%s hours".formatted(hours)); + } + + if (minutes > 0){ + if (!builder.isEmpty()){ + builder.append(" "); + } + builder.append("%s minutes".formatted(minutes)); + } + + if (seconds > 0){ + if (!builder.isEmpty()){ + builder.append(" "); + } + builder.append("%s seconds".formatted(seconds)); + } + + return builder.toString(); + } +}