Resource icon

Discontinued ServerCommunicator 1.0.0

Compatible API Versions
1.0.0-1.0.12
Broke on Nukkit API Update 1.0.13 and I dont have the sourcecode anymore

Some plugins need some data from another Servers. Bukkit has an API to get the data. Nukkit does not.
Thats why I made ServerCommunicator.
It gets this data from another Nukkit Server. The called server have to install this Plugin.

How to use as consumer:
Download the file and put it in your plugin folder. Thats it

How to use as developer:
First of all, you should add ServerCommunicator to your dependencies in your plugin.yml
To build a connection to another Server, create a NukkitConnection:
Java:
NukkitConnection example = new NukkitConnection("127.0.0.1", 19132);

NukkitConnection example2 = new NukkitConnection();
example2.setAddress("127.0.0.1");
example2.setPort(19132);
To get the data, use the following API:

Java:
NukkitConnection con = new NukkitConnection("127.0.0.1", 19132);

int onlineplayers = con.getOnlinePlayers();
int maxplayers = con.getMaxPlayers();
String motd = con.getMotd();
I would prefer to check if a connection is possible:
Java:
NukkitConnection con = new NukkitConnection("127.0.0.1", 19132);
if(con.canConnect()){
//do code
} else Server.getInstance().getLogger().critical("Connection to " + con.address + ":" + con.port + " not possible");


But the servers where you want to get the data from still need to install this plugin
Java:
package your.package;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

public class NukkitConnection {

//Code from ServerConnector

    String address;
    Integer port;

    public NukkitConnection() {
   
    }

    public NukkitConnection(String address, Integer port) {
   
        this.address = address;
        this.port = port;
   
    }

    public void setAddress(String address) {
   
        this.address = address;
   
    }

    public void setPort(Integer port) {
   
        this.port =  port;
   
    }
public int getOnlinePlayers() {
   
        net.minidev.json.JSONObject data;
        try {
            data = getDataPack();
            if(data.containsKey("OnlinePlayers")) {
                return Integer.parseInt(data.get("OnlinePlayers").toString());
            } else return -1;
        } catch (DOMException | ParserConfigurationException | ParseException | SAXException | IOException e) {
       
            e.printStackTrace();
            return -1;
        }
   
   
    }
public int getMaxPlayers() {
   
        net.minidev.json.JSONObject data;
        try {
            data = getDataPack();
            if(data.containsKey("MaxPlayers")) {
                return Integer.parseInt(data.get("MaxPlayers").toString());
            } else return -1;
        } catch (DOMException | ParserConfigurationException | ParseException | SAXException | IOException e) {
       
            e.printStackTrace();
            return -1;
        }
   
   
    }
public String getMotd() {

    net.minidev.json.JSONObject data;
    try {
        data = getDataPack();
        if(data.containsKey("Motd")) {
            return data.get("Motd").toString();
        } else return "-1";
    } catch (DOMException | ParserConfigurationException | ParseException | SAXException | IOException e) {
   
        e.printStackTrace();
        return "-1";
    }


}


    public JSONObject getDataPack() throws ParserConfigurationException, DOMException, ParseException, MalformedURLException, SAXException, IOException {
        String url = "http://" + this.address + ":" + this.port;
        JSONObject data = new JSONObject();
   
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new URL(url).openStream());
            data = (JSONObject) new JSONParser().parse(doc.getDocumentElement().getTextContent());
   
        return data;
   
    }
    public boolean canConnect() {
        try {
            getDataPack();
            return true;
        } catch (DOMException | ParserConfigurationException | ParseException | SAXException | IOException e) {
            return false;
        }
   
    }
}
  • Like
Reactions: BanZang
Author
BuddelStudios
Downloads
561
Views
2,008
First release
Last update
Rating
5.00 star(s) 1 ratings

More resources from BuddelStudios

  • LobbyNK
    LobbyNK
    The most powerful and customizable Lobbysystem on Cloudburst.org! 10K DOWNLOADS!! 🥰
  • WorldManager (Advanced MultiWorld Plugin)
    WorldManager (Advanced MultiWorld Plugin)
    A high-end multiworld plugin with more multiworld features than any other multiworld plugin 😁💖5K

Latest reviews

This API is very useful! It is also simple! Thank you! :D
BuddelStudios
BuddelStudios
No problemo :D
Top