2026-01-18 03:19:30 +02:00
|
|
|
package noppes.npcs.controllers.data;
|
|
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
|
|
|
|
import noppes.npcs.config.ConfigScript;
|
|
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public abstract class ScriptHandler implements IScriptHandler {
|
2026-01-18 17:50:04 +02:00
|
|
|
public List<IScriptUnit> scripts = new ArrayList<>();
|
2026-01-18 03:19:30 +02:00
|
|
|
protected String scriptLanguage = "ECMAScript";
|
|
|
|
|
protected boolean enabled = false;
|
2026-01-18 17:50:04 +02:00
|
|
|
protected long lastInited = -1;
|
2026-01-18 03:19:30 +02:00
|
|
|
|
|
|
|
|
protected boolean canRunScripts() {
|
2026-01-18 17:50:04 +02:00
|
|
|
return enabled && ScriptController.HasStart && ConfigScript.ScriptingEnabled && scripts != null && !scripts.isEmpty();
|
2026-01-18 03:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void callScript(String hookName, Event event) {
|
2026-01-18 17:50:04 +02:00
|
|
|
if (!canRunScripts())
|
2026-01-18 03:19:30 +02:00
|
|
|
return;
|
2026-01-18 17:50:04 +02:00
|
|
|
|
|
|
|
|
if (ScriptController.Instance.lastLoaded > lastInited) {
|
|
|
|
|
lastInited = ScriptController.Instance.lastLoaded;
|
|
|
|
|
for (IScriptUnit script : scripts) {
|
|
|
|
|
if (script != null)
|
|
|
|
|
script.setErrored(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (IScriptUnit script : scripts) {
|
|
|
|
|
if (script == null || script.hasErrored() || !script.hasCode())
|
|
|
|
|
continue;
|
|
|
|
|
script.run(hookName, event);
|
2026-01-18 03:19:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean getEnabled() {
|
|
|
|
|
return enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
|
this.enabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getLanguage() {
|
|
|
|
|
return scriptLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setLanguage(String language) {
|
|
|
|
|
this.scriptLanguage = language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setScripts(List<IScriptUnit> list) {
|
2026-01-18 17:50:04 +02:00
|
|
|
this.scripts = list;
|
2026-01-18 03:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<IScriptUnit> getScripts() {
|
2026-01-18 17:50:04 +02:00
|
|
|
return this.scripts;
|
2026-01-18 03:19:30 +02:00
|
|
|
}
|
2026-01-22 14:27:48 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the lastInited timestamp to force re-initialization on next script call.
|
|
|
|
|
*/
|
|
|
|
|
public void resetLastInited() {
|
|
|
|
|
this.lastInited = -1;
|
|
|
|
|
}
|
2026-01-18 03:19:30 +02:00
|
|
|
}
|