API Specstable
2 min read time

Documentation

$ - Magic imports

An overview of magic imports in PixelScript and how they facilitate module loading.

apimodulesimport

You're a developer, and therefore inherently lazy in one way or another. You'll be needing Java classes from time to time (wether that'd be the bukkit Material enum, or your own plugin's UserManager service) and importing them constantly can be a drag.

Though you can just make a simple js file that exports them in one go (which is the recommended way), PixelScript also gives you the $ global object to cheat your way through imports.

The class-searchcontext.yml file lists a few packages that will be searched by the $ global when you try to import a class through it, with the default config being

search-packages: - 'org.bukkit' - 'com.destroystokyo.paper' - 'org.spigotmc'

Then, you can find a class by trying to access it through the $ global, like so:

const buildableBlocks = [ $.Material.OAK_PLANKS, $.Material.STONE_BRICKS, $.Material.GLASS_PANE, ];

This will search through the listed packages for the Material class, and return it if found. The initial invocation of this can be a bit slow (depending on how many packages you have listed), but subsequent calls are cached for speed, but will never be as fast as simply having a defined global. Still, I like to use this for code paths that can spare the cost of a map lookup here and there. Try adding your own framework or plugin packages to the list for easy access to your own classes!

Documentation in early stages. Not meant for public consumption.