mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 22:27:32 +00:00
79 lines
2.0 KiB
Java
79 lines
2.0 KiB
Java
package noppes.npcs.controllers.data;
|
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
|
import noppes.npcs.CustomNpcs;
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public abstract class ScriptHandler implements IScriptHandler {
|
|
public List<IScriptUnit> scripts = new ArrayList<>();
|
|
protected String scriptLanguage = "ECMAScript";
|
|
protected boolean enabled = false;
|
|
protected long lastInited = -1;
|
|
|
|
protected boolean canRunScripts() {
|
|
return enabled && ScriptController.HasStart && CustomNpcs.proxy.isScriptingEnabled() && scripts != null && !scripts.isEmpty();
|
|
}
|
|
|
|
@Override
|
|
public void callScript(String hookName, Event event) {
|
|
if (!canRunScripts())
|
|
return;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
@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) {
|
|
this.scripts = list;
|
|
}
|
|
|
|
public List<IScriptUnit> getScripts() {
|
|
return this.scripts;
|
|
}
|
|
|
|
public void clear() {
|
|
this.scripts = new ArrayList<>();
|
|
}
|
|
|
|
/**
|
|
* Reset the lastInited timestamp to force re-initialization on next script call.
|
|
*/
|
|
public void resetLastInited() {
|
|
this.lastInited = -1;
|
|
}
|
|
}
|