2021-10-11 06:49:30 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
|
|
|
|
|
|
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import noppes.npcs.EventHooks;
|
|
|
|
|
import noppes.npcs.NBTTags;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.config.ConfigScript;
|
2021-10-11 06:49:30 -05:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
2021-10-11 06:49:30 -05:00
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
|
2024-02-15 17:54:28 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2023-01-12 21:33:06 -05:00
|
|
|
import java.util.Map.Entry;
|
2024-02-15 17:54:28 -05:00
|
|
|
import java.util.TreeMap;
|
2023-01-12 21:33:06 -05:00
|
|
|
|
2023-07-13 13:56:32 -05:00
|
|
|
public class ForgeDataScript implements INpcScriptHandler {
|
2026-01-13 17:34:14 +02:00
|
|
|
private List<IScriptUnit> scripts = new ArrayList<>();
|
2021-10-11 06:49:30 -05:00
|
|
|
private String scriptLanguage = "ECMAScript";
|
|
|
|
|
public long lastInited = -1L;
|
2023-02-21 12:45:48 -06:00
|
|
|
private long lastForgeUpdate = -1L;
|
2021-10-11 06:49:30 -05:00
|
|
|
private boolean enabled = false;
|
|
|
|
|
|
|
|
|
|
public ForgeDataScript() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void clear() {
|
2026-01-13 17:34:14 +02:00
|
|
|
this.scripts = new ArrayList<>();
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
2023-02-19 13:38:43 -06:00
|
|
|
if (compound.hasKey("Scripts")) {
|
2026-01-13 17:34:14 +02:00
|
|
|
this.scripts = new ArrayList<>(NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this));
|
2023-02-19 13:38:43 -06:00
|
|
|
} else {
|
2026-01-13 17:34:14 +02:00
|
|
|
this.scripts = new ArrayList<>(NBTTags.GetScript(compound, this));
|
2023-02-19 13:38:43 -06:00
|
|
|
}
|
2021-10-11 06:49:30 -05:00
|
|
|
this.scriptLanguage = compound.getString("ScriptLanguage");
|
2022-05-29 16:01:05 -05:00
|
|
|
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
|
|
|
|
|
if (!ScriptController.Instance.languages.isEmpty()) {
|
|
|
|
|
this.scriptLanguage = (String) ScriptController.Instance.languages.keySet().toArray()[0];
|
|
|
|
|
} else {
|
|
|
|
|
this.scriptLanguage = "ECMAScript";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-11 06:49:30 -05:00
|
|
|
this.enabled = compound.getBoolean("ScriptEnabled");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
2025-03-12 20:12:01 -04:00
|
|
|
compound.setInteger("TotalScripts", this.scripts.size());
|
2023-02-19 13:38:43 -06:00
|
|
|
for (int i = 0; i < this.scripts.size(); i++) {
|
2025-03-12 20:12:01 -04:00
|
|
|
compound.setTag("Tab" + i, this.scripts.get(i).writeToNBT(new NBTTagCompound()));
|
2023-02-19 13:38:43 -06:00
|
|
|
}
|
2021-10-11 06:49:30 -05:00
|
|
|
compound.setString("ScriptLanguage", this.scriptLanguage);
|
|
|
|
|
compound.setBoolean("ScriptEnabled", this.enabled);
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 06:33:39 -05:00
|
|
|
@Override
|
2021-12-16 21:54:05 -06:00
|
|
|
public void callScript(EnumScriptType var1, Event var2) {
|
2023-07-13 13:56:32 -05:00
|
|
|
this.callScript(var1.function, var2);
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void callScript(String type, Event event) {
|
2025-03-12 20:12:01 -04:00
|
|
|
if (this.isEnabled()) {
|
2023-02-21 12:45:48 -06:00
|
|
|
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastForgeUpdate > this.lastForgeUpdate) {
|
2022-12-23 20:45:04 -06:00
|
|
|
this.lastInited = ScriptController.Instance.lastLoaded;
|
2023-02-21 12:45:48 -06:00
|
|
|
this.lastForgeUpdate = ScriptController.Instance.lastForgeUpdate;
|
|
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
for (IScriptUnit script : this.scripts) {
|
|
|
|
|
if (script instanceof ScriptContainer)
|
|
|
|
|
((ScriptContainer) script).errored = false;
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-23 20:45:04 -06:00
|
|
|
if (!type.equals("init")) {
|
|
|
|
|
EventHooks.onForgeInit(this);
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
2022-12-23 20:45:04 -06:00
|
|
|
}
|
2021-10-11 06:49:30 -05:00
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
for (IScriptUnit script : this.scripts) {
|
|
|
|
|
if (script == null || !script.hasCode())
|
2023-02-21 12:45:48 -06:00
|
|
|
continue;
|
2026-01-13 17:34:14 +02:00
|
|
|
if (script instanceof ScriptContainer) {
|
|
|
|
|
ScriptContainer container = (ScriptContainer) script;
|
|
|
|
|
if (container.errored)
|
|
|
|
|
continue;
|
|
|
|
|
container.run(type, event);
|
|
|
|
|
}
|
2022-12-23 20:45:04 -06:00
|
|
|
}
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isEnabled() {
|
2023-01-07 00:21:08 -05:00
|
|
|
return this.enabled && ConfigScript.GlobalForgeScripts && ScriptController.HasStart && this.scripts.size() > 0;
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isClient() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getEnabled() {
|
2022-11-02 14:00:03 -04:00
|
|
|
return this.enabled;
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setEnabled(boolean bo) {
|
|
|
|
|
this.enabled = bo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getLanguage() {
|
|
|
|
|
return this.scriptLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLanguage(String lang) {
|
|
|
|
|
this.scriptLanguage = lang;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
public void setScripts(List<IScriptUnit> list) {
|
2023-02-19 16:22:28 -06:00
|
|
|
this.scripts = list;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 17:34:14 +02:00
|
|
|
public List<IScriptUnit> getScripts() {
|
2021-10-11 06:49:30 -05:00
|
|
|
return this.scripts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String noticeString() {
|
|
|
|
|
return "ForgeScript";
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 15:20:51 -06:00
|
|
|
public Map<Long, String> getConsoleText() {
|
2023-02-21 12:45:48 -06:00
|
|
|
TreeMap<Long, String> map = new TreeMap<>();
|
|
|
|
|
int tab = 0;
|
2026-01-13 17:34:14 +02:00
|
|
|
for (IScriptUnit script : this.getScripts()) {
|
2023-02-21 12:45:48 -06:00
|
|
|
++tab;
|
2026-01-13 17:34:14 +02:00
|
|
|
for (Entry<Long, String> entry : script.getConsole().entrySet()) {
|
|
|
|
|
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return map;
|
2023-02-19 15:20:51 -06:00
|
|
|
}
|
2023-02-21 12:45:48 -06:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public void clearConsole() {
|
2026-01-13 17:34:14 +02:00
|
|
|
for (IScriptUnit script : this.getScripts()) {
|
|
|
|
|
script.clearConsole();
|
2023-02-21 12:45:48 -06:00
|
|
|
}
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
|
|
|
|
}
|