Files
sweetgum-server/src/main/java/scripting/map/MapScriptManager.java
P0nk 46c2d92082 refactor: use Invocable instead of ScriptEngine in script managers
- Make fields final
- Inject event script variable earlier
- Remove redundant fields
2021-04-18 11:26:05 +02:00

83 lines
2.7 KiB
Java

/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation version 3 as published by
the Free Software Foundation. You may not use, modify or distribute
this program under any other version of the GNU Affero General Public
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package scripting.map;
import client.MapleCharacter;
import client.MapleClient;
import scripting.AbstractScriptManager;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptException;
import java.util.HashMap;
import java.util.Map;
public class MapScriptManager extends AbstractScriptManager {
private static final MapScriptManager instance = new MapScriptManager();
private final Map<String, Invocable> scripts = new HashMap<>();
public static MapScriptManager getInstance() {
return instance;
}
public void reloadScripts() {
scripts.clear();
}
public boolean runMapScript(MapleClient c, String mapScriptPath, boolean firstUser) {
if (firstUser) {
MapleCharacter chr = c.getPlayer();
int mapid = chr.getMapId();
if (chr.hasEntered(mapScriptPath, mapid)) {
return false;
} else {
chr.enteredScript(mapScriptPath, mapid);
}
}
Invocable iv = scripts.get(mapScriptPath);
if (iv != null) {
try {
iv.invokeFunction("start", new MapScriptMethods(c));
return true;
} catch (final ScriptException | NoSuchMethodException e) {
e.printStackTrace();
}
}
try {
iv = (Invocable) getInvocableScriptEngine("map/" + mapScriptPath + ".js");
if (iv == null) {
return false;
}
scripts.put(mapScriptPath, iv);
iv.invokeFunction("start", new MapScriptMethods(c));
return true;
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.MAP_SCRIPT + mapScriptPath + ".txt", e);
}
return false;
}
}