Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

LiquidBounce Forum

  1. Home
  2. Resources
  3. Scripts
  4. [Script] Mediator 0.2.1 (Update)

[Script] Mediator 0.2.1 (Update)

Scheduled Pinned Locked Moved Scripts
1 Posts 1 Posters 559 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CookieChineseC Offline
    CookieChineseC Offline
    CookieChinese
    wrote on last edited by CookieChinese
    #1

    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 Instance

    Code Example

    var mediator;
    script.on("enable", () => {
        mediator = Client.getModuleManager().get("Mediator");
    });
    

    Step-by-Step Explanation

    1. 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.
    2. Client.getModuleManager():

      • Client.getModuleManager() is an API that retrieves the client module manager. It is responsible for managing all loaded and enabled modules.
    3. .get("Mediator"):

      • get("Mediator") is used to fetch the instance of the module named Mediator from the module manager. "Mediator" is the unique identifier for this module.
      • If the Mediator module is loaded and enabled, the get() method will return the module's instance. If the module is not enabled or loaded, it may return null.
    4. Saving the Module Instance:

      • The retrieved module instance is saved to the mediator variable, which can be used for subsequent operations.

    Notes

    • The mediator will only be correctly assigned after the script is enabled, so the fetching operation should be inside the script.on("enable", ...) callback.
    • If the module doesn't exist or wasn't loaded correctly, mediator will be null.

    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 data any/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, or null 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) and name (listener name) { key: string; name: string }
    callback The callback function for the listener, which accepts value and state (PRE or POST) 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 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    About
    • Terms of Service
    • Privacy Policy
    • Status
    • Contact Us
    Downloads
    • Releases
    • Source code
    • License
    Docs
    • Tutorials
    • CustomHUD
    • AutoSettings
    • ScriptAPI
    Community
    • Forum
    • Guilded
    • YouTube
    • Twitter
    • D.Tube
    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups