mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-18 06:55:03 +00:00
Proper Reformating
This commit is contained in:
1 parent
ce75880aba
commit
180d257024
1179 files changed
+59847
-61583
No files matched your search
@@ -34,152 +34,153 @@ import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
|
||||
|
||||
public class ScriptController {
|
||||
|
||||
public static ScriptController Instance;
|
||||
public static boolean HasStart = false;
|
||||
private final ScriptEngineManager manager;
|
||||
private ScriptEngineFactory nashornFactory;
|
||||
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();
|
||||
public static ScriptController Instance;
|
||||
public static boolean HasStart = false;
|
||||
private final ScriptEngineManager manager;
|
||||
private ScriptEngineFactory nashornFactory;
|
||||
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();
|
||||
|
||||
public boolean shouldSave = false;
|
||||
public PlayerDataScript playerScripts = new PlayerDataScript((EntityPlayer)null);
|
||||
public long lastPlayerUpdate = 0L;
|
||||
public boolean shouldSave = false;
|
||||
public PlayerDataScript playerScripts = new PlayerDataScript((EntityPlayer) null);
|
||||
public long lastPlayerUpdate = 0L;
|
||||
|
||||
public ForgeDataScript forgeScripts = new ForgeDataScript();
|
||||
public long lastForgeUpdate = 0L;
|
||||
public ForgeDataScript forgeScripts = new ForgeDataScript();
|
||||
public long lastForgeUpdate = 0L;
|
||||
|
||||
public GlobalNPCDataScript globalNpcScripts = new GlobalNPCDataScript((EntityNPCInterface) null);
|
||||
public long lastGlobalNpcUpdate = 0L;
|
||||
public GlobalNPCDataScript globalNpcScripts = new GlobalNPCDataScript((EntityNPCInterface) null);
|
||||
public long lastGlobalNpcUpdate = 0L;
|
||||
|
||||
public ScriptController(){
|
||||
Instance = this;
|
||||
manager = new ScriptEngineManager();
|
||||
if(!ConfigScript.ScriptingEnabled)
|
||||
return;
|
||||
LogWriter.info("Script Engines Available:");
|
||||
public ScriptController() {
|
||||
Instance = this;
|
||||
manager = new ScriptEngineManager();
|
||||
if (!ConfigScript.ScriptingEnabled)
|
||||
return;
|
||||
LogWriter.info("Script Engines Available:");
|
||||
|
||||
ScriptEngine engine = manager.getEngineByName("nashorn");
|
||||
if (engine != null) {
|
||||
this.nashornFactory = engine.getFactory();
|
||||
}
|
||||
ScriptEngine engine = manager.getEngineByName("nashorn");
|
||||
if (engine != null) {
|
||||
this.nashornFactory = engine.getFactory();
|
||||
}
|
||||
|
||||
for(ScriptEngineFactory fac : manager.getEngineFactories()){
|
||||
if(fac.getExtensions().isEmpty())
|
||||
continue;
|
||||
for (ScriptEngineFactory fac : manager.getEngineFactories()) {
|
||||
if (fac.getExtensions().isEmpty())
|
||||
continue;
|
||||
|
||||
ScriptEngine scriptEngine = fac.getScriptEngine();
|
||||
try {
|
||||
scriptEngine.put("$RunTest",null);
|
||||
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) {}
|
||||
}
|
||||
}
|
||||
ScriptEngine scriptEngine = fac.getScriptEngine();
|
||||
try {
|
||||
scriptEngine.put("$RunTest", null);
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private File forgeScriptsFile() {
|
||||
return new File(this.dir, "forge_scripts.json");
|
||||
}
|
||||
private File forgeScriptsFile() {
|
||||
return new File(this.dir, "forge_scripts.json");
|
||||
}
|
||||
|
||||
public boolean loadForgeScripts() {
|
||||
this.forgeScripts.clear();
|
||||
File file = this.forgeScriptsFile();
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public void saveForgeScripts() {
|
||||
File file = this.forgeScriptsFile();
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(file, this.forgeScripts.writeToNBT(new NBTTagCompound()));
|
||||
this.forgeScripts.lastInited = -1L;
|
||||
} catch (IOException | JsonException var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void saveForgeScripts() {
|
||||
File file = this.forgeScriptsFile();
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(file, this.forgeScripts.writeToNBT(new NBTTagCompound()));
|
||||
this.forgeScripts.lastInited = -1L;
|
||||
} catch (IOException | JsonException var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private File playerScriptsFile() {
|
||||
return new File(dir, "player_scripts.json");
|
||||
}
|
||||
private File playerScriptsFile() {
|
||||
return new File(dir, "player_scripts.json");
|
||||
}
|
||||
|
||||
private File npcScriptsFile() {
|
||||
return new File(dir, "npc_scripts.json");
|
||||
}
|
||||
private File npcScriptsFile() {
|
||||
return new File(dir, "npc_scripts.json");
|
||||
}
|
||||
|
||||
public boolean loadPlayerScripts() {
|
||||
this.playerScripts.clear();
|
||||
File file = this.playerScriptsFile();
|
||||
public boolean loadPlayerScripts() {
|
||||
this.playerScripts.clear();
|
||||
File file = this.playerScriptsFile();
|
||||
|
||||
try {
|
||||
if(!file.exists()) {
|
||||
return false;
|
||||
} else {
|
||||
this.playerScripts.readFromNBT(NBTJsonUtil.LoadFile(file));
|
||||
shouldSave = false;
|
||||
return true;
|
||||
}
|
||||
} catch (Exception var3) {
|
||||
LogWriter.error("Error loading: " + file.getAbsolutePath(), var3);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
return false;
|
||||
} else {
|
||||
this.playerScripts.readFromNBT(NBTJsonUtil.LoadFile(file));
|
||||
shouldSave = false;
|
||||
return true;
|
||||
}
|
||||
} catch (Exception var3) {
|
||||
LogWriter.error("Error loading: " + file.getAbsolutePath(), var3);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void savePlayerScripts() {
|
||||
File file = this.playerScriptsFile();
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(file, this.playerScripts.writeToNBT(new NBTTagCompound()));
|
||||
this.playerScripts.lastInited = -1L;
|
||||
} catch (IOException | JsonException var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void savePlayerScripts() {
|
||||
File file = this.playerScriptsFile();
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(file, this.playerScripts.writeToNBT(new NBTTagCompound()));
|
||||
this.playerScripts.lastInited = -1L;
|
||||
} catch (IOException | JsonException var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean loadGlobalNPCScripts() {
|
||||
this.globalNpcScripts.clear();
|
||||
File file = this.npcScriptsFile();
|
||||
public boolean loadGlobalNPCScripts() {
|
||||
this.globalNpcScripts.clear();
|
||||
File file = this.npcScriptsFile();
|
||||
|
||||
try {
|
||||
if(!file.exists()) {
|
||||
return false;
|
||||
} else {
|
||||
this.globalNpcScripts.readFromNBT(NBTJsonUtil.LoadFile(file));
|
||||
shouldSave = false;
|
||||
return true;
|
||||
}
|
||||
} catch (Exception var3) {
|
||||
LogWriter.error("Error loading: " + file.getAbsolutePath(), var3);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
return false;
|
||||
} else {
|
||||
this.globalNpcScripts.readFromNBT(NBTJsonUtil.LoadFile(file));
|
||||
shouldSave = false;
|
||||
return true;
|
||||
}
|
||||
} catch (Exception var3) {
|
||||
LogWriter.error("Error loading: " + file.getAbsolutePath(), var3);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void saveGlobalNpcScripts() {
|
||||
File file = this.npcScriptsFile();
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(file, this.globalNpcScripts.writeToNBT(new NBTTagCompound()));
|
||||
this.globalNpcScripts.lastInited = -1L;
|
||||
} catch (IOException | JsonException var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void saveGlobalNpcScripts() {
|
||||
File file = this.npcScriptsFile();
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(file, this.globalNpcScripts.writeToNBT(new NBTTagCompound()));
|
||||
this.globalNpcScripts.lastInited = -1L;
|
||||
} catch (IOException | JsonException var4) {
|
||||
var4.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void saveForgeScriptsSync(){
|
||||
public synchronized void saveForgeScriptsSync() {
|
||||
customNPCThread.execute(this::saveForgeScripts);
|
||||
}
|
||||
|
||||
public synchronized void savePlayerScriptsSync(){
|
||||
public synchronized void savePlayerScriptsSync() {
|
||||
customNPCThread.execute(() -> {
|
||||
File file = this.playerScriptsFile();
|
||||
try {
|
||||
@@ -191,86 +192,88 @@ public class ScriptController {
|
||||
});
|
||||
}
|
||||
|
||||
public synchronized void saveGlobalScriptsSync(){
|
||||
public synchronized void saveGlobalScriptsSync() {
|
||||
customNPCThread.execute(this::saveGlobalNpcScripts);
|
||||
}
|
||||
|
||||
public 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(){
|
||||
loadCategories();
|
||||
File file = getSavedFile();
|
||||
try {
|
||||
if(!file.exists())
|
||||
return false;
|
||||
this.compound = NBTJsonUtil.LoadFile(file);
|
||||
shouldSave = false;
|
||||
}
|
||||
catch (Exception e) {
|
||||
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public 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 File getSavedFile(){
|
||||
return new File(dir, "world_data.json");
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String readFile(File file) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = br.readLine();
|
||||
public boolean loadStoredData() {
|
||||
loadCategories();
|
||||
File file = getSavedFile();
|
||||
try {
|
||||
if (!file.exists())
|
||||
return false;
|
||||
this.compound = NBTJsonUtil.LoadFile(file);
|
||||
shouldSave = false;
|
||||
} catch (Exception e) {
|
||||
LogWriter.error("Error loading: " + file.getAbsolutePath(), e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
while (line != null) {
|
||||
sb.append(line);
|
||||
sb.append("\n");
|
||||
line = br.readLine();
|
||||
}
|
||||
return sb.toString();
|
||||
} finally {
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
private File getSavedFile() {
|
||||
return new File(dir, "world_data.json");
|
||||
}
|
||||
|
||||
private static final List<String> nashornNames = immutableList("nashorn", "Nashorn", "js", "JS", "JavaScript", "javascript", "ECMAScript", "ecmascript");
|
||||
public ScriptEngine getEngineByName(String language) {
|
||||
if (nashornNames.contains(language) && this.nashornFactory != null) {
|
||||
private String readFile(File file) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new FileReader(file));
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = br.readLine();
|
||||
|
||||
while (line != null) {
|
||||
sb.append(line);
|
||||
sb.append("\n");
|
||||
line = br.readLine();
|
||||
}
|
||||
return sb.toString();
|
||||
} finally {
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static final List<String> nashornNames = immutableList("nashorn", "Nashorn", "js", "JS", "JavaScript", "javascript", "ECMAScript", "ecmascript");
|
||||
|
||||
public ScriptEngine getEngineByName(String language) {
|
||||
if (nashornNames.contains(language) && this.nashornFactory != null) {
|
||||
ScriptEngine scriptEngine;
|
||||
if(ConfigScript.EnableBannedClasses){
|
||||
if (ConfigScript.EnableBannedClasses) {
|
||||
try {
|
||||
ClassFilter filter = s -> {
|
||||
for (String className : ConfigScript.BannedClasses) {
|
||||
@@ -288,55 +291,55 @@ public class ScriptController {
|
||||
} else {
|
||||
scriptEngine = this.nashornFactory.getScriptEngine();
|
||||
}
|
||||
scriptEngine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||
return scriptEngine;
|
||||
}
|
||||
return manager.getEngineByName(language);
|
||||
}
|
||||
private static List<String> immutableList(String... elements) {
|
||||
return Collections.unmodifiableList(Arrays.asList(elements));
|
||||
}
|
||||
scriptEngine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||
return scriptEngine;
|
||||
}
|
||||
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;
|
||||
}
|
||||
private static List<String> immutableList(String... elements) {
|
||||
return Collections.unmodifiableList(Arrays.asList(elements));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void invoke(WorldEvent.Save event){
|
||||
if(!shouldSave || event.world.isRemote || event.world != MinecraftServer.getServer().worldServers[0])
|
||||
return;
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(getSavedFile(), compound);
|
||||
}
|
||||
catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
}
|
||||
@SubscribeEvent
|
||||
public void invoke(WorldEvent.Save event) {
|
||||
if (!shouldSave || event.world.isRemote || event.world != MinecraftServer.getServer().worldServers[0])
|
||||
return;
|
||||
|
||||
shouldSave = false;
|
||||
}
|
||||
try {
|
||||
NBTJsonUtil.SaveFile(getSavedFile(), compound);
|
||||
} catch (Exception e) {
|
||||
LogWriter.except(e);
|
||||
}
|
||||
|
||||
shouldSave = false;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user