https://github.com/leonardosnt/bungeechannelapi
An easy way to work with the "BungeeCord Plugin Messaging Channel" (https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/)
https://github.com/leonardosnt/bungeechannelapi
api bungeecord channel
Last synced: 5 months ago
JSON representation
An easy way to work with the "BungeeCord Plugin Messaging Channel" (https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/)
- Host: GitHub
- URL: https://github.com/leonardosnt/bungeechannelapi
- Owner: leonardosnt
- License: mit
- Created: 2017-03-05T23:39:00.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-03T16:52:39.000Z (about 7 years ago)
- Last Synced: 2025-04-01T14:56:08.807Z (6 months ago)
- Topics: api, bungeecord, channel
- Language: Java
- Homepage: https://leonardosnt.github.io/BungeeChannelApi/io/github/leonardosnt/bungeechannelapi/BungeeChannelApi.html
- Size: 50.8 KB
- Stars: 44
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BungeeChannelApi
[Javadocs](https://ci.codemc.org/job/leonardosnt/job/BungeeChannelApi/javadoc)
##### Development builds:
[](https://ci.codemc.org/job/leonardosnt/job/BungeeChannelApi)
##### Maven dependency:
How to include BungeeChannelApi into your maven project:
```xml
codemc-repo
https://repo.codemc.org/repository/maven-public/
io.github.leonardosnt
bungeechannelapi
1.0.0-SNAPSHOT
```Remember to include/relocate the library into your final jar, example:
```xml
org.apache.maven.plugins
maven-shade-plugin
3.1.1
package
shade
io.github.leonardosnt.bungeechannelapi
YOUR.PLUGIN.PACKAGE.libs.bungeechannelapi
```##### Some examples:
BungeeChannelApi uses [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html)'s, so make sure you know how it works before using this.
Instantiate:
```java
// Create new instance for this plugin.
// Same of "new BungeeChannelApi(this);"
BungeeChannelApi api = BungeeChannelApi.of(this); // this = Plugin instance.
```Get online player count:
```java
api.getPlayerCount("ALL")
.whenComplete((result, error) -> {
sender.sendMessage("§aThere are " + result + " players online on all servers.");
});
```Get a list of players connected on a certain server, or on ALL the servers:
```java
api.getPlayerList("ALL")
.whenComplete((result, error) -> {
sender.sendMessage("§aPlayers online: " + result.stream().collect(Collectors.joining(", ")));
});
```Send a message to a player:
```java
api.sendMessage("leonardosnt", "§cHello world");
```#### Custom subchannels/messages using [Forward](https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#forward)
Send:
```java
byte[] ourData = "Hello World".getBytes();
api.forward("serverName", "example", ourData);
```Listen:
```java
// global listener (for all subchannels)
api.registerForwardListener((channelName, player, data) -> {
Bukkit.broadcastMessage(channelName + " -> " + Arrays.toString(data));
});// specific channel
api.registerForwardListener("example", (channelName, player, data) -> {
// in this case channelName is "example"
Bukkit.broadcastMessage("Message sent using forward: " + new String(data));
});
```##### Comparison (without BungeeChannelApi)
This code does the same thing that the first example.
```javapublic class Test extends JavaPlugin implements PluginMessageListener {
@Override
public void onEnable() {
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
}// Send a message requesting player count
public void requestPlayerCount() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("PlayerCount");
out.writeUTF("ALL"); // all serversPlayer player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null);
player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("BungeeCord")) return;
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
if (subchannel.equals("PlayerCount")) {
String server = in.readUTF();
int count = in.readInt();
// do something with 'count'
}
}}
```