From b52e1d17f3ad96afc8f08a1548f0727700d071a5 Mon Sep 17 00:00:00 2001 From: 3944Realms Date: Wed, 15 Oct 2025 19:04:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3ISyncManager=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../lib39/core/sync/ISyncManager.java | 109 ++++++++++++++---- .../lib39/core/sync/SyncData2Manager.java | 2 +- 3 files changed, 89 insertions(+), 24 deletions(-) diff --git a/gradle.properties b/gradle.properties index 94fcdd2..8235d74 100644 --- a/gradle.properties +++ b/gradle.properties @@ -33,7 +33,7 @@ mod_name=3944Realms 's Lib Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=MIT # The mod version. See https://semver.org/ -mod_version=0.0.12 +mod_version=0.0.13 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/top/r3944realms/lib39/core/sync/ISyncManager.java b/src/main/java/top/r3944realms/lib39/core/sync/ISyncManager.java index b228529..9ae3085 100644 --- a/src/main/java/top/r3944realms/lib39/core/sync/ISyncManager.java +++ b/src/main/java/top/r3944realms/lib39/core/sync/ISyncManager.java @@ -1,35 +1,100 @@ package top.r3944realms.lib39.core.sync; -import org.jetbrains.annotations.NotNull; - -import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.function.Consumer; -public interface ISyncManager> { +public interface ISyncManager> { + + /** + * 获取同步映射 + */ Map getSyncMap(); + + /** + * 获取同步集合 + */ default Set getSyncSet() { - return new HashSet<>(getSyncMap().values()); - } - default void track(K key, T instance) { - Set syncSet = checkAndGetSet(); - syncSet.add(instance); - } - default void untrack(K key, T instance) { - Set syncSet = checkAndGetSet(); - syncSet.remove(instance); - } - default void foreach(Consumer consumer) { - Set syncSet = checkAndGetSet(); - syncSet.forEach(consumer); + Map syncMap = getSyncMap(); + return Set.copyOf(syncMap.values()); } - private @NotNull Set checkAndGetSet() throws IllegalArgumentException { - Set syncSet = getSyncSet(); - if (syncSet == null) { - throw new IllegalStateException("SyncSet is not initialized"); + /** + * 跟踪实例 + */ + default void track(K key, T instance) { + Map syncMap = getSyncMap(); + if (syncMap == null) { + throw new IllegalStateException("SyncMap is not initialized"); + } + syncMap.put(key, instance); + } + + /** + * 取消跟踪 + */ + default void untrack(K key, T instance) { + Map syncMap = getSyncMap(); + if (syncMap == null) { + throw new IllegalStateException("SyncMap is not initialized"); + } + // 只有当key对应的value确实是instance时才移除,避免误删 + syncMap.remove(key, instance); + } + + /** + * 遍历操作 + */ + default void foreach(Consumer consumer) { + Map syncMap = getSyncMap(); + if (syncMap == null) { + throw new IllegalStateException("SyncMap is not initialized"); + } + syncMap.values().forEach(consumer); + } + + /** + * 批量操作 + */ + default void trackAll(Map instances) { + Map syncMap = getSyncMap(); + if (syncMap == null) { + throw new IllegalStateException("SyncMap is not initialized"); + } + syncMap.putAll(instances); + } + + /** + * 获取大小 + */ + default int size() { + Map syncMap = getSyncMap(); + return syncMap != null ? syncMap.size() : 0; + } + + /** + * 检查是否包含key + */ + default boolean containsKey(K key) { + Map syncMap = getSyncMap(); + return syncMap != null && syncMap.containsKey(key); + } + + /** + * 检查是否包含value + */ + default boolean containsValue(T value) { + Map syncMap = getSyncMap(); + return syncMap != null && syncMap.containsValue(value); + } + + /** + * 清空所有数据 + */ + default void clear() { + Map syncMap = getSyncMap(); + if (syncMap != null) { + syncMap.clear(); } - return syncSet; } } diff --git a/src/main/java/top/r3944realms/lib39/core/sync/SyncData2Manager.java b/src/main/java/top/r3944realms/lib39/core/sync/SyncData2Manager.java index 9bcf6ad..0bb275d 100644 --- a/src/main/java/top/r3944realms/lib39/core/sync/SyncData2Manager.java +++ b/src/main/java/top/r3944realms/lib39/core/sync/SyncData2Manager.java @@ -182,7 +182,7 @@ public class SyncData2Manager { private > void untrackEntityWithTypedEntry(Entity entity, @NotNull TypedSyncEntry entry) { if (entry.capability != null) { entity.getCapability(entry.capability) - .ifPresent(cap -> entry.manager.track(entity.getUUID(), cap)); + .ifPresent(cap -> entry.manager.untrack(entity.getUUID(), cap)); } }