Fix trader edits being lost on an existing linked market

Closing the setup GUI saved the market from the server''s stale role and then
re-read that market over the edits it had just applied, so stock and currency
cost changes never survived. The role now saves first and the market is written
from it, and relinking to another market no longer pushes pre-link data over it.
This commit is contained in:
KAMKEEL committed 2026-08-11 03:21:46 -04:00
1 parent 17f61de57a
commit 68b5191db6
3 files changed
+29 -2

No files matched your search

@@ -14,6 +14,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.CustomNpcsPermissions;
import noppes.npcs.roles.RoleTrader;
import java.io.IOException;
@@ -63,7 +64,13 @@ public final class RoleSavePacket extends AbstractPacket {
if (!PacketUtil.verifyItemPacket(packetName, EnumItemPacketType.WAND, player))
return;
npc.roleInterface.readFromNBT(ByteBufUtils.readNBT(in));
NBTTagCompound roleCompound = ByteBufUtils.readNBT(in);
if (npc.roleInterface instanceof RoleTrader) {
// An admin edit wins over the linked market; the market is written from it afterwards.
((RoleTrader) npc.roleInterface).readFromNBT(roleCompound, false);
} else {
npc.roleInterface.readFromNBT(roleCompound);
}
npc.updateClient = true;
}
}
@@ -21,6 +21,7 @@ public class GuiNpcTraderSetup extends GuiContainerNPCInterface2 implements ITex
private final ResourceLocation slot = new ResourceLocation("customnpcs", "textures/gui/slot.png");
private RoleTrader role;
private boolean marketRelinked = false;
// Currency cost textfield IDs start at 100 (100-117 for slots 0-17)
private static final int CURRENCY_FIELD_ID_START = 100;
@@ -116,8 +117,14 @@ public class GuiNpcTraderSetup extends GuiContainerNPCInterface2 implements ITex
@Override
public void save() {
PacketClient.sendClient(new TraderMarketSavePacket(role.marketName, false));
// Linking to a different market already replaced the server's role with that market's
// data. Pushing this GUI's pre-link state back would overwrite the market being linked to.
if (marketRelinked)
return;
// Save the role first so the market is written from the edited data, not the stale data.
PacketClient.sendClient(new RoleSavePacket(role.writeToNBT(new NBTTagCompound())));
PacketClient.sendClient(new TraderMarketSavePacket(role.marketName, false));
}
@Override
@@ -129,6 +136,7 @@ public class GuiNpcTraderSetup extends GuiContainerNPCInterface2 implements ITex
String name = guiNpcTextField.getText();
if (!name.equalsIgnoreCase(role.marketName)) {
role.marketName = name;
marketRelinked = true;
PacketClient.sendClient(new TraderMarketSavePacket(role.marketName, true));
}
return;
@@ -94,6 +94,15 @@ public class RoleTrader extends RoleInterface {
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
readFromNBT(nbttagcompound, true);
}
/**
* @param loadFromMarket pull the linked market's stored data over the compound just read.
* False when the compound is an admin edit, which is authoritative and
* would otherwise be overwritten by the market it is about to be saved to.
*/
public void readFromNBT(NBTTagCompound nbttagcompound, boolean loadFromMarket) {
String oldMarket = marketName;
marketName = nbttagcompound.getString("TraderMarket");
readNBT(nbttagcompound);
@@ -101,6 +110,9 @@ public class RoleTrader extends RoleInterface {
// Register with MarketRegistry for linked market sync
MarketRegistry.updateTraderMarket(oldMarket, marketName, this);
if (!loadFromMarket)
return;
try {
Market.getMarket(this, marketName);
} catch (Exception ex) {