Replace NashornScriptEngine with implicit GraalJSScriptEngine

GraalJSScriptEngine implements Invocable, which is why casting to it works.
However, this is just a quick and ugly fix to make it compile.
A better solution would be to cast it once, immediately after `eval`,
and from then on only handle it as Invocable.

Scripts still need to be fixed. They are still using Rhino and Nashorn-specific
ways of importing packages.
Usages of "importPackage" and "Packages" need to be replaced with
the Graal specific "Java.type".
This commit is contained in:
P0nk
2021-04-17 15:08:45 +02:00
parent e42fb27459
commit a18a1cb8ce
13 changed files with 284 additions and 364 deletions

View File

@@ -28,7 +28,6 @@ import client.inventory.Item;
import client.inventory.MapleInventoryType;
import config.YamlConfig;
import constants.inventory.ItemConstants;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractPlayerInteraction;
import scripting.event.EventInstanceManager;
import scripting.event.EventManager;
@@ -44,6 +43,8 @@ import server.partyquest.MapleCarnivalFactory;
import server.partyquest.MapleCarnivalFactory.MCSkill;
import tools.MaplePacketCreator;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.awt.*;
import java.util.ArrayList;
@@ -59,10 +60,10 @@ import java.util.logging.Logger;
*/
public class ReactorActionManager extends AbstractPlayerInteraction {
private MapleReactor reactor;
private NashornScriptEngine iv;
private ScriptEngine iv;
private ScheduledFuture<?> sprayTask = null;
public ReactorActionManager(MapleClient c, MapleReactor reactor, NashornScriptEngine iv) {
public ReactorActionManager(MapleClient c, MapleReactor reactor, ScriptEngine iv) {
super(c);
this.reactor = reactor;
this.iv = iv;
@@ -319,7 +320,7 @@ public class ReactorActionManager extends AbstractPlayerInteraction {
public ScheduledFuture<?> schedule(final String methodName, final EventInstanceManager eim, long delay) {
return TimerManager.getInstance().schedule(() -> {
try {
iv.invokeFunction(methodName, eim);
((Invocable) iv).invokeFunction(methodName, eim);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -329,7 +330,7 @@ public class ReactorActionManager extends AbstractPlayerInteraction {
public ScheduledFuture<?> scheduleAtTimestamp(final String methodName, long timestamp) {
return TimerManager.getInstance().scheduleAtTimestamp(() -> {
try {
iv.invokeFunction(methodName, (Object) null);
((Invocable) iv).invokeFunction(methodName, (Object) null);
} catch (ScriptException | NoSuchMethodException ex) {
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
}

View File

@@ -22,13 +22,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package scripting.reactor;
import client.MapleClient;
import jdk.nashorn.api.scripting.NashornScriptEngine;
import scripting.AbstractScriptManager;
import server.maps.MapleReactor;
import server.maps.ReactorDropEntry;
import tools.DatabaseConnection;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -53,12 +54,12 @@ public class ReactorScriptManager extends AbstractScriptManager {
public void onHit(MapleClient c, MapleReactor reactor) {
try {
NashornScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
if (iv == null) return;
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
iv.put("rm", rm);
iv.invokeFunction("hit");
((Invocable) iv).invokeFunction("hit");
} catch (final NoSuchMethodException e) {} //do nothing, hit is OPTIONAL
catch (final ScriptException | NullPointerException e) {
@@ -68,12 +69,12 @@ public class ReactorScriptManager extends AbstractScriptManager {
public void act(MapleClient c, MapleReactor reactor) {
try {
NashornScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
if (iv == null) return;
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
iv.put("rm", rm);
iv.invokeFunction("act");
((Invocable) iv).invokeFunction("act");
} catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
}
@@ -114,15 +115,15 @@ public class ReactorScriptManager extends AbstractScriptManager {
private void touching(MapleClient c, MapleReactor reactor, boolean touching) {
try {
NashornScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
ScriptEngine iv = getScriptEngine("reactor/" + reactor.getId() + ".js", c);
if (iv == null) return;
ReactorActionManager rm = new ReactorActionManager(c, reactor, iv);
iv.put("rm", rm);
if (touching) {
iv.invokeFunction("touch");
((Invocable) iv).invokeFunction("touch");
} else {
iv.invokeFunction("untouch");
((Invocable) iv).invokeFunction("untouch");
}
} catch (final ScriptException | NoSuchMethodException | NullPointerException ute) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", ute);