Coder Social home page Coder Social logo

kratess / bungeechannelapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leonardosnt/bungeechannelapi

0.0 0.0 0.0 52 KB

An easy way to work with the "BungeeCord Plugin Messaging Channel" (https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/)

Home Page: https://leonardosnt.github.io/BungeeChannelApi/io/github/leonardosnt/bungeechannelapi/BungeeChannelApi.html

License: MIT License

Java 100.00%

bungeechannelapi's Introduction

BungeeChannelApi

Javadocs

Development builds:

Build Status

Maven dependency:

How to include BungeeChannelApi into your maven project:

    <repositories>
        <repository>
            <id>codemc-repo</id>
            <url>https://repo.codemc.org/repository/maven-public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>io.github.leonardosnt</groupId>
            <artifactId>bungeechannelapi</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Remember to include/relocate the library into your final jar, example:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <relocations>
                        <relocation>
                            <pattern>io.github.leonardosnt.bungeechannelapi</pattern>
                            <shadedPattern>YOUR.PLUGIN.PACKAGE.libs.bungeechannelapi</shadedPattern>
                        </relocation>
                    </relocations>
                </configuration>
            </plugin>
        </plugins>
    </build>
Some examples:

BungeeChannelApi uses CompletableFuture's, so make sure you know how it works before using this.

Instantiate:

// Create new instance for this plugin.
// Same of "new BungeeChannelApi(this);"
BungeeChannelApi api = BungeeChannelApi.of(this); // this = Plugin instance.

Get online player count:

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:

api.getPlayerList("ALL")
  .whenComplete((result, error) -> {
    sender.sendMessage("§aPlayers online: " + result.stream().collect(Collectors.joining(", ")));
  });

Send a message to a player:

api.sendMessage("leonardosnt", "§cHello world");

Custom subchannels/messages using Forward

Send:

byte[] ourData = "Hello World".getBytes();
api.forward("serverName", "example", ourData);

Listen:

// 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.

public 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 servers

      Player 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'
      }
    }

}

bungeechannelapi's People

Contributors

leonardosnt avatar sgdc3 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.