mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-18 07:59:08 +00:00
Abstracted ScriptContainer & JaninoScript to IScriptUnit + global implementation
This commit is contained in:
1 parent
1372127266
commit
c3e032dcf2
21 files changed
+518
-220
No files matched your search
@@ -36,7 +36,7 @@ import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class DataScript implements INpcScriptHandler {
|
||||
public List<ScriptContainer> eventScripts = new ArrayList<>();
|
||||
public List<IScriptUnit> eventScripts = new ArrayList<>();
|
||||
|
||||
private HashMap<EnumScriptType, ScriptContainer> scripts = new HashMap<>();
|
||||
private final static EntityType entities = new EntityType();
|
||||
@@ -146,14 +146,16 @@ public class DataScript implements INpcScriptHandler {
|
||||
|
||||
if (!hasInited && !npc.isRemote() && type != EnumScriptType.INIT) {
|
||||
hasInited = true;
|
||||
for (ScriptContainer scriptContainer : this.eventScripts) {
|
||||
scriptContainer.errored = false;
|
||||
for (IScriptUnit scriptUnit : this.eventScripts) {
|
||||
scriptUnit.setErrored(false);
|
||||
}
|
||||
EventHooks.onNPCInit(this.npc);
|
||||
}
|
||||
|
||||
for (ScriptContainer script : this.eventScripts) {
|
||||
script.run(type, event);
|
||||
for (IScriptUnit script : this.eventScripts) {
|
||||
if (script instanceof ScriptContainer) {
|
||||
((ScriptContainer) script).run(type, event);
|
||||
}
|
||||
}
|
||||
|
||||
ScriptContainer script = scripts.get(type);
|
||||
@@ -224,10 +226,10 @@ public class DataScript implements INpcScriptHandler {
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
++tab;
|
||||
|
||||
for (Map.Entry<Long, String> longStringEntry : script.console.entrySet()) {
|
||||
for (Map.Entry<Long, String> longStringEntry : script.getConsole().entrySet()) {
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
|
||||
}
|
||||
}
|
||||
@@ -235,8 +237,8 @@ public class DataScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
public void clearConsole() {
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,11 +276,11 @@ public class DataScript implements INpcScriptHandler {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
this.eventScripts = list;
|
||||
}
|
||||
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
return this.eventScripts;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,13 +15,12 @@ import noppes.npcs.scripted.event.player.PlayerEvent;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class EffectScript implements INpcScriptHandler {
|
||||
public ScriptContainer container;
|
||||
public IScriptUnit container;
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
public boolean enabled = false;
|
||||
|
||||
@@ -64,7 +63,8 @@ public class EffectScript implements INpcScriptHandler {
|
||||
if (!this.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
container.run(s, event);
|
||||
if (container instanceof ScriptContainer)
|
||||
((ScriptContainer) container).run(s, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -93,7 +93,7 @@ public class EffectScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
container = null;
|
||||
return;
|
||||
@@ -102,7 +102,7 @@ public class EffectScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
if (container == null)
|
||||
return new ArrayList<>();
|
||||
return Collections.singletonList(container);
|
||||
@@ -115,31 +115,21 @@ public class EffectScript implements INpcScriptHandler {
|
||||
|
||||
@Override
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap();
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
Iterator var3 = this.getScripts().iterator();
|
||||
|
||||
while (var3.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer) var3.next();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
++tab;
|
||||
Iterator var5 = script.console.entrySet().iterator();
|
||||
|
||||
while (var5.hasNext()) {
|
||||
Map.Entry<Long, String> longStringEntry = (Map.Entry) var5.next();
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + (String) longStringEntry.getValue());
|
||||
for (Map.Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
Iterator var1 = this.getScripts().iterator();
|
||||
|
||||
while (var1.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer) var1.next();
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,4 +170,4 @@ public class EffectScript implements INpcScriptHandler {
|
||||
this.function = functionName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class ForgeDataScript implements INpcScriptHandler {
|
||||
private List<ScriptContainer> scripts = new ArrayList();
|
||||
private List<IScriptUnit> scripts = new ArrayList<>();
|
||||
private String scriptLanguage = "ECMAScript";
|
||||
public long lastInited = -1L;
|
||||
private long lastForgeUpdate = -1L;
|
||||
@@ -26,14 +26,14 @@ public class ForgeDataScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.scripts = new ArrayList();
|
||||
this.scripts = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
if (compound.hasKey("Scripts")) {
|
||||
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this));
|
||||
} else {
|
||||
this.scripts = NBTTags.GetScript(compound, this);
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScript(compound, this));
|
||||
}
|
||||
this.scriptLanguage = compound.getString("ScriptLanguage");
|
||||
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
|
||||
@@ -67,8 +67,9 @@ public class ForgeDataScript implements INpcScriptHandler {
|
||||
this.lastInited = ScriptController.Instance.lastLoaded;
|
||||
this.lastForgeUpdate = ScriptController.Instance.lastForgeUpdate;
|
||||
|
||||
for (ScriptContainer script : this.scripts) {
|
||||
script.errored = false;
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script instanceof ScriptContainer)
|
||||
((ScriptContainer) script).errored = false;
|
||||
}
|
||||
|
||||
if (!type.equals("init")) {
|
||||
@@ -76,11 +77,15 @@ public class ForgeDataScript implements INpcScriptHandler {
|
||||
}
|
||||
}
|
||||
|
||||
for (ScriptContainer script : this.scripts) {
|
||||
if (script == null || script.errored || !script.hasCode())
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script == null || !script.hasCode())
|
||||
continue;
|
||||
|
||||
script.run(type, event);
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
if (container.errored)
|
||||
continue;
|
||||
container.run(type, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,11 +114,11 @@ public class ForgeDataScript implements INpcScriptHandler {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
this.scripts = list;
|
||||
}
|
||||
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
@@ -124,19 +129,18 @@ public class ForgeDataScript implements INpcScriptHandler {
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
++tab;
|
||||
|
||||
for (Entry<Long, String> longStringEntry : script.console.entrySet()) {
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
|
||||
for (Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void clearConsole() {
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ import java.util.OptionalLong;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class GlobalNPCDataScript implements INpcScriptHandler {
|
||||
public List<ScriptContainer> scripts = new ArrayList();
|
||||
public List<IScriptUnit> scripts = new ArrayList<>();
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
private EntityNPCInterface npc;
|
||||
private ICustomNpc npcAPI;
|
||||
@@ -42,14 +42,14 @@ public class GlobalNPCDataScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.scripts = new ArrayList();
|
||||
this.scripts = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
if (compound.hasKey("Scripts")) {
|
||||
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this));
|
||||
} else {
|
||||
this.scripts = NBTTags.GetScript(compound, this);
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScript(compound, this));
|
||||
}
|
||||
this.scriptLanguage = compound.getString("ScriptLanguage");
|
||||
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
|
||||
@@ -87,16 +87,21 @@ public class GlobalNPCDataScript implements INpcScriptHandler {
|
||||
this.lastInited = ScriptController.Instance.lastLoaded;
|
||||
this.lastNpcUpdate = ScriptController.Instance.lastGlobalNpcUpdate;
|
||||
|
||||
for (ScriptContainer script : this.scripts) {
|
||||
script.errored = false;
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script instanceof ScriptContainer)
|
||||
((ScriptContainer) script).errored = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (ScriptContainer script : this.scripts) {
|
||||
if (script == null || script.errored || !script.hasCode())
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script == null || !script.hasCode())
|
||||
continue;
|
||||
|
||||
script.run(hookName, event);
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
if (container.errored)
|
||||
continue;
|
||||
container.run(hookName, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,11 +126,11 @@ public class GlobalNPCDataScript implements INpcScriptHandler {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
this.scripts = list;
|
||||
}
|
||||
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
@@ -148,19 +153,18 @@ public class GlobalNPCDataScript implements INpcScriptHandler {
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
++tab;
|
||||
|
||||
for (Map.Entry<Long, String> longStringEntry : script.console.entrySet()) {
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
|
||||
for (Map.Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void clearConsole() {
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -19,9 +18,9 @@ public interface IScriptHandler {
|
||||
|
||||
void setLanguage(String language);
|
||||
|
||||
void setScripts(List<ScriptContainer> list);
|
||||
void setScripts(List<IScriptUnit> list);
|
||||
|
||||
List<ScriptContainer> getScripts();
|
||||
List<IScriptUnit> getScripts();
|
||||
|
||||
String noticeString();
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package noppes.npcs.controllers.data;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* Common interface for script units that can be edited in a script GUI.
|
||||
* Implemented by both ScriptContainer (ECMAScript) and JaninoScript (Java/Janino).
|
||||
* This allows GuiScriptInterface to work with both script types uniformly.
|
||||
*/
|
||||
public interface IScriptUnit {
|
||||
|
||||
// ==================== SCRIPT CONTENT ====================
|
||||
|
||||
/**
|
||||
* Get the main script text content.
|
||||
*/
|
||||
String getScript();
|
||||
|
||||
/**
|
||||
* Set the main script text content.
|
||||
*/
|
||||
void setScript(String script);
|
||||
|
||||
/**
|
||||
* Get the list of external script file names loaded for this unit.
|
||||
*/
|
||||
List<String> getExternalScripts();
|
||||
|
||||
/**
|
||||
* Set the list of external script file names to load.
|
||||
*/
|
||||
void setExternalScripts(List<String> scripts);
|
||||
|
||||
// ==================== CONSOLE ====================
|
||||
|
||||
/**
|
||||
* Get the console output map (timestamp -> message).
|
||||
*/
|
||||
TreeMap<Long, String> getConsole();
|
||||
|
||||
/**
|
||||
* Clear all console output.
|
||||
*/
|
||||
void clearConsole();
|
||||
|
||||
/**
|
||||
* Append a message to the console.
|
||||
*/
|
||||
void appendConsole(String message);
|
||||
|
||||
// ==================== LANGUAGE ====================
|
||||
|
||||
/**
|
||||
* Get the scripting language for this unit.
|
||||
* Returns "ECMAScript", "Java", etc.
|
||||
*/
|
||||
String getLanguage();
|
||||
|
||||
/**
|
||||
* Set the scripting language for this unit.
|
||||
* @param language The language name (e.g., "ECMAScript", "Java")
|
||||
*/
|
||||
void setLanguage(String language);
|
||||
|
||||
/**
|
||||
* Check if this script unit uses Janino (Java) compilation.
|
||||
*/
|
||||
default boolean isJanino() {
|
||||
return "Java".equals(getLanguage());
|
||||
}
|
||||
|
||||
// ==================== HOOK GENERATION ====================
|
||||
|
||||
/**
|
||||
* Generate a stub/template for the given hook name.
|
||||
* ECMAScript: function hookName(event) { }
|
||||
* Janino: full Java method signature with return type
|
||||
*
|
||||
* @param hookName The hook name to generate a stub for
|
||||
* @param hookData Optional data (e.g., Method object for Janino)
|
||||
* @return The generated stub string
|
||||
*/
|
||||
String generateHookStub(String hookName, Object hookData);
|
||||
|
||||
// ==================== NBT ====================
|
||||
|
||||
/**
|
||||
* Write this script unit to NBT.
|
||||
*/
|
||||
NBTTagCompound writeToNBT(NBTTagCompound compound);
|
||||
|
||||
/**
|
||||
* Read this script unit from NBT.
|
||||
*/
|
||||
void readFromNBT(NBTTagCompound compound);
|
||||
|
||||
// ==================== STATE ====================
|
||||
|
||||
/**
|
||||
* Check if this script unit has any code (script or external scripts).
|
||||
*/
|
||||
boolean hasCode();
|
||||
|
||||
/**
|
||||
* Check if this script unit has encountered an error during execution.
|
||||
*/
|
||||
boolean hasErrored();
|
||||
|
||||
/**
|
||||
* Set the error state of this script unit.
|
||||
*/
|
||||
void setErrored(boolean errored);
|
||||
}
|
||||
@@ -14,13 +14,12 @@ import noppes.npcs.controllers.ScriptController;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class LinkedItemScript implements INpcScriptHandler {
|
||||
public ScriptContainer container;
|
||||
public IScriptUnit container;
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
public boolean enabled = false;
|
||||
|
||||
@@ -58,7 +57,8 @@ public class LinkedItemScript implements INpcScriptHandler {
|
||||
if (!this.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
container.run(s, event);
|
||||
if (container instanceof ScriptContainer)
|
||||
((ScriptContainer) container).run(s, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,7 +87,7 @@ public class LinkedItemScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
container = null;
|
||||
return;
|
||||
@@ -96,7 +96,7 @@ public class LinkedItemScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
if (container == null)
|
||||
return new ArrayList<>();
|
||||
return Collections.singletonList(container);
|
||||
@@ -109,31 +109,21 @@ public class LinkedItemScript implements INpcScriptHandler {
|
||||
|
||||
@Override
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap();
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
Iterator var3 = this.getScripts().iterator();
|
||||
|
||||
while (var3.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer) var3.next();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
++tab;
|
||||
Iterator var5 = script.console.entrySet().iterator();
|
||||
|
||||
while (var5.hasNext()) {
|
||||
Map.Entry<Long, String> longStringEntry = (Map.Entry) var5.next();
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + (String) longStringEntry.getValue());
|
||||
for (Map.Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
Iterator var1 = this.getScripts().iterator();
|
||||
|
||||
while (var1.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer) var1.next();
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import java.util.OptionalLong;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class PlayerDataScript implements INpcScriptHandler {
|
||||
public List<ScriptContainer> scripts = new ArrayList();
|
||||
public List<IScriptUnit> scripts = new ArrayList<>();
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
private EntityPlayer player;
|
||||
private IPlayer playerAPI;
|
||||
@@ -53,14 +53,14 @@ public class PlayerDataScript implements INpcScriptHandler {
|
||||
public void clear() {
|
||||
console = new TreeMap<Long, String>();
|
||||
errored = new ArrayList<Integer>();
|
||||
scripts = new ArrayList<ScriptContainer>();
|
||||
scripts = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
if (compound.hasKey("Scripts")) {
|
||||
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this));
|
||||
} else {
|
||||
this.scripts = NBTTags.GetScript(compound, this);
|
||||
this.scripts = new ArrayList<>(NBTTags.GetScript(compound, this));
|
||||
}
|
||||
this.scriptLanguage = compound.getString("ScriptLanguage");
|
||||
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
|
||||
@@ -104,10 +104,12 @@ public class PlayerDataScript implements INpcScriptHandler {
|
||||
errored.clear();
|
||||
if (player != null) {
|
||||
scripts.clear();
|
||||
for (ScriptContainer script : ScriptController.Instance.playerScripts.scripts) {
|
||||
ScriptContainer s = new ScriptContainer(this);
|
||||
s.readFromNBT(script.writeToNBT(new NBTTagCompound()));
|
||||
scripts.add(s);
|
||||
for (IScriptUnit script : ScriptController.Instance.playerScripts.scripts) {
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer s = new ScriptContainer(this);
|
||||
s.readFromNBT(((ScriptContainer) script).writeToNBT(new NBTTagCompound()));
|
||||
scripts.add(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
|
||||
@@ -117,26 +119,30 @@ public class PlayerDataScript implements INpcScriptHandler {
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < scripts.size(); i++) {
|
||||
ScriptContainer script = scripts.get(i);
|
||||
IScriptUnit script = scripts.get(i);
|
||||
if (errored.contains(i))
|
||||
continue;
|
||||
script.run(hookName, event);
|
||||
if (script.errored) {
|
||||
errored.add(i);
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
container.run(hookName, event);
|
||||
if (container.errored) {
|
||||
errored.add(i);
|
||||
}
|
||||
for (Entry<Long, String> entry : container.console.entrySet()) {
|
||||
if (!console.containsKey(entry.getKey()))
|
||||
console.put(entry.getKey(), " tab " + (i + 1) + ":\n" + entry.getValue());
|
||||
}
|
||||
container.console.clear();
|
||||
}
|
||||
for (Entry<Long, String> entry : script.console.entrySet()) {
|
||||
if (!console.containsKey(entry.getKey()))
|
||||
console.put(entry.getKey(), " tab " + (i + 1) + ":\n" + entry.getValue());
|
||||
}
|
||||
script.console.clear();
|
||||
}
|
||||
} else {
|
||||
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
|
||||
this.lastInited = ScriptController.Instance.lastLoaded;
|
||||
this.lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
|
||||
|
||||
for (ScriptContainer scriptContainer : this.scripts) {
|
||||
scriptContainer.errored = false;
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script instanceof ScriptContainer)
|
||||
((ScriptContainer) script).errored = false;
|
||||
}
|
||||
|
||||
if (!Objects.equals(hookName, EnumScriptType.INIT.function) && event instanceof PlayerEvent) {
|
||||
@@ -145,11 +151,13 @@ public class PlayerDataScript implements INpcScriptHandler {
|
||||
}
|
||||
}
|
||||
|
||||
for (ScriptContainer script : this.scripts) {
|
||||
if (script == null || script.errored || !script.hasCode())
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script == null || script.hasErrored()|| !script.hasCode())
|
||||
continue;
|
||||
|
||||
script.run(hookName, event);
|
||||
if (script instanceof ScriptContainer) {
|
||||
ScriptContainer container = (ScriptContainer) script;
|
||||
container.run(hookName, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,11 +183,11 @@ public class PlayerDataScript implements INpcScriptHandler {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
this.scripts = list;
|
||||
}
|
||||
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
@@ -205,11 +213,10 @@ public class PlayerDataScript implements INpcScriptHandler {
|
||||
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
++tab;
|
||||
|
||||
for (Entry<Long, String> longStringEntry : script.console.entrySet()) {
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
|
||||
for (Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
@@ -219,8 +226,8 @@ public class PlayerDataScript implements INpcScriptHandler {
|
||||
if (ConfigScript.IndividualPlayerScripts)
|
||||
console.clear();
|
||||
else {
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class RecipeScript implements INpcScriptHandler {
|
||||
public ScriptContainer container;
|
||||
public IScriptUnit container;
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
public boolean enabled = false;
|
||||
|
||||
@@ -54,7 +54,8 @@ public class RecipeScript implements INpcScriptHandler {
|
||||
public void callScript(String s, Event event) {
|
||||
if (!this.isEnabled())
|
||||
return;
|
||||
container.run(s, event);
|
||||
if (container instanceof ScriptContainer)
|
||||
((ScriptContainer) container).run(s, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,7 +84,7 @@ public class RecipeScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
container = null;
|
||||
return;
|
||||
@@ -92,7 +93,7 @@ public class RecipeScript implements INpcScriptHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
if (container == null)
|
||||
return new ArrayList<>();
|
||||
return Collections.singletonList(container);
|
||||
@@ -107,9 +108,9 @@ public class RecipeScript implements INpcScriptHandler {
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
++tab;
|
||||
for (Map.Entry<Long, String> entry : script.console.entrySet()) {
|
||||
for (Map.Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
||||
}
|
||||
}
|
||||
@@ -118,8 +119,8 @@ public class RecipeScript implements INpcScriptHandler {
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
for (ScriptContainer script : this.getScripts()) {
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : this.getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,4 +157,4 @@ public class RecipeScript implements INpcScriptHandler {
|
||||
this.function = functionName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user