Player, Forge, & NPC scripts are saved in separate packets for separate tabs now, so script save/get packets aren't too big for the server.

This commit is contained in:
Luis C.M committed 2023-02-19 13:38:43 -06:00
1 parent 0ddcd15162
commit b8dc090db8
11 files changed
+190 -78

No files matched your search

@@ -236,6 +236,9 @@ public class CustomNpcs {
public void stopped(FMLServerStoppedEvent event){
ServerCloneController.Instance = null;
GlobalDataController.instance.saveData();
ScriptController.Instance.saveForgeScripts();
ScriptController.Instance.savePlayerScripts();
ScriptController.Instance.saveNpcScripts();
}
@EventHandler
+15 -2
View File
@@ -144,7 +144,7 @@ public class NBTTags {
/**
* @param tagList
* @param arraySize if an int array is not this length a new int array with this length is made
* @param arrayLength if an int array is not this length a new int array with this length is made
* @return
*/
public static HashMap<String, int[]> getStringIntegerArrayMap(NBTTagList tagList, int arrayLength) {
@@ -476,7 +476,7 @@ public class NBTTags {
}
}
public static List<ScriptContainer> GetScript(NBTTagList list, IScriptHandler handler) {
public static List<ScriptContainer> GetScriptOld(NBTTagList list, IScriptHandler handler) {
ArrayList scripts = new ArrayList();
for(int i = 0; i < list.tagCount(); ++i) {
@@ -489,6 +489,19 @@ public class NBTTags {
return scripts;
}
public static List<ScriptContainer> GetScript(NBTTagCompound compound, IScriptHandler handler) {
ArrayList<ScriptContainer> scripts = new ArrayList<>();
for(int i = 0; i < compound.getInteger("TotalScripts"); ++i) {
NBTTagCompound containerCompound = compound.getCompoundTag("Tab"+i);
ScriptContainer script = new ScriptContainer(handler);
script.readFromNBT(containerCompound);
scripts.add(script);
}
return scripts;
}
public static NBTTagList NBTScript(List<ScriptContainer> scripts) {
NBTTagList list = new NBTTagList();
Iterator var2 = scripts.iterator();
@@ -205,42 +205,66 @@ public class PacketHandlerServer{
}
}
private void getScripts(IScriptHandler data, ByteBuf buffer, EntityPlayerMP player) throws Exception {
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("ScriptEnabled", data.getEnabled());
compound.setString("ScriptLanguage", data.getLanguage());
compound.setTag("Languages", ScriptController.Instance.nbtLanguages());
Server.sendData(player, EnumPacketClient.GUI_DATA, compound);
List<ScriptContainer> containers = data.getScripts();
for (int i = 0; i < containers.size(); i++) {
ScriptContainer container = containers.get(i);
NBTTagCompound tabCompound = new NBTTagCompound();
tabCompound.setInteger("Tab",i);
tabCompound.setTag("Script",container.writeToNBT(new NBTTagCompound()));
tabCompound.setInteger("TotalScripts",containers.size());
Server.sendData(player, EnumPacketClient.GUI_DATA, tabCompound);
}
}
private void saveScripts(IScriptHandler data, ByteBuf buffer, EntityPlayerMP player) throws Exception {
int tab = buffer.readInt();
if (tab >= 0) {
NBTTagCompound tabCompound = Server.readNBT(buffer);
data.getScripts().get(tab).readFromNBT(tabCompound);
} else {
NBTTagCompound compound = Server.readNBT(buffer);
data.setLanguage(compound.getString("ScriptLanguage"));
if (!ScriptController.Instance.languages.containsKey(data.getLanguage())) {
if (!ScriptController.Instance.languages.isEmpty()) {
data.setLanguage((String) ScriptController.Instance.languages.keySet().toArray()[0]);
} else {
data.setLanguage("ECMAScript");
}
}
data.setEnabled(compound.getBoolean("ScriptEnabled"));
}
}
private void playerScriptPackets(EnumPacketServer type, ByteBuf buffer, EntityPlayerMP player) throws Exception {
NBTTagCompound compound;
PlayerDataScript data = ScriptController.Instance.playerScripts;
if(type == EnumPacketServer.ScriptPlayerGet) {
PlayerDataScript data = ScriptController.Instance.playerScripts;
compound = data.writeToNBT(new NBTTagCompound());
compound.setBoolean("ScriptEnabled", ScriptController.Instance.playerScripts.getEnabled());
compound.setTag("Languages", ScriptController.Instance.nbtLanguages());
Server.sendData(player, EnumPacketClient.GUI_DATA, compound);
this.getScripts(data,buffer,player);
} else if(type == EnumPacketServer.ScriptPlayerSave) {
ScriptController.Instance.setPlayerScripts(Server.readNBT(buffer));
this.saveScripts(data,buffer,player);
}
}
private void forgeScriptPackets(EnumPacketServer type, ByteBuf buffer, EntityPlayerMP player) throws Exception {
NBTTagCompound compound;
ForgeDataScript data = ScriptController.Instance.forgeScripts;
if (type == EnumPacketServer.ScriptForgeGet) {
ForgeDataScript data = ScriptController.Instance.forgeScripts;
compound = data.writeToNBT(new NBTTagCompound());
compound.setBoolean("ScriptEnabled", ScriptController.Instance.forgeScripts.getEnabled());
compound.setTag("Languages", ScriptController.Instance.nbtLanguages());
Server.sendData(player, EnumPacketClient.GUI_DATA, new Object[]{compound});
this.getScripts(data,buffer,player);
} else if (type == EnumPacketServer.ScriptForgeSave) {
ScriptController.Instance.setForgeScripts(Server.readNBT(buffer));
this.saveScripts(data,buffer,player);
}
}
private void npcScriptPackets(EnumPacketServer type, ByteBuf buffer, EntityPlayerMP player) throws Exception {
NBTTagCompound compound;
NPCDataScript data = ScriptController.Instance.npcScripts;
if(type == EnumPacketServer.ScriptNPCGet) {
NPCDataScript data = ScriptController.Instance.npcScripts;
compound = data.writeToNBT(new NBTTagCompound());
compound.setBoolean("ScriptEnabled", ScriptController.Instance.npcScripts.getEnabled());
compound.setTag("Languages", ScriptController.Instance.nbtLanguages());
Server.sendData(player, EnumPacketClient.GUI_DATA, compound);
this.getScripts(data,buffer,player);
} else if(type == EnumPacketServer.ScriptNPCSave) {
ScriptController.Instance.setNPCScripts(Server.readNBT(buffer));
this.saveScripts(data,buffer,player);
}
}
@@ -888,12 +912,9 @@ public class PacketHandlerServer{
}
else if (type == EnumPacketServer.AnimationSave) {
String prevName = Server.readString(buffer);
AnimationController.instance.animations.remove(prevName);
AnimationController.instance.delete(prevName);
Animation animation = new Animation();
animation.readFromNBT(Server.readNBT(buffer));
if (AnimationController.instance.has(animation.getName())) {
animation.name = prevName;
}
animation.save();
Server.sendData(player, EnumPacketClient.SCROLL_LIST, new ArrayList<>(AnimationController.instance.animations.keySet()));
}
@@ -3,27 +3,48 @@ package noppes.npcs.client.gui.script;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.client.Client;
import noppes.npcs.constants.EnumPacketServer;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.controllers.data.NPCDataScript;
import java.util.List;
public class GuiScriptAllNPCs extends GuiScriptInterface {
private NPCDataScript script = new NPCDataScript(null);
public GuiScriptAllNPCs() {
this.handler = this.script;
Client.sendData(EnumPacketServer.ScriptNPCGet, new Object[0]);
Client.sendData(EnumPacketServer.ScriptNPCGet);
}
public void setGuiData(NBTTagCompound compound) {
this.script.readFromNBT(compound);
super.setGuiData(compound);
if (!compound.hasKey("Tab")) {
script.setLanguage(compound.getString("ScriptLanguage"));
script.setEnabled(compound.getBoolean("ScriptEnabled"));
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(script);
container.readFromNBT(compound.getCompoundTag("Script"));
if (script.getScripts().isEmpty()) {
for (int i = 0; i < compound.getInteger("TotalScripts"); i++) {
script.getScripts().add(new ScriptContainer(script));
}
}
script.getScripts().set(tab,container);
initGui();
}
}
public void save() {
super.save();
Client.sendData(EnumPacketServer.ScriptNPCSave, new Object[]{this.script.writeToNBT(new NBTTagCompound())});
}
public void close() {
super.close();
List<ScriptContainer> containers = this.script.getScripts();
for (int i = 0; i < containers.size(); i++) {
ScriptContainer container = containers.get(i);
Client.sendData(EnumPacketServer.ScriptNPCSave, i, container.writeToNBT(new NBTTagCompound()));
}
NBTTagCompound scriptData = new NBTTagCompound();
scriptData.setString("ScriptLanguage", this.script.getLanguage());
scriptData.setBoolean("ScriptEnabled", this.script.getEnabled());
Client.sendData(EnumPacketServer.ScriptNPCSave, -1, scriptData);
}
}
@@ -8,23 +8,48 @@ package noppes.npcs.client.gui.script;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.client.Client;
import noppes.npcs.constants.EnumPacketServer;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.controllers.data.ForgeDataScript;
import java.util.List;
public class GuiScriptForge extends GuiScriptInterface {
private ForgeDataScript script = new ForgeDataScript();
public GuiScriptForge() {
this.handler = this.script;
Client.sendData(EnumPacketServer.ScriptForgeGet, new Object[0]);
Client.sendData(EnumPacketServer.ScriptForgeGet);
}
public void setGuiData(NBTTagCompound compound) {
this.script.readFromNBT(compound);
super.setGuiData(compound);
if (!compound.hasKey("Tab")) {
script.setLanguage(compound.getString("ScriptLanguage"));
script.setEnabled(compound.getBoolean("ScriptEnabled"));
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(script);
container.readFromNBT(compound.getCompoundTag("Script"));
if (script.getScripts().isEmpty()) {
for (int i = 0; i < compound.getInteger("TotalScripts"); i++) {
script.getScripts().add(new ScriptContainer(script));
}
}
script.getScripts().set(tab,container);
initGui();
}
}
public void save() {
super.save();
Client.sendData(EnumPacketServer.ScriptForgeSave, new Object[]{this.script.writeToNBT(new NBTTagCompound())});
List<ScriptContainer> containers = this.script.getScripts();
for (int i = 0; i < containers.size(); i++) {
ScriptContainer container = containers.get(i);
Client.sendData(EnumPacketServer.ScriptForgeSave, i, container.writeToNBT(new NBTTagCompound()));
}
NBTTagCompound scriptData = new NBTTagCompound();
scriptData.setString("ScriptLanguage", this.script.getLanguage());
scriptData.setBoolean("ScriptEnabled", this.script.getEnabled());
Client.sendData(EnumPacketServer.ScriptForgeSave, -1, scriptData);
}
}
@@ -9,29 +9,49 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.client.Client;
import noppes.npcs.constants.EnumPacketServer;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.controllers.ScriptController;
import noppes.npcs.controllers.data.PlayerDataScript;
import java.util.List;
public class GuiScriptPlayers extends GuiScriptInterface {
private PlayerDataScript script = new PlayerDataScript((EntityPlayer)null);
public GuiScriptPlayers() {
this.handler = this.script;
Client.sendData(EnumPacketServer.ScriptPlayerGet, new Object[0]);
Client.sendData(EnumPacketServer.ScriptPlayerGet);
}
public void setGuiData(NBTTagCompound compound) {
this.script.readFromNBT(compound);
super.setGuiData(compound);
if (!compound.hasKey("Tab")) {
script.setLanguage(compound.getString("ScriptLanguage"));
script.setEnabled(compound.getBoolean("ScriptEnabled"));
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(script);
container.readFromNBT(compound.getCompoundTag("Script"));
if (script.getScripts().isEmpty()) {
for (int i = 0; i < compound.getInteger("TotalScripts"); i++) {
script.getScripts().add(new ScriptContainer(script));
}
}
script.getScripts().set(tab,container);
initGui();
}
}
public void save() {
super.save();
Client.sendData(EnumPacketServer.ScriptPlayerSave, new Object[]{this.script.writeToNBT(new NBTTagCompound())});
}
public void close() {
super.close();
ScriptController.Instance.playerScripts.errored.clear();
List<ScriptContainer> containers = this.script.getScripts();
for (int i = 0; i < containers.size(); i++) {
ScriptContainer container = containers.get(i);
Client.sendData(EnumPacketServer.ScriptPlayerSave, i, container.writeToNBT(new NBTTagCompound()));
}
NBTTagCompound scriptData = new NBTTagCompound();
scriptData.setString("ScriptLanguage", this.script.getLanguage());
scriptData.setBoolean("ScriptEnabled", this.script.getEnabled());
Client.sendData(EnumPacketServer.ScriptPlayerSave, -1, scriptData);
}
}
@@ -89,19 +89,14 @@ public class ScriptController {
}
}
public void setForgeScripts(NBTTagCompound compound) {
this.forgeScripts.readFromNBT(compound);
public void saveForgeScripts() {
File file = this.forgeScriptsFile();
try {
NBTJsonUtil.SaveFile(file, compound);
NBTJsonUtil.SaveFile(file, this.forgeScripts.writeToNBT(new NBTTagCompound()));
this.forgeScripts.lastInited = -1L;
} catch (IOException var4) {
} catch (IOException | JsonException var4) {
var4.printStackTrace();
} catch (JsonException var5) {
var5.printStackTrace();
}
}
private File playerScriptsFile() {
@@ -130,17 +125,13 @@ public class ScriptController {
}
}
public void setPlayerScripts(NBTTagCompound compound) {
this.playerScripts.readFromNBT(compound);
public void savePlayerScripts() {
File file = this.playerScriptsFile();
try {
NBTJsonUtil.SaveFile(file, compound);
this.lastPlayerUpdate = System.currentTimeMillis();
} catch (IOException var4) {
NBTJsonUtil.SaveFile(file, this.playerScripts.writeToNBT(new NBTTagCompound()));
this.playerScripts.lastInited = -1L;
} catch (IOException | JsonException var4) {
var4.printStackTrace();
} catch (JsonException var5) {
var5.printStackTrace();
}
}
@@ -162,16 +153,13 @@ public class ScriptController {
}
}
public void setNPCScripts(NBTTagCompound compound) {
this.npcScripts.readFromNBT(compound);
public void saveNpcScripts() {
File file = this.npcScriptsFile();
try {
NBTJsonUtil.SaveFile(file, compound);
} catch (IOException var4) {
NBTJsonUtil.SaveFile(file, this.npcScripts.writeToNBT(new NBTTagCompound()));
this.npcScripts.lastInited = -1L;
} catch (IOException | JsonException var4) {
var4.printStackTrace();
} catch (JsonException var5) {
var5.printStackTrace();
}
}
@@ -31,7 +31,11 @@ public class ForgeDataScript implements IScriptHandler {
}
public void readFromNBT(NBTTagCompound compound) {
this.scripts = NBTTags.GetScript(compound.getTagList("Scripts", 10), this);
if (compound.hasKey("Scripts")) {
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
} else {
this.scripts = NBTTags.GetScript(compound,this);
}
this.scriptLanguage = compound.getString("ScriptLanguage");
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
if (!ScriptController.Instance.languages.isEmpty()) {
@@ -44,7 +48,10 @@ public class ForgeDataScript implements IScriptHandler {
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setTag("Scripts", NBTTags.NBTScript(this.scripts));
compound.setInteger("TotalScripts",this.scripts.size());
for (int i = 0; i < this.scripts.size(); i++) {
compound.setTag("Tab"+i,this.scripts.get(i).writeToNBT(new NBTTagCompound()));
}
compound.setString("ScriptLanguage", this.scriptLanguage);
compound.setBoolean("ScriptEnabled", this.enabled);
return compound;
@@ -36,7 +36,11 @@ public class NPCDataScript implements IScriptHandler {
this.scripts = new ArrayList();
}
public void readFromNBT(NBTTagCompound compound) {
this.scripts = NBTTags.GetScript(compound.getTagList("Scripts", 10), this);
if (compound.hasKey("Scripts")) {
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
} else {
this.scripts = NBTTags.GetScript(compound,this);
}
this.scriptLanguage = compound.getString("ScriptLanguage");
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
if (!ScriptController.Instance.languages.isEmpty()) {
@@ -49,7 +53,10 @@ public class NPCDataScript implements IScriptHandler {
this.console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setTag("Scripts", NBTTags.NBTScript(this.scripts));
compound.setInteger("TotalScripts",this.scripts.size());
for (int i = 0; i < this.scripts.size(); i++) {
compound.setTag("Tab"+i,this.scripts.get(i).writeToNBT(new NBTTagCompound()));
}
compound.setString("ScriptLanguage", this.scriptLanguage);
compound.setBoolean("ScriptEnabled", this.enabled);
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(this.console));
@@ -45,7 +45,11 @@ public class PlayerDataScript implements IScriptHandler {
this.scripts = new ArrayList();
}
public void readFromNBT(NBTTagCompound compound) {
this.scripts = NBTTags.GetScript(compound.getTagList("Scripts", 10), this);
if (compound.hasKey("Scripts")) {
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
} else {
this.scripts = NBTTags.GetScript(compound,this);
}
this.scriptLanguage = compound.getString("ScriptLanguage");
if (!ScriptController.Instance.languages.containsKey(scriptLanguage)) {
if (!ScriptController.Instance.languages.isEmpty()) {
@@ -58,7 +62,10 @@ public class PlayerDataScript implements IScriptHandler {
this.console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setTag("Scripts", NBTTags.NBTScript(this.scripts));
compound.setInteger("TotalScripts",this.scripts.size());
for (int i = 0; i < this.scripts.size(); i++) {
compound.setTag("Tab"+i,this.scripts.get(i).writeToNBT(new NBTTagCompound()));
}
compound.setString("ScriptLanguage", this.scriptLanguage);
compound.setBoolean("ScriptEnabled", this.enabled);
compound.setTag("ScriptConsole", NBTTags.NBTLongStringMap(this.console));
@@ -61,7 +61,7 @@ public class ScriptCustomItem extends ScriptItemStack implements IItemCustom, IS
public void setScriptNBT(NBTTagCompound compound) {
if (compound.hasKey("Scripts")) {
this.scripts = NBTTags.GetScript(compound.getTagList("Scripts", 10), this);
this.scripts = NBTTags.GetScriptOld(compound.getTagList("Scripts", 10), this);
this.scriptLanguage = compound.getString("ScriptLanguage");
this.enabled = compound.getBoolean("ScriptEnabled");
this.console = NBTTags.GetLongStringMap(compound.getTagList("ScriptConsole", 10));