Skip to content

Commit facee7c

Browse files
committed
Expanded SyncController scope to allow syncing based on string operation keys instead of just int ids (i.e ability keys)
1 parent 22de352 commit facee7c

5 files changed

Lines changed: 84 additions & 44 deletions

File tree

src/main/java/kamkeel/npcs/controllers/SyncController.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import cpw.mods.fml.relauncher.SideOnly;
55
import io.netty.buffer.ByteBuf;
66
import io.netty.buffer.Unpooled;
7-
import kamkeel.npcdbc.network.DBCPacketHandler;
8-
import kamkeel.npcdbc.network.packets.get.DBCInfoSyncPacket;
97
import kamkeel.npcs.addon.DBCAddon;
108
import kamkeel.npcs.controllers.sync.SyncHandler;
119
import kamkeel.npcs.controllers.sync.SyncRegistry;
@@ -26,7 +24,6 @@
2624
import noppes.npcs.client.ClientCacheHandler;
2725
import noppes.npcs.controllers.data.PlayerData;
2826

29-
import java.io.IOException;
3027
import java.util.HashMap;
3128
import java.util.LinkedHashMap;
3229
import java.util.Map;
@@ -146,7 +143,7 @@ private static void sendPostLoginPackets(EntityPlayerMP player) {
146143
* revision state. Used after the login handshake and by server-side callers
147144
* that need to refresh a player's global cached sync families.
148145
*/
149-
public static void syncPlayer(EntityPlayerMP player) {
146+
public static void dispatchSyncForAllTypes(EntityPlayerMP player) {
150147
PlayerSyncState state = playerSyncState.computeIfAbsent(player.getUniqueID(), PlayerSyncState::new);
151148

152149
// Registry-driven login iteration: iterate only cached types
@@ -194,7 +191,7 @@ public static void completeLoginRevisionHandshake(EntityPlayerMP player, String
194191
else // The client and server agree on identity, so seed the stored state from the report.
195192
state.applyHandshake(clientRevisions);
196193

197-
syncPlayer(player);
194+
dispatchSyncForAllTypes(player);
198195
}
199196

200197

@@ -217,21 +214,42 @@ public static void syncAll(SyncType type) {
217214
// ═══════════════════════════════════════════════════════════════════════
218215
// Server-side dispatchers — registry-driven
219216
// ═══════════════════════════════════════════════════════════════════════
217+
public static void syncUpdate(SyncType type, NBTTagCompound compound) {
218+
Map<SyncType, Integer> revisions = invalidateCaches(type);
219+
int revision = getRequestedInvalidationRevision(type, revisions);
220+
PacketHandler.Instance.sendToAll(new SyncPacket(type, EnumSyncAction.UPDATE, -1, null, revision, compound));
221+
updateAllPlayerRevisions(revisions);
222+
}
220223

221-
public static void syncRemove(SyncType syncType, int id) {
222-
Map<SyncType, Integer> revisions = invalidateCaches(syncType);
223-
int revision = getRequestedInvalidationRevision(syncType, revisions);
224-
PacketHandler.Instance.sendToAll(new SyncPacket(syncType, EnumSyncAction.REMOVE, id, revision, new NBTTagCompound()));
224+
public static void syncUpdate(SyncType type, String key, NBTTagCompound compound) {
225+
Map<SyncType, Integer> revisions = invalidateCaches(type);
226+
int revision = getRequestedInvalidationRevision(type, revisions);
227+
PacketHandler.Instance.sendToAll(new SyncPacket(type, EnumSyncAction.UPDATE, -1, key, revision, compound));
225228
updateAllPlayerRevisions(revisions);
226229
}
227230

228231
public static void syncUpdate(SyncType type, int cat, NBTTagCompound compound) {
229232
Map<SyncType, Integer> revisions = invalidateCaches(type);
230233
int revision = getRequestedInvalidationRevision(type, revisions);
231-
PacketHandler.Instance.sendToAll(new SyncPacket(type, EnumSyncAction.UPDATE, cat, revision, compound));
234+
PacketHandler.Instance.sendToAll(new SyncPacket(type, EnumSyncAction.UPDATE, cat, null, revision, compound));
232235
updateAllPlayerRevisions(revisions);
233236
}
234237

238+
public static void syncRemove(SyncType syncType, String key) {
239+
Map<SyncType, Integer> revisions = invalidateCaches(syncType);
240+
int revision = getRequestedInvalidationRevision(syncType, revisions);
241+
PacketHandler.Instance.sendToAll(new SyncPacket(syncType, EnumSyncAction.REMOVE, -1, key, revision, null));
242+
updateAllPlayerRevisions(revisions);
243+
}
244+
245+
public static void syncRemove(SyncType syncType, int id) {
246+
Map<SyncType, Integer> revisions = invalidateCaches(syncType);
247+
int revision = getRequestedInvalidationRevision(syncType, revisions);
248+
PacketHandler.Instance.sendToAll(new SyncPacket(syncType, EnumSyncAction.REMOVE, id, null, revision, null));
249+
updateAllPlayerRevisions(revisions);
250+
}
251+
252+
235253
// ═══════════════════════════════════════════════════════════════════════
236254
// Client-side handlers — registry-driven handler delegation
237255
// ═══════════════════════════════════════════════════════════════════════
@@ -250,11 +268,12 @@ public static void clientHandleAll(SyncType syncType, int revision, NBTTagCompou
250268
}
251269

252270
@SideOnly(Side.CLIENT)
253-
public static void clientHandleUpdate(SyncType syncType, int category_id, int revision, NBTTagCompound compound) {
271+
public static void clientHandleUpdate(SyncType syncType, int id, String key, int revision, NBTTagCompound compound) {
254272
SyncHandler handler = SyncRegistry.getHandler(syncType);
255273
if (handler == null) return;
256274
try {
257-
handler.clientHandleUpdate(compound, category_id);
275+
handler.clientHandleUpdate(compound, id);
276+
handler.clientHandleUpdate(compound, key);
258277
} catch (Exception e) {
259278
LogWriter.error("[SyncController] Failed to handle UPDATE for sync type " + syncType, e);
260279
}
@@ -263,12 +282,12 @@ public static void clientHandleUpdate(SyncType syncType, int category_id, int re
263282
}
264283

265284
@SideOnly(Side.CLIENT)
266-
public static void clientHandleRemove(SyncType syncType, int id, int revision, NBTTagCompound compound) {
285+
public static void clientHandleRemove(SyncType syncType, int id, String key, int revision) {
267286
SyncHandler handler = SyncRegistry.getHandler(syncType);
268287
if (handler == null) return;
269288
try {
270289
handler.clientHandleRemove(id);
271-
handler.clientHandleRemove(compound);
290+
handler.clientHandleRemove(key);
272291
} catch (Exception e) {
273292
LogWriter.error("[SyncController] Failed to handle REMOVE for sync type " + syncType, e);
274293
}

src/main/java/kamkeel/npcs/controllers/sync/SyncHandler.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,26 @@ public interface SyncHandler {
5252
* @param fullCompound the full serialized dataset from {@link #serializeAll()}
5353
*/
5454
@SideOnly(Side.CLIENT)
55-
void clientHandleReload(NBTTagCompound fullCompound);
55+
default void clientHandleReload(NBTTagCompound fullCompound){
56+
// No-op: type does not support individual updates
57+
}
5658

59+
/**
60+
* Handle an UPDATE action on the client.
61+
* Deserialize a single object and insert/replace it in the client-side data.
62+
*
63+
* <p>Default: no-op. Types that don't support individual updates
64+
* use {@code syncAll()} for full reload instead.</p>
65+
*
66+
* @param compound the serialized single object
67+
* @param categoryKey the parent category key (null if not applicable)
68+
*/
69+
@SideOnly(Side.CLIENT)
70+
default void clientHandleUpdate(NBTTagCompound compound, String categoryKey) {
71+
// No-op: type does not support individual updates
72+
}
73+
74+
5775
/**
5876
* Handle an UPDATE action on the client. Deserialize a single
5977
* entity and insert/replace it in the client-side data.
@@ -75,7 +93,7 @@ default void clientHandleUpdate(NBTTagCompound compound, int categoryId) {
7593
* Int-keyed handlers should override {@link #clientHandleRemove(int)} instead.
7694
*/
7795
@SideOnly(Side.CLIENT)
78-
default void clientHandleRemove(NBTTagCompound compound) {
96+
default void clientHandleRemove(String key) {
7997
}
8098

8199
/**

src/main/java/kamkeel/npcs/network/packets/data/large/SyncPacket.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class SyncPacket extends LargeAbstractPacket {
2828
private EnumSyncAction enumSyncAction;
2929
private NBTTagCompound syncData;
3030
private int operationID;
31+
private String operationKey;
3132
private int revision = -1;
3233
private byte[] cachedPayload;
3334
private byte[][] cachedChunks;
@@ -36,14 +37,15 @@ public SyncPacket() {
3637
}
3738

3839
public SyncPacket(SyncType syncType, EnumSyncAction enumSyncAction, int catId, NBTTagCompound syncData) {
39-
this(syncType, enumSyncAction, catId, -1, syncData);
40+
this(syncType, enumSyncAction, catId, null, -1, syncData);
4041
}
4142

42-
public SyncPacket(SyncType syncType, EnumSyncAction enumSyncAction, int catId, int revision, NBTTagCompound syncData) {
43+
public SyncPacket(SyncType syncType, EnumSyncAction enumSyncAction, int operationId, String operationKey, int revision, NBTTagCompound syncData) {
4344
this.syncType = syncType;
4445
this.enumSyncAction = enumSyncAction;
4546
this.syncData = syncData;
46-
this.operationID = catId;
47+
this.operationID = operationId;
48+
this.operationKey = operationKey;
4749
this.revision = revision;
4850
}
4951

@@ -72,13 +74,19 @@ protected byte[] getData() throws IOException {
7274
return cachedPayload;
7375
}
7476

77+
boolean hasKey = operationKey != null;
78+
boolean hasNbt = syncData != null;
79+
7580
ByteBuf buffer = Unpooled.buffer();
7681
try {
7782
buffer.writeInt(syncType.ordinal());
7883
buffer.writeInt(enumSyncAction.ordinal());
7984
buffer.writeInt(operationID);
85+
buffer.writeBoolean(hasKey);
86+
if (hasKey) ByteBufUtils.writeString(buffer, operationKey);
8087
buffer.writeInt(revision);
81-
ByteBufUtils.writeBigNBT(buffer, syncData);
88+
buffer.writeBoolean(hasNbt);
89+
if (hasNbt) ByteBufUtils.writeBigNBT(buffer, syncData);
8290

8391
byte[] bytes = new byte[buffer.readableBytes()];
8492
buffer.readBytes(bytes);
@@ -126,39 +134,32 @@ protected void handleCompleteData(ByteBuf data, EntityPlayer player) throws IOEx
126134

127135
int syncTypeOrdinal = data.readInt();
128136
int syncActionOrdinal = data.readInt();
129-
int categoryID = data.readInt();
130-
int incomingRevision = data.readInt();
137+
operationID = data.readInt();
138+
if (data.readBoolean()) operationKey = ByteBufUtils.readString(data);
139+
revision = data.readInt();
140+
if (data.readBoolean()) syncData = ByteBufUtils.readBigNBT(data);
131141

132-
SyncType type = SyncType.byOrdinal(syncTypeOrdinal);
133-
if (type == null) {
142+
143+
syncType = SyncType.byOrdinal(syncTypeOrdinal);
144+
if (syncType == null) {
134145
LogWriter.error("[SyncPacket] Unknown sync type ordinal: " + syncTypeOrdinal + "; skipping");
135146
return;
136147
}
148+
137149
EnumSyncAction action = EnumSyncAction.values()[syncActionOrdinal];
138-
try {
139-
NBTTagCompound tag = ByteBufUtils.readBigNBT(data);
140-
handleSyncPacketClient(type, action, categoryID, incomingRevision, tag);
141-
} catch (RuntimeException e) {
142-
LogWriter.error(String.format("Attempted to Sync %s but it was too big", type.toString()));
143-
}
150+
handleSync(syncType, action, operationID, operationKey, revision, syncData);
144151
}
145152

146-
private void handleSyncPacketClient(
147-
SyncType syncType,
148-
EnumSyncAction enumSyncAction,
149-
int id,
150-
int incomingRevision,
151-
NBTTagCompound data
152-
) {
153-
switch (enumSyncAction) {
153+
private void handleSync(SyncType type, EnumSyncAction action, int id, String key, int revision, NBTTagCompound data) {
154+
switch (action) {
154155
case RELOAD:
155-
SyncController.clientHandleAll(syncType, incomingRevision, data);
156+
SyncController.clientHandleAll(type, revision, data);
156157
break;
157158
case UPDATE:
158-
SyncController.clientHandleUpdate(syncType, id, incomingRevision, data);
159+
SyncController.clientHandleUpdate(type, id, key, revision, data);
159160
break;
160161
case REMOVE:
161-
SyncController.clientHandleRemove(syncType, id, incomingRevision, data);
162+
SyncController.clientHandleRemove(type, id, key, revision);
162163
break;
163164
}
164165
}

src/main/java/noppes/npcs/NoppesUtilServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ public static void removePlayerData(ByteBuf buffer, EntityPlayerMP player) throw
640640
playerdata.save();
641641
}
642642
if (pl != null) {
643-
SyncController.syncPlayer((EntityPlayerMP) pl);
643+
SyncController.dispatchSyncForAllTypes((EntityPlayerMP) pl);
644644
}
645645
sendPlayerData(type, player, name);
646646
}
@@ -1229,7 +1229,7 @@ public static void removePlayerDataInfo(String playerName, int tabType, int valu
12291229
playerdata.save();
12301230

12311231
if (pl != null) {
1232-
SyncController.syncPlayer((EntityPlayerMP) pl);
1232+
SyncController.dispatchSyncForAllTypes((EntityPlayerMP) pl);
12331233
}
12341234
}
12351235

@@ -1261,7 +1261,7 @@ public static void savePlayerDataInfo(String playerName, int tabType, NBTTagComp
12611261

12621262
playerdata.save();
12631263
if (pl != null) {
1264-
SyncController.syncPlayer((EntityPlayerMP) pl);
1264+
SyncController.dispatchSyncForAllTypes((EntityPlayerMP) pl);
12651265
}
12661266
}
12671267
}

src/main/java/noppes/npcs/ServerEventsHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ public void invoke(PlayerInteractEvent event) {
260260
SyncType.WORKBENCH_RECIPES,
261261
EnumSyncAction.RELOAD,
262262
-1,
263+
null,
263264
SyncController.getCurrentServerRevision(SyncType.WORKBENCH_RECIPES),
264265
WorkbenchRecipeSyncHandler.getInstance().serializeAll()
265266
), (EntityPlayerMP) player);
@@ -269,6 +270,7 @@ public void invoke(PlayerInteractEvent event) {
269270
SyncType.CARPENTRY_RECIPES,
270271
EnumSyncAction.RELOAD,
271272
-1,
273+
null,
272274
SyncController.getCurrentServerRevision(SyncType.CARPENTRY_RECIPES),
273275
CarpentryRecipeSyncHandler.getInstance().serializeAll()
274276
), (EntityPlayerMP) player);

0 commit comments

Comments
 (0)