Player, Forge, & NPC script console changes, & further fix to concurrent modification exception on script container.

This commit is contained in:
Luis C.M committed 2023-02-21 12:45:48 -06:00
1 parent 9b330007fd
commit 5b589768f6
11 files changed
+77 -88

No files matched your search

@@ -245,7 +245,6 @@ public class PacketHandlerServer{
}
}
data.setEnabled(compound.getBoolean("ScriptEnabled"));
data.setConsoleText(NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10)));
}
}
@@ -255,8 +254,7 @@ public class PacketHandlerServer{
this.getScripts(data,buffer,player);
} else if(type == EnumPacketServer.ScriptPlayerSave) {
this.saveScripts(data,buffer,player);
ScriptController.Instance.lastLoaded = System.currentTimeMillis();
ScriptController.Instance.playerScripts.lastInited = System.currentTimeMillis();
ScriptController.Instance.lastPlayerUpdate = System.currentTimeMillis();
}
}
@@ -266,8 +264,7 @@ public class PacketHandlerServer{
this.getScripts(data,buffer,player);
} else if (type == EnumPacketServer.ScriptForgeSave) {
this.saveScripts(data,buffer,player);
ScriptController.Instance.lastLoaded = System.currentTimeMillis();
ScriptController.Instance.forgeScripts.lastInited = System.currentTimeMillis();
ScriptController.Instance.lastForgeUpdate = System.currentTimeMillis();
}
}
@@ -277,8 +274,7 @@ public class PacketHandlerServer{
this.getScripts(data,buffer,player);
} else if(type == EnumPacketServer.ScriptNPCSave) {
this.saveScripts(data,buffer,player);
ScriptController.Instance.lastLoaded = System.currentTimeMillis();
ScriptController.Instance.npcScripts.lastInited = System.currentTimeMillis();
ScriptController.Instance.lastNpcUpdate = System.currentTimeMillis();
}
}
@@ -21,7 +21,6 @@ public class GuiScriptAllNPCs extends GuiScriptInterface {
if (!compound.hasKey("Tab")) {
script.setLanguage(compound.getString("ScriptLanguage"));
script.setEnabled(compound.getBoolean("ScriptEnabled"));
script.setConsoleText(NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10)));
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
@@ -26,7 +26,6 @@ public class GuiScriptForge extends GuiScriptInterface {
if (!compound.hasKey("Tab")) {
script.setLanguage(compound.getString("ScriptLanguage"));
script.setEnabled(compound.getBoolean("ScriptEnabled"));
script.setConsoleText(NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10)));
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
@@ -28,7 +28,6 @@ public class GuiScriptPlayers extends GuiScriptInterface {
if (!compound.hasKey("Tab")) {
script.setLanguage(compound.getString("ScriptLanguage"));
script.setEnabled(compound.getBoolean("ScriptEnabled"));
script.setConsoleText(NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10)));
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
@@ -155,8 +155,8 @@ public class ScriptContainer {
}
engine.put("API", NpcAPI.Instance());
Set<Map.Entry<String, Object>> engineEntries = NpcAPI.engineObjects.entrySet();
for (Map.Entry<String,Object> objectEntry : engineEntries) {
HashMap<String,Object> engineEntries = new HashMap<>(NpcAPI.engineObjects);
for (Map.Entry<String,Object> objectEntry : engineEntries.entrySet()) {
engine.put(objectEntry.getKey(),objectEntry.getValue());
}
@@ -47,8 +47,10 @@ public class ScriptController {
public long lastPlayerUpdate = 0L;
public ForgeDataScript forgeScripts = new ForgeDataScript();
public long lastForgeUpdate = 0L;
public NPCDataScript npcScripts = new NPCDataScript((EntityNPCInterface) null);
public long lastNpcUpdate = 0L;
public ScriptController(){
loaded = false;
@@ -21,15 +21,14 @@ public class ForgeDataScript implements IScriptHandler {
private List<ScriptContainer> scripts = new ArrayList();
private String scriptLanguage = "ECMAScript";
public long lastInited = -1L;
private long lastForgeUpdate = -1L;
private boolean enabled = false;
private Map<Long, String> console = new TreeMap();
public ForgeDataScript() {
}
public void clear() {
this.scripts = new ArrayList();
this.console = new TreeMap();
}
public void readFromNBT(NBTTagCompound compound) {
@@ -47,7 +46,6 @@ public class ForgeDataScript implements IScriptHandler {
}
}
this.enabled = compound.getBoolean("ScriptEnabled");
this.console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
@@ -57,7 +55,6 @@ public class ForgeDataScript implements IScriptHandler {
}
compound.setString("ScriptLanguage", this.scriptLanguage);
compound.setBoolean("ScriptEnabled", this.enabled);
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(this.console));
return compound;
}
@@ -68,23 +65,24 @@ public class ForgeDataScript implements IScriptHandler {
public void callScript(String type, Event event) {
if(this.isEnabled()) {
if (ScriptController.Instance.lastLoaded > this.lastInited) {
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastForgeUpdate > this.lastForgeUpdate) {
this.lastInited = ScriptController.Instance.lastLoaded;
this.lastForgeUpdate = ScriptController.Instance.lastForgeUpdate;
for (ScriptContainer script : this.scripts) {
script.errored = false;
}
if (!type.equals("init")) {
EventHooks.onForgeInit(this);
}
}
int i = 0;
for (ScriptContainer script : this.scripts) {
script.run(type, event);
if(script == null || script.errored || !script.hasCode() )
continue;
for (Entry<Long, String> longStringEntry : script.console.entrySet()) {
if (!ScriptController.Instance.forgeScripts.console.containsKey(longStringEntry.getKey())) {
ScriptController.Instance.forgeScripts.console.put(longStringEntry.getKey(), " tab " + (i + 1) + ":\n" + longStringEntry.getValue());
}
}
i++;
script.run(type, event);
}
}
}
@@ -125,14 +123,22 @@ public class ForgeDataScript implements IScriptHandler {
return "ForgeScript";
}
public void setConsoleText(Map<Long, String> map) {
this.console = map;
public Map<Long, String> getConsoleText() {
TreeMap<Long, String> map = new TreeMap<>();
int tab = 0;
for (ScriptContainer script : this.getScripts()) {
++tab;
for (Entry<Long, String> longStringEntry : script.console.entrySet()) {
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
}
}
return map;
}
public Map<Long, String> getConsoleText() {
return this.console;
}
public void clearConsole() {
this.console.clear();
for (ScriptContainer script : this.getScripts()) {
script.console.clear();
}
}
}
@@ -31,8 +31,6 @@ public interface IScriptHandler {
String noticeString();
void setConsoleText(Map<Long, String> map);
Map<Long, String> getConsoleText();
void clearConsole();
@@ -23,8 +23,8 @@ public class NPCDataScript implements IScriptHandler {
private EntityNPCInterface npc;
private ICustomNpc npcAPI;
public long lastInited = -1L;
private long lastNpcUpdate = -1L;
public boolean enabled = false;
private Map<Long, String> console = new TreeMap();
public NPCDataScript(EntityNPCInterface npc) {
if(npc != null) {
@@ -32,7 +32,6 @@ public class NPCDataScript implements IScriptHandler {
}
}
public void clear() {
this.console = new TreeMap();
this.scripts = new ArrayList();
}
public void readFromNBT(NBTTagCompound compound) {
@@ -50,7 +49,6 @@ public class NPCDataScript implements IScriptHandler {
}
}
this.enabled = compound.getBoolean("ScriptEnabled");
this.console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setInteger("TotalScripts",this.scripts.size());
@@ -59,26 +57,25 @@ public class NPCDataScript implements IScriptHandler {
}
compound.setString("ScriptLanguage", this.scriptLanguage);
compound.setBoolean("ScriptEnabled", this.enabled);
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(this.console));
return compound;
}
public void callScript(EnumScriptType type, Event event) {
if (this.isEnabled()) {
if (ScriptController.Instance.lastLoaded > this.lastInited) {
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastNpcUpdate > this.lastNpcUpdate) {
this.lastInited = ScriptController.Instance.lastLoaded;
this.lastNpcUpdate = ScriptController.Instance.lastNpcUpdate;
for (ScriptContainer script : this.scripts) {
script.errored = false;
}
}
int i = 0;
for (ScriptContainer script : this.scripts) {
script.run(type, event);
if(script == null || script.errored || !script.hasCode() )
continue;
for (Map.Entry<Long, String> longStringEntry : script.console.entrySet()) {
if (!ScriptController.Instance.npcScripts.console.containsKey(longStringEntry.getKey())) {
ScriptController.Instance.npcScripts.console.put(longStringEntry.getKey(), " tab " + (i + 1) + ":\n" + longStringEntry.getValue());
}
}
i++;
script.run(type, event);
}
}
}
@@ -122,16 +119,23 @@ public class NPCDataScript implements IScriptHandler {
return this.npcAPI;
}
public void setConsoleText(Map<Long, String> map) {
this.console = map;
}
public Map<Long, String> getConsoleText() {
return this.console;
TreeMap<Long, String> map = new TreeMap<>();
int tab = 0;
for (ScriptContainer script : this.getScripts()) {
++tab;
for (Map.Entry<Long, String> longStringEntry : script.console.entrySet()) {
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
}
}
return map;
}
public void clearConsole() {
this.console.clear();
for (ScriptContainer script : this.getScripts()) {
script.console.clear();
}
}
public static final class ToStringHelper {
@@ -31,8 +31,6 @@ public class PlayerDataScript implements IScriptHandler {
private long lastPlayerUpdate = 0L;
public long lastInited = -1L;
public boolean enabled = false;
private Map<Long, String> console = new TreeMap();
public List<Integer> errored = new ArrayList();
public PlayerDataScript(EntityPlayer player) {
if(player != null) {
@@ -40,8 +38,6 @@ public class PlayerDataScript implements IScriptHandler {
}
}
public void clear() {
this.console = new TreeMap();
this.errored = new ArrayList();
this.scripts = new ArrayList();
}
public void readFromNBT(NBTTagCompound compound) {
@@ -59,7 +55,6 @@ public class PlayerDataScript implements IScriptHandler {
}
}
this.enabled = compound.getBoolean("ScriptEnabled");
this.console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setInteger("TotalScripts",this.scripts.size());
@@ -68,47 +63,31 @@ public class PlayerDataScript implements IScriptHandler {
}
compound.setString("ScriptLanguage", this.scriptLanguage);
compound.setBoolean("ScriptEnabled", this.enabled);
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(this.console));
return compound;
}
public void callScript(EnumScriptType type, Event event) {
if (this.isEnabled()) {
ScriptContainer script;
if (ScriptController.Instance.lastLoaded > this.lastInited || ScriptController.Instance.lastPlayerUpdate > this.lastPlayerUpdate) {
this.lastInited = ScriptController.Instance.lastLoaded;
errored.clear();
this.lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
for (ScriptContainer scriptContainer : this.scripts) {
scriptContainer.errored = false;
}
if (type != EnumScriptType.INIT) {
noppes.npcs.scripted.event.PlayerEvent playerEvent = (noppes.npcs.scripted.event.PlayerEvent)event;
EventHooks.onPlayerInit(this, playerEvent.player);
}
}
for(int i = 0; i < ScriptController.Instance.playerScripts.scripts.size(); ++i) {
script = (ScriptContainer)ScriptController.Instance.playerScripts.scripts.get(i);
if (!errored.contains(i)) {
if(script == null || script.errored || !script.hasCode() || ScriptController.Instance.playerScripts.errored.contains(i))
continue;
for (ScriptContainer script : this.scripts) {
if(script == null || script.errored || !script.hasCode() )
continue;
script.run(type, event);
if (script.errored) {
ScriptController.Instance.playerScripts.errored.add(i);
}
for (Entry<Long, String> longStringEntry : script.console.entrySet()) {
if (!ScriptController.Instance.playerScripts.console.containsKey(longStringEntry.getKey())) {
ScriptController.Instance.playerScripts.console.put(longStringEntry.getKey(), " tab " + (i + 1) + ":\n" + longStringEntry.getValue());
}
}
script.console.clear();
}
script.run(type, event);
}
}
}
@@ -152,16 +131,23 @@ public class PlayerDataScript implements IScriptHandler {
return this.playerAPI;
}
public void setConsoleText(Map<Long, String> map) {
this.console = map;
}
public Map<Long, String> getConsoleText() {
return this.console;
TreeMap<Long, String> map = new TreeMap<>();
int tab = 0;
for (ScriptContainer script : this.getScripts()) {
++tab;
for (Entry<Long, String> longStringEntry : script.console.entrySet()) {
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + longStringEntry.getValue());
}
}
return map;
}
public void clearConsole() {
this.console.clear();
for (ScriptContainer script : this.getScripts()) {
script.console.clear();
}
}
public static final class ToStringHelper {