修正ISyncManager类

This commit is contained in:
叁玖领域 2025-10-15 19:04:57 +08:00
parent 7fca59b84c
commit b52e1d17f3
3 changed files with 89 additions and 24 deletions

View File

@ -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

View File

@ -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<K, T extends ISyncData<?>> {
/**
* 获取同步映射
*/
Map<K, T> getSyncMap();
/**
* 获取同步集合
*/
default Set<T> getSyncSet() {
return new HashSet<>(getSyncMap().values());
}
default void track(K key, T instance) {
Set<T> syncSet = checkAndGetSet();
syncSet.add(instance);
}
default void untrack(K key, T instance) {
Set<T> syncSet = checkAndGetSet();
syncSet.remove(instance);
}
default void foreach(Consumer<T> consumer) {
Set<T> syncSet = checkAndGetSet();
syncSet.forEach(consumer);
Map<K, T> syncMap = getSyncMap();
return Set.copyOf(syncMap.values());
}
private @NotNull Set<T> checkAndGetSet() throws IllegalArgumentException {
Set<T> syncSet = getSyncSet();
if (syncSet == null) {
throw new IllegalStateException("SyncSet is not initialized");
/**
* 跟踪实例
*/
default void track(K key, T instance) {
Map<K, T> syncMap = getSyncMap();
if (syncMap == null) {
throw new IllegalStateException("SyncMap is not initialized");
}
syncMap.put(key, instance);
}
/**
* 取消跟踪
*/
default void untrack(K key, T instance) {
Map<K, T> syncMap = getSyncMap();
if (syncMap == null) {
throw new IllegalStateException("SyncMap is not initialized");
}
// 只有当key对应的value确实是instance时才移除避免误删
syncMap.remove(key, instance);
}
/**
* 遍历操作
*/
default void foreach(Consumer<T> consumer) {
Map<K, T> syncMap = getSyncMap();
if (syncMap == null) {
throw new IllegalStateException("SyncMap is not initialized");
}
syncMap.values().forEach(consumer);
}
/**
* 批量操作
*/
default void trackAll(Map<K, T> instances) {
Map<K, T> syncMap = getSyncMap();
if (syncMap == null) {
throw new IllegalStateException("SyncMap is not initialized");
}
syncMap.putAll(instances);
}
/**
* 获取大小
*/
default int size() {
Map<K, T> syncMap = getSyncMap();
return syncMap != null ? syncMap.size() : 0;
}
/**
* 检查是否包含key
*/
default boolean containsKey(K key) {
Map<K, T> syncMap = getSyncMap();
return syncMap != null && syncMap.containsKey(key);
}
/**
* 检查是否包含value
*/
default boolean containsValue(T value) {
Map<K, T> syncMap = getSyncMap();
return syncMap != null && syncMap.containsValue(value);
}
/**
* 清空所有数据
*/
default void clear() {
Map<K, T> syncMap = getSyncMap();
if (syncMap != null) {
syncMap.clear();
}
return syncSet;
}
}

View File

@ -182,7 +182,7 @@ public class SyncData2Manager {
private <T extends ISyncData<?>> void untrackEntityWithTypedEntry(Entity entity, @NotNull TypedSyncEntry<UUID, T> 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));
}
}