1
mirror of https://github.com/Shiewk/SModeration.git synced 2026-04-28 05:54:16 +02:00

Make punishments actually work, custom event for issuing

This commit is contained in:
Shy
2024-06-07 17:26:59 +02:00
parent 88cb2b3cb6
commit 1bb4c2fc47
6 changed files with 240 additions and 3 deletions
@@ -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
}
}
@@ -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;
}
}
@@ -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());
}
}
}
}
}
@@ -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);
}
}
}
@@ -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();
}
}
@@ -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();
}
}