Skip to content

Introduction: API

Ture Bentzin edited this page Feb 3, 2023 · 7 revisions

This page will introduce you to the general functions of the API

You should check out this Page before you start developing!

How to access the API?

API.get()

You should not save the API longer than for a method. If the API is not available an ApiException will be thrown!

Getting Started PaperAPI

Welcome to my Tutorial on how to use the PaperAPI. First, you need to import the PaperAPI via Maven:

<repository>
  <id>juligames-juligames</id>
  <name>Juligames Maven</name>
  <url>https://maven.juligames.net/juligames</url>
</repository>
<dependency>
  <groupId>net.juligames.core</groupId>
  <artifactId>PaperAPI</artifactId>
  <version>1.0</version>
</dependency>

For further information check out our Maven Repository

If you successfully imported the API, you are ready to create your Paper (Bukkit) Plugin as usual. Make sure that you add the following part to your plugin.yml

depend:
  - PaperCore

This will ensure that your Plugin is enabled after the PaperCore is ready.

Now you can access the API in your plugin. To visualize this I will give you some example Core that is executed in your onEnable() method.

    @Override
    public void onEnable() {
        // Plugin startup logic
        API.get().getAPILogger().info("Welcome to my JuliGamesCore Plugin!");
        MessageApi messageApi = API.get().getMessageApi();
        messageApi.broadcastMessage("key");
    }

Now you are able to use the PaperAPI!

MessageAPI with Paper

The following section will show you how to use the MessageAPI with Paper (PaperMessageRecipeient)

If you want to send a Message with the MessageAPI you need to provide a MessageRecipeint. In case of use on paper, the implementation provided is "PaperMessageRecipent". This Class is available with the PaperAPI.

Example of how to send a message with key "key" to a Player with the UUID: 7ca58747-8073-4aa3-b6bf-14addc9f776b.

        PaperMessageRecipient paperMessageRecipient = new PaperMessageRecipient(Bukkit.getPlayer("7ca58747-8073-4aa3-b6bf-14addc9f776b"));
        API.get().getMessageApi().sendMessage("key",paperMessageRecipient);

Important to notice that the PaperMessageRecipeint#new takes a CommandSender. For further information on how to get and handle a CommandSender please check out the Bukkit documentation. What this means is, that you can use the PaperMessageRecipent for Players, and the Console.

The Console will always display the Messages in the Default Locale (EN_US)

MessageAPI with Velocity

The following section will show you how to use the MessageAPI with Velocity

If you want to send a Message with the MessageAPI you need to provide a MessageRecipeint. In the case of the use of velocity, there are some different options on how to get your MessageRecipient.

Example of how to send a message with key "key" to a Player with the UUID: 7ca58747-8073-4aa3-b6bf-14addc9f776b using the VelocityPlayerMessageRecipent

        Player player = proxyServer.getPlayer("7ca58747-8073-4aa3-b6bf-14addc9f776b").get();
        VelocityPlayerMessageRecipient velocityPlayerMessageRecipient = new VelocityPlayerMessageRecipient(player);
        API.get().getMessageApi().sendMessage("key",velocityPlayerMessageRecipient);

The use of this class is suggested for Players because it can handle the locales of the players.

In general, you could also use the AudienceMessageRecipeint provided by AdventureAPI. This can receive Messages from a wide range of different objects including Players, the Paper Console, Paper Servers, and a lot more. For further information about Audiences check out the Adventure Documentation

Here is an example of sending a Message to a Server called "Lobby-1":

        RegisteredServer server = proxyServer.getServer("Lobby-1").get();
        ServerMessageRecipient serverMessageRecipient = new ServerMessageRecipient(server);
        API.get().getMessageApi().sendMessage("key",serverMessageRecipient);

Here is the same function but utilizing the AdventureAPI (which is a lot more effort but possible)

        RegisteredServer server = proxyServer.getServer("Lobby-1").get();
        Supplier<String> localeSupplier = () -> API.get().getHazelDataApi().getMasterInformation().get("default_locale");
        AudienceMessageRecipient audienceMessageRecipient = new AudienceMessageRecipient("Lobby-1", localeSupplier,server);
        API.get().getMessageApi().sendMessage("key",audienceMessageRecipient);

Using JDBI with the JuliGamesCore

The JuliGamesCore will provide a fully functional JDBI Instance to you via the API. This Instance is accessible from all Core Instances. You are free to use the inbuild "Beans and DAOs".

If you want to use the endless possibilities provided by JDBI through the Core, then consider reading carefully through the JDBI Documentation! If you encounter issues with JDBI´s API, you will not receive support here via the JuliGamesCore (except it is related to the connection to the database)

You obtain your JuliGamesCore JDBI Instance as follows:

JDBI jdbi = API.get().getSQLManager().getJDBI();

You can also create a simple Handle for your first tests with JDBI as follows:

        try (Handle sql = API.get().getSQLManager().openHandle()) {
            Query query = sql.createQuery("SELECT name FROM players");
            ResultIterable<Map<String, Object>> maps = query.mapToMap(); //each row = one map
            List<String> names = query.mapTo(String.class).list();
        }

Or even use the lambda method for this and extract the result:

        List<String> selectNameFromPlayers = API.get().getSQLManager().useHandle(handle -> {
            Query query = handle.createQuery("SELECT name FROM players");
            ResultIterable<Map<String, Object>> maps = query.mapToMap(); //each row = one map
            return query.mapTo(String.class).list();
        });