SyncController OVERHAUL PHASE #1:

- Refactored EnumSyncType into object based SyncType registry, so other mods can register their own types
This commit is contained in:
bigguy345 committed 2026-03-31 19:59:53 +02:00
1 parent b2c279feeb
commit 48281c8d34
13 files changed
+673 -558

No files matched your search

File diff suppressed because it is too large. Load diff
@@ -1,18 +0,0 @@
package kamkeel.npcs.network.enums;
public enum EnumSyncType {
FACTION,
DIALOG,
DIALOG_CATEGORY,
QUEST,
QUEST_CATEGORY,
PLAYERDATA,
MAGIC,
MAGIC_CYCLE,
WORKBENCH_RECIPES,
CARPENTRY_RECIPES,
ANVIL_RECIPES,
CUSTOM_EFFECTS,
CUSTOM_ABILITY,
CHAINED_ABILITY
}
@@ -0,0 +1,178 @@
package kamkeel.npcs.network.enums;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Immutable, object-based sync-type identity for the CNPC+ sync backend.
*
* <p>Each {@code SyncType} has a canonical namespaced string ID ({@code modid:path})
* and is registered via the static {@link #register} factory. Built-in CNPC+
* families are exposed as {@code public static final} constants.</p>
*
* <p>Identity is based on the canonical {@link #id} string, which is unique
* across all registered types. Two references with the same {@code id}
* are always the same object (identity == equality for registered types).</p>
*
* <p>The {@code ordinal} field provides a stable integer identity for
* wire encoding during the migration period. It is assigned at registration
* time in declaration order and must NOT be used as a protocol identifier
* once string-on-wire migration is complete.</p>
*/
public final class SyncType {
// ──────────────────── Registry ────────────────────
private static final Map<String, SyncType> REGISTRY = new LinkedHashMap<>();
private static int nextOrdinal = 0;
private static boolean frozen = false;
// ──────────────────── CNPC+ families ────────────────────
public static final SyncType FACTION = register("cnpc:faction");
public static final SyncType DIALOG = register("cnpc:dialog");
public static final SyncType DIALOG_CATEGORY = register("cnpc:dialog_category");
public static final SyncType QUEST = register("cnpc:quest");
public static final SyncType QUEST_CATEGORY = register("cnpc:quest_category");
public static final SyncType PLAYERDATA = register("cnpc:playerdata");
public static final SyncType MAGIC = register("cnpc:magic");
public static final SyncType MAGIC_CYCLE = register("cnpc:magic_cycle");
public static final SyncType WORKBENCH_RECIPES = register("cnpc:workbench_recipes");
public static final SyncType CARPENTRY_RECIPES = register("cnpc:carpentry_recipes");
public static final SyncType ANVIL_RECIPES = register("cnpc:anvil_recipes");
public static final SyncType CUSTOM_EFFECTS = register("cnpc:custom_effects");
public static final SyncType CUSTOM_ABILITY = register("cnpc:custom_ability");
public static final SyncType CHAINED_ABILITY = register("cnpc:chained_ability");
// ──────────────────── Instance fields ────────────────────
private final String id;
private final int ordinal;
private SyncType(String id, int ordinal) {
this.id = id;
this.ordinal = ordinal;
}
// ──────────────────── Public API ────────────────────
/**
* The canonical namespaced identifier, e.g. {@code "cnpc:faction"}.
*/
public String getId() {
return id;
}
/**
* Stable integer ordinal assigned at registration time.
* Used for wire encoding during the migration period only.
*/
public int ordinal() {
return ordinal;
}
// ──────────────────── Registration ────────────────────
/**
* Register a new {@code SyncType} with the given canonical ID.
*
* @param id namespaced identifier ({@code modid:path})
* @return the newly created and registered {@code SyncType}
* @throws IllegalArgumentException if the ID is null, empty, or lacks a colon
* @throws IllegalStateException if a type with the same ID is already registered
* @throws IllegalStateException if the registry has been frozen
*/
public static SyncType register(String id) {
if (id == null || id.isEmpty()) {
throw new IllegalArgumentException("SyncType id must not be null or empty");
}
if (!id.contains(":")) {
throw new IllegalArgumentException("SyncType id must be namespaced (modid:path): " + id);
}
if (frozen) {
throw new IllegalStateException("SyncType registry is frozen; cannot register: " + id);
}
if (REGISTRY.containsKey(id)) {
throw new IllegalStateException("Duplicate SyncType registration: " + id);
}
SyncType type = new SyncType(id, nextOrdinal++);
REGISTRY.put(id, type);
return type;
}
/**
* Look up a registered SyncType by its canonical string ID.
*
* @return the registered type, or {@code null} if not found
*/
public static SyncType byId(String id) {
return id == null ? null : REGISTRY.get(id);
}
/**
* Look up a registered SyncType by its ordinal.
* Used during migration for decoding ordinal-based wire traffic.
*
* @return the registered type, or {@code null} if ordinal is out of range
*/
public static SyncType byOrdinal(int ordinal) {
if (ordinal < 0) {
return null;
}
for (SyncType type : REGISTRY.values()) {
if (type.ordinal == ordinal) {
return type;
}
}
return null;
}
/**
* Returns all registered SyncType instances in registration order.
*/
public static Collection<SyncType> values() {
return Collections.unmodifiableCollection(REGISTRY.values());
}
/**
* Freeze the registry. After freezing, no new types can be registered.
* Should be called after all mods have had a chance to register types.
*/
public static void freeze() {
frozen = true;
}
/**
* Whether the registry has been frozen.
*/
public static boolean isFrozen() {
return frozen;
}
/**
* Unfreeze the registry. Called during world reload to allow addon
* re-registration. Does NOT clear existing registrations — built-in
* constants stay stable.
*/
public static void unfreeze() {
frozen = false;
}
// ──────────────────── Object overrides ────────────────────
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SyncType)) return false;
return id.equals(((SyncType) o).id);
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public String toString() {
return id;
}
}
@@ -8,7 +8,7 @@ import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumDataPacket;
import kamkeel.npcs.network.enums.EnumSyncType;
import kamkeel.npcs.network.enums.SyncType;
import kamkeel.npcs.network.packets.player.SyncRevisionInfoPacket;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
@@ -21,19 +21,20 @@ import noppes.npcs.controllers.AuctionConfigSync;
import noppes.npcs.controllers.ScriptConfigSync;
import java.io.IOException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public final class LoginPacket extends AbstractPacket {
public static final String packetName = "CNPC+|Login";
private String serverCacheKey = "";
private final EnumMap<EnumSyncType, Integer> revisionSnapshot = new EnumMap<>(EnumSyncType.class);
private final Map<SyncType, Integer> revisionSnapshot = new LinkedHashMap<>();
public LoginPacket() {
}
public LoginPacket(String serverCacheKey, EnumMap<EnumSyncType, Integer> revisionSnapshot) {
public LoginPacket(String serverCacheKey, Map<SyncType, Integer> revisionSnapshot) {
if (serverCacheKey != null) {
this.serverCacheKey = serverCacheKey;
}
@@ -58,17 +59,15 @@ public final class LoginPacket extends AbstractPacket {
out.writeBoolean(ConfigMain.ProfilesEnabled);
ByteBufUtils.writeString(out, serverCacheKey);
out.writeShort(revisionSnapshot.size());
for (Map.Entry<EnumSyncType, Integer> entry : revisionSnapshot.entrySet()) {
for (Map.Entry<SyncType, Integer> entry : revisionSnapshot.entrySet()) {
out.writeInt(entry.getKey().ordinal());
out.writeInt(entry.getValue());
}
// Send auction config (server-side sync)
NBTTagCompound auctionConfig = new NBTTagCompound();
AuctionConfigSync.writeToNBT(auctionConfig);
ByteBufUtils.writeNBT(out, auctionConfig);
// Send script config (server-side sync)
NBTTagCompound scriptConfig = new NBTTagCompound();
ScriptConfigSync.writeToNBT(scriptConfig);
ByteBufUtils.writeNBT(out, scriptConfig);
@@ -82,20 +81,20 @@ public final class LoginPacket extends AbstractPacket {
String serverKey = ByteBufUtils.readString(in);
int revisionCount = in.readUnsignedShort();
EnumMap<EnumSyncType, Integer> serverRevisions = new EnumMap<>(EnumSyncType.class);
Map<SyncType, Integer> serverRevisions = new LinkedHashMap<>();
for (int i = 0; i < revisionCount; i++) {
EnumSyncType type = EnumSyncType.values()[in.readInt()];
SyncType type = SyncType.byOrdinal(in.readInt());
int revision = in.readInt();
serverRevisions.put(type, revision);
if (type != null) {
serverRevisions.put(type, revision);
}
}
ClientCacheHandler.setActiveServer(serverKey, serverRevisions);
// Read auction config
NBTTagCompound auctionConfig = ByteBufUtils.readNBT(in);
AuctionClientConfig.readFromNBT(auctionConfig);
// Read script config
NBTTagCompound scriptConfig = ByteBufUtils.readNBT(in);
ScriptClientConfig.readFromNBT(scriptConfig);
@@ -10,7 +10,7 @@ import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumDataPacket;
import kamkeel.npcs.network.enums.EnumSyncAction;
import kamkeel.npcs.network.enums.EnumSyncType;
import kamkeel.npcs.network.enums.SyncType;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
@@ -22,13 +22,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* A large sync packet that sends chunked data to the client for a given SyncType
* along with a SyncAction (RELOAD, UPDATE, REMOVE).
*/
public final class SyncPacket extends LargeAbstractPacket {
private EnumSyncType enumSyncType;
private SyncType syncType;
private EnumSyncAction enumSyncAction;
private NBTTagCompound syncData;
private int operationID;
@@ -39,23 +35,20 @@ public final class SyncPacket extends LargeAbstractPacket {
public SyncPacket() {
}
/**
* Constructs a new LargeSyncPacket.
*/
public SyncPacket(EnumSyncType enumSyncType, EnumSyncAction enumSyncAction, int catId, NBTTagCompound syncData) {
this(enumSyncType, enumSyncAction, catId, -1, syncData);
public SyncPacket(SyncType syncType, EnumSyncAction enumSyncAction, int catId, NBTTagCompound syncData) {
this(syncType, enumSyncAction, catId, -1, syncData);
}
public SyncPacket(EnumSyncType enumSyncType, EnumSyncAction enumSyncAction, int catId, int revision, NBTTagCompound syncData) {
this.enumSyncType = enumSyncType;
public SyncPacket(SyncType syncType, EnumSyncAction enumSyncAction, int catId, int revision, NBTTagCompound syncData) {
this.syncType = syncType;
this.enumSyncAction = enumSyncAction;
this.syncData = syncData;
this.operationID = catId;
this.revision = revision;
}
public SyncPacket(EnumSyncType enumSyncType, SyncController.CachedSyncPayload payload) {
this.enumSyncType = enumSyncType;
public SyncPacket(SyncType syncType, SyncController.CachedSyncPayload payload) {
this.syncType = syncType;
this.enumSyncAction = EnumSyncAction.RELOAD;
this.operationID = -1;
this.revision = payload.getRevision();
@@ -81,7 +74,7 @@ public final class SyncPacket extends LargeAbstractPacket {
ByteBuf buffer = Unpooled.buffer();
try {
buffer.writeInt(enumSyncType.ordinal());
buffer.writeInt(syncType.ordinal());
buffer.writeInt(enumSyncAction.ordinal());
buffer.writeInt(operationID);
buffer.writeInt(revision);
@@ -131,17 +124,19 @@ public final class SyncPacket extends LargeAbstractPacket {
if (CustomNpcs.side() != Side.CLIENT)
return;
// Reconstruct everything from the ByteBuf
int syncTypeOrdinal = data.readInt();
int syncActionOrdinal = data.readInt();
int categoryID = data.readInt();
int incomingRevision = data.readInt();
EnumSyncType type = EnumSyncType.values()[syncTypeOrdinal];
SyncType type = SyncType.byOrdinal(syncTypeOrdinal);
if (type == null) {
LogWriter.error("[SyncPacket] Unknown sync type ordinal: " + syncTypeOrdinal + "; skipping");
return;
}
EnumSyncAction action = EnumSyncAction.values()[syncActionOrdinal];
try {
NBTTagCompound tag = ByteBufUtils.readBigNBT(data);
// Now do your client-side logic (similar to your old clientSync() or clientSyncUpdate() approach)
handleSyncPacketClient(type, action, categoryID, incomingRevision, tag);
} catch (RuntimeException e) {
LogWriter.error(String.format("Attempted to Sync %s but it was too big", type.toString()));
@@ -149,7 +144,7 @@ public final class SyncPacket extends LargeAbstractPacket {
}
private void handleSyncPacketClient(
EnumSyncType enumSyncType,
SyncType syncType,
EnumSyncAction enumSyncAction,
int id,
int incomingRevision,
@@ -157,13 +152,13 @@ public final class SyncPacket extends LargeAbstractPacket {
) {
switch (enumSyncAction) {
case RELOAD:
SyncController.clientSync(enumSyncType, incomingRevision, data);
SyncController.clientSync(syncType, incomingRevision, data);
break;
case UPDATE:
SyncController.clientUpdate(enumSyncType, id, incomingRevision, data);
SyncController.clientUpdate(syncType, id, incomingRevision, data);
break;
case REMOVE:
SyncController.clientSyncRemove(enumSyncType, id, incomingRevision);
SyncController.clientSyncRemove(syncType, id, incomingRevision);
break;
}
}
@@ -8,13 +8,13 @@ import kamkeel.npcs.network.AbstractPacket;
import kamkeel.npcs.network.PacketChannel;
import kamkeel.npcs.network.PacketHandler;
import kamkeel.npcs.network.enums.EnumPlayerPacket;
import kamkeel.npcs.network.enums.EnumSyncType;
import kamkeel.npcs.network.enums.SyncType;
import kamkeel.npcs.util.ByteBufUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import java.io.IOException;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class SyncRevisionInfoPacket extends AbstractPacket {
@@ -23,12 +23,12 @@ public class SyncRevisionInfoPacket extends AbstractPacket {
private String serverKey = "";
private String previousServerKey = "";
private final EnumMap<EnumSyncType, Integer> revisions = new EnumMap<>(EnumSyncType.class);
private final Map<SyncType, Integer> revisions = new LinkedHashMap<>();
public SyncRevisionInfoPacket() {
}
public SyncRevisionInfoPacket(String serverKey, String previousServerKey, EnumMap<EnumSyncType, Integer> revisions) {
public SyncRevisionInfoPacket(String serverKey, String previousServerKey, Map<SyncType, Integer> revisions) {
if (serverKey != null) {
this.serverKey = serverKey;
}
@@ -56,7 +56,7 @@ public class SyncRevisionInfoPacket extends AbstractPacket {
ByteBufUtils.writeString(out, serverKey);
ByteBufUtils.writeString(out, previousServerKey);
out.writeShort(revisions.size());
for (Map.Entry<EnumSyncType, Integer> entry : revisions.entrySet()) {
for (Map.Entry<SyncType, Integer> entry : revisions.entrySet()) {
out.writeInt(entry.getKey().ordinal());
out.writeInt(entry.getValue());
}
@@ -71,11 +71,13 @@ public class SyncRevisionInfoPacket extends AbstractPacket {
String incomingServerKey = ByteBufUtils.readString(in);
String incomingPreviousKey = ByteBufUtils.readString(in);
int revisionCount = in.readUnsignedShort();
EnumMap<EnumSyncType, Integer> incomingRevisions = new EnumMap<>(EnumSyncType.class);
Map<SyncType, Integer> incomingRevisions = new LinkedHashMap<>();
for (int i = 0; i < revisionCount; i++) {
EnumSyncType type = EnumSyncType.values()[in.readInt()];
SyncType type = SyncType.byOrdinal(in.readInt());
int revision = in.readInt();
incomingRevisions.put(type, revision);
if (type != null) {
incomingRevisions.put(type, revision);
}
}
SyncController.handleClientRevisionReport((EntityPlayerMP) player, incomingServerKey, incomingPreviousKey, incomingRevisions);