2022-05-29 16:01:05 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
import java.util.*;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
2018-09-13 12:03:28 -04:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraft.world.WorldServer;
|
2022-05-29 16:01:05 -05:00
|
|
|
import noppes.npcs.EventHooks;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
|
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
2021-12-01 15:31:05 -06:00
|
|
|
import noppes.npcs.controllers.IScriptHandler;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
2022-05-11 22:55:26 -05:00
|
|
|
import noppes.npcs.scripted.entity.ScriptNpc;
|
2021-10-18 06:33:39 -05:00
|
|
|
import noppes.npcs.scripted.event.ScriptEvent;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.scripted.ScriptWorld;
|
|
|
|
|
import noppes.npcs.scripted.constants.EntityType;
|
|
|
|
|
import noppes.npcs.scripted.constants.JobType;
|
|
|
|
|
import noppes.npcs.scripted.constants.RoleType;
|
2022-05-11 16:43:14 -05:00
|
|
|
import noppes.npcs.scripted.interfaces.entity.ICustomNpc;
|
2021-11-10 02:21:03 -06:00
|
|
|
import noppes.npcs.scripted.interfaces.IWorld;
|
2022-05-11 22:08:31 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
public class DataScript implements IScriptHandler {
|
|
|
|
|
public List<ScriptContainer> scripts = new ArrayList();
|
2018-09-13 12:03:28 -04:00
|
|
|
private final static EntityType entities = new EntityType();
|
|
|
|
|
private final static JobType jobs = new JobType();
|
|
|
|
|
private final static RoleType roles = new RoleType();
|
|
|
|
|
|
|
|
|
|
public String scriptLanguage = "ECMAScript";
|
|
|
|
|
private EntityNPCInterface npc;
|
|
|
|
|
public boolean enabled = false;
|
|
|
|
|
|
2021-12-29 16:22:05 -06:00
|
|
|
public ICustomNpc dummyNpc;
|
2021-11-10 02:21:03 -06:00
|
|
|
public IWorld dummyWorld;
|
2018-09-13 12:03:28 -04:00
|
|
|
public boolean clientNeedsUpdate = false;
|
|
|
|
|
public boolean aiNeedsUpdate = false;
|
|
|
|
|
public boolean hasInited = false;
|
|
|
|
|
|
|
|
|
|
public DataScript(EntityNPCInterface npc) {
|
2021-12-01 17:42:48 -06:00
|
|
|
for (int i = 0; i < 12; i++) {
|
2021-12-01 15:31:05 -06:00
|
|
|
scripts.add(new ScriptContainer(this));
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
this.npc = npc;
|
2022-05-11 22:55:26 -05:00
|
|
|
if (npc.wrappedNPC == null) {
|
|
|
|
|
npc.wrappedNPC = new ScriptNpc(this.npc);
|
|
|
|
|
}
|
|
|
|
|
dummyNpc = npc.wrappedNPC;
|
2021-12-29 16:22:05 -06:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
if(npc.worldObj instanceof WorldServer)
|
2021-12-12 23:43:44 -06:00
|
|
|
dummyWorld = NpcAPI.Instance().getIWorld((WorldServer) npc.worldObj);//new ScriptWorld((WorldServer) npc.worldObj);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
|
|
|
|
scripts = readScript(compound.getTagList("ScriptsContainers", 10));
|
2022-05-29 16:01:05 -05:00
|
|
|
this.scriptLanguage = compound.getString("ScriptLanguage");
|
|
|
|
|
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
|
|
|
|
|
if (!ScriptController.Instance.languages.isEmpty()) {
|
|
|
|
|
this.scriptLanguage = (String) ScriptController.Instance.languages.keySet().toArray()[0];
|
|
|
|
|
} else {
|
|
|
|
|
this.scriptLanguage = "ECMAScript";
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
enabled = compound.getBoolean("ScriptEnabled");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
|
|
|
|
compound.setTag("ScriptsContainers", writeScript(scripts));
|
|
|
|
|
compound.setString("ScriptLanguage", scriptLanguage);
|
|
|
|
|
compound.setBoolean("ScriptEnabled", enabled);
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
private List<ScriptContainer> readScript(NBTTagList list){
|
|
|
|
|
List<ScriptContainer> scripts = new ArrayList<>();
|
2021-12-01 17:42:48 -06:00
|
|
|
for (int i = 0; i < 12; i++) {
|
2021-12-01 15:31:05 -06:00
|
|
|
scripts.add(new ScriptContainer(this));
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
for(int i = 0; i < list.tagCount(); i++){
|
|
|
|
|
NBTTagCompound compoundd = list.getCompoundTagAt(i);
|
2021-12-01 15:31:05 -06:00
|
|
|
ScriptContainer script = new ScriptContainer(this);
|
2018-09-13 12:03:28 -04:00
|
|
|
script.readFromNBT(compoundd);
|
2021-12-01 15:31:05 -06:00
|
|
|
if(script.hasCode() || npc.isRemote())
|
|
|
|
|
scripts.set(compoundd.getInteger("Type"), script);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
return scripts;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
private NBTTagList writeScript(List<ScriptContainer> scripts){
|
2018-09-13 12:03:28 -04:00
|
|
|
NBTTagList list = new NBTTagList();
|
2021-12-01 15:31:05 -06:00
|
|
|
for(int type = 0; type < scripts.size(); type++){
|
2018-09-13 12:03:28 -04:00
|
|
|
NBTTagCompound compoundd = new NBTTagCompound();
|
|
|
|
|
compoundd.setInteger("Type", type);
|
|
|
|
|
ScriptContainer script = scripts.get(type);
|
|
|
|
|
script.writeToNBT(compoundd);
|
|
|
|
|
list.appendTag(compoundd);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean callScript(EnumScriptType type, Object... obs){
|
|
|
|
|
if(aiNeedsUpdate){
|
|
|
|
|
npc.updateAI = true;
|
|
|
|
|
aiNeedsUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
if(clientNeedsUpdate){
|
|
|
|
|
npc.updateClient = true;
|
|
|
|
|
clientNeedsUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
if(!isEnabled())
|
|
|
|
|
return false;
|
|
|
|
|
if(!hasInited){
|
|
|
|
|
hasInited = true;
|
2022-03-23 03:54:06 -05:00
|
|
|
EventHooks.onNPCInit(this.npc);
|
2018-09-13 12:03:28 -04:00
|
|
|
callScript(EnumScriptType.INIT);
|
|
|
|
|
}
|
|
|
|
|
ScriptContainer script = scripts.get(type.ordinal());
|
|
|
|
|
if(script == null || script.errored || !script.hasCode())
|
|
|
|
|
return false;
|
|
|
|
|
script.setEngine(scriptLanguage);
|
|
|
|
|
if(script.engine == null)
|
|
|
|
|
return false;
|
|
|
|
|
for(int i = 0; i + 1 < obs.length; i += 2){
|
|
|
|
|
Object ob = obs[i + 1];
|
|
|
|
|
if(ob instanceof Entity)
|
2022-05-11 22:08:31 -05:00
|
|
|
ob = NpcAPI.Instance().getIEntity((Entity)ob);
|
2018-09-13 12:03:28 -04:00
|
|
|
script.engine.put(obs[i].toString(), ob);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 04:20:33 -05:00
|
|
|
return callScript(script);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
2021-10-12 04:20:33 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private boolean callScript(ScriptContainer script){
|
|
|
|
|
ScriptEngine engine = script.engine;
|
|
|
|
|
engine.put("npc", dummyNpc);
|
|
|
|
|
engine.put("world", dummyWorld);
|
|
|
|
|
ScriptEvent result = (ScriptEvent) engine.get("event");
|
|
|
|
|
if(result == null)
|
|
|
|
|
engine.put("event", result = new ScriptEvent());
|
2022-05-11 17:12:48 -05:00
|
|
|
engine.put("API", new NpcAPI());
|
2018-09-13 12:03:28 -04:00
|
|
|
engine.put("EntityType", entities);
|
|
|
|
|
engine.put("RoleType", roles);
|
|
|
|
|
engine.put("JobType", jobs);
|
|
|
|
|
script.run(engine);
|
2021-10-11 06:49:30 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
if(clientNeedsUpdate){
|
|
|
|
|
npc.updateClient = true;
|
|
|
|
|
clientNeedsUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
if(aiNeedsUpdate){
|
|
|
|
|
npc.updateAI = true;
|
|
|
|
|
aiNeedsUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
return result.isCancelled();
|
|
|
|
|
}
|
2021-10-11 06:49:30 -05:00
|
|
|
|
|
|
|
|
public boolean isEnabled(){
|
|
|
|
|
return enabled && ScriptController.HasStart && !npc.worldObj.isRemote && !scripts.isEmpty();
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
public void callScript(EnumScriptType var1, Event var2, Object... obs) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Map<Long, String> getConsoleText() {
|
|
|
|
|
Map<Long, String> map = new TreeMap();
|
|
|
|
|
int tab = 0;
|
|
|
|
|
Iterator var3 = this.getScripts().iterator();
|
|
|
|
|
|
|
|
|
|
while(var3.hasNext()) {
|
|
|
|
|
ScriptContainer script = (ScriptContainer)var3.next();
|
|
|
|
|
++tab;
|
|
|
|
|
Iterator var5 = script.console.entrySet().iterator();
|
|
|
|
|
|
|
|
|
|
while(var5.hasNext()) {
|
|
|
|
|
Map.Entry<Long, String> entry = (Map.Entry)var5.next();
|
|
|
|
|
map.put(entry.getKey(), " tab " + tab + ":\n" + (String)entry.getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void clearConsole() {
|
|
|
|
|
Iterator var1 = this.getScripts().iterator();
|
|
|
|
|
|
|
|
|
|
while(var1.hasNext()) {
|
|
|
|
|
ScriptContainer script = (ScriptContainer)var1.next();
|
|
|
|
|
script.console.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:54:05 -06:00
|
|
|
@Override
|
|
|
|
|
public void callScript(EnumScriptType var1, Event var2) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
public boolean isClient() {
|
|
|
|
|
return this.npc.isRemote();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getEnabled() {
|
|
|
|
|
return this.enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setEnabled(boolean bo) {
|
|
|
|
|
this.enabled = bo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getLanguage() {
|
|
|
|
|
return this.scriptLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLanguage(String lang) {
|
|
|
|
|
this.scriptLanguage = lang;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ScriptContainer> getScripts() {
|
|
|
|
|
return this.scripts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String noticeString() {
|
|
|
|
|
//BlockPos pos = this.npc.func_180425_c();
|
|
|
|
|
//return MoreObjects.toStringHelper(this.npc).add("x", pos.func_177958_n()).add("y", pos.func_177956_o()).add("z", pos.func_177952_p()).toString();
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public void setWorld(World world) {
|
|
|
|
|
if(world instanceof WorldServer)
|
|
|
|
|
dummyWorld = new ScriptWorld((WorldServer) world);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|