#How do I expose an API ?
1 messages · Page 1 of 1 (latest)
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
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
To connect to my centralized Server that Manage Maintenance, Discord Bot and Launcher, WS for Instant
(SocketIO)
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
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 ?
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?
Example :
I have a plugin DiscordSync
And my API plugin (connected WS)
I want that the Plugin DiscordSync could receive event from WS and send) THROUGH my API
I want execute functions on my API by other plugins
There is no need for web sockets when sending data between plugins
I want the Event to do
A Plugin ==> My API ==> My WS Server to do Stuff
And
My WS ==> My API ==> A Plugin
Okay I see
WS are used for other things, forget I saiy that
I just want to execute functions on my api with my other plugins
So do you know Java?
Yep
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
Okay, and if I want to add random functions that have absolutely nothing to do with events ?
You can create a method in the api for sending data and having a consumer for when data is recieved
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
Simply create a BossBar manager class with methods that the plugins can use
How do the OTHERS plugins access the API class and executes its methods ?
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!");
how do I expose/get the API variable ?
i don't understand
Do you know what static is in java?
I think it means it cannot be modified and for a method it can be called without the need to instantiate the class
A static variable can be modified. Second part is true
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
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
I remember now where i saw this, in usage of LuckPerms API :
That's getRegistration
Yeah LuckPerms is a bit special
Yeah
You do not need to do it that way
The part in LP that interested me was
Yeah that's easy
Just create a static method called get in your class
In other plugins simply call Class.get()
But this is static ? So same pb
pb?
Then you do it this way
something is the plugin name btw
okay
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() ?
Use JavaPlugin.getPlugin(NewzenAPIPlugin.class).getAPI()