ScriptCraft

ScriptCraft

14.6k Downloads

HTTP request module failing

technobly opened this issue ยท 8 comments

commented

I'm trying to use the HTTP request module and I'm getting errors. Hopefully it's something simple that I'm just not seeing, but I've tried a whole bunch of different things today to solve it and the same thing keeps failing.

I downloaded the latest CanaryMod for Minecraft 1.8 and also just tried the latest nightly build with the same issue. I'm also using the latest 3.1.1 of ScriptCraft and it's the only plugin in CanaryMod at the moment.

I start my CanaryMod server with a Start.command bash script on Mac OS 10.9. I just rename JAR to canarymod.jar to keep it simple.

#!/bin/bash
cd "$(dirname "$0")"
exec java -Xmx1024M -Xms1024M -jar canarymod.jar -o true

My java version is:

>java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

I created a test plugin called test.js in the scriptcraft\plugins directory which contains the following:

exports.testme = function() {
    var jsResponse;
    var http = require('../modules/http/request');
    http.request("http://scriptcraftjs.org/sample.json",function(responseCode, responseBody){
        jsResponse = eval("(" + responseBody +  ")");
    });
}

Here's the error I'm getting when running /js testme() from the console:

> > /js testme()
[20:24:22] [org.scriptcraftjs.canarymod.ScriptCraftPlugin] [ERROR]: Error while trying to evaluate javascript: testme(), Error: javax.script.ScriptException: TypeError: Cannot read property "runTaskAsynchronously" from undefined in <eval> at line number 67
> javax.script.ScriptException: javax.script.ScriptException: TypeError: Cannot read property "runTaskAsynchronously" from undefined in <eval> at line number 67 in <eval> at line number 623 at column number 8
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:511)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:227)
    at org.scriptcraftjs.canarymod.ScriptCraftPlugin.executeCommand(ScriptCraftPlugin.java:102)
    at org.scriptcraftjs.canarymod.ScriptCraftPlugin.jsCommand(ScriptCraftPlugin.java:120)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at net.canarymod.commandsys.CommandManager$1.execute(CommandManager.java:322)
    at net.canarymod.commandsys.CanaryCommand.parseCommand(CanaryCommand.java:103)
    at net.canarymod.commandsys.CommandManager.parseCommand(CommandManager.java:186)
    at net.canarymod.api.CanaryServer.consoleCommand(CanaryServer.java:172)
    at net.minecraft.server.dedicated.DedicatedServer$2.run(DedicatedServer.java:140)
Caused by: <eval>:623:8 javax.script.ScriptException: TypeError: Cannot read property "runTaskAsynchronously" from undefined in <eval> at line number 67
    at jdk.nashorn.internal.runtime.ECMAException.create(ECMAException.java:111)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.__onEnable$__onCommand(<eval>:623)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:526)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
    at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:185)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:505)
    ... 12 more
Caused by: javax.script.ScriptException: TypeError: Cannot read property "runTaskAsynchronously" from undefined in <eval> at line number 67
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:570)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.__onEnable$__onCommand(<eval>:598)
    ... 17 more
Caused by: <eval>:67 TypeError: Cannot read property "runTaskAsynchronously" from undefined
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:58)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:214)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:186)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:173)
    at jdk.nashorn.internal.runtime.Undefined.lookupTypeError(Undefined.java:128)
    at jdk.nashorn.internal.runtime.Undefined.lookup(Undefined.java:113)
    at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:98)
    at jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:176)
    at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124)
    at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:144)
    at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:232)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.L:1$L:51(<eval>:67)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.L:1$L:1(<eval>:4)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
    ... 22 more
[20:24:22] [CanaryMod] [INFO] [MESSAGE]: Error while trying to evaluate javascript: testme(), Error: javax.script.ScriptException: TypeError: Cannot read property "runTaskAsynchronously" from undefined in <eval> at line number 67
[20:24:22] [org.scriptcraftjs.canarymod.ScriptCraftPlugin] [ERROR]: javax.script.ScriptException: javax.script.ScriptException: TypeError: Cannot read property "runTaskAsynchronously" from undefined in <eval> at line number 67 in <eval> at line number 623 at column number 8
[20:24:22] [CanaryMod] [INFO] [MESSAGE]: javax.script.ScriptException: TypeError: Cannot read property "runTaskAsynchronously" from undefined in <eval> at line number 67 in <eval> at line number 623 at column number 8
> 

Line 67 of the HTTP request module contains this code:
server.scheduler.runTaskAsynchronously( __plugin, function() {
https://github.com/walterhiggins/ScriptCraft/blob/master/src/main/js/modules/http/request.js#L67

I was able to access the server variable from the console, but some methods I've seen used before don't work, and maybe .scheduler.runTaskAsynchronously is another one of those?

If I type /js server I get:
[20:56:52] [CanaryMod] [INFO] [MESSAGE]: net.canarymod.api.CanaryServer@1f10821c

If I type /js server.canaryModVersion I get:
[21:07:04] [CanaryMod] [INFO] [MESSAGE]: "1.8.0-1.2.0-SNAPSHOT"

Any idea on what could be the problem here? Thanks in advance :)

commented

Ok, not willing to give up I just got Spigot 1.8 compiled and running ScriptCraft. It seems to work now :) Although I had to change the example code to not use eval as it was still crashing. I just changed it to a console.log() and it works.

Code:

exports.testme = function(msg) {
    var jsResponse;
    var http = require('../modules/http/request');
    http.request("http://scriptcraftjs.org/sample.json",function(responseCode, responseBody){
        // jsResponse = eval("(" + responseBody +  ")");
        console.log(responseBody);
        console.log(responseCode);
    });
}

Output:

>js testme()
[22:02:20 INFO]: [scriptcraft] {
    name: "ScriptCraft"
    language: "Javascript",
    releases: ["1.4.5","1.4.6","1.4.7","1.5.1"]
}      

[22:02:20 INFO]: [scriptcraft] 200
>
commented

Some more info that might help...

With Spigot this command works:

>js server.getWorld('world').getFullTime()
[09:15:34 INFO]: "23059"

But with CanaryMod this it doesn't seem to be defined:

> js server.getWorld('world').getFullTime()
javax.script.ScriptException: javax.script.ScriptException: TypeError: null has no such function "getFullTime" in <eval> at line number 1 in <eval> at line number 623 at column number 8
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:511)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:227)
    at org.scriptcraftjs.canarymod.ScriptCraftPlugin.executeCommand(ScriptCraftPlugin.java:102)
    at org.scriptcraftjs.canarymod.ScriptCraftPlugin.jsCommand(ScriptCraftPlugin.java:120)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at net.canarymod.commandsys.CommandManager$1.execute(CommandManager.java:322)
    at net.canarymod.commandsys.CanaryCommand.parseCommand(CanaryCommand.java:103)
    at net.canarymod.commandsys.CommandManager.parseCommand(CommandManager.java:186)
    at net.canarymod.api.CanaryServer.consoleCommand(CanaryServer.java:172)
    at net.minecraft.server.dedicated.DedicatedServer$2.run(DedicatedServer.java:140)
Caused by: <eval>:623:8 javax.script.ScriptException: TypeError: null has no such function "getFullTime" in <eval> at line number 1
    at jdk.nashorn.internal.runtime.ECMAException.create(ECMAException.java:111)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.__onEnable$__onCommand(<eval>:623)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:526)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
    at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:185)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:505)
    ... 12 more
Caused by: javax.script.ScriptException: TypeError: null has no such function "getFullTime" in <eval> at line number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:570)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.__onEnable$__onCommand(<eval>:598)
    ... 17 more
Caused by: <eval>:1 TypeError: null has no such function "getFullTime"
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:58)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:214)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:186)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:173)
    at jdk.nashorn.internal.runtime.linker.NashornBottomLinker.linkNull(NashornBottomLinker.java:179)
    at jdk.nashorn.internal.runtime.linker.NashornBottomLinker.getGuardedInvocation(NashornBottomLinker.java:66)
    at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124)
    at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:144)
    at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:232)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
    ... 22 more
[09:14:51] [org.scriptcraftjs.canarymod.ScriptCraftPlugin] [ERROR]: Error while trying to evaluate javascript: server.getWorld('world').getFullTime(), Error: javax.script.ScriptException: TypeError: null has no such function "getFullTime" in <eval> at line number 1
[09:14:51] [CanaryMod] [INFO] [MESSAGE]: Error while trying to evaluate javascript: server.getWorld('world').getFullTime(), Error: javax.script.ScriptException: TypeError: null has no such function "getFullTime" in <eval> at line number 1
[09:14:51] [org.scriptcraftjs.canarymod.ScriptCraftPlugin] [ERROR]: javax.script.ScriptException: javax.script.ScriptException: TypeError: null has no such function "getFullTime" in <eval> at line number 1 in <eval> at line number 623 at column number 8
[09:14:51] [CanaryMod] [INFO] [MESSAGE]: javax.script.ScriptException: TypeError: null has no such function "getFullTime" in <eval> at line number 1 in <eval> at line number 623 at column number 8
commented

Just tested this 3.1.2 release today and the HTTP requests are working great! The server.getWorld('world').getFullTime() doesn't work, but I'm guessing that's just a Bukkit only API call. I figured out another way to read the time using events.timeChange(). Thanks for your very fast resolution on this issue!

commented

Take a look at the utils.time and utils.time24 functions in the utils module

commented

https://github.com/walterhiggins/ScriptCraft/blob/master/docs/API-Reference.md#utilities-module

Assuming you have a world called 'myWorld':

var utils = require('utils');
var myWorld = utils.world('myWorld');
var time = utils.time(myWorld);

I'll open an enhancement req to accept either world names or world objects as a parameter to the time functions.

commented

I can open a new issue for this is you'd like, but I'm not getting a time returned with either function:

> js var utils = require('utils')
> js utils.time('world')
[01:03:06] [CanaryMod] [INFO] [MESSAGE]: "NaN"
> js utils.time24('world')
[01:03:13] [CanaryMod] [INFO] [MESSAGE]: "NaN"
commented

Initially I did try to find some world object to use as an argument instead of the literal 'world', and after looking at the time() and time24() functions I see it's necessary.

> js var world = utils.world('world')
[01:19:39] [org.scriptcraftjs.canarymod.ScriptCraftPlugin] [ERROR]: utils.world() failed to load world,Error:net.canarymod.api.world.UnknownWorldException: World world is unknown. Autoload was enabled for this call.

All of the documentation here is great but I'm finding a lot of missing details. Is there another API reference that is used for CanaryMod?

Also, it sort of seems like the server object is not completely functional for me.

Thanks for the help!


FWIW: Here's how I got the time out of the timeChange() event

my_module.js

var globalTime = 0;

if (__plugin.canary) {
  // canarymod
  events.timeChange( function( evt, cancel ) {
    globalTime = eval(evt).time;
  } );
}

// use globalTime in other functions
commented

Ok I see the problem now... the default world name in CanaryMod is 'default_NORMAL', so this works:

> js var utils = require('utils');
> js var myWorld = utils.world('default_NORMAL');
> js utils.time(myWorld);
[10:32:52] [CanaryMod] [INFO] [MESSAGE]: "2208084"

Sounds good about the enhancement. Might also be good to have utils.world() without an argument try 'default_NORMAL' on Canary servers. Bukkit servers are still defaulting to 'world'.