Search in sources :

Example 1 with Script

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

the class DragonetServer method initialize.

/**
 * Initialize the server, DO NOT CALL IT YOURSELF. Only called by Glowstone
 * main class.
 */
public void initialize() {
    this.logger.info("Starting Dragonet Server version " + (RELEASE ? "Release " : "") + DragonetVersioning.DRAGONET_VERSION + "... ");
    if (!RELEASE) {
        this.logger.warn("** This is a snapshot or an un-official build of Dragonet, you may expirence bugs and errors! ");
    }
    File fileConfig = new File(this.server.getConfigDir() + File.separator + "dragonet.yml");
    if (!fileConfig.exists()) {
        try {
            InputStream inp = DragonetServer.class.getResourceAsStream("/defaults/dragonet.yml");
            try {
                FileOutputStream oup = new FileOutputStream(fileConfig);
                try {
                    int data = -1;
                    while ((data = inp.read()) != -1) {
                        oup.write(data);
                    }
                } finally {
                    oup.close();
                }
            } finally {
                if (inp != null) {
                    inp.close();
                }
            }
        } catch (IOException e) {
        }
    }
    Configuration config = YamlConfiguration.loadConfiguration(fileConfig);
    enabledJs = config.getBoolean("plugin-support.dapis", true);
    enabledPhp = config.getBoolean("plugin-support.php", true);
    enabledDragonPortal = config.getBoolean("dragonportal.enabled", false);
    if (RELEASE) {
        try {
            metrics = new DragonetMetrics(this);
            metrics.start();
        } catch (IOException ex) {
            this.logger.error("Faild to send statistics! ");
        }
    }
    this.logger.info("Current Minecraft PC Version: " + DragonetVersioning.MINECRAFT_PC_VERSION);
    this.logger.info("Current Minecraft: PE Version: " + DragonetVersioning.MINECRAFT_PE_VERSION);
    this.threadPool = Executors.newFixedThreadPool(64);
    String ip = config.getString("server-ip", "0.0.0.0");
    int port = config.getInt("server-port", 19132);
    this.logger.info("Trying to bind on UDP address " + ip + ":" + port + "... ");
    try {
        this.network = new RakNetInterface(sessionManager, ip, port);
    } catch (Exception ex) {
        this.getLogger().error("FAILD TO BIND ON THE Minecraft: Pocket Edition PORT " + port + "(UDP)! ");
        this.getLogger().error("CLOSE THE PROGRAM USING THAT PORT OR CHANGE THE PORT TO SOLVE THIS PROBLEM! ");
        this.getLogger().error("ERROR MESSAGE: " + ex.getMessage());
        ex.printStackTrace();
        this.getServer().shutdown();
        return;
    }
    this.rhino = new Rhino(this.getServer());
    if (enabledJs) {
        this.logger.info("Loading DAPIS scripts... ");
        this.rhino.loadScripts();
    } else {
        this.logger.info("DAPIS Javascript plugins disabled! ");
    }
    this.php = new PHPManager(this);
    if (enabledPhp) {
        this.logger.info("Loading PHP scripts");
        this.php.loadScripts();
    } else {
        this.logger.info("PHP plugins disabled! ");
    }
    // DragonPortal server
    if (enabledDragonPortal) {
        this.getLogger().info("Enabling DragonPortal server... ");
        try {
            this.portalServer = new DragonPortalServer(this, config.getString("dragonportal.bind-address", "127.0.0.1"), config.getInt("dragonportal.bind-port", 25590), config.getString("dragonportal.password", "NOT_SET"));
            this.portalServer.initialize();
        } catch (UnknownHostException ex) {
            this.getLogger().error("Faild to create DragonPoral server. ", ex);
        } catch (IOException ex) {
            this.getLogger().error("Faild to bind DragonPoral server on [" + config.getString("dragonportal.bind-address", "127.0.0.1") + ":" + config.getInt("dragonportal.bind-port", 25590) + "]. ", ex);
        } catch (PasswordNotSetException ex) {
            this.getLogger().error(ex.getMessage());
        }
        enabledDragonPortal = true;
    } else {
        enabledDragonPortal = false;
    }
    // This exists because ScriptAPI.addMethod() must be called AFTER Dragonet initialization
    for (Script s : rhino.getScripts()) {
        this.getLogger().info("[DragonetAPI] Loading script " + s.getUID());
        s.runFunction("onLoad", new Object[] { s });
    }
    this.playerSpawnThreshold = config.getInt("player-spawn-chunk-threshold", 36);
    this.logger.info("Dragonet successfully initialized! ");
}
Also used : Rhino(org.dragonet.rhino.Rhino) Script(org.dragonet.rhino.Script) Configuration(org.bukkit.configuration.Configuration) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) DragonPortalServer(org.dragonet.net.inf.portal.DragonPortalServer) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) RakNetInterface(org.dragonet.net.inf.mcpe.RakNetInterface) PHPManager(org.dragonet.plugin.php.PHPManager) IOException(java.io.IOException) PasswordNotSetException(org.dragonet.net.inf.portal.PasswordNotSetException) IOException(java.io.IOException) PasswordNotSetException(org.dragonet.net.inf.portal.PasswordNotSetException) UnknownHostException(java.net.UnknownHostException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with Script

use of org.dragonet.rhino.Script 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

Script (org.dragonet.rhino.Script)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnknownHostException (java.net.UnknownHostException)1 Configuration (org.bukkit.configuration.Configuration)1 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)1 RakNetInterface (org.dragonet.net.inf.mcpe.RakNetInterface)1 DragonPortalServer (org.dragonet.net.inf.portal.DragonPortalServer)1 PasswordNotSetException (org.dragonet.net.inf.portal.PasswordNotSetException)1 PHPManager (org.dragonet.plugin.php.PHPManager)1 CustomMethod (org.dragonet.rhino.CustomMethod)1 Rhino (org.dragonet.rhino.Rhino)1 WrappedException (org.mozilla.javascript.WrappedException)1 JSFunction (org.mozilla.javascript.annotations.JSFunction)1