Plugin with a script engine?

First, thanks everyone for making powerNukkit available! I am looking forward to being able to contribute something useful.

My aim is to have a plugin (based on the Mobs plugin) that uses an external script. Two things. First, is there an example somewhere of how to access a text file on the local file system from code in a jar file? The getResource(…) gets things from within the jar but I want others to provide their own data without having to rebuild the jar file. And second, the text file contains (among other things) some javascript. In the past I used the rhino interpreter that I could add via my local class path but I am not sure how I would make that jar file available to the code in my powerNukkit plugin. By default I will unpack the rhino jar into my plugin directory and then re jar it all, but perhaps there is a better way? I included it in the classpath next to the powerNukkit jar but get a ClassNotFoundException.

1 Like

You have a lot of questions, every time I start to answer I have to stop because something that requires my attention distracted me lol. So I will be replying in parts.

Everybody will love having you involved with the project :smiley:
We talk a lot on discord too. You are invited to join our discussions :stuck_out_tongue:

So you are going to make a scripting plugin that allows server owners to customize their servers with scripts? That’s great! I’m sure people will love that!

The file management is the same as any Java program. You can get a folder for your plugin with Plugin.getDataFolder().

Here’s an example:

public class YourPlugin extends PluginBase {
    @Override
    public void onEnable() {
        try {
            loadScripts();
        } catch (IOException e) {
            getLogger().emergency("Could not load "+getName()+". The plugin will be disabled.", e);
            getPluginLoader().disablePlugin(this);
            return;
        }
        // More stuff?
    }
    
    private void loadScripts() throws IOException {
        Path scriptsFolder = Files.createDirectories(getDataFolder().toPath().resolve("scripts"));
        HumanStringComparator comparator = HumanStringComparator.getInstance();
        AtomicLong counter = new AtomicLong();
        Files.list(scriptsFolder)
                .filter(path -> path.getFileName().toString().toLowerCase(Locale.ENGLISH).endsWith(".script"))
                .sorted((a,b)-> comparator.compare(a.getFileName().toString(), b.getFileName().toString()))
                .forEachOrdered(script-> {
                    try {
                        getLogger().info("Loading "+script);
                        loadScript(script);
                        counter.incrementAndGet();
                    } catch (Exception e) {
                        getLogger().error("Could not load the script "+script, e);
                    }
                });
        getLogger().info("Loaded "+counter.get()+" script(s)");
    }
    
    private void loadScript(Path scriptPath) throws IOException {
        // TODO Implement
        throw new NotImplementedException();
    }
}

You could use the maven shade plugin, like what we do in PN to include all libraries in a single jar, but you will include only the rhino lib. Here’s an example:

<project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <includes>
                                   <include>org.mozilla:rhino</include>
                                </includes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
    </build>
    ...
</project>

Thanks for the guidance - got side tracked but am back on it now :slight_smile:

1 Like