mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-17 16:37:43 +01:00
104 lines
3.0 KiB
Diff
104 lines
3.0 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: BillyGalbreath <blake.galbreath@gmail.com>
|
|
Date: Mon, 11 Jul 2022 21:09:14 -0500
|
|
Subject: [PATCH] Chat Preview API
|
|
|
|
|
|
diff --git a/src/main/java/org/purpurmc/purpur/event/player/PlayerPreviewChatEvent.java b/src/main/java/org/purpurmc/purpur/event/player/PlayerPreviewChatEvent.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..94ccdf11201774f9bea3ba56c4dde148ff5199f8
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/purpurmc/purpur/event/player/PlayerPreviewChatEvent.java
|
|
@@ -0,0 +1,91 @@
|
|
+package org.purpurmc.purpur.event.player;
|
|
+
|
|
+import net.kyori.adventure.text.Component;
|
|
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
|
+import org.bukkit.Bukkit;
|
|
+import org.bukkit.entity.Player;
|
|
+import org.bukkit.event.Cancellable;
|
|
+import org.bukkit.event.HandlerList;
|
|
+import org.bukkit.event.player.PlayerEvent;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+
|
|
+public class PlayerPreviewChatEvent extends PlayerEvent implements Cancellable {
|
|
+ private static final HandlerList handlers = new HandlerList();
|
|
+ private final String originalQuery;
|
|
+ private Component query;
|
|
+
|
|
+ public PlayerPreviewChatEvent(@NotNull Player who, @Nullable Component query) {
|
|
+ super(who, !Bukkit.isPrimaryThread());
|
|
+ this.originalQuery = query == null ? "" : LegacyComponentSerializer.legacySection().serialize(query);
|
|
+ this.query = query;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * The original query string as sent from the client
|
|
+ *
|
|
+ * @return The original query string
|
|
+ */
|
|
+ @NotNull
|
|
+ public String getOriginalQuery() {
|
|
+ return originalQuery;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Get the current query.
|
|
+ * <p>
|
|
+ * Null queries represent "no changes".
|
|
+ *
|
|
+ * @return Current query
|
|
+ */
|
|
+ @Nullable
|
|
+ public Component getQuery() {
|
|
+ return this.query;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Get the current query.
|
|
+ * <p>
|
|
+ * Null queries represent "no changes".
|
|
+ *
|
|
+ * @param query The current query
|
|
+ */
|
|
+ public void setQuery(@Nullable Component query) {
|
|
+ this.query = query;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Gets the cancellation state of this event.
|
|
+ * <p>
|
|
+ * A cancelled event tells the client there are "no changes"
|
|
+ * to the chat, the same as setting the query to null.
|
|
+ *
|
|
+ * @return true if this event is cancelled
|
|
+ */
|
|
+ @Override
|
|
+ public boolean isCancelled() {
|
|
+ return this.query == null;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Sets the cancellation state of this event.
|
|
+ * <p>
|
|
+ * A cancelled event tells the client there are "no changes"
|
|
+ * to the chat, the same as setting the query to null.
|
|
+ */
|
|
+ @Override
|
|
+ public void setCancelled(boolean cancel) {
|
|
+ this.query = null;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ @NotNull
|
|
+ public HandlerList getHandlers() {
|
|
+ return handlers;
|
|
+ }
|
|
+
|
|
+ @NotNull
|
|
+ public static HandlerList getHandlerList() {
|
|
+ return handlers;
|
|
+ }
|
|
+}
|