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

Add chat prefix

This commit is contained in:
Shy
2024-06-08 19:26:05 +02:00
parent fc061d64ac
commit 3542997bba
7 changed files with 41 additions and 65 deletions
@@ -4,6 +4,8 @@ import de.shiewk.smoderation.command.*;
import de.shiewk.smoderation.event.CustomInventoryEvents;
import de.shiewk.smoderation.listener.PunishmentListener;
import de.shiewk.smoderation.storage.PunishmentContainer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.command.PluginCommand;
@@ -19,6 +21,7 @@ public final class SModeration extends JavaPlugin {
public static final TextColor PRIMARY_COLOR = TextColor.color(212, 0, 255);
public static final TextColor SECONDARY_COLOR = TextColor.color(52, 143, 255);
public static final TextColor INACTIVE_COLOR = NamedTextColor.GRAY;
public static final TextComponent CHAT_PREFIX = Component.text("SM \u00BB ").color(SECONDARY_COLOR);
@Override
public void onLoad() {
@@ -42,9 +42,9 @@ public class ModLogsCommand implements CommandExecutor, TabCompleter {
return true;
}
name = PlayerUtil.offlinePlayerName(uuid);
sender.sendMessage(Component.text("Player ").color(PRIMARY_COLOR)
sender.sendMessage(CHAT_PREFIX.append(Component.text("Player ").color(PRIMARY_COLOR)
.append(Component.text(name).color(SECONDARY_COLOR))
.append(Component.text(" (%s)".formatted(uuid)).color(INACTIVE_COLOR)));
.append(Component.text(" (%s)".formatted(uuid)).color(INACTIVE_COLOR))));
UUID finalUuid = uuid;
final List<Punishment> punishments = SModeration.container.findAll(p -> p.to.equals(finalUuid) && p.isActive());
for (Punishment punishment : punishments) {
@@ -48,8 +48,8 @@ public class UnbanCommand implements CommandExecutor, TabCompleter {
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);
punishment.undo(senderUUID);
punishment.broadcastUndo(SModeration.container);
} else {
sender.sendMessage(Component.text("This player is not banned.").color(NamedTextColor.RED));
}
@@ -48,8 +48,8 @@ public class UnmuteCommand implements CommandExecutor, TabCompleter {
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);
punishment.undo(senderUUID);
punishment.broadcastUndo(SModeration.container);
} else {
sender.sendMessage(Component.text("This player is not muted.").color(NamedTextColor.RED));
}
@@ -204,12 +204,12 @@ public class SModMenu extends PageableCustomInventory {
lore.add(applyFormatting(Component.text("Expires: ").color(SECONDARY_COLOR).append(Component.text(expires).color(PRIMARY_COLOR))));
}
lore.add(applyFormatting(Component.text("Reason: ").color(SECONDARY_COLOR).append(Component.text(punishment.reason).color(PRIMARY_COLOR))));
if (punishment.wasCancelled()){
lore.add(applyFormatting(Component.text("Cancelled by: ").color(NamedTextColor.RED).append(Component.text(PlayerUtil.offlinePlayerName(punishment.cancelledBy())).color(NamedTextColor.GOLD))));
if (punishment.wasUndone()){
lore.add(applyFormatting(Component.text("Undone by: ").color(NamedTextColor.RED).append(Component.text(PlayerUtil.offlinePlayerName(punishment.undoneBy())).color(NamedTextColor.GOLD))));
} else if (punishment.isActive()) {
if ((punishment.type == PunishmentType.BAN && player.hasPermission("smod.unban")) || (punishment.type == PunishmentType.MUTE && player.hasPermission("smod.unmute"))){
lore.add(Component.empty());
lore.add(applyFormatting(Component.text("\u00BB Click to cancel punishment").color(NamedTextColor.GOLD)));
lore.add(applyFormatting(Component.text("\u00BB Click to undo punishment").color(NamedTextColor.GOLD)));
}
}
meta.lore(lore);
@@ -260,9 +260,9 @@ public class SModMenu extends PageableCustomInventory {
if (timestamp != null) {
final Punishment punishment = container.findByTimestamp(timestamp);
if (punishment != null) {
new ConfirmationInventory(player, "Do you want to cancel this punishment?", () -> {
punishment.cancel(player.getUniqueId());
punishment.broadcastCancellation(container);
new ConfirmationInventory(player, "Do you want to undo this punishment?", () -> {
punishment.undo(player.getUniqueId());
punishment.broadcastUndo(container);
player.playSound(player, Sound.BLOCK_NOTE_BLOCK_PLING, 1f, 2f);
this.open();
}, this::open, false).open();
@@ -13,6 +13,8 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import static de.shiewk.smoderation.SModeration.CHAT_PREFIX;
public class PunishmentListener implements Listener {
@EventHandler(priority = EventPriority.LOW)
@@ -22,7 +24,7 @@ public class PunishmentListener implements Listener {
&& p.to.equals(event.getPlayer().getUniqueId())
&& p.isActive());
if (punishment != null){
event.disallow(PlayerLoginEvent.Result.KICK_BANNED, punishment.playerMessage());
event.disallow(PlayerLoginEvent.Result.KICK_BANNED, CHAT_PREFIX.append(punishment.playerMessage()));
}
}
@@ -35,7 +37,7 @@ public class PunishmentListener implements Listener {
&& p.isActive());
if (punishment != null) {
event.setCancelled(true);
player.sendMessage(punishment.playerMessage());
player.sendMessage(CHAT_PREFIX.append(punishment.playerMessage()));
}
}
@@ -52,7 +54,7 @@ public class PunishmentListener implements Listener {
case KICK, BAN -> {
final Player player = Bukkit.getPlayer(punishment.to);
if (player != null) {
player.kick(punishment.playerMessage());
player.kick(CHAT_PREFIX.append(punishment.playerMessage()));
}
}
}
@@ -8,13 +8,11 @@ import de.shiewk.smoderation.util.TimeUtil;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer;
import java.util.UUID;
import static de.shiewk.smoderation.SModeration.PRIMARY_COLOR;
import static de.shiewk.smoderation.SModeration.SECONDARY_COLOR;
import static de.shiewk.smoderation.SModeration.*;
public class Punishment {
public static final String DEFAULT_REASON = "No reason provided.";
@@ -24,7 +22,7 @@ public class Punishment {
public final UUID by;
public final UUID to;
public final String reason;
private UUID cancelledBy;
private UUID undoneBy;
public Punishment(PunishmentType type, long time, long until, UUID by, UUID to, String reason) {
this.type = type;
@@ -35,23 +33,23 @@ public class Punishment {
this.reason = reason;
}
public boolean wasCancelled(){
return cancelledBy != null;
public boolean wasUndone(){
return undoneBy != null;
}
public UUID cancelledBy() {
return cancelledBy;
public UUID undoneBy() {
return undoneBy;
}
public void cancel(UUID cancelledBy){
if (this.cancelledBy != null){
throw new IllegalArgumentException("This punishment is already cancelled.");
public void undo(UUID undoneBy){
if (this.undoneBy != null){
throw new IllegalArgumentException("This punishment was already undone.");
}
this.cancelledBy = cancelledBy;
this.undoneBy = undoneBy;
}
public boolean isActive(){
return until > System.currentTimeMillis() && !wasCancelled();
return until > System.currentTimeMillis() && !wasUndone();
}
public static Punishment mute(long time, long until, UUID by, UUID to, String reason){
@@ -70,7 +68,7 @@ public class Punishment {
public byte[] toBytes(){
final byte[] reasonBytes = reason.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_LENGTH + reasonBytes.length + (cancelledBy != null ? 17 : 1));
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_LENGTH + reasonBytes.length + (undoneBy != null ? 17 : 1));
buffer.putInt(0, type.ordinal());
buffer.putLong(4, time);
buffer.putLong(12, until);
@@ -78,14 +76,14 @@ public class Punishment {
buffer.put(36, ByteUtil.uuidToBytes(to));
buffer.putInt(40, reason.length());
buffer.put(44, reasonBytes);
buffer.put(44+reasonBytes.length, cancelledBy != null ? (byte) 1 : (byte) 0);
if (cancelledBy != null){
buffer.put(44+reasonBytes.length+1, ByteUtil.uuidToBytes(cancelledBy));
buffer.put(44+reasonBytes.length, undoneBy != null ? (byte) 1 : (byte) 0);
if (undoneBy != null){
buffer.put(44+reasonBytes.length+1, ByteUtil.uuidToBytes(undoneBy));
}
return buffer.array();
}
private Component cancellationMessage(){
private Component undoMessage(){
String msg = "";
switch (type){
case MUTE -> msg = "unmuted";
@@ -96,43 +94,16 @@ public class Punishment {
.append(Component.text(" was ").color(PRIMARY_COLOR))
.append(Component.text(msg))
.append(Component.text(" by ").color(PRIMARY_COLOR))
.append(Component.text(PlayerUtil.offlinePlayerName(cancelledBy)))
.append(Component.text(PlayerUtil.offlinePlayerName(undoneBy)))
.append(Component.text(".").color(PRIMARY_COLOR));
}
public void broadcastCancellation(PunishmentContainer container){
public void broadcastUndo(PunishmentContainer container){
for (CommandSender sender : container.collectBroadcastTargets()) {
sender.sendMessage(cancellationMessage());
sender.sendMessage(CHAT_PREFIX.append(undoMessage()));
}
}
/**
* @deprecated behaves weirdly, does not support punishment reasons
*/
@Deprecated(forRemoval = true)
public static @NotNull Punishment fromBytes(byte[] bytes){
if (bytes.length != BUFFER_LENGTH){
throw new IllegalArgumentException("the array has to be %s in length".formatted(BUFFER_LENGTH));
}
ByteBuffer buffer = ByteBuffer.wrap(bytes);
final int ptypeIndex = buffer.getInt(0);
PunishmentType ptype;
try {
ptype = PunishmentType.values()[ptypeIndex];
} catch (IndexOutOfBoundsException e){
throw new IllegalArgumentException("The punishment type %s does not exist.".formatted(ptypeIndex));
}
final long time = buffer.getLong(4);
final long until = buffer.getLong(12);
final byte[] byBytes = new byte[16];
System.arraycopy(bytes, 20, byBytes, 0, 16);
final byte[] toBytes = new byte[16];
System.arraycopy(bytes, 36, toBytes, 0, 16);
final UUID by = ByteUtil.bytesToUuid(byBytes);
final UUID to = ByteUtil.bytesToUuid(toBytes);
return new Punishment(ptype, time, until, by, to, "");
}
@Override
public String toString() {
return "Punishment{" +
@@ -190,7 +161,7 @@ public class Punishment {
private void broadcastIssue(PunishmentContainer container){
for (CommandSender sender : container.collectBroadcastTargets()) {
sender.sendMessage(broadcastMessage());
sender.sendMessage(CHAT_PREFIX.append(broadcastMessage()));
}
}
@@ -199,7 +170,7 @@ public class Punishment {
case MUTE, BAN -> {
final CommandSender sender = PlayerUtil.senderByUUID(to);
if (sender != null) {
sender.sendMessage(playerMessage());
sender.sendMessage(CHAT_PREFIX.append(playerMessage()));
}
}
}