Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extend PluginBase? #39

Closed
dhalucario opened this issue Dec 26, 2022 · 3 comments
Closed

How to extend PluginBase? #39

dhalucario opened this issue Dec 26, 2022 · 3 comments

Comments

@dhalucario
Copy link

Player.hidePlayer() expects a Plugin as it's first argument, the other version without is deprecated.
I would like to follow the best practice of course. How am I supposed to extend PluginBase from Javascript?

I tried doing the following:

const PluginBase = core.type('org.bukkit.plugin.PluginBase');
class TestPlugin extends PluginBase {}
const TEST_PLUGIN = new TestPlugin();

...

// Later in my event handler:
targetPlayer.hidePlayer(TEST_PLUGIN, player);

But this seems to cause the following error:
[19:12:21 WARN]: TypeError: protoParent is neither Object nor Null

@Losin6450
Copy link

Losin6450 commented Dec 26, 2022

if you don't need a specific plugin you can just use core.plugin, but if you want to make your own instance of PluginBase you can use Java.extend

Example:

const PluginBase = Java.type("org.bukkit.plugin.PluginBase")
const TestPlugin = Java.extend(PluginBase, {
    onEnable: ()=>{
        console.log("New Plugin enabled")
    },
    isEnabled:()=>{
        return true
    }
})
const plugin = new TestPlugin()
const player = core.type("org.bukkit.Bukkit").getPlayerExact("Losin6450_2nd")
player.hidePlayer(plugin, player)

But i wouldn't recommend using Java.extend to make an instance of PluginBase as you could just use core.plugin

@dhalucario
Copy link
Author

dhalucario commented Dec 26, 2022

Hey, thank you for your help. I have figured out some similar solution. I just noticed two issues:

  1. onEnable doesn't seem to run at all. (At least console.log doesn't seem to work in that state)
  2. You need to implement getDescription

This is the code I used:

const PluginBase = core.type('org.bukkit.plugin.PluginBase'),
const PluginDescriptionFile = core.type('org.bukkit.plugin.PluginDescriptionFile');

const pluginDescriptionFile = new PluginDescriptionFile('Test Plugin', '1.0', 'TestPlugin');

const TestPlugin = new PluginBaseAdapter({
    getDescription: () => {
        return pluginDescriptionFile;
    },
    onEnable: () => {
        console.log("Test");
    },
    isEnabled: () => {
        return true;
    }
});

But yeah, core.plugin is prolly the better and easier solution.

Notes for other people who might stumble across this:
https://www.graalvm.org/latest/reference-manual/js/JavaInteroperability/
https://hub.spigotmc.org/javadocs/spigot/org/bukkit/plugin/PluginBase.html

@Yu-Vitaqua-fer-Chronos
Copy link
Member

The reason why onEnable doesn't fire is probably because the plugin isn't being loaded by a plugin manager, so it just never runs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants