Skip to content

Nameless 2.0 Module Developer Documentation

Tadhg Boyle edited this page Oct 25, 2020 · 24 revisions

Greetings, developer!

This guide assumes intermediate PHP knowledge. If you do not know PHP and want to make NamelessMC modules, we highly recommend you learn the basics first - rather than jumping right into this.

All modules require at least one file to work, named init.php. This file must be placed in modules/<your module name>/init.php. Each time a page loads, NamelessMC will detect this file and execute the code inside of it automatically. It is recommended to separate your init.php file and your module's main class, most people make the main class in a file called module.php.

In your init.php file, you must create a new instance of your module's main class, which must extend the Module class. For example:

<?php
// Demo module by Aberdeener
// init.php file

require_once ROOT_PATH . '/modules/DemoModule/module.php';
$module = new Demo_Module();
<?php
// Demo module by Aberdeener
// module.php file
// Absolute minimum in terms of code, this has no functionality

class Demo_Module extends Module {

    public function __construct() {

        $name = 'Demo Module';
	$author = 'Aberdeener';
	$module_version = '0.0.1';
	$nameless_version = '2.0.0-pr8';

	parent::__construct($this, $name, $author, $module_version, $nameless_version);
    }

    public function onInstall() {
    }

    public function onUninstall() {
    }

    public function onEnable() {
    }

    public function onDisable() {
    }

    public onPageLoad($user, $pages, $cache, $smarty, $navs, $widgets, $template) {
    }

}

You will notice that when your module's main class extends the Module class, it is required to have some functions in it. These functions are as follow:

  • onInstall();
  • onUninstall(); (Not used yet:tm:)
  • onEnable();
  • onDisable();
  • onPageLoad();

Their names are fairly understandable, they will each be called when that action happens. Obviously, most of your module's core code will be in the onPageLoad(); function, as that is called every time a page is loaded.

In your module's constructor, this is where you will want to:

  • Initiate your module's information variables (name, author, version, etc)
  • Add your pages to NamelessMC's page system
  • Set your module's required variables which are not provided in the onPageLoad(); function (Endpoints, Queries, etc)

In the onPageLoad(); function, you will want to:

  • Add your permissions to NamelessMC's permission system
  • Add a sitemap method
  • Add your pages to the navigation bars
  • Initiate your module's widgets
  • Initiate your module's API endpoints
  • Add your module's hooks to NamelessMC's hook system

More detail about your module's constructor:

  • There are no required parameters.
  • It must construct the parent Module class, and pass:
    • itself ($this)
    • the name of the module
    • the version of the module
    • the minimum NamelessMC version required (In the format 2.0.0-prX where X is the pre-release number), this is used to warn users if they have modules which may not work with their version of NamelessMC.
    • If you do not construct the parent class, your module will not be be registered and all your code will seem to have no effect.

Useful classes and their functions:

  • Cache
    • Store data in text files for specific periods of time for quicker and easier access when compared to the database.
    • Useful functions:
      • setCache($name);
        • Set your current $cache file. Useful for organizing data into different files, rather than one file for all information.
      • isCached($key);
        • Returns true/false depending if $key is cached in the current file (whatever was last set as the cache via setCache($name);).
      • retrieve($key);
        • Retrieves object under the key $key in the current cache file, or null if it does not exist.
      • store($key, $value, $expiration = 0);
        • Stores $value object with the key $key and sets it to expire in $expiration seconds in the current cache file.
        • If $expiration is not provided, it will never expire.
  • Pages
    • things
  • Language
    • Allows modules to get/set phrases in a specific locale.
    • Useful functions:
      • get($file, $term);
        • Returns the string with the key $term in the specified language file $file.
        • If the $term is not in the $file, it will try to find it in the EnglishUK file (as this is the default language used by NamelessMC developers. If it is not in the EnglishUK file, it will return "Term $term not set".
      • set($file, $term, $value);
        • Set (or update) the $term in $file to $value.
        • Used internally for email message editing via StaffCP.
  • Widgets
    • Allows modules to add widgets to NamelessMC's widget system.
    • Useful functions:
      • add($widget);
        • Add $widget to the widget system.
        • $widget must be an instance of a class which extends the WidgetBase class.
      • getPages($name);
        • Returns an array of page names that the widget named $name is enabled on.
  • User (Current user instance is accessible globally via $user)
  • Endpoints