#How do I expose an API ?

1 messages · Page 1 of 1 (latest)

marble salmon
#

As said in title, I want to expose an API which can later be accessed by other plugins

So :
How do I expose an API ?
How to I access it from other Plugins ?

lunar hill
#

break down your project into 2 modules: api and plugin. In api module, you make interfaces to interact with your plugin, in plugin module - implement them

marble salmon
#

I want a plugin that Acts as an API (nothing else) for use in only my Server

#

I want in this plugin a WebSocket connection

#

That others of my plugins can use

quick fjord
#

Uh why a websocket

#

Do you want to send data between servers?

marble salmon
#

(SocketIO)

quick fjord
#

Ah ok

#

So what part do you need help with

lunar hill
#

Ah

#

Thats what you meant by api

marble salmon
#

In my API plugin It will establish a connection but this I will search later

I want others of my plugins can do things like
API.sendSocketEvent('eventName', 'eventData')

#

Then my API will send the event trough it's WS connection

#

To prevent using as many WS connection as I have plugins because since ALL my launchers will be connected to ONE instance of SocketIO Server I don't want also one per server per plugin

marble salmon
# quick fjord So what part do you need help with

I need the way to expose functions that other plugins can call and that will be executed on my API plugin (Also the way to access functions exposed, I saw something about getPlugin() but no more infos)

#

Anyone ?

#

@quick fjord ?

quick fjord
# marble salmon <@!437524407435395082> ?

I'm not an expert with WebSockets but do I understand this correctly: You want something else to send a call through the websocket that then will trigger a method in your plugin

#

Or do you want the plugin to send events through the web socket?

marble salmon
#

I want execute functions on my API by other plugins

quick fjord
#

There is no need for web sockets when sending data between plugins

marble salmon
quick fjord
#

Okay I see

marble salmon
quick fjord
#

So do you know Java?

marble salmon
quick fjord
#

Okay

#

So what I would do is use the spigot event api

#

When you recieve data through your web socket pass that data in to your own event and call it. Then in your other plugins simply use an event listener and read the data

marble salmon
#

Okay, and if I want to add random functions that have absolutely nothing to do with events ?

quick fjord
#

You can create a method in the api for sending data and having a consumer for when data is recieved

marble salmon
#

I mean if I have a complex function that I want to use in many plugins that must be executed by the API

#

Like show a custom Boss Bar (or something else idk)

#

To have for example all plugins can edit the same BossBar for example

quick fjord
#

Simply create a BossBar manager class with methods that the plugins can use

marble salmon
#

How do the OTHERS plugins access the API class and executes its methods ?

quick fjord
#

There are many ways to go about this the easiest way is to create a static access method for the boss bar manager class

#

Example API.getBossbarManager().setText("Bossbar!");

marble salmon
#

how do I expose/get the API variable ?

quick fjord
#

It's just a class

#

Or if you want a static variable

marble salmon
#

i don't understand

quick fjord
#

Do you know what static is in java?

marble salmon
#

I think it means it cannot be modified and for a method it can be called without the need to instantiate the class

quick fjord
#

A static variable can be modified. Second part is true

marble salmon
#

But if my class isn't instantiated there will be two bossbar or an error as they will want to create a bossbar with same name

#

I need to get an Instance of my Class, that were instantiated by the API

#

I saw this idk where but they used server.getPluginManager().getPlugin('something') but I don't rememeber exactly

quick fjord
#

Yeah you can do that

#

That will return the instance of the main plugin class of the specified plugin

#

This is another way of doing it

marble salmon
#

I remember now where i saw this, in usage of LuckPerms API :

#

That's getRegistration

quick fjord
#

Yeah LuckPerms is a bit special

marble salmon
quick fjord
#

You do not need to do it that way

marble salmon
quick fjord
#

Yeah that's easy

#

Just create a static method called get in your class

#

In other plugins simply call Class.get()

marble salmon
#

But this is static ? So same pb

quick fjord
#

pb?

marble salmon
#

problem

#

I want an instance not a static

quick fjord
#

something is the plugin name btw

marble salmon
#

So

#

I can do this ? (Newzen is the name of my Server)

#

⬆️ NewzenAPIPlugin.java => Main Class plugin.yml

#

And NewzenAPI.java :

#

then I put my functions inside NewzenAPI.java and i access by other plugins by

Bukkit.getServer().getPluginManager().getPlugin(NewzenAPIPlugin.class).getAPI() ?

quick fjord
#

Use JavaPlugin.getPlugin(NewzenAPIPlugin.class).getAPI()