Skip to content

Plugins & Modules

Sceleratis edited this page Apr 7, 2020 · 12 revisions

Adonis expects any modules it's loading to return a function containing the module's code to run. Adonis will require the module, set the returned function's environment to a custom one containing all important variables, and will execute the function.

Plugins are loaded without yielding and will be loaded only after all of the core modules are loaded.

User defined modules (plugins)

Developers can create custom modules for Adonis to load without needing to alter Adonis's MainModule. Simply add modules to Adonis_Loader > Config > Plugins

Server modules should have names starting with "Server: " Client modules should have names starting with "Client: " Example: "Server: CustomChatHandler"

The module's name will be used by Adonis to determine if the module is a client plugin or a server plugin. The modules will be loaded after the "Core" modules finish loading.

Plugins have the same level of access as any of Adonis's "Core" modules. Because of this, plugin modules are free to add, remove, and change whatever they like. It is advised, however, that you avoid removing any tables, functions, or objects and instead replace them with "dummy" alternatives to avoid causing serious errors.

Example server plugin

The following is an example server plugin

server = nil -- Mutes warnings about unknown globals; This isn't required; All globals will be replaced when the function runs anyway
service = nil
return function()
        --// Add a new command to the Commands table at index "ExampleCommand"
	server.Commands.ExampleCommand = {
		Prefix = server.Settings.Prefix;	-- Prefix to use for command
		Commands = {"example"};	-- Commands
		Args = {"arg1"};	-- Command arguments
		Description = "Example command";	-- Command Description
		Hidden = true; -- Is it hidden from the command list?
		Fun = false;	-- Is it fun?
		AdminLevel = "Players";	    -- Admin level; If using settings.CustomRanks set this to the custom rank name (eg. "Baristas")
		Function = function(plr,args)    -- Function to run for command
			print("HELLO WORLD FROM AN EXAMPLE COMMAND :)")
			print("Player supplied args[1] "..tostring(args[1]))
		end
	}
end

In this example, we create a new command named "ExampleCommand" which can be ran using ":example" (Assuming the Prefix hasn't been changed)

In the same way we can add commands, we can use the same method to remove or alter commands. Instead of creating an entirely new command named ExampleCommand, the following would remove the command ":ff" from the script and make it so the :kick command still exists but does nothing. For a full list of commands and their indexes, refer to the Commands.lua module in Server > Core > Commands

return function()
	--// Remove ForceField from the commands table
	server.Commands.ForceField = nil;

	--// Change the kick command to do nothing:
	server.Commands.Kick.Function = function(plr, args)
		print(plr.Name .." tried to kick someone")
	end
end