2022-05-29 16:01:05 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2022-11-03 02:47:55 -04:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
2021-12-01 15:31:05 -06:00
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
2022-11-03 02:47:55 -04:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
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;
|
2022-11-03 02:47:55 -04:00
|
|
|
import noppes.npcs.LogWriter;
|
2023-03-16 04:27:19 -05:00
|
|
|
import noppes.npcs.NBTTags;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.api.IWorld;
|
|
|
|
|
import noppes.npcs.api.entity.ICustomNpc;
|
2023-01-07 00:21:08 -05:00
|
|
|
import noppes.npcs.config.ConfigDebug;
|
|
|
|
|
import noppes.npcs.config.ConfigScript;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
|
|
|
|
import noppes.npcs.controllers.ScriptContainer;
|
|
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
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;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.scripted.entity.ScriptNpc;
|
|
|
|
|
|
|
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
|
import java.util.*;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2023-07-13 13:56:32 -05:00
|
|
|
public class DataScript implements INpcScriptHandler {
|
2023-05-10 22:57:58 -05:00
|
|
|
public List<ScriptContainer> eventScripts = new ArrayList<>();
|
2023-03-16 04:27:19 -05:00
|
|
|
|
2023-05-10 22:57:58 -05:00
|
|
|
private HashMap<EnumScriptType,ScriptContainer> scripts = new HashMap<>();
|
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();
|
2022-11-07 01:49:54 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public String scriptLanguage = "ECMAScript";
|
|
|
|
|
private EntityNPCInterface npc;
|
|
|
|
|
public boolean enabled = false;
|
2022-11-07 01:49:54 -05:00
|
|
|
|
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;
|
2022-11-07 01:49:54 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public DataScript(EntityNPCInterface npc) {
|
2023-03-16 04:56:33 -05:00
|
|
|
for (int i = 0; i < 15; i++) {
|
2023-05-10 22:57:58 -05:00
|
|
|
this.setNPCScript(i, new ScriptContainer(this));
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-29 23:57:45 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
enabled = compound.getBoolean("ScriptEnabled");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 04:27:19 -05:00
|
|
|
public void readEventsFromNBT(NBTTagCompound compound) {
|
|
|
|
|
eventScripts = NBTTags.GetScript(compound,this);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
|
|
|
|
compound.setTag("ScriptsContainers", writeScript(scripts));
|
|
|
|
|
compound.setString("ScriptLanguage", scriptLanguage);
|
|
|
|
|
compound.setBoolean("ScriptEnabled", enabled);
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
2022-11-07 01:49:54 -05:00
|
|
|
|
2023-03-16 04:27:19 -05:00
|
|
|
public NBTTagCompound writeEventsToNBT(NBTTagCompound compound) {
|
|
|
|
|
compound.setInteger("TotalScripts",this.eventScripts.size());
|
|
|
|
|
for (int i = 0; i < this.eventScripts.size(); i++) {
|
|
|
|
|
compound.setTag("Tab"+i,this.eventScripts.get(i).writeToNBT(new NBTTagCompound()));
|
|
|
|
|
}
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 22:57:58 -05:00
|
|
|
private HashMap<EnumScriptType,ScriptContainer> readScript(NBTTagList list){
|
|
|
|
|
HashMap<EnumScriptType,ScriptContainer> scripts = new HashMap<>();
|
2023-03-16 23:58:45 -05:00
|
|
|
for (int i = 0; i < 15; i++) {
|
2023-05-10 22:57:58 -05:00
|
|
|
scripts.put(EnumScriptType.values()[i], new ScriptContainer(this));
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
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);
|
2023-05-10 22:57:58 -05:00
|
|
|
if (script.hasCode() || npc.isRemote()) {
|
|
|
|
|
scripts.put(EnumScriptType.values()[compoundd.getInteger("Type")], script);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
return scripts;
|
|
|
|
|
}
|
2022-11-07 01:49:54 -05:00
|
|
|
|
2023-05-10 22:57:58 -05:00
|
|
|
private NBTTagList writeScript(HashMap<EnumScriptType,ScriptContainer> scripts){
|
2018-09-13 12:03:28 -04:00
|
|
|
NBTTagList list = new NBTTagList();
|
2023-05-10 22:57:58 -05:00
|
|
|
for (Map.Entry<EnumScriptType,ScriptContainer> entry : scripts.entrySet()) {
|
|
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
2023-08-01 16:39:08 -05:00
|
|
|
ScriptContainer container = entry.getValue();
|
|
|
|
|
if (container.hasCode()) {
|
|
|
|
|
tagCompound.setInteger("Type", entry.getKey().ordinal());
|
|
|
|
|
container.writeToNBT(tagCompound);
|
|
|
|
|
list.appendTag(tagCompound);
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 18:32:42 -05:00
|
|
|
public boolean callScript(EnumScriptType type, Event event, Object... obs){
|
2018-09-13 12:03:28 -04:00
|
|
|
if(aiNeedsUpdate){
|
|
|
|
|
npc.updateAI = true;
|
|
|
|
|
aiNeedsUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
if(clientNeedsUpdate){
|
|
|
|
|
npc.updateClient = true;
|
|
|
|
|
clientNeedsUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
if(!isEnabled())
|
|
|
|
|
return false;
|
2023-03-16 04:27:19 -05:00
|
|
|
|
2022-06-10 18:32:42 -05:00
|
|
|
if(!hasInited && !npc.isRemote() && type != EnumScriptType.INIT){
|
2018-09-13 12:03:28 -04:00
|
|
|
hasInited = true;
|
2023-03-16 16:43:05 -05:00
|
|
|
for (ScriptContainer scriptContainer : this.eventScripts) {
|
|
|
|
|
scriptContainer.errored = false;
|
|
|
|
|
}
|
2022-03-23 03:54:06 -05:00
|
|
|
EventHooks.onNPCInit(this.npc);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
2023-03-16 16:43:05 -05:00
|
|
|
|
|
|
|
|
for (ScriptContainer script : this.eventScripts) {
|
2023-03-16 04:27:19 -05:00
|
|
|
script.run(type, event);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 22:57:58 -05:00
|
|
|
ScriptContainer script = scripts.get(type);
|
2018-09-13 12:03:28 -04:00
|
|
|
if(script == null || script.errored || !script.hasCode())
|
|
|
|
|
return false;
|
2023-06-02 17:17:00 -05:00
|
|
|
|
|
|
|
|
script.setEngine(this.getLanguage());
|
2018-09-13 12:03:28 -04:00
|
|
|
if(script.engine == null)
|
|
|
|
|
return false;
|
2023-06-02 17:17:00 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
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);
|
|
|
|
|
}
|
2023-01-07 00:21:08 -05:00
|
|
|
if(ConfigDebug.ScriptLogging && FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER){
|
2022-11-07 01:49:54 -05:00
|
|
|
if(obs.length > 1 && obs[1] == null){
|
|
|
|
|
LogWriter.postScriptLog(npc.field_110179_h, type, String.format("[%s] NPC %s (%s, %s, %s)", ((String)type.function).toUpperCase(), npc.display.name, (int)npc.posX, (int)npc.posY, (int)npc.posZ));
|
|
|
|
|
} else {
|
|
|
|
|
LogWriter.postScriptLog(npc.field_110179_h, type, String.format("[%s] NPC %s (%s, %s, %s) | Objects: %s", ((String)type.function).toUpperCase(), npc.display.name, (int)npc.posX, (int)npc.posY, (int)npc.posZ, Arrays.toString(obs)));
|
|
|
|
|
}
|
2022-11-03 02:47:55 -04:00
|
|
|
}
|
2022-06-10 18:32:42 -05:00
|
|
|
return callScript(script, event);
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
2021-10-12 04:20:33 -05:00
|
|
|
|
2022-06-10 18:32:42 -05:00
|
|
|
private boolean callScript(ScriptContainer script, Event event){
|
2018-09-13 12:03:28 -04:00
|
|
|
ScriptEngine engine = script.engine;
|
|
|
|
|
engine.put("npc", dummyNpc);
|
|
|
|
|
engine.put("world", dummyWorld);
|
2022-06-10 18:32:42 -05:00
|
|
|
engine.put("event", event);
|
2023-05-30 01:27:34 -05:00
|
|
|
engine.put("API", NpcAPI.Instance());
|
2018-09-13 12:03:28 -04:00
|
|
|
engine.put("EntityType", entities);
|
|
|
|
|
engine.put("RoleType", roles);
|
|
|
|
|
engine.put("JobType", jobs);
|
2022-12-14 17:53:57 -06:00
|
|
|
for (Map.Entry<String,Object> engineObjects : NpcAPI.engineObjects.entrySet()) {
|
|
|
|
|
engine.put(engineObjects.getKey(),engineObjects.getValue());
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2022-06-10 18:32:42 -05:00
|
|
|
return event.isCanceled();
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
2022-11-07 01:49:54 -05:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public boolean isEnabled(){
|
2023-01-07 00:21:08 -05:00
|
|
|
return enabled && ScriptController.HasStart && !npc.worldObj.isRemote && !scripts.isEmpty() && ConfigScript.ScriptingEnabled;
|
2021-10-11 06:49:30 -05:00
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2023-03-16 16:43:05 -05:00
|
|
|
public Map<Long, String> getOldConsoleText() {
|
2023-05-10 22:57:58 -05:00
|
|
|
Map<Long, String> map = new TreeMap<>();
|
2021-12-01 15:31:05 -06:00
|
|
|
|
2023-05-10 22:57:58 -05:00
|
|
|
for (Map.Entry<EnumScriptType,ScriptContainer> entry : scripts.entrySet()) {
|
|
|
|
|
for (Map.Entry<Long,String> consoleEntry : entry.getValue().console.entrySet()) {
|
|
|
|
|
map.put(consoleEntry.getKey(), " tab " + entry.getKey().ordinal() + ":\n" + consoleEntry.getValue());
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 16:43:05 -05:00
|
|
|
public Map<Long, String> getConsoleText() {
|
|
|
|
|
TreeMap<Long, String> map = new TreeMap<>();
|
|
|
|
|
int tab = 0;
|
|
|
|
|
for (ScriptContainer script : this.getScripts()) {
|
|
|
|
|
++tab;
|
2021-12-01 15:31:05 -06:00
|
|
|
|
2023-03-16 16:43:05 -05:00
|
|
|
for (Map.Entry<Long, String> longStringEntry : script.console.entrySet()) {
|
|
|
|
|
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
|
|
|
|
|
}
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
2023-03-16 16:43:05 -05:00
|
|
|
return map;
|
|
|
|
|
}
|
2021-12-01 15:31:05 -06:00
|
|
|
|
2023-03-16 16:43:05 -05:00
|
|
|
public void clearConsole() {
|
|
|
|
|
for (ScriptContainer script : this.getScripts()) {
|
|
|
|
|
script.console.clear();
|
|
|
|
|
}
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:54:05 -06:00
|
|
|
@Override
|
2023-03-16 04:27:19 -05:00
|
|
|
public void callScript(EnumScriptType type, Event event) {
|
|
|
|
|
callScript(type, event, "$$IGNORED$$", null);
|
2021-12-16 21:54:05 -06:00
|
|
|
}
|
|
|
|
|
|
2023-07-13 13:56:32 -05:00
|
|
|
@Override
|
|
|
|
|
public void callScript(String hookName, Event event) {
|
|
|
|
|
try {
|
2024-08-22 16:47:45 -04:00
|
|
|
EnumScriptType enumScriptType = EnumScriptType.valueOf(hookName);
|
2023-07-13 13:56:32 -05:00
|
|
|
this.callScript(enumScriptType, event);
|
|
|
|
|
} catch (IllegalArgumentException ignored) {}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 16:22:28 -06:00
|
|
|
public void setScripts(List<ScriptContainer> list) {
|
2023-05-10 22:57:58 -05:00
|
|
|
this.eventScripts = list;
|
2023-02-19 16:22:28 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
public List<ScriptContainer> getScripts() {
|
2023-03-16 04:27:19 -05:00
|
|
|
return this.eventScripts;
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2022-11-07 01:49:54 -05:00
|
|
|
|
2023-05-10 22:57:58 -05:00
|
|
|
public ScriptContainer getNPCScript(EnumScriptType scriptType) {
|
|
|
|
|
return this.scripts.get(scriptType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ScriptContainer getNPCScript(int ordinal) {
|
|
|
|
|
return this.getNPCScript(EnumScriptType.values()[ordinal]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setNPCScript(EnumScriptType scriptType, ScriptContainer container) {
|
|
|
|
|
this.scripts.put(scriptType, container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setNPCScript(int ordinal, ScriptContainer container) {
|
|
|
|
|
this.setNPCScript(EnumScriptType.values()[ordinal], container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Collection<ScriptContainer> getNPCScripts() {
|
|
|
|
|
return this.scripts.values();
|
|
|
|
|
}
|
2024-03-24 17:03:58 -05:00
|
|
|
}
|