mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 02:37:27 +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
@@ -15,6 +15,7 @@ import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.controllers.data.IScriptHandler;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.items.ItemNpcTool;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -221,9 +222,9 @@ public class PacketUtil {
|
||||
compound.setTag("Languages", ScriptController.Instance.nbtLanguages());
|
||||
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(data.getConsoleText()));
|
||||
GuiDataPacket.sendGuiData(player, compound);
|
||||
List<ScriptContainer> containers = data.getScripts();
|
||||
List<IScriptUnit> containers = data.getScripts();
|
||||
for (int i = 0; i < containers.size(); i++) {
|
||||
ScriptContainer container = containers.get(i);
|
||||
IScriptUnit container = containers.get(i);
|
||||
NBTTagCompound tabCompound = new NBTTagCompound();
|
||||
tabCompound.setInteger("Tab", i);
|
||||
tabCompound.setTag("Script", container.writeToNBT(new NBTTagCompound()));
|
||||
|
||||
+5
-5
@@ -16,7 +16,7 @@ import noppes.npcs.CustomNpcsPermissions;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.api.item.IItemStack;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.item.ScriptCustomItem;
|
||||
|
||||
@@ -72,10 +72,10 @@ public final class ItemScriptErrorPacket extends AbstractPacket {
|
||||
if (requestedAction == Action.GET) {
|
||||
TreeMap<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
for (ScriptContainer script : ((ScriptCustomItem) iw).scripts) {
|
||||
for (IScriptUnit script : ((ScriptCustomItem) iw).scripts) {
|
||||
++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());
|
||||
}
|
||||
}
|
||||
@@ -84,8 +84,8 @@ public final class ItemScriptErrorPacket extends AbstractPacket {
|
||||
compound.setTag("ItemScriptConsole", NBTTags.NBTLongStringMap(map));
|
||||
GuiDataPacket.sendGuiData((EntityPlayerMP) player, compound);
|
||||
} else {
|
||||
for (ScriptContainer script : ((ScriptCustomItem) iw).scripts) {
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : ((ScriptCustomItem) iw).scripts) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import net.minecraft.nbt.NBTTagInt;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.data.IScriptHandler;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -540,8 +541,8 @@ public class NBTTags {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<ScriptContainer> GetScriptOld(NBTTagList list, IScriptHandler handler) {
|
||||
ArrayList scripts = new ArrayList();
|
||||
public static List<IScriptUnit> GetScriptOld(NBTTagList list, IScriptHandler handler) {
|
||||
ArrayList<IScriptUnit> scripts = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < list.tagCount(); ++i) {
|
||||
NBTTagCompound compoundd = list.getCompoundTagAt(i);
|
||||
@@ -553,8 +554,8 @@ public class NBTTags {
|
||||
return scripts;
|
||||
}
|
||||
|
||||
public static List<ScriptContainer> GetScript(NBTTagCompound compound, IScriptHandler handler) {
|
||||
ArrayList<ScriptContainer> scripts = new ArrayList<>();
|
||||
public static List<IScriptUnit> GetScript(NBTTagCompound compound, IScriptHandler handler) {
|
||||
ArrayList<IScriptUnit> scripts = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < compound.getInteger("TotalScripts"); ++i) {
|
||||
NBTTagCompound containerCompound = compound.getCompoundTag("Tab" + i);
|
||||
@@ -566,12 +567,9 @@ public class NBTTags {
|
||||
return scripts;
|
||||
}
|
||||
|
||||
public static NBTTagList NBTScript(List<ScriptContainer> scripts) {
|
||||
public static NBTTagList NBTScript(List<IScriptUnit> scripts) {
|
||||
NBTTagList list = new NBTTagList();
|
||||
Iterator var2 = scripts.iterator();
|
||||
|
||||
while (var2.hasNext()) {
|
||||
ScriptContainer script = (ScriptContainer) var2.next();
|
||||
for (IScriptUnit script : scripts) {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
script.writeToNBT(compound);
|
||||
list.appendTag(compound);
|
||||
|
||||
@@ -28,6 +28,7 @@ import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.controllers.data.IScriptBlockHandler;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.entity.data.DataTimers;
|
||||
import noppes.npcs.scripted.BlockScriptedWrapper;
|
||||
import noppes.npcs.util.ValueUtil;
|
||||
@@ -40,7 +41,7 @@ import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class TileScripted extends TileEntity implements IScriptBlockHandler {
|
||||
public List<ScriptContainer> scripts = new ArrayList<>();
|
||||
public List<IScriptUnit> scripts = new ArrayList<>();
|
||||
|
||||
public Map<String, Object> tempData = new HashMap<>();
|
||||
|
||||
@@ -367,8 +368,10 @@ public class TileScripted extends TileEntity implements IScriptBlockHandler {
|
||||
EventHooks.onScriptBlockInit(this);
|
||||
}
|
||||
|
||||
for (ScriptContainer script : scripts) {
|
||||
script.run(type, event);
|
||||
for (IScriptUnit script : scripts) {
|
||||
if (script instanceof ScriptContainer) {
|
||||
((ScriptContainer) script).run(type, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,12 +411,12 @@ public class TileScripted extends TileEntity implements IScriptBlockHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScripts(List<ScriptContainer> list) {
|
||||
public void setScripts(List<IScriptUnit> list) {
|
||||
scripts = list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ScriptContainer> getScripts() {
|
||||
public List<IScriptUnit> getScripts() {
|
||||
return scripts;
|
||||
}
|
||||
|
||||
@@ -421,9 +424,9 @@ public class TileScripted extends TileEntity implements IScriptBlockHandler {
|
||||
public Map<Long, String> getConsoleText() {
|
||||
Map<Long, String> map = new TreeMap<>();
|
||||
int tab = 0;
|
||||
for (ScriptContainer script : getScripts()) {
|
||||
for (IScriptUnit script : getScripts()) {
|
||||
tab++;
|
||||
for (Entry<Long, String> entry : script.console.entrySet()) {
|
||||
for (Entry<Long, String> entry : script.getConsole().entrySet()) {
|
||||
map.put(entry.getKey(), " tab " + tab + ":\n" + entry.getValue());
|
||||
}
|
||||
}
|
||||
@@ -432,8 +435,8 @@ public class TileScripted extends TileEntity implements IScriptBlockHandler {
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
for (ScriptContainer script : getScripts()) {
|
||||
script.console.clear();
|
||||
for (IScriptUnit script : getScripts()) {
|
||||
script.clearConsole();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptHookController;
|
||||
import noppes.npcs.controllers.data.DataScript;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.entity.EntityNPCInterface;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -49,9 +50,9 @@ public class GuiNPCEventScripts extends GuiScriptInterface {
|
||||
public void save() {
|
||||
if (loaded) {
|
||||
super.save();
|
||||
List<ScriptContainer> containers = this.script.getScripts();
|
||||
List<IScriptUnit> containers = this.script.getScripts();
|
||||
for (int i = 0; i < containers.size(); i++) {
|
||||
ScriptContainer container = containers.get(i);
|
||||
IScriptUnit container = containers.get(i);
|
||||
EventScriptPacket.Save(i, containers.size(), container.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
NBTTagCompound scriptData = new NBTTagCompound();
|
||||
|
||||
@@ -7,6 +7,7 @@ import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptHookController;
|
||||
import noppes.npcs.controllers.data.GlobalNPCDataScript;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -47,9 +48,9 @@ public class GuiScriptAllNPCs extends GuiScriptInterface {
|
||||
public void save() {
|
||||
if (loaded) {
|
||||
super.save();
|
||||
List<ScriptContainer> containers = this.script.getScripts();
|
||||
List<IScriptUnit> containers = this.script.getScripts();
|
||||
for (int i = 0; i < containers.size(); i++) {
|
||||
ScriptContainer container = containers.get(i);
|
||||
IScriptUnit container = containers.get(i);
|
||||
GlobalNPCScriptPacket.Save(i, containers.size(), container.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
NBTTagCompound scriptData = new NBTTagCompound();
|
||||
|
||||
@@ -17,6 +17,7 @@ import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptHookController;
|
||||
import noppes.npcs.controllers.data.ForgeDataScript;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -102,9 +103,9 @@ public class GuiScriptForge extends GuiScriptInterface {
|
||||
public void save() {
|
||||
if (loaded) {
|
||||
super.save();
|
||||
List<ScriptContainer> containers = this.script.getScripts();
|
||||
List<IScriptUnit> containers = this.script.getScripts();
|
||||
for (int i = 0; i < containers.size(); i++) {
|
||||
ScriptContainer container = containers.get(i);
|
||||
IScriptUnit container = containers.get(i);
|
||||
ForgeScriptPacket.Save(i, containers.size(), container.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
NBTTagCompound scriptData = new NBTTagCompound();
|
||||
|
||||
@@ -25,6 +25,7 @@ import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.controllers.data.ForgeDataScript;
|
||||
import noppes.npcs.controllers.data.IScriptHandler;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.scripted.item.ScriptCustomItem;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
@@ -159,7 +160,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
* Handles both normal and fullscreen modes.
|
||||
*/
|
||||
private void initScriptEditorTab(int yoffset) {
|
||||
ScriptContainer container = getCurrentContainer();
|
||||
IScriptUnit container = getCurrentContainer();
|
||||
|
||||
// ==================== CALCULATE VIEWPORT BOUNDS ====================
|
||||
int editorX, editorY, editorWidth, editorHeight;
|
||||
@@ -207,13 +208,13 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
GuiScriptTextArea activeArea = getActiveScriptArea();
|
||||
if (activeArea == null) {
|
||||
activeArea = new GuiScriptTextArea(this, 2, editorX, editorY, editorWidth, editorHeight,
|
||||
container == null ? "" : container.script);
|
||||
container == null ? "" : container.getScript());
|
||||
activeArea.setListener(this);
|
||||
this.closeOnEsc(activeArea::closeOnEsc);
|
||||
textAreas.put(idx, activeArea);
|
||||
} else {
|
||||
activeArea.init(editorX, editorY, editorWidth, editorHeight,
|
||||
container == null ? "" : container.script);
|
||||
container == null ? "" : container.getScript());
|
||||
}
|
||||
|
||||
// Setup fullscreen key binding
|
||||
@@ -244,7 +245,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
scroll.guiLeft = left1;
|
||||
scroll.guiTop = this.guiTop + 88 + yoffset;
|
||||
if (container != null) {
|
||||
scroll.setList(container.scripts);
|
||||
scroll.setList(container.getExternalScripts());
|
||||
}
|
||||
this.addScroll(scroll);
|
||||
}
|
||||
@@ -420,7 +421,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
* Get the current script container.
|
||||
* Override in subclasses with different container access patterns.
|
||||
*/
|
||||
protected ScriptContainer getCurrentContainer() {
|
||||
protected IScriptUnit getCurrentContainer() {
|
||||
int idx = getActiveScriptIndex();
|
||||
if (idx >= 0 && idx < this.handler.getScripts().size())
|
||||
return this.handler.getScripts().get(idx);
|
||||
@@ -437,7 +438,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
return super.getTextField(id);
|
||||
}
|
||||
|
||||
public ScriptContainer getFirst() {
|
||||
public IScriptUnit getFirst() {
|
||||
if (this.handler.getScripts().isEmpty())
|
||||
return null;
|
||||
return this.handler.getScripts().get(0);
|
||||
@@ -530,7 +531,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
|
||||
protected void setScript() {
|
||||
if (this.activeTab > 0 || useScrollTabs) {
|
||||
ScriptContainer container = getCurrentContainer();
|
||||
IScriptUnit container = getCurrentContainer();
|
||||
if (container == null) {
|
||||
container = new ScriptContainer(this.handler);
|
||||
if (singleContainer)
|
||||
@@ -542,7 +543,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
String text = (this.getTextField(2)).getText();
|
||||
text = text.replace("\r\n", "\n");
|
||||
text = text.replace("\r", "\n");
|
||||
container.script = text;
|
||||
container.setScript(text);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -629,7 +630,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
/**
|
||||
* Set the handler's container. Override if handler is not IScriptHandler.
|
||||
*/
|
||||
protected void setHandlerContainer(ScriptContainer container) {
|
||||
protected void setHandlerContainer(IScriptUnit container) {
|
||||
// Default implementation - subclasses may need to cast and set differently
|
||||
// e.g., ((LinkedItemScript) handler).container = container;
|
||||
}
|
||||
@@ -642,9 +643,9 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
if (loaded) {
|
||||
this.setScript();
|
||||
|
||||
List<ScriptContainer> containers = this.handler.getScripts();
|
||||
List<IScriptUnit> containers = this.handler.getScripts();
|
||||
for (int i = 0; i < containers.size(); i++) {
|
||||
ScriptContainer container = containers.get(i);
|
||||
IScriptUnit container = containers.get(i);
|
||||
sendSavePacket(i, containers.size(), container.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
|
||||
@@ -673,9 +674,9 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
}
|
||||
|
||||
public void textUpdate(String text) {
|
||||
ScriptContainer container = getCurrentContainer();
|
||||
IScriptUnit container = getCurrentContainer();
|
||||
if (container != null) {
|
||||
container.script = text;
|
||||
container.setScript(text);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -684,9 +685,9 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
@Override
|
||||
//NEVER USED
|
||||
public void saveText(String text) {
|
||||
ScriptContainer container = getCurrentContainer();
|
||||
IScriptUnit container = getCurrentContainer();
|
||||
if (container != null)
|
||||
container.script = text;
|
||||
container.setScript(text);
|
||||
initGui();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptHookController;
|
||||
import noppes.npcs.controllers.data.EffectScript;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.controllers.data.PlayerDataScript;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -49,9 +50,9 @@ public class GuiScriptPlayers extends GuiScriptInterface {
|
||||
public void save() {
|
||||
if (loaded) {
|
||||
super.save();
|
||||
List<ScriptContainer> containers = this.script.getScripts();
|
||||
List<IScriptUnit> containers = this.script.getScripts();
|
||||
for (int i = 0; i < containers.size(); i++) {
|
||||
ScriptContainer container = containers.get(i);
|
||||
IScriptUnit container = containers.get(i);
|
||||
PlayerScriptPacket.Save(i, containers.size(), container.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
NBTTagCompound scriptData = new NBTTagCompound();
|
||||
|
||||
@@ -7,6 +7,7 @@ import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.data.IScriptHandler;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
|
||||
import javax.script.Compilable;
|
||||
@@ -24,7 +25,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class ScriptContainer {
|
||||
public class ScriptContainer implements IScriptUnit {
|
||||
private static final String lock = "lock";
|
||||
public static ScriptContainer Current;
|
||||
private static String CurrentType;
|
||||
@@ -251,6 +252,7 @@ public class ScriptContainer {
|
||||
return evaluated && !errored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCode() {
|
||||
if (!scripts.isEmpty())
|
||||
return true;
|
||||
@@ -267,4 +269,73 @@ public class ScriptContainer {
|
||||
this.evaluated = false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== IScriptUnit IMPLEMENTATION ====================
|
||||
|
||||
@Override
|
||||
public String getScript() {
|
||||
return this.script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScript(String script) {
|
||||
this.script = script;
|
||||
this.evaluated = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getExternalScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExternalScripts(List<String> scripts) {
|
||||
this.scripts = scripts;
|
||||
this.evaluated = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeMap<Long, String> getConsole() {
|
||||
return this.console;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
this.console.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
// If a specific language is set for this container, use it
|
||||
// Otherwise fall back to the handler's language
|
||||
if (currentScriptLanguage != null) {
|
||||
return currentScriptLanguage;
|
||||
}
|
||||
return handler != null ? handler.getLanguage() : "ECMAScript";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLanguage(String language) {
|
||||
if (!Objects.equals(this.currentScriptLanguage, language)) {
|
||||
this.currentScriptLanguage = language;
|
||||
this.evaluated = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateHookStub(String hookName, Object hookData) {
|
||||
// ECMAScript function template
|
||||
return "function " + hookName + "(event) {\n \n}\n";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasErrored() {
|
||||
return this.errored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrored(boolean errored) {
|
||||
this.errored = errored;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,16 +8,21 @@ import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import org.codehaus.commons.compiler.InternalCompilerException;
|
||||
import org.codehaus.commons.compiler.Sandbox;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.security.*;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class JaninoScript<T> {
|
||||
public abstract class JaninoScript<T> implements IScriptUnit {
|
||||
public boolean enabled;
|
||||
public boolean errored = false;
|
||||
private String language = "Java";
|
||||
public TreeMap<Long, String> console = new TreeMap();
|
||||
|
||||
@@ -184,18 +189,132 @@ public abstract class JaninoScript<T> {
|
||||
sb.append(code).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== IScriptUnit IMPLEMENTATION ====================
|
||||
|
||||
@Override
|
||||
public String getScript() {
|
||||
return this.script;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScript(String script) {
|
||||
this.script = script;
|
||||
this.evaluated = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getExternalScripts() {
|
||||
return this.externalScripts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExternalScripts(List<String> externalScripts) {
|
||||
this.externalScripts = externalScripts;
|
||||
this.evaluated = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeMap<Long, String> getConsole() {
|
||||
return this.console;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearConsole() {
|
||||
console.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendConsole(String message) {
|
||||
if (message != null && !message.isEmpty()) {
|
||||
long time = System.currentTimeMillis();
|
||||
if (this.console.containsKey(time)) {
|
||||
message = (String) this.console.get(time) + "\n" + message;
|
||||
}
|
||||
|
||||
this.console.put(time, message);
|
||||
|
||||
while (this.console.size() > 40) {
|
||||
this.console.remove(this.console.firstKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return this.language;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLanguage(String language) {
|
||||
// JaninoScript is always Java, ignore attempts to change
|
||||
// This method exists for IScriptUnit interface compatibility
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCode() {
|
||||
if (!externalScripts.isEmpty())
|
||||
return true;
|
||||
return script != null && !script.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateHookStub(String hookName, Object hookData) {
|
||||
// If hookData is a Method, generate full Java method stub
|
||||
if (hookData instanceof Method) {
|
||||
return generateMethodStub((Method) hookData);
|
||||
}
|
||||
// Fallback for simple hook names - generate basic override
|
||||
return String.format("@Override\npublic void %s() {\n \n}\n", hookName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a stub string for a given Method.
|
||||
*/
|
||||
public static String generateMethodStub(Method method) {
|
||||
String mods = Modifier.toString(method.getModifiers());
|
||||
// Remove 'abstract' from modifiers for implementation
|
||||
mods = mods.replace("abstract ", "").replace("abstract", "").trim();
|
||||
if (!mods.isEmpty())
|
||||
mods += " ";
|
||||
|
||||
String returnTypeStr = method.getReturnType().getSimpleName();
|
||||
String name = method.getName();
|
||||
|
||||
Map<String, Integer> typeCount = new HashMap<>();
|
||||
String params = Arrays.stream(method.getParameters())
|
||||
.map(p -> {
|
||||
String typeName = p.getType().getSimpleName();
|
||||
String baseName = Character.toLowerCase(typeName.charAt(0)) + typeName.substring(1);
|
||||
|
||||
int count = typeCount.getOrDefault(baseName, 0) + 1;
|
||||
typeCount.put(baseName, count);
|
||||
|
||||
String paramName = count == 1 ? baseName : baseName + (count - 1);
|
||||
return typeName + " " + paramName;
|
||||
})
|
||||
.collect(Collectors.joining(", "));
|
||||
|
||||
String body;
|
||||
Class<?> returnType = method.getReturnType();
|
||||
if (returnType == void.class)
|
||||
body = "";
|
||||
else if (returnType.isPrimitive()) {
|
||||
if (returnType == boolean.class)
|
||||
body = " return false;";
|
||||
else if (returnType == char.class)
|
||||
body = " return '\\0';";
|
||||
else
|
||||
body = " return 0;";
|
||||
} else {
|
||||
body = " return null;";
|
||||
}
|
||||
|
||||
return String.format("@Override\n%s%s %s(%s) {\n%s\n}\n", mods, returnTypeStr, name, params, body);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
compound.setBoolean("enabled", enabled);
|
||||
compound.setTag("console", NBTTags.NBTLongStringMap(this.console));
|
||||
@@ -204,12 +323,12 @@ public abstract class JaninoScript<T> {
|
||||
return compound;
|
||||
}
|
||||
|
||||
public JaninoScript readFromNBT(NBTTagCompound compound) {
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
this.enabled = compound.getBoolean("enabled");
|
||||
this.console = NBTTags.GetLongStringMap(compound.getTagList("console", 10));
|
||||
setExternalScripts(NBTTags.getStringList(compound.getTagList("externalScripts", 10)));
|
||||
setScript(compound.getString("script")); //after everything else is read
|
||||
return this;
|
||||
setScript(compound.getString("script"));
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
@@ -233,25 +352,6 @@ public abstract class JaninoScript<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
return this.language;
|
||||
}
|
||||
|
||||
public void appendConsole(String message) {
|
||||
if (message != null && !message.isEmpty()) {
|
||||
long time = System.currentTimeMillis();
|
||||
if (this.console.containsKey(time)) {
|
||||
message = (String) this.console.get(time) + "\n" + message;
|
||||
}
|
||||
|
||||
this.console.put(time, message);
|
||||
|
||||
while (this.console.size() > 40) {
|
||||
this.console.remove(this.console.firstKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Long, String> getConsoleText() {
|
||||
TreeMap<Long, String> map = new TreeMap();
|
||||
int tab = 0;
|
||||
@@ -264,13 +364,16 @@ public abstract class JaninoScript<T> {
|
||||
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + (String) longStringEntry.getValue());
|
||||
}
|
||||
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public void clearConsole() {
|
||||
console.clear();
|
||||
|
||||
@Override
|
||||
public boolean hasErrored() {
|
||||
return this.errored;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void setErrored(boolean errored) {
|
||||
this.errored = errored;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import noppes.npcs.EventHooks;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.api.item.IItemCustom;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.controllers.data.INpcScriptHandler;
|
||||
@@ -19,7 +20,7 @@ import java.util.Objects;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCustom, INpcScriptHandler {
|
||||
public List<ScriptContainer> scripts = new ArrayList();
|
||||
public List<IScriptUnit> scripts = new ArrayList();
|
||||
public List<Integer> errored = new ArrayList();
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
public boolean enabled = false;
|
||||
@@ -95,14 +96,16 @@ public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCus
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.scripts.size(); i++) {
|
||||
ScriptContainer script = this.scripts.get(i);
|
||||
IScriptUnit script = this.scripts.get(i);
|
||||
if (!this.errored.contains(i)) {
|
||||
if (script == null || script.errored || !script.hasCode())
|
||||
if (script == null || script.hasErrored() || !script.hasCode())
|
||||
continue;
|
||||
|
||||
script.run(hookName, event);
|
||||
if (script instanceof ScriptContainer) {
|
||||
((ScriptContainer) script).run(hookName, event);
|
||||
}
|
||||
|
||||
if (script.errored) {
|
||||
if (script.hasErrored()) {
|
||||
this.errored.add(i);
|
||||
}
|
||||
}
|
||||
@@ -129,11 +132,11 @@ public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCus
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user