Last active
August 12, 2018 06:03
-
-
Save Pikachu920/a8e6f0ad83ce2d9529d906983ca183ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static CompletableFuture<ByteArrayDataInput> sendPluginMessage(String channel, String... data) { | |
| return sendPluginMessage(Iterables.getFirst(Bukkit.getOnlinePlayers(), null), channel, data); | |
| } | |
| public static CompletableFuture<ByteArrayDataInput> sendPluginMessage(Player player, String channel, String... data) { | |
| CompletableFuture<ByteArrayDataInput> completableFuture = new CompletableFuture<>(); | |
| if (player == null) { | |
| completableFuture.completeExceptionally(new IllegalStateException("Can't send plugin messages without a player")); | |
| return completableFuture; | |
| } | |
| Bukkit.getMessenger().registerIncomingPluginChannel(Skript.getInstance(), channel, new PluginMessageListener() { | |
| { | |
| ByteArrayDataOutput out = ByteStreams.newDataOutput(); | |
| Stream.of(data).forEach(out::writeUTF); | |
| player.sendPluginMessage(Skript.getInstance(), channel, out.toByteArray()); | |
| } | |
| @Override | |
| public void onPluginMessageReceived(String sendingChannel, Player sendingPlayer, byte[] message) { | |
| if (channel.equals(sendingChannel) && sendingPlayer == player && !completableFuture.isDone()) { | |
| completableFuture.complete(ByteStreams.newDataInput(message)); | |
| } | |
| } | |
| }); | |
| return completableFuture; | |
| } | |
| @Override | |
| protected void execute(Event e) { | |
| String server = this.server.getSingle(e); | |
| if (server == null) | |
| return; | |
| // the message channel is case sensitive so let's fix that | |
| Utils.sendPluginMessage("BungeeCord", "GetServers") | |
| .thenAccept(response -> | |
| Stream.of(response.readUTF().split(", ")) | |
| .filter(s -> s.equalsIgnoreCase(server)) | |
| .findFirst() | |
| .ifPresent(s -> { | |
| for (Player player : players.getArray(e)) | |
| Utils.sendPluginMessage(player, "BungeeCord", "Connect", s); | |
| }) | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment