Allow listening to plugin messages during configuration phase (#12775)

This commit is contained in:
Owen
2025-07-01 09:40:50 -04:00
committed by GitHub
parent 51d28af8fa
commit 6ea679eb07
12 changed files with 216 additions and 128 deletions
@@ -1,9 +1,11 @@
package org.bukkit.plugin.messaging;
import java.util.Set;
import io.papermc.paper.connection.PlayerConnection;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
@@ -230,4 +232,14 @@ public interface Messenger {
* @param message Raw payload of the message.
*/
public void dispatchIncomingMessage(@NotNull Player source, @NotNull String channel, byte @NotNull [] message);
/**
* Dispatches the specified incoming message to any registered listeners.
*
* @param source Source of the message.
* @param channel Channel that the message was sent by.
* @param message Raw payload of the message.
*/
@ApiStatus.Experimental
public void dispatchIncomingMessage(@NotNull PlayerConnection source, @NotNull String channel, byte @NotNull [] message);
}
@@ -1,7 +1,9 @@
package org.bukkit.plugin.messaging;
import io.papermc.paper.connection.PlayerCommonConnection;
import io.papermc.paper.connection.PlayerConnection;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
@@ -28,7 +30,8 @@ public interface PluginMessageListener {
* @param connection Source of the message.
* @param message The raw message that was sent.
*/
default void onPluginMessageReceived(@NotNull String channel, @NotNull PlayerCommonConnection connection, byte @NotNull [] message) {
@ApiStatus.Experimental
default void onPluginMessageReceived(@NotNull String channel, @NotNull PlayerConnection connection, byte @NotNull [] message) {
}
}
@@ -8,6 +8,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import io.papermc.paper.connection.PlayerConnection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
@@ -462,6 +463,30 @@ public class StandardMessenger implements Messenger {
}
}
@Override
public void dispatchIncomingMessage(@NotNull PlayerConnection source, @NotNull String channel, byte @NotNull [] message) {
if (source == null) {
throw new IllegalArgumentException("Player source cannot be null");
}
if (message == null) {
throw new IllegalArgumentException("Message cannot be null");
}
channel = validateAndCorrectChannel(channel);
Set<PluginMessageListenerRegistration> registrations = getIncomingChannelRegistrations(channel);
for (PluginMessageListenerRegistration registration : registrations) {
try {
registration.getListener().onPluginMessageReceived(channel, source, message);
} catch (Throwable t) {
registration.getPlugin().getLogger().log(Level.WARNING,
String.format("Plugin %s generated an exception whilst handling plugin message",
registration.getPlugin().getDescription().getFullName()
), t);
}
}
}
/**
* Validates a Plugin Channel name.
*