org.bukkit.plugin.messaging.PluginMessageListener Java Examples

The following examples show how to use org.bukkit.plugin.messaging.PluginMessageListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sends a plugin message.
 *
 * Example usage using the "GetServers" bungee plugin message channel via an overload:
 * <code>
 *     Utils.sendPluginMessage("BungeeCord", r -> "GetServers".equals(r.readUTF()), "GetServers")
 *     			.thenAccept(response -> Bukkit.broadcastMessage(response.readUTF()) // comma delimited server broadcast
 *     			.exceptionally(ex -> {
 *     			 	Skript.warning("Failed to get servers because there are no players online");
 *     			 	return null;
 *     			});
 * </code>
 *
 * @param player the player to send the plugin message through
 * @param channel the channel for this plugin message
 * @param messageVerifier verifies that a plugin message is the response to the sent message
 * @param data the data to add to the outgoing message
 * @return a completable future for the message of the responding plugin message, if there is one.
 * this completable future will complete exceptionally if the player is null.
 */
public static CompletableFuture<ByteArrayDataInput> sendPluginMessage(Player player, String channel,
		Predicate<ByteArrayDataInput> messageVerifier, String... data) {
	CompletableFuture<ByteArrayDataInput> completableFuture = new CompletableFuture<>();

	if (player == null) {
		completableFuture.completeExceptionally(new IllegalStateException("Can't send plugin messages from a null player"));
		return completableFuture;
	}

	Skript skript = Skript.getInstance();
	Messenger messenger = Bukkit.getMessenger();

	messenger.registerOutgoingPluginChannel(skript, channel);

	PluginMessageListener listener = (sendingChannel, sendingPlayer, message) -> {
		ByteArrayDataInput input = ByteStreams.newDataInput(message);
		if (channel.equals(sendingChannel) && sendingPlayer == player && !completableFuture.isDone()
				&& !completableFuture.isCancelled() && messageVerifier.test(input)) {
			completableFuture.complete(input);
		}
	};

	messenger.registerIncomingPluginChannel(skript, channel, listener);

	completableFuture.whenComplete((r, ex) -> messenger.unregisterIncomingPluginChannel(skript, channel, listener));

	// if we haven't gotten a response after a minute, let's just assume there wil never be one
	Bukkit.getScheduler().scheduleSyncDelayedTask(skript, () -> {

		if (!completableFuture.isDone())
			completableFuture.cancel(true);

	}, 60 * 20);

	ByteArrayDataOutput out = ByteStreams.newDataOutput();
	Stream.of(data).forEach(out::writeUTF);
	player.sendPluginMessage(Skript.getInstance(), channel, out.toByteArray());

	return completableFuture;
}
 
Example #2
Source File: MessageChannel.java    From AACAdditionPro with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Registers the incoming channel for a certain {@link PluginMessageListener}
 */
public void registerIncomingChannel(final PluginMessageListener listener)
{
    Bukkit.getMessenger().registerIncomingPluginChannel(AACAdditionPro.getInstance(), this.getChannel(), listener);
}
 
Example #3
Source File: MessageChannel.java    From AACAdditionPro with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Unregisters the incoming channel for a certain {@link PluginMessageListener}
 */
public void unregisterIncomingChannel(final PluginMessageListener listener)
{
    Bukkit.getMessenger().unregisterIncomingPluginChannel(AACAdditionPro.getInstance(), this.getChannel(), listener);
}