2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
2023-01-12 21:33:06 -05:00
|
|
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
2021-09-30 01:48:54 -05:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2018-09-13 12:03:28 -04:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
import net.minecraft.nbt.NBTTagString;
|
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
|
import net.minecraftforge.event.world.WorldEvent;
|
|
|
|
|
import noppes.npcs.CustomNpcs;
|
|
|
|
|
import noppes.npcs.LogWriter;
|
2021-10-11 06:49:30 -05:00
|
|
|
import noppes.npcs.controllers.data.ForgeDataScript;
|
2022-03-23 03:54:06 -05:00
|
|
|
import noppes.npcs.controllers.data.NPCDataScript;
|
2021-10-11 06:49:30 -05:00
|
|
|
import noppes.npcs.controllers.data.PlayerDataScript;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.entity.EntityNPCInterface;
|
|
|
|
|
import noppes.npcs.scripted.ScriptWorld;
|
2021-09-30 01:48:54 -05:00
|
|
|
import noppes.npcs.util.JsonException;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.util.NBTJsonUtil;
|
2023-01-12 21:33:06 -05:00
|
|
|
|
|
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
|
import javax.script.ScriptEngineFactory;
|
|
|
|
|
import javax.script.ScriptEngineManager;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
public class ScriptController {
|
2021-10-19 02:49:46 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public static ScriptController Instance;
|
|
|
|
|
public static boolean HasStart = false;
|
|
|
|
|
private ScriptEngineManager manager;
|
|
|
|
|
public Map<String, String> languages = new HashMap<String, String>();
|
|
|
|
|
public Map<String, String> scripts = new HashMap<String, String>();
|
|
|
|
|
public long lastLoaded = 0;
|
|
|
|
|
public File dir;
|
|
|
|
|
public NBTTagCompound compound = new NBTTagCompound();
|
2021-09-30 01:48:54 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private boolean loaded = false;
|
|
|
|
|
public boolean shouldSave = false;
|
2021-09-30 01:48:54 -05:00
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public PlayerDataScript playerScripts = new PlayerDataScript((EntityPlayer)null);
|
2021-09-30 01:48:54 -05:00
|
|
|
public long lastPlayerUpdate = 0L;
|
|
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
public ForgeDataScript forgeScripts = new ForgeDataScript();
|
|
|
|
|
|
2022-03-23 03:54:06 -05:00
|
|
|
public NPCDataScript npcScripts = new NPCDataScript((EntityNPCInterface) null);
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
public ScriptController(){
|
|
|
|
|
loaded = false;
|
|
|
|
|
Instance = this;
|
|
|
|
|
manager = new ScriptEngineManager();
|
|
|
|
|
LogWriter.info("Script Engines Available:");
|
|
|
|
|
for(ScriptEngineFactory fac : manager.getEngineFactories()){
|
|
|
|
|
if(fac.getExtensions().isEmpty())
|
|
|
|
|
continue;
|
2022-05-29 16:01:05 -05:00
|
|
|
ScriptEngine scriptEngine = fac.getScriptEngine();
|
|
|
|
|
try {
|
|
|
|
|
scriptEngine.put("$RunTest",null);
|
2022-12-14 01:55:32 -06:00
|
|
|
String ext = "." + fac.getExtensions().get(0).toLowerCase();
|
|
|
|
|
LogWriter.info("Engine " + fac.getEngineName() + " running " + fac.getLanguageName() + " with extension: " + ext);
|
|
|
|
|
languages.put(fac.getLanguageName(), ext);
|
|
|
|
|
} catch (Exception ignored) {}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-11 06:49:30 -05:00
|
|
|
private File forgeScriptsFile() {
|
|
|
|
|
return new File(this.dir, "forge_scripts.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean loadForgeScripts() {
|
|
|
|
|
this.forgeScripts.clear();
|
|
|
|
|
File file = this.forgeScriptsFile();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if(!file.exists()) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
this.forgeScripts.readFromNBT(NBTJsonUtil.LoadFile(file));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception var3) {
|
|
|
|
|
LogWriter.error("Error loading: " + file.getAbsolutePath(), var3);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 13:38:43 -06:00
|
|
|
public void saveForgeScripts() {
|
2021-10-11 06:49:30 -05:00
|
|
|
File file = this.forgeScriptsFile();
|
|
|
|
|
try {
|
2023-02-19 13:38:43 -06:00
|
|
|
NBTJsonUtil.SaveFile(file, this.forgeScripts.writeToNBT(new NBTTagCompound()));
|
2021-10-11 06:49:30 -05:00
|
|
|
this.forgeScripts.lastInited = -1L;
|
2023-02-19 13:38:43 -06:00
|
|
|
} catch (IOException | JsonException var4) {
|
2021-10-11 06:49:30 -05:00
|
|
|
var4.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 01:48:54 -05:00
|
|
|
private File playerScriptsFile() {
|
2021-12-16 04:05:05 -06:00
|
|
|
return new File(dir, "player_scripts.json");
|
2021-09-30 01:48:54 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-23 03:54:06 -05:00
|
|
|
private File npcScriptsFile() {
|
|
|
|
|
return new File(dir, "npc_scripts.json");
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 01:48:54 -05:00
|
|
|
public boolean loadPlayerScripts() {
|
|
|
|
|
this.playerScripts.clear();
|
|
|
|
|
File file = this.playerScriptsFile();
|
|
|
|
|
|
|
|
|
|
try {
|
2021-12-16 04:05:05 -06:00
|
|
|
if(!file.exists()) {
|
2021-09-30 01:48:54 -05:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
this.playerScripts.readFromNBT(NBTJsonUtil.LoadFile(file));
|
2021-12-16 04:05:05 -06:00
|
|
|
shouldSave = false;
|
2021-09-30 01:48:54 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception var3) {
|
|
|
|
|
LogWriter.error("Error loading: " + file.getAbsolutePath(), var3);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 13:38:43 -06:00
|
|
|
public void savePlayerScripts() {
|
2021-09-30 01:48:54 -05:00
|
|
|
File file = this.playerScriptsFile();
|
|
|
|
|
try {
|
2023-02-19 13:38:43 -06:00
|
|
|
NBTJsonUtil.SaveFile(file, this.playerScripts.writeToNBT(new NBTTagCompound()));
|
|
|
|
|
this.playerScripts.lastInited = -1L;
|
|
|
|
|
} catch (IOException | JsonException var4) {
|
2021-09-30 01:48:54 -05:00
|
|
|
var4.printStackTrace();
|
2022-03-23 03:54:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean loadNPCScripts() {
|
|
|
|
|
this.npcScripts.clear();
|
|
|
|
|
File file = this.npcScriptsFile();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if(!file.exists()) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
this.npcScripts.readFromNBT(NBTJsonUtil.LoadFile(file));
|
|
|
|
|
shouldSave = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception var3) {
|
|
|
|
|
LogWriter.error("Error loading: " + file.getAbsolutePath(), var3);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 13:38:43 -06:00
|
|
|
public void saveNpcScripts() {
|
2022-03-23 03:54:06 -05:00
|
|
|
File file = this.npcScriptsFile();
|
|
|
|
|
try {
|
2023-02-19 13:38:43 -06:00
|
|
|
NBTJsonUtil.SaveFile(file, this.npcScripts.writeToNBT(new NBTTagCompound()));
|
|
|
|
|
this.npcScripts.lastInited = -1L;
|
|
|
|
|
} catch (IOException | JsonException var4) {
|
2022-03-23 03:54:06 -05:00
|
|
|
var4.printStackTrace();
|
2021-09-30 01:48:54 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private void loadCategories(){
|
|
|
|
|
dir = new File(CustomNpcs.getWorldSaveDirectory(), "scripts");
|
|
|
|
|
if(!dir.exists())
|
|
|
|
|
dir.mkdir();
|
|
|
|
|
if(!getSavedFile().exists())
|
|
|
|
|
shouldSave = true;
|
|
|
|
|
new ScriptWorld(null).clearTempData();
|
|
|
|
|
scripts.clear();
|
|
|
|
|
for(String language : languages.keySet()){
|
|
|
|
|
String ext = languages.get(language);
|
|
|
|
|
File scriptDir = new File(dir,language.toLowerCase());
|
|
|
|
|
if(!scriptDir.exists())
|
|
|
|
|
scriptDir.mkdir();
|
|
|
|
|
else
|
|
|
|
|
loadDir(scriptDir, "", ext);
|
|
|
|
|
}
|
|
|
|
|
lastLoaded = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
private void loadDir(File dir, String name, String ext){
|
|
|
|
|
for(File file : dir.listFiles()){
|
|
|
|
|
String filename = name + file.getName().toLowerCase();
|
|
|
|
|
if(file.isDirectory()){
|
|
|
|
|
loadDir(file, filename + "/", ext);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(!filename.endsWith(ext))
|
|
|
|
|
continue;
|
|
|
|
|
try {
|
|
|
|
|
scripts.put(filename, readFile(file));
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public boolean loadStoredData(){
|
2021-10-19 02:49:46 -05:00
|
|
|
loadCategories();
|
|
|
|
|
File file = getSavedFile();
|
|
|
|
|
try {
|
|
|
|
|
if(!file.exists())
|
|
|
|
|
return false;
|
|
|
|
|
this.compound = NBTJsonUtil.LoadFile(file);
|
|
|
|
|
shouldSave = false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2018-09-13 12:03:28 -04:00
|
|
|
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
|
2021-10-19 02:49:46 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
2021-10-19 02:49:46 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private File getSavedFile(){
|
|
|
|
|
return new File(dir, "world_data.json");
|
|
|
|
|
}
|
2021-10-19 02:49:46 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private String readFile(File file) throws IOException {
|
2021-10-19 02:49:46 -05:00
|
|
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
|
|
|
|
try {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
String line = br.readLine();
|
2018-09-13 12:03:28 -04:00
|
|
|
|
2021-10-19 02:49:46 -05:00
|
|
|
while (line != null) {
|
|
|
|
|
sb.append(line);
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
line = br.readLine();
|
|
|
|
|
}
|
|
|
|
|
return sb.toString();
|
|
|
|
|
} finally {
|
|
|
|
|
br.close();
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ScriptEngine getEngineByName(String language) {
|
|
|
|
|
return manager.getEngineByName(language);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagList nbtLanguages() {
|
|
|
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
|
for(String language : languages.keySet()){
|
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
|
NBTTagList scripts = new NBTTagList();
|
|
|
|
|
for(String script : getScripts(language)){
|
|
|
|
|
scripts.appendTag(new NBTTagString(script));
|
|
|
|
|
}
|
|
|
|
|
compound.setTag("Scripts", scripts);
|
|
|
|
|
compound.setString("Language", language);
|
|
|
|
|
list.appendTag(compound);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2021-10-19 02:49:46 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
private List<String> getScripts(String language){
|
|
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
|
String ext = languages.get(language);
|
|
|
|
|
if(ext == null)
|
|
|
|
|
return list;
|
|
|
|
|
for(String script : scripts.keySet()){
|
|
|
|
|
if(script.endsWith(ext)){
|
|
|
|
|
list.add(script);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2022-05-11 22:08:31 -05:00
|
|
|
public void invoke(WorldEvent.Save event){
|
2018-09-13 12:03:28 -04:00
|
|
|
if(!shouldSave || event.world.isRemote || event.world != MinecraftServer.getServer().worldServers[0])
|
|
|
|
|
return;
|
2021-10-19 02:49:46 -05:00
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
try {
|
|
|
|
|
NBTJsonUtil.SaveFile(getSavedFile(), compound);
|
2021-10-19 02:49:46 -05:00
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
catch (Exception e) {
|
|
|
|
|
LogWriter.except(e);
|
2021-10-19 02:49:46 -05:00
|
|
|
}
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
shouldSave = false;
|
|
|
|
|
}
|
2021-10-19 02:49:46 -05:00
|
|
|
}
|