Fix some Packet / Language Handling for Race Condition

This commit is contained in:
Kam committed 2026-03-05 01:37:44 -05:00
1 parent 6007b49271
commit dbca3da639
3 files changed
+57 -11

No files matched your search

@@ -184,6 +184,14 @@ public class GuiScript extends GuiScriptInterface {
addButton(new GuiNpcButton(100, guiLeft + 315, guiTop + 25, 50, 20, "gui.copy"));
addButton(new GuiNpcButton(107, guiLeft + 315, guiTop + 70, 80, 20, "script.loadscript"));
// Disable all buttons until server data arrives
if (!serverDataReceived) {
for (int btnId : new int[]{100, 101, 102, 107}) {
GuiNpcButton btn = getButton(btnId);
if (btn != null) btn.enabled = false;
}
}
GuiCustomScroll scroll = new GuiCustomScroll(this, 0).setUnselectable();
scroll.setSize(100, 120);
scroll.guiLeft = guiLeft + 315;
@@ -222,12 +230,18 @@ public class GuiScript extends GuiScriptInterface {
addButton(new GuiNpcButton(104, guiLeft + 294, guiTop + 48, 50, 20,
new String[]{"gui.no", "gui.yes"}, script.enabled ? 1 : 0));
// Disable language/enabled controls until server data actually arrives
getButton(103).enabled = serverDataReceived && languageOptions.size() > 0;
getButton(104).enabled = serverDataReceived;
if (MinecraftServer.getServer() != null)
addButton(new GuiNpcButton(106, guiLeft + 232, guiTop + 71, 150, 20, "script.openfolder"));
// Disable all buttons until server data arrives
if (!serverDataReceived) {
for (int btnId : new int[]{100, 102, 103, 104, 105, 106}) {
GuiNpcButton btn = getButton(btnId);
if (btn != null) btn.enabled = false;
}
} else {
getButton(103).enabled = languageOptions.size() > 0;
}
}
@Override
@@ -342,6 +356,11 @@ public class GuiScript extends GuiScriptInterface {
@Override
public void setGuiData(NBTTagCompound compound) {
// Only process the actual NPCScriptPacket response (has ScriptLanguage + Languages)
// Ignore unrelated packets (e.g. script config with ScriptsEnabled/GlobalNPCScriptsEnabled)
if (!compound.hasKey("ScriptLanguage") && !compound.hasKey("Languages")) {
return;
}
script.readFromNBT(compound);
loadLanguagesData(compound);
loaded = true;
@@ -349,7 +368,7 @@ public class GuiScript extends GuiScriptInterface {
@Override
public void save() {
if (loaded) {
if (loaded && serverDataReceived) {
setScript();
NPCScriptPacket.Save(script.writeToNBT(new NBTTagCompound()));
}
@@ -311,6 +311,14 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
this.addButton(new GuiNpcButton(107, left1, this.guiTop + 66 + yoffset, 80, 20, "script.loadscript"));
// Disable all buttons until server data arrives
if (!serverDataReceived) {
for (int btnId : new int[]{100, 101, 102, 105, 107, 113}) {
GuiNpcButton btn = getButton(btnId);
if (btn != null) btn.enabled = false;
}
}
GuiCustomScroll scroll = (new GuiCustomScroll(this, 0)).setUnselectable();
scroll.setSize(100, (int) ((double) this.ySize * 0.54D) - yoffset * 2);
scroll.guiLeft = left1;
@@ -344,11 +352,6 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
this.addButton(new GuiNpcButton(104, var9 + 60, this.guiTop + 31, 50, 20,
new String[]{"gui.no", "gui.yes"}, this.handler.getEnabled() ? 1 : 0));
// Disable language/enabled controls until server data actually arrives
// (serverDataReceived is only set in loadLanguagesData, not by getLanguageOptions injecting Java)
this.getButton(103).enabled = serverDataReceived && languageOptions.size() > 0;
this.getButton(104).enabled = serverDataReceived;
if (this.player.worldObj.isRemote) {
this.addButton(new GuiNpcButton(106, var9, this.guiTop + 55, 150, 20, "script.openfolder"));
}
@@ -357,6 +360,17 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
this.addButton(new GuiNpcButton(112, var9 + 81, this.guiTop + 78, 80, 20, "gui.forum"));
this.addButton(new GuiNpcButton(110, var9, this.guiTop + 99, 80, 20, "script.apidoc"));
this.addButton(new GuiNpcButton(111, var9 + 81, this.guiTop + 99, 80, 20, "script.apisrc"));
// Disable all buttons until server data arrives
if (!serverDataReceived) {
for (int btnId : new int[]{100, 102, 103, 104, 106, 109, 110, 111, 112}) {
GuiNpcButton btn = getButton(btnId);
if (btn != null) btn.enabled = false;
}
} else {
// Language button requires options to be available
this.getButton(103).enabled = languageOptions.size() > 0;
}
}
public GuiScriptInterface setDimensions(int x, int y) {
@@ -750,6 +764,9 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
public void setGuiData(NBTTagCompound compound) {
if (handler instanceof IScriptHandlerPacket) {
IScriptHandlerPacket.GuiDataResult result = ((IScriptHandlerPacket) handler).setGuiData(compound);
if (result == null) {
return; // Unrelated packet, ignore
}
if (result.kind == IScriptHandlerPacket.GuiDataKind.LOAD_COMPLETE) {
loaded = true;
return;
@@ -765,6 +782,10 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
return;
}
// For non-IScriptHandlerPacket handlers, ignore unrelated packets
if (!compound.hasKey("ScriptLanguage") && !compound.hasKey("Languages")) {
return;
}
loadLanguagesData(compound);
}
@@ -808,7 +829,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
}
public void save() {
if (!loaded)
if (!loaded || !serverDataReceived)
return;
this.setScript();
@@ -53,6 +53,12 @@ public interface IScriptHandlerPacket extends IScriptHandler {
return new GuiDataResult(GuiDataKind.LOAD_COMPLETE, -1);
}
// Ignore unrelated packets (e.g. ScriptInfoPacket config data)
// that don't contain any expected keys for this handler
if (!compound.hasKey("Tab") && !compound.hasKey("ScriptLanguage") && !compound.hasKey("ScriptEnabled")) {
return null;
}
if (!compound.hasKey("Tab")) {
setLanguage(compound.getString("ScriptLanguage"));
setEnabled(compound.getBoolean("ScriptEnabled"));