[Script] Mediator 0.2.1 (Update)
-
CookieChinesewrote on 21 Feb 2025, 06:41 last edited by CookieChinese 3 Jul 2025, 17:53
Mediator (v0.2)
Author : mumy255
This is a JavaScript script designed for managing and transmitting data between different modules or components. Its core functionality is to store data in the form of key-value pairs and use a listener mechanism to respond to data changes.
Download:
Mediator 0.2.1 and Document.zip (for nextgen 0.27)
Mediator 0.2 and Document.zip (for nextgen 0.26)
Mediator 0.1 and Document.zip (for nextgen 0.26)
Getting the
Mediator
Module InstanceCode Example
var mediator; script.on("enable", () => { mediator = Client.getModuleManager().get("Mediator"); });
Step-by-Step Explanation
-
script.on("enable", callback)
:script.on("enable", ...)
is an event listener that triggers when the script is enabled. The callback function is executed once the script is enabled.- Inside this callback, the operation to fetch the
Mediator
module instance is performed.
-
Client.getModuleManager()
:Client.getModuleManager()
is an API that retrieves the client module manager. It is responsible for managing all loaded and enabled modules.
-
.get("Mediator")
:get("Mediator")
is used to fetch the instance of the module namedMediator
from the module manager."Mediator"
is the unique identifier for this module.- If the
Mediator
module is loaded and enabled, theget()
method will return the module's instance. If the module is not enabled or loaded, it may returnnull
.
-
Saving the Module Instance:
- The retrieved module instance is saved to the
mediator
variable, which can be used for subsequent operations.
- The retrieved module instance is saved to the
Notes
- The
mediator
will only be correctly assigned after the script is enabled, so the fetching operation should be inside thescript.on("enable", ...)
callback. - If the module doesn't exist or wasn't loaded correctly,
mediator
will benull
.
Extended Example
var mediator; script.on("enable", () => { mediator = Client.getModuleManager().get("Mediator"); if (mediator) { Client.displayChatMessage("Mediator module is now available."); } else { Client.displayChatMessage("Failed to get Mediator module."); } });
In this example, the code checks if
mediator
was successfully fetched, and displays a message based on the result.
Methods
putData(key: string, value: any | null): boolean
Adds data to the data store and notifies relevant listeners when the data changes.
Parameters:
Property Description Type key The key of the data item string
value The value of the data item, can be null
to clear the dataany/null
Return Value:
Returns a boolean indicating whether the operation was successful.Example:
const success = dataStore.putData("user", { name: "John", age: 30 }); Client.displayChatMessage(success); // true
getData(key: string): any | null
Retrieves the data for the specified key.
Parameters:
Property Description Type key The key of the data item string
Return Value:
Returns the data for the specified key, ornull
if the data does not exist.Example:
const user = dataStore.getData("user"); Client.displayChatMessage(user); // { name: "John", age: 30 }
clearData(key: string): boolean
Clears the data for the specified key.
Parameters:
Property Description Type key The key of the data item string
Return Value:
Returns a boolean indicating whether the operation was successful.Example:
const success = dataStore.clearData("user"); Client.displayChatMessage(success); // true
hasData(key: string): boolean
Checks if data exists for the specified key.
Parameters:
Property Description Type key The key of the data item string
Return Value:
Returns a boolean indicating whether data exists for the specified key.Example:
const hasData = dataStore.hasData("user"); Client.displayChatMessage(hasData); // true
registerListener(jsObject: { key: string; name: string }, callback: Listener): boolean
Registers a listener to monitor changes in data for the specified key.
Parameters:
Property Description Type jsObject An object containing key
(data item's key) andname
(listener name){ key: string; name: string }
callback The callback function for the listener, which accepts value
andstate
(PRE
orPOST
) as parameters(value: any/null, state: string) => void
Return Value:
Returns a boolean indicating whether the listener was successfully registered.Example:
const listener: Listener = (value, state) => { Client.displayChatMessage(`Data changed: ${value}, State: ${state}`); }; mediator.registerListener({ key: "user", name: "userListener" }, listener);
unregisterListener(key: string, name: string): boolean
Unregisters a listener to stop monitoring data changes for the specified key.
Parameters:
Property Description Type key The key of the data item string
name The name of the listener string
Return Value:
Returns a boolean indicating whether the listener was successfully unregistered.Example:
const success = mediator.unregisterListener("user", "userListener"); Client.displayChatMessage(success); // true
Example Usage
// Add data to the data store mediator.putData("user", { name: "John", age: 30 }); // Retrieve the data const user = mediator.getData("user"); console.log(user); // Output: { name: "John", age: 30 } // Check if the data exists const hasData = mediator.hasData("user"); console.log(hasData); // Output: true // Register a listener const listener: Listener = (value, state) => { Client.displayChatMessage(`Data changed: ${value}, State: ${state}`); }; mediator.registerListener({ key: "user", name: "userListener" }, listener); // Update the data and trigger the listener mediator.putData("user", { name: "Jane", age: 25 }); // The state will be "POST" // Unregister the listener mediator.unregisterListener("user", "userListener"); // Clear the data mediator.clearData("user");
-
1/1