Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to fall back to scripts named after MapId.PortalName #207

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/scripting/portal/PortalScriptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ public static PortalScriptManager getInstance() {
return instance;
}

public boolean isScriptAvailable(String scriptPath) {
ScriptEngine engine = getInvocableScriptEngine(scriptPath);

return engine instanceof Invocable;
}

private PortalScript getPortalScript(String scriptName) throws ScriptException {
// Additional protection in case fallback has some issue
if (scriptName == null || scriptName.isEmpty()) {
return null;
}

String scriptPath = "portal/" + scriptName + ".js";
PortalScript script = scripts.get(scriptPath);
if (script != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/server/maps/MapFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static MapleMap loadMapFromWz(int mapid, int world, int channel, EventIns
map.setMobInterval((short) DataTool.getInt(infoData.getChildByPath("createMobInterval"), 5000));
PortalFactory portalFactory = new PortalFactory();
for (Data portal : mapData.getChildByPath("portal")) {
map.addPortal(portalFactory.makePortal(DataTool.getInt(portal.getChildByPath("pt")), portal));
map.addPortal(portalFactory.makePortal(DataTool.getInt(portal.getChildByPath("pt")), map.getId(), portal));
}
Data timeMob = infoData.getChildByPath("timeMob");
if (timeMob != null) {
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/server/maps/PortalFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import provider.Data;
import provider.DataTool;
import scripting.portal.PortalScriptManager;

import java.awt.*;

Expand All @@ -33,34 +34,48 @@ public PortalFactory() {
nextDoorPortal = 0x80;
}

public Portal makePortal(int type, Data portal) {
public Portal makePortal(int type, int mapId, Data portal) {
GenericPortal ret = null;
if (type == Portal.MAP_PORTAL) {
ret = new MapPortal();
} else {
ret = new GenericPortal(type);
}
loadPortal(ret, portal);
loadPortal(ret, mapId, portal);
return ret;
}

private void loadPortal(GenericPortal myPortal, Data portal) {
private void loadPortal(GenericPortal myPortal, int mapId, Data portal) {
myPortal.setName(DataTool.getString(portal.getChildByPath("pn")));
myPortal.setTarget(DataTool.getString(portal.getChildByPath("tn")));
myPortal.setTargetMapId(DataTool.getInt(portal.getChildByPath("tm")));
int x = DataTool.getInt(portal.getChildByPath("x"));
int y = DataTool.getInt(portal.getChildByPath("y"));
myPortal.setPosition(new Point(x, y));
String script = DataTool.getString("script", portal, null);
if (script != null && script.equals("")) {
script = null;
}
myPortal.setScriptName(script);
if (myPortal.getType() == Portal.DOOR_PORTAL) {
myPortal.setId(nextDoorPortal);
nextDoorPortal++;
} else {
myPortal.setId(Integer.parseInt(portal.getName()));
}

// If no script node is set, try to find the fallback script and set it
// otherwise, don't set the script name. Portals break if the script is not found
// and a portal name is set.
if (script != null && !script.isBlank()) {
myPortal.setScriptName(script);
return;
}

// Check for and set fallback portal script name if available
String fallbackScriptPath = String.format("%s_%s", mapId, portal.getName());
boolean isScriptAvailable = PortalScriptManager.getInstance().isScriptAvailable("portal/" + fallbackScriptPath + ".js");

if (!isScriptAvailable) {
return;
}

myPortal.setScriptName(fallbackScriptPath);
}
}