en
Switch Language
  1. PowerNukkitX
  2. Get Started
  3. Config

    1. server.properties
    2. nukkit.yml
    3. pnx-cli-config.ini
    4. Anti-xray
    5. Resource Encryption
    6. Compression Acceleration
    7. PNX with WaterDogPE
    8. Flat World
  4. FAQ

    1. To Newcomers
    2. How to use PNX-CLI
    3. Common Questions
    4. Terra Questions
    5. Spawn-point Questions
    6. Convert Worlds
  5. For Devs

    1. Java

      1. Goto JavaDoc
      2. Mod API

        1. Custom Block
        2. Custom Item
        3. Custom Entity
        4. Custom Enchantment
      3. Entity AI

        1. Behavior
        2. Memory
        3. Sensor
        4. Motion Controller
        5. Navigator
        6. Behavior Group
        7. Work Cycle
    2. JavaScript

      1. setup dev env
      2. Base LLSELib dev
      3. Hello World
      4. Basic knowledge
      5. Event Listener
      6. FAQ
    3. Resources

      1. Event List
      2. IDs(block & item)

HelloWorld

In this chapter, I will take you to create your first js plugin.

Plugin Format

A JS plugin is the contents of a folder starting with @ in the plugins folder.
Each JS plugin needs to contain the plugin.yml configuration file and at least one js file, and can contain any number of arbitrary files and folders.
Specific formatting instructions can be found in the plugin format section. It is highly recommended that you read the plugin format chapter carefully

Create plugin.yml

Again, the @dir folder refers to the plugin folder you created

Create the plugin.yml configuration file in the @dir folder and fill in the basic information of the plugin in it.

name: HelloWorld
main: index.js
version: "1.0.0"
api: ["1.0.13"]
load: POSTWORLD
author: Please replace the characters in this line with your name
description: The first HelloWorld plugin

Create js file

We just specified our js entry file called index.js in the main field in plugin.yml, now we create a js file called index.js in the @dir folder.

console.log("Hello World")

export function main() {
	console.warn("Hello World")
}

export function close() {
	console.error("Hello World");
}

The above code will output Hello World in the console as INFO, WARN and ERROR when the server starts, when the server starts initializing the plugin, and when the server is closed respectively.

PNX JS adheres to the ESM (EcmaScript Module) specification, so you need to write in compliance with the ESM specification
Functions that need to be called externally should include the export keyword
You can find more information about ESM at here

Some explanations

The entire JS code will be loaded when the server is first started, so be sure to keep in mind that **most game-related content such as listening for events, generating items, manipulating the map, controlling creatures, etc. cannot be done at the very beginning! **

The export main function will be called after the PNX server has been initialized, and you can perform game-related operations when this function is called and thereafter.
The export close function will be called when the PNX server is shut down or when your plugin is uninstalled, when you should do some cleanup work.


© PowerNukkitX Dev team