mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 16:59:43 +00:00
Script Token Handling
This commit is contained in:
1 parent
6489f75b7d
commit
7df1cfbfb9
19 files changed
+150
-31
No files matched your search
@@ -188,8 +188,11 @@ public class TileScripted extends TileEntity implements IScriptBlockHandler, ISc
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sync() {
|
||||
sendSavePacket(-1, getScripts().size(), getNBT(new NBTTagCompound()));
|
||||
public void sync(String sessionToken) {
|
||||
NBTTagCompound compound = getNBT(new NBTTagCompound());
|
||||
if (sessionToken != null && !sessionToken.isEmpty())
|
||||
compound.setString("ScriptSessionToken", sessionToken);
|
||||
sendSavePacket(-1, getScripts().size(), compound);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -343,6 +343,8 @@ public class GuiScript extends GuiScriptInterface {
|
||||
@Override
|
||||
public void setGuiData(NBTTagCompound compound) {
|
||||
script.readFromNBT(compound);
|
||||
if (compound.hasKey("ScriptSessionToken"))
|
||||
scriptSessionToken = compound.getString("ScriptSessionToken");
|
||||
loadLanguagesData(compound);
|
||||
loaded = true;
|
||||
}
|
||||
@@ -351,7 +353,10 @@ public class GuiScript extends GuiScriptInterface {
|
||||
public void save() {
|
||||
if (loaded) {
|
||||
setScript();
|
||||
NPCScriptPacket.Save(script.writeToNBT(new NBTTagCompound()));
|
||||
NBTTagCompound compound = script.writeToNBT(new NBTTagCompound());
|
||||
if (scriptSessionToken != null)
|
||||
compound.setString("ScriptSessionToken", scriptSessionToken);
|
||||
NPCScriptPacket.Save(compound);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,8 +49,9 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
public Map<String, List<String>> languages = new HashMap();
|
||||
protected int scriptLimit = 1;
|
||||
public List<String> hookList = new ArrayList<String>();
|
||||
protected boolean loaded = false;
|
||||
protected volatile boolean loaded = false;
|
||||
protected boolean serverDataReceived = false;
|
||||
protected String scriptSessionToken = null;
|
||||
|
||||
protected Map<Integer, GuiScriptTextArea> textAreas = new HashMap<>();
|
||||
protected GuiScreen parent;
|
||||
@@ -755,6 +756,8 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
return;
|
||||
}
|
||||
if (result.kind == IScriptHandlerPacket.GuiDataKind.METADATA) {
|
||||
if (compound.hasKey("ScriptSessionToken"))
|
||||
scriptSessionToken = compound.getString("ScriptSessionToken");
|
||||
loadLanguagesData(compound);
|
||||
return;
|
||||
}
|
||||
@@ -765,6 +768,8 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
return;
|
||||
}
|
||||
|
||||
if (compound.hasKey("ScriptSessionToken"))
|
||||
scriptSessionToken = compound.getString("ScriptSessionToken");
|
||||
loadLanguagesData(compound);
|
||||
}
|
||||
|
||||
@@ -814,9 +819,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
this.setScript();
|
||||
|
||||
if (handler instanceof IScriptHandlerPacket)
|
||||
((IScriptHandlerPacket) handler).sync();
|
||||
|
||||
|
||||
((IScriptHandlerPacket) handler).sync(scriptSessionToken);
|
||||
}
|
||||
|
||||
public void textUpdate(String text) {
|
||||
|
||||
@@ -71,16 +71,29 @@ public interface IScriptHandlerPacket extends IScriptHandler {
|
||||
* GuiScriptInterface is responsible for updating the active {@link IScriptUnit} with current editor text first.
|
||||
*/
|
||||
default void sync() {
|
||||
sync(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit save packets with a session token for server-side verification.
|
||||
* The token prevents saving unloaded default data due to race conditions.
|
||||
*/
|
||||
default void sync(String sessionToken) {
|
||||
List<IScriptUnit> containers = getScripts();
|
||||
for (int i = 0; i < containers.size(); i++) {
|
||||
IScriptUnit container = containers.get(i);
|
||||
sendSavePacket(i, containers.size(), container.writeToNBT(new NBTTagCompound()));
|
||||
NBTTagCompound nbt = container.writeToNBT(new NBTTagCompound());
|
||||
if (sessionToken != null && !sessionToken.isEmpty())
|
||||
nbt.setString("ScriptSessionToken", sessionToken);
|
||||
sendSavePacket(i, containers.size(), nbt);
|
||||
}
|
||||
|
||||
NBTTagCompound scriptData = new NBTTagCompound();
|
||||
scriptData.setString("ScriptLanguage", getLanguage());
|
||||
scriptData.setBoolean("ScriptEnabled", getEnabled());
|
||||
scriptData.setTag("ScriptConsole", NBTTags.NBTLongStringMap(getConsoleText()));
|
||||
if (sessionToken != null && !sessionToken.isEmpty())
|
||||
scriptData.setString("ScriptSessionToken", sessionToken);
|
||||
sendSavePacket(-1, containers.size(), scriptData);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package noppes.npcs.controllers.data;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.Event;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import kamkeel.npcs.network.PacketUtil;
|
||||
import kamkeel.npcs.util.ByteBufUtils;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.util.Constants;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
@@ -117,9 +119,16 @@ public abstract class SingleScriptHandler extends ScriptHandler {
|
||||
* - tab == 0: Script content NBT follows
|
||||
* - tab != 0 (typically -1): Script metadata (language, enabled) follows
|
||||
*/
|
||||
public void saveScript(ByteBuf buffer) throws IOException {
|
||||
public boolean saveScript(ByteBuf buffer, EntityPlayer player) throws IOException {
|
||||
int tab = buffer.readInt();
|
||||
int totalScripts = buffer.readInt();
|
||||
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
|
||||
|
||||
// Verify session token
|
||||
String token = compound.getString("ScriptSessionToken");
|
||||
if (!PacketUtil.verifyScriptSession(player, token)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (totalScripts == 0) {
|
||||
this.container = null;
|
||||
@@ -127,15 +136,14 @@ public abstract class SingleScriptHandler extends ScriptHandler {
|
||||
|
||||
if (tab == 0) {
|
||||
// Script content
|
||||
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
|
||||
this.container = IScriptUnit.createFromNBT(tabCompound, this);
|
||||
this.container = IScriptUnit.createFromNBT(compound, this);
|
||||
} else {
|
||||
// Metadata (language, enabled)
|
||||
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
|
||||
this.setLanguage(compound.getString("ScriptLanguage"));
|
||||
normalizeLanguage();
|
||||
this.setEnabled(compound.getBoolean("ScriptEnabled"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -99,8 +99,11 @@ public class ScriptCustomItem extends ScriptCustomizableItem implements IItemCus
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sync() {
|
||||
sendSavePacket(-1, getScripts().size(), getMCNbt());
|
||||
public void sync(String sessionToken) {
|
||||
NBTTagCompound compound = getMCNbt();
|
||||
if (sessionToken != null && !sessionToken.isEmpty())
|
||||
compound.setString("ScriptSessionToken", sessionToken);
|
||||
sendSavePacket(-1, getScripts().size(), compound);
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
|
||||
Reference in new issue
Block a user