Enforce implementation of PortalScript
This commit is contained in:
@@ -34,8 +34,7 @@ import java.sql.ResultSet;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class PortalPlayerInteraction extends AbstractPlayerInteraction {
|
public class PortalPlayerInteraction extends AbstractPlayerInteraction {
|
||||||
|
private final MaplePortal portal;
|
||||||
private MaplePortal portal;
|
|
||||||
|
|
||||||
public PortalPlayerInteraction(MapleClient c, MaplePortal portal) {
|
public PortalPlayerInteraction(MapleClient c, MaplePortal portal) {
|
||||||
super(c);
|
super(c);
|
||||||
|
|||||||
@@ -22,48 +22,56 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package scripting.portal;
|
package scripting.portal;
|
||||||
|
|
||||||
import client.MapleClient;
|
import client.MapleClient;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import scripting.AbstractScriptManager;
|
import scripting.AbstractScriptManager;
|
||||||
import server.maps.MaplePortal;
|
import server.maps.MaplePortal;
|
||||||
import tools.FilePrinter;
|
|
||||||
|
|
||||||
import javax.script.Invocable;
|
import javax.script.Invocable;
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import javax.script.ScriptException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class PortalScriptManager extends AbstractScriptManager {
|
public class PortalScriptManager extends AbstractScriptManager {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PortalScriptManager.class);
|
||||||
private static final PortalScriptManager instance = new PortalScriptManager();
|
private static final PortalScriptManager instance = new PortalScriptManager();
|
||||||
|
|
||||||
private final Map<String, Invocable> scripts = new HashMap<>();
|
private final Map<String, PortalScript> scripts = new HashMap<>();
|
||||||
|
|
||||||
public static PortalScriptManager getInstance() {
|
public static PortalScriptManager getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Invocable getPortalScript(String scriptName) {
|
private PortalScript getPortalScript(String scriptName) throws ScriptException {
|
||||||
String scriptPath = "portal/" + scriptName + ".js";
|
String scriptPath = "portal/" + scriptName + ".js";
|
||||||
Invocable iv = scripts.get(scriptPath);
|
PortalScript script = scripts.get(scriptPath);
|
||||||
if (iv != null) {
|
if (script != null) {
|
||||||
return iv;
|
return script;
|
||||||
}
|
}
|
||||||
|
|
||||||
iv = (Invocable) getInvocableScriptEngine(scriptPath);
|
ScriptEngine engine = getInvocableScriptEngine(scriptPath);
|
||||||
if (iv == null) {
|
if (!(engine instanceof Invocable iv)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
scripts.put(scriptPath, iv);
|
script = iv.getInterface(PortalScript.class);
|
||||||
return iv;
|
if (script == null) {
|
||||||
|
throw new ScriptException(String.format("Portal script \"%s\" fails to implement the PortalScript interface", scriptName));
|
||||||
|
}
|
||||||
|
|
||||||
|
scripts.put(scriptPath, script);
|
||||||
|
return script;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean executePortalScript(MaplePortal portal, MapleClient c) {
|
public boolean executePortalScript(MaplePortal portal, MapleClient c) {
|
||||||
try {
|
try {
|
||||||
Invocable iv = getPortalScript(portal.getScriptName());
|
PortalScript script = getPortalScript(portal.getScriptName());
|
||||||
if (iv != null) {
|
if (script != null) {
|
||||||
boolean couldWarp = (boolean) iv.invokeFunction("enter", new PortalPlayerInteraction(c, portal));
|
return script.enter(new PortalPlayerInteraction(c, portal));
|
||||||
return couldWarp;
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
FilePrinter.printError(FilePrinter.PORTAL + portal.getScriptName() + ".txt", e);
|
log.warn("Portal script {}", portal.getScriptName(), e);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user