Made a new interface for CNPC+ script handlers and changed callScript functions to make them more 3rd party API-friendly

This commit is contained in:
Luis C.M committed 2023-07-13 13:56:32 -05:00
1 parent d433eccef1
commit 3b0879b0db
8 files changed
+59 -25

No files matched your search

@@ -147,7 +147,7 @@ public class ScriptContainer {
}
public void run(String type, Object event) {
if(errored || !hasCode() || unknownFunctions.contains(type) || !ConfigScript.ScriptingEnabled)
if(!ConfigScript.ScriptingEnabled || errored || !hasCode() || unknownFunctions.contains(type))
return;
this.setEngine(handler.getLanguage());
@@ -29,7 +29,7 @@ import noppes.npcs.scripted.entity.ScriptNpc;
import javax.script.ScriptEngine;
import java.util.*;
public class DataScript implements IScriptHandler {
public class DataScript implements INpcScriptHandler {
public List<ScriptContainer> eventScripts = new ArrayList<>();
private HashMap<EnumScriptType,ScriptContainer> scripts = new HashMap<>();
@@ -236,6 +236,14 @@ public class DataScript implements IScriptHandler {
callScript(type, event, "$$IGNORED$$", null);
}
@Override
public void callScript(String hookName, Event event) {
try {
EnumScriptType enumScriptType = EnumScriptType.valueOf(hookName);
this.callScript(enumScriptType, event);
} catch (IllegalArgumentException ignored) {}
}
public boolean isClient() {
return this.npc.isRemote();
}
@@ -17,7 +17,7 @@ import noppes.npcs.controllers.ScriptController;
import java.util.*;
import java.util.Map.Entry;
public class ForgeDataScript implements IScriptHandler {
public class ForgeDataScript implements INpcScriptHandler {
private List<ScriptContainer> scripts = new ArrayList();
private String scriptLanguage = "ECMAScript";
public long lastInited = -1L;
@@ -60,7 +60,7 @@ public class ForgeDataScript implements IScriptHandler {
@Override
public void callScript(EnumScriptType var1, Event var2) {
callScript(var1.function, var2);
this.callScript(var1.function, var2);
}
public void callScript(String type, Event event) {
@@ -17,7 +17,7 @@ import javax.annotation.CheckForNull;
import java.lang.reflect.Array;
import java.util.*;
public class GlobalNPCDataScript implements IScriptHandler {
public class GlobalNPCDataScript implements INpcScriptHandler {
public List<ScriptContainer> scripts = new ArrayList();
public String scriptLanguage = "ECMAScript";
private EntityNPCInterface npc;
@@ -60,7 +60,16 @@ public class GlobalNPCDataScript implements IScriptHandler {
return compound;
}
public boolean isEnabled() {
return ConfigScript.GlobalNPCScripts && this.enabled && ScriptController.HasStart && this.scripts.size() > 0;
}
public void callScript(EnumScriptType type, Event event) {
this.callScript(type.function, event);
}
@Override
public void callScript(String hookName, Event event) {
if (this.isEnabled()) {
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastGlobalNpcUpdate > this.lastNpcUpdate) {
this.lastInited = ScriptController.Instance.lastLoaded;
@@ -75,14 +84,11 @@ public class GlobalNPCDataScript implements IScriptHandler {
if(script == null || script.errored || !script.hasCode() )
continue;
script.run(type, event);
script.run(hookName, event);
}
}
}
public boolean isEnabled() {
return ConfigScript.GlobalNPCScripts && this.enabled && ScriptController.HasStart && this.scripts.size() > 0;
}
public boolean isClient() {
return this.npc.isClientWorld();
}
@@ -0,0 +1,8 @@
package noppes.npcs.controllers.data;
import cpw.mods.fml.common.eventhandler.Event;
import noppes.npcs.constants.EnumScriptType;
public interface INpcScriptHandler extends IScriptHandler {
void callScript(EnumScriptType var1, Event var2);
}
@@ -13,17 +13,17 @@ import java.util.List;
import java.util.Map;
public interface IScriptHandler {
void callScript(EnumScriptType var1, Event var2);
void callScript(String hookName, Event event);
boolean isClient();
boolean getEnabled();
void setEnabled(boolean var1);
void setEnabled(boolean enabled);
String getLanguage();
void setLanguage(String var1);
void setLanguage(String language);
void setScripts(List<ScriptContainer> list);
@@ -23,7 +23,7 @@ import java.lang.reflect.Array;
import java.util.*;
import java.util.Map.Entry;
public class PlayerDataScript implements IScriptHandler {
public class PlayerDataScript implements INpcScriptHandler {
public List<ScriptContainer> scripts = new ArrayList();
public String scriptLanguage = "ECMAScript";
private EntityPlayer player;
@@ -66,7 +66,16 @@ public class PlayerDataScript implements IScriptHandler {
return compound;
}
public boolean isEnabled() {
return ConfigScript.GlobalPlayerScripts && ScriptController.Instance.playerScripts.enabled && ScriptController.HasStart && (this.player == null || !this.player.worldObj.isRemote);
}
public void callScript(EnumScriptType type, Event event) {
this.callScript(type.function, event);
}
@Override
public void callScript(String hookName, Event event) {
if (this.isEnabled()) {
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
this.lastInited = ScriptController.Instance.lastLoaded;
@@ -76,7 +85,7 @@ public class PlayerDataScript implements IScriptHandler {
scriptContainer.errored = false;
}
if (type != EnumScriptType.INIT) {
if (!Objects.equals(hookName, EnumScriptType.INIT.function)) {
noppes.npcs.scripted.event.PlayerEvent playerEvent = (noppes.npcs.scripted.event.PlayerEvent)event;
EventHooks.onPlayerInit(this, playerEvent.player);
}
@@ -86,14 +95,11 @@ public class PlayerDataScript implements IScriptHandler {
if(script == null || script.errored || !script.hasCode() )
continue;
script.run(type, event);
script.run(hookName, event);
}
}
}
public boolean isEnabled() {
return ConfigScript.GlobalPlayerScripts && ScriptController.Instance.playerScripts.enabled && ScriptController.HasStart && (this.player == null || !this.player.worldObj.isRemote);
}
public boolean isClient() {
return this.player.isClientWorld();
}
@@ -9,12 +9,13 @@ import noppes.npcs.api.item.IItemCustom;
import noppes.npcs.constants.EnumScriptType;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.controllers.ScriptController;
import noppes.npcs.controllers.data.INpcScriptHandler;
import noppes.npcs.controllers.data.IScriptHandler;
import noppes.npcs.scripted.CustomNPCsException;
import java.util.*;
public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IScriptHandler {
public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, INpcScriptHandler {
public List<ScriptContainer> scripts = new ArrayList();
public List<Integer> errored = new ArrayList();
public String scriptLanguage = "ECMAScript";
@@ -66,11 +67,20 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IS
return 6;
}
private boolean isEnabled() {
return this.enabled && ScriptController.HasStart;
}
public void callScript(EnumScriptType type, Event event) {
this.callScript(type.function, event);
}
@Override
public void callScript(String hookName, Event event) {
if (!this.loaded) {
this.loadScriptData();
this.loaded = true;
if (type != EnumScriptType.INIT) {
if (!Objects.equals(hookName, EnumScriptType.INIT.function)) {
EventHooks.onScriptItemInit(this);
}
}
@@ -82,7 +92,7 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IS
if(script == null || script.errored || !script.hasCode())
continue;
script.run(type, event);
script.run(hookName, event);
if (script.errored) {
this.errored.add(i);
@@ -92,10 +102,6 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IS
}
}
private boolean isEnabled() {
return this.enabled && ScriptController.HasStart;
}
public boolean isClient() {
return false;
}