mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-22 13:10:04 +00:00
Consolidated EventScriptContainer and ScriptContainer into a single ScriptContainer class. NPCs are now able to fly when ai.movementType == 1.
This commit is contained in:
1 parent
f3ebf86b1e
commit
72869e6a78
14 files changed
+422
-457
No files matched your search
@@ -8,7 +8,6 @@ package noppes.npcs.controllers;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import noppes.npcs.EventScriptContainer;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
|
||||
public interface IScriptHandler {
|
||||
@@ -24,7 +23,7 @@ public interface IScriptHandler {
|
||||
|
||||
void setLanguage(String var1);
|
||||
|
||||
List<EventScriptContainer> getScripts();
|
||||
List<ScriptContainer> getScripts();
|
||||
|
||||
String noticeString();
|
||||
|
||||
|
||||
@@ -1,94 +1,225 @@
|
||||
//
|
||||
// Source code recreated from a .class file by IntelliJ IDEA
|
||||
// (powered by Fernflower decompiler)
|
||||
//
|
||||
|
||||
package noppes.npcs.controllers;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import javax.script.Compilable;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.NoppesUtilServer;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
|
||||
public class ScriptContainer {
|
||||
public String fullscript = "";
|
||||
public String script = "";
|
||||
public String console = "";
|
||||
public boolean errored = false;
|
||||
public List<String> scripts = new ArrayList<String>();
|
||||
private long lastCreated = 0;
|
||||
private CompiledScript compScript = null;
|
||||
private String currentScriptLanguage = null;
|
||||
public ScriptEngine engine = null;
|
||||
private static final String lock = "lock";
|
||||
public static ScriptContainer Current;
|
||||
private static String CurrentType;
|
||||
private static final HashMap<String, Object> Data = new HashMap();
|
||||
public String fullscript = "";
|
||||
public String script = "";
|
||||
public TreeMap<Long, String> console = new TreeMap();
|
||||
public boolean errored = false;
|
||||
public List<String> scripts = new ArrayList();
|
||||
private HashSet<String> unknownFunctions = new HashSet();
|
||||
public long lastCreated = 0L;
|
||||
private String currentScriptLanguage = null;
|
||||
public ScriptEngine engine = null;
|
||||
private IScriptHandler handler = null;
|
||||
private boolean init = false;
|
||||
private static Method luaCoerce;
|
||||
private static Method luaCall;
|
||||
private CompiledScript compScript = null;
|
||||
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
script = compound.getString("Script");
|
||||
console = compound.getString("ScriptConsole");
|
||||
scripts = NBTTags.getStringList(compound.getTagList("ScriptList", 10));
|
||||
lastCreated = 0;
|
||||
}
|
||||
public ScriptContainer(IScriptHandler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public void writeToNBT(NBTTagCompound compound) {
|
||||
compound.setString("Script", script);
|
||||
compound.setString("ScriptConsole", console);
|
||||
compound.setTag("ScriptList", NBTTags.nbtStringList(scripts));
|
||||
}
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
this.script = compound.getString("Script");
|
||||
//this.type = compound.getString("Type");
|
||||
this.console = NBTTags.GetLongStringMap(compound.getTagList("Console", 10));
|
||||
this.scripts = NBTTags.getStringList(compound.getTagList("ScriptList", 10));
|
||||
this.lastCreated = 0L;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
if(ScriptController.Instance.lastLoaded > lastCreated){
|
||||
lastCreated = ScriptController.Instance.lastLoaded;
|
||||
fullscript = script;
|
||||
if(!fullscript.isEmpty())
|
||||
fullscript += "\n";
|
||||
for(String loc : scripts){
|
||||
String code = ScriptController.Instance.scripts.get(loc);
|
||||
if(code != null && !code.isEmpty())
|
||||
fullscript += code + "\n";
|
||||
}
|
||||
compScript = null;
|
||||
}
|
||||
return fullscript;
|
||||
}
|
||||
|
||||
public void run(ScriptEngine engine){
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
engine.getContext().setWriter(pw);
|
||||
engine.getContext().setErrorWriter(pw);
|
||||
try {
|
||||
if(compScript == null && engine instanceof Compilable)
|
||||
compScript = ((Compilable)engine).compile(getCode());
|
||||
|
||||
if(compScript != null){
|
||||
compScript.eval(engine.getContext());
|
||||
}
|
||||
else
|
||||
engine.eval(getCode());
|
||||
|
||||
} catch (Exception e) {
|
||||
errored = true;
|
||||
appandConsole(e.getMessage());
|
||||
}
|
||||
appandConsole(sw.getBuffer().toString().trim());
|
||||
}
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
compound.setString("Script", this.script);
|
||||
//compound.setString("Type", this.type);
|
||||
compound.setTag("Console", NBTTags.NBTLongStringMap(this.console));
|
||||
compound.setTag("ScriptList", NBTTags.nbtStringList(this.scripts));
|
||||
return compound;
|
||||
}
|
||||
|
||||
public void appandConsole(String message) {
|
||||
if(message == null || message.isEmpty())
|
||||
return;
|
||||
console = message + "\n" + console;
|
||||
}
|
||||
private String getFullCode() {
|
||||
if(!this.init) {
|
||||
this.fullscript = this.script;
|
||||
if(!this.fullscript.isEmpty()) {
|
||||
this.fullscript = this.fullscript + "\n";
|
||||
}
|
||||
|
||||
public boolean hasCode() {
|
||||
return !getCode().isEmpty();
|
||||
}
|
||||
Iterator var1 = this.scripts.iterator();
|
||||
|
||||
public void setEngine(String scriptLanguage) {
|
||||
if(currentScriptLanguage != null && currentScriptLanguage.equals(scriptLanguage))
|
||||
return;
|
||||
engine = ScriptController.Instance.getEngineByName(scriptLanguage.toLowerCase());
|
||||
currentScriptLanguage = scriptLanguage;
|
||||
}
|
||||
while(var1.hasNext()) {
|
||||
String loc = (String)var1.next();
|
||||
String code = (String)ScriptController.Instance.scripts.get(loc);
|
||||
if(code != null && !code.isEmpty()) {
|
||||
this.fullscript = this.fullscript + code + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.fullscript;
|
||||
}
|
||||
|
||||
public void run(ScriptEngine engine){
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
engine.getContext().setWriter(pw);
|
||||
engine.getContext().setErrorWriter(pw);
|
||||
try {
|
||||
if(compScript == null && engine instanceof Compilable)
|
||||
compScript = ((Compilable)engine).compile(getFullCode());
|
||||
|
||||
if(compScript != null){
|
||||
compScript.eval(engine.getContext());
|
||||
}
|
||||
else
|
||||
engine.eval(getFullCode());
|
||||
|
||||
} catch (Exception e) {
|
||||
errored = true;
|
||||
appandConsole(e.getMessage());
|
||||
}
|
||||
appandConsole(sw.getBuffer().toString().trim());
|
||||
}
|
||||
|
||||
public void run(EnumScriptType type, Event event) {
|
||||
this.run((String)type.function, (Object)event);
|
||||
}
|
||||
|
||||
public void run(String type, Object event) {
|
||||
if (!this.errored && this.hasCode() && !this.unknownFunctions.contains(type)) {
|
||||
this.setEngine(this.handler.getLanguage());
|
||||
if (this.engine != null) {
|
||||
if (ScriptController.Instance.lastLoaded > this.lastCreated) {
|
||||
this.lastCreated = ScriptController.Instance.lastLoaded;
|
||||
this.init = false;
|
||||
}
|
||||
|
||||
String var3 = "lock";
|
||||
synchronized("lock") {
|
||||
Current = this;
|
||||
CurrentType = type;
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
this.engine.getContext().setWriter(pw);
|
||||
this.engine.getContext().setErrorWriter(pw);
|
||||
|
||||
try {
|
||||
if (!this.init) {
|
||||
this.engine.eval(this.getFullCode());
|
||||
this.init = true;
|
||||
}
|
||||
|
||||
if (this.engine.getFactory().getLanguageName().equals("lua")) {
|
||||
Object ob = this.engine.get(type);
|
||||
if (ob == null) {
|
||||
this.unknownFunctions.add(type);
|
||||
}
|
||||
} else {
|
||||
((Invocable)this.engine).invokeFunction(type, new Object[]{event});
|
||||
}
|
||||
} catch (NoSuchMethodException var13) {
|
||||
this.unknownFunctions.add(type);
|
||||
} catch (Throwable var14) {
|
||||
this.errored = true;
|
||||
var14.printStackTrace(pw);
|
||||
} finally {
|
||||
this.appandConsole(sw.getBuffer().toString().trim());
|
||||
pw.close();
|
||||
Current = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*public void run(String type, Object event) {
|
||||
if (!this.errored && this.hasCode() && !this.unknownFunctions.contains(type)) {
|
||||
this.setEngine(this.handler.getLanguage());
|
||||
if (this.engine != null) {
|
||||
if (ScriptController.Instance.lastLoaded > this.lastCreated) {
|
||||
this.lastCreated = ScriptController.Instance.lastLoaded;
|
||||
this.init = false;
|
||||
}
|
||||
|
||||
String var3 = "lock";
|
||||
synchronized("lock") {
|
||||
Current = this;
|
||||
CurrentType = type;
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
this.engine.getContext().setWriter(pw);
|
||||
this.engine.getContext().setErrorWriter(pw);
|
||||
|
||||
try {
|
||||
if (!this.init) {
|
||||
this.engine.eval(this.getFullCode());
|
||||
this.init = true;
|
||||
}
|
||||
|
||||
((Invocable)this.engine).invokeFunction(type, new Object[]{event});
|
||||
} catch (NoSuchMethodException var13) {
|
||||
this.unknownFunctions.add(type);
|
||||
} catch (Throwable var14) {
|
||||
this.errored = true;
|
||||
var14.printStackTrace(pw);
|
||||
} finally {
|
||||
this.appandConsole(sw.getBuffer().toString().trim());
|
||||
pw.close();
|
||||
Current = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
public void appandConsole(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 boolean hasCode() {
|
||||
return !this.getFullCode().isEmpty();
|
||||
}
|
||||
|
||||
public void setEngine(String scriptLanguage) {
|
||||
if(currentScriptLanguage != null && currentScriptLanguage.equals(scriptLanguage))
|
||||
return;
|
||||
engine = ScriptController.Instance.getEngineByName(scriptLanguage.toLowerCase());
|
||||
currentScriptLanguage = scriptLanguage;
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,9 @@ import java.util.TreeMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.EventHooks;
|
||||
import noppes.npcs.EventScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.IScriptHandler;
|
||||
@@ -26,7 +25,7 @@ import noppes.npcs.scripted.wrapper.WrapperNpcAPI;
|
||||
import javax.script.ScriptEngine;
|
||||
|
||||
public class ForgeDataScript implements IScriptHandler {
|
||||
private List<EventScriptContainer> scripts = new ArrayList();
|
||||
private List<ScriptContainer> scripts = new ArrayList();
|
||||
private String scriptLanguage = "ECMAScript";
|
||||
public long lastInited = -1L;
|
||||
private boolean enabled = false;
|
||||
@@ -69,7 +68,7 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
Iterator var3 = this.scripts.iterator();
|
||||
|
||||
while (var3.hasNext()) {
|
||||
EventScriptContainer script = (EventScriptContainer) var3.next();
|
||||
ScriptContainer script = (ScriptContainer) var3.next();
|
||||
|
||||
script.setEngine(scriptLanguage);
|
||||
if(script.engine == null)
|
||||
@@ -80,8 +79,6 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
script.engine.put("event", event);
|
||||
script.engine.put("API", new WrapperNpcAPI());
|
||||
|
||||
ScriptEngine engine = script.engine;
|
||||
|
||||
script.run(type, event);
|
||||
}
|
||||
//});
|
||||
@@ -112,7 +109,7 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public List<EventScriptContainer> getScripts() {
|
||||
public List<ScriptContainer> getScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
@@ -126,7 +123,7 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
Iterator var3 = this.getScripts().iterator();
|
||||
|
||||
while(var3.hasNext()) {
|
||||
EventScriptContainer script = (EventScriptContainer)var3.next();
|
||||
ScriptContainer script = (ScriptContainer)var3.next();
|
||||
++tab;
|
||||
Iterator var5 = script.console.entrySet().iterator();
|
||||
|
||||
@@ -143,7 +140,7 @@ public class ForgeDataScript implements IScriptHandler {
|
||||
Iterator var1 = this.getScripts().iterator();
|
||||
|
||||
while(var1.hasNext()) {
|
||||
EventScriptContainer script = (EventScriptContainer)var1.next();
|
||||
ScriptContainer script = (ScriptContainer)var1.next();
|
||||
script.console.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,21 +12,17 @@ import java.util.Map.Entry;
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import noppes.npcs.EventHooks;
|
||||
import noppes.npcs.EventScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.NBTTags;
|
||||
import noppes.npcs.controllers.PlayerData;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.entity.ScriptPlayer;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.controllers.IScriptHandler;
|
||||
import noppes.npcs.controllers.ScriptController;
|
||||
import noppes.npcs.scripted.ScriptWorld;
|
||||
import noppes.npcs.scripted.event.PlayerEvent;
|
||||
import noppes.npcs.scripted.interfaces.IPlayer;
|
||||
import noppes.npcs.scripted.interfaces.IWorld;
|
||||
@@ -36,7 +32,7 @@ import javax.annotation.CheckForNull;
|
||||
import javax.script.ScriptEngine;
|
||||
|
||||
public class PlayerDataScript implements IScriptHandler {
|
||||
public List<EventScriptContainer> scripts = new ArrayList();
|
||||
public List<ScriptContainer> scripts = new ArrayList();
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
private EntityPlayer player;
|
||||
private IPlayer playerAPI;
|
||||
@@ -82,71 +78,50 @@ public class PlayerDataScript implements IScriptHandler {
|
||||
}
|
||||
|
||||
public void callScript(EnumScriptType type, Event event, Object... obs) {
|
||||
if(this.isEnabled()) {
|
||||
EventScriptContainer script;
|
||||
if(ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
|
||||
if (this.isEnabled()) {
|
||||
ScriptContainer script;
|
||||
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
|
||||
this.lastInited = ScriptController.Instance.lastLoaded;
|
||||
//ScriptController.Instance.playerScripts.errored.clear();
|
||||
if(this.player != null) {
|
||||
errored.clear();
|
||||
if (this.player != null) {
|
||||
this.scripts.clear();
|
||||
Iterator i = ScriptController.Instance.playerScripts.scripts.iterator();
|
||||
Iterator var3 = ScriptController.Instance.playerScripts.scripts.iterator();
|
||||
|
||||
while(i.hasNext()) {
|
||||
script = (EventScriptContainer)i.next();
|
||||
EventScriptContainer s = new EventScriptContainer(this);
|
||||
while(var3.hasNext()) {
|
||||
script = (ScriptContainer)var3.next();
|
||||
ScriptContainer s = new ScriptContainer(this);
|
||||
s.readFromNBT(script.writeToNBT(new NBTTagCompound()));
|
||||
|
||||
this.scripts.add(s);
|
||||
}
|
||||
}
|
||||
|
||||
this.lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
|
||||
if(type != EnumScriptType.INIT) {
|
||||
if (type != EnumScriptType.INIT) {
|
||||
EventHooks.onPlayerInit(this);
|
||||
}
|
||||
}
|
||||
|
||||
for(int var7 = 0; var7 < this.scripts.size(); ++var7) {
|
||||
script = (EventScriptContainer)this.scripts.get(var7);
|
||||
|
||||
if(!ScriptController.Instance.playerScripts.errored.contains(Integer.valueOf(var7))) {
|
||||
if(script == null || script.errored || !script.hasCode())
|
||||
return;
|
||||
script.setEngine(scriptLanguage);
|
||||
if(script.engine == null)
|
||||
return;
|
||||
for(int i = 0; i + 1 < obs.length; i += 2){
|
||||
Object ob = obs[i + 1];
|
||||
if(ob instanceof Entity)
|
||||
ob = ScriptController.Instance.getScriptForEntity((Entity)ob);
|
||||
script.engine.put(obs[i].toString(), ob);
|
||||
}
|
||||
|
||||
ScriptEngine engine = script.engine;
|
||||
engine.put("world", dummyWorld);
|
||||
engine.put("player", dummyPlayer);
|
||||
PlayerEvent result = (PlayerEvent) engine.get("event");
|
||||
if(result == null)
|
||||
engine.put("event", result = new PlayerEvent(this.getPlayer()));
|
||||
script.engine.put("API", new WrapperNpcAPI());
|
||||
for(int i = 0; i < this.scripts.size(); ++i) {
|
||||
script = (ScriptContainer)this.scripts.get(i);
|
||||
if (!errored.contains(i)) {
|
||||
script.run(type, event);
|
||||
|
||||
if (script.errored) {
|
||||
ScriptController.Instance.playerScripts.errored.add(var7);
|
||||
errored.add(i);
|
||||
}
|
||||
|
||||
Iterator var8 = script.console.entrySet().iterator();
|
||||
|
||||
while(var8.hasNext()) {
|
||||
Entry<Long, String> entry = (Entry)var8.next();
|
||||
if (!ScriptController.Instance.playerScripts.console.containsKey(entry.getKey())) {
|
||||
ScriptController.Instance.playerScripts.console.put(entry.getKey(), " tab " + (var7 + 1) + ":\n" + (String)entry.getValue());
|
||||
if (!console.containsKey(entry.getKey())) {
|
||||
console.put(entry.getKey(), " tab " + (i + 1) + ":\n" + (String)entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
script.console.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +150,7 @@ public class PlayerDataScript implements IScriptHandler {
|
||||
this.scriptLanguage = lang;
|
||||
}
|
||||
|
||||
public List<EventScriptContainer> getScripts() {
|
||||
public List<ScriptContainer> getScripts() {
|
||||
return this.scripts;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user