Search in sources :

Example 1 with CustomMethod

use of org.dragonet.rhino.CustomMethod in project Dragonet-Legacy by DragonetMC.

the class ScriptAPI method addMethod.

@JSFunction
public static void addMethod(String method, String handler, String ownerUID) {
    //Check if ownerUID belongs to a valid script
    Script scr = null;
    try {
        for (Script s : DragonetServer.instance().getRhino().getScripts()) {
            if (s.getUID().equals(ownerUID)) {
                scr = s;
            }
        }
    } catch (WrappedException e) {
        DragonetServer.instance().getLogger().error("[DragonetAPI] Script tried to add a method before initialization finished! Please use postInit for this.");
    }
    if (scr == null) {
        DragonetServer.instance().getLogger().error("[DragonetAPI] Script doesn't have a valid UID but is trying to register method " + method + "! Received '" + ownerUID + "', this does not belong to any script!");
        DragonetServer.instance().getLogger().error("[DragonetAPI] Method " + method + " will not be defined. This will cause issues with other scripts.");
        return;
    }
    //Check if method name is already taken
    for (CustomMethod m : CustomMethod.methods) {
        if (m.method.equals(method)) {
            DragonetServer.instance().getLogger().error("[DragonetAPI] Script " + scr.getUID() + " (" + scr.getName() + ")" + " tried to reserve method " + method + ", but this has already been reserved by " + m.owner.getName() + "!");
            DragonetServer.instance().getLogger().error("[DragonetAPI] Method " + method + " will not be defined. This will cause issues with other scripts.");
        }
    }
    //Finally, add method
    CustomMethod.methods.add(new CustomMethod(method, handler, scr));
    DragonetServer.instance().getLogger().info("[DragonetAPI] Script " + scr.getUID() + " (" + scr.getName() + ") added API method " + method + " sucessfully.");
}
Also used : Script(org.dragonet.rhino.Script) WrappedException(org.mozilla.javascript.WrappedException) CustomMethod(org.dragonet.rhino.CustomMethod) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

CustomMethod (org.dragonet.rhino.CustomMethod)1 Script (org.dragonet.rhino.Script)1 WrappedException (org.mozilla.javascript.WrappedException)1 JSFunction (org.mozilla.javascript.annotations.JSFunction)1