ScriptableMC-Engine icon indicating copy to clipboard operation
ScriptableMC-Engine copied to clipboard

How to access/depend on other Plugins

Open Lubjan opened this issue 4 years ago • 2 comments

So I've started using SMC recently and wanted to depend on some functionality of some other plugins, in this specific case LuckPerms.

I'm not quite sure how I would retrieve an instance of it, their wiki shows 3 ways on how to do it, that said I searched for the getProvider method (inside the available methods from the TypeScript example lib) found a few, but they weren't of any help.

So my questing is, how would I depend on other plugins? Do I have to convert the plugins Java source code to TypeScript interfaces and classes and maybe more or are there easier ways?

Thanks in advance

Lubjan avatar Mar 21 '21 14:03 Lubjan

Hey, sorry about the delay. I have been super busy with work but finally have a bit of free time.

Yes it is possible to get any loaded Java class from inside the JavaScript engine. Unfortunately since there is no TypeScript type definitions for the LuckPerms API it's a little more difficult and there is no intelli-sense/autocomplete in vs code. However it looks like they have some decent JavaDocs that should help you figure out methods if needed.

I am in the process of creating a TypeScript definition exporter that can run on a server as a plugin or as a standalone jar that can generate typescript definitions from a running server or from specific jar files. I'll let you know when this is ready for use and it should make accessing any Java class much easier.

As for now you can still access java classes manually via the Java object provided by the JavaScript engine. Here is an example that should get you started for LuckPerms. Let me know if you need any help.

import JsPlugin from '../lib/JsPlugin.js';

// We have to tell the TypeScript compiler about our global Java object provided by GraalVM to access Java classes.
// More info about this object here: https://github.com/oracle/graaljs/blob/master/docs/user/JavaScriptCompatibility.md#java
declare var Java: any;

// Get an instance of the Java class net.luckperms.api.LuckPermsProvider.
// This is an un-initialized class, with it we can access static fields/methods and create a new instance.
// We set the type to any since we don't have a defined type for the object.
const LuckPermsProvider: any = Java.type('net.luckperms.api.LuckPermsProvider');

export default class LuckPermsTestPlugin extends JsPlugin {

    // Declare a private field to hold the LuckPerms API instance
    // As per usual type any since we don't have declared types.
    private luckPermsApi: any;

    onLoad() {
        // Get an instance of the LuckPerms API object
        this.luckPermsApi = LuckPermsProvider.get();
    }

    onEnable() {
        // Do stuff with this.luckPermsApi which will be an instance of LuckPerms
        // More info here: https://javadoc.io/doc/net.luckperms/api/latest/net/luckperms/api/LuckPerms.html
    }
}

astorks avatar Jun 13 '21 19:06 astorks

Thanks for the answer, I think I'll wait for the the exporter then ^^ I used Java for the plugin for now

Lubjan avatar Jun 13 '21 20:06 Lubjan