PatchedDataComponentMap.java

net.minecraft.core.component.PatchedDataComponentMap

信息

  • 全限定名:net.minecraft.core.component.PatchedDataComponentMap
  • 类型:public final class
  • 包:net.minecraft.core.component
  • 源码路径:src/main/java/net/minecraft/core/component/PatchedDataComponentMap.java
  • 起始行号:L17
  • 实现:DataComponentMap
  • 职责:

    TODO

字段/常量

  • prototype

    • 类型: DataComponentMap
    • 修饰符: private final
    • 源码定位: L18
    • 说明:

      TODO

  • patch

    • 类型: Reference2ObjectMap<DataComponentType<?>,Optional<?>>
    • 修饰符: private
    • 源码定位: L19
    • 说明:

      TODO

  • copyOnWrite

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L20
    • 说明:

      TODO

内部类/嵌套类型

构造器

public PatchedDataComponentMap(DataComponentMap prototype) @ L22

  • 构造器名:PatchedDataComponentMap
  • 源码定位:L22
  • 修饰符:public

参数:

  • prototype: DataComponentMap

说明:

TODO

private PatchedDataComponentMap(DataComponentMap prototype, Reference2ObjectMap<DataComponentType<?>,Optional<?>> patch, boolean copyOnWrite) @ L26

  • 构造器名:PatchedDataComponentMap
  • 源码定位:L26
  • 修饰符:private

参数:

  • prototype: DataComponentMap
  • patch: Reference2ObjectMap<DataComponentType,Optional>
  • copyOnWrite: boolean

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static PatchedDataComponentMap fromPatch(DataComponentMap prototype, DataComponentPatch patch) @ L32

  • 方法名:fromPatch
  • 源码定位:L32
  • 返回类型:PatchedDataComponentMap
  • 修饰符:public static

参数:

  • prototype: DataComponentMap
  • patch: DataComponentPatch

说明:

TODO

private static boolean isPatchSanitized(DataComponentMap prototype, Reference2ObjectMap<DataComponentType<?>,Optional<?>> patch) @ L42

  • 方法名:isPatchSanitized
  • 源码定位:L42
  • 返回类型:boolean
  • 修饰符:private static

参数:

  • prototype: DataComponentMap
  • patch: Reference2ObjectMap<DataComponentType,Optional>

说明:

TODO

public <T> T get(DataComponentType<?extends T> type) @ L58

  • 方法名:get
  • 源码定位:L58
  • 返回类型: T
  • 修饰符:public

参数:

  • type: DataComponentType<?extends T>

说明:

TODO

public boolean hasNonDefault(DataComponentType<?> type) @ L63

  • 方法名:hasNonDefault
  • 源码定位:L63
  • 返回类型:boolean
  • 修饰符:public

参数:

  • type: DataComponentType<?>

说明:

TODO

public <T> T set(DataComponentType<T> type, T value) @ L67

  • 方法名:set
  • 源码定位:L67
  • 返回类型: T
  • 修饰符:public

参数:

  • type: DataComponentType
  • value: T

说明:

TODO

public <T> T set(TypedDataComponent<T> value) @ L80

  • 方法名:set
  • 源码定位:L80
  • 返回类型: T
  • 修饰符:public

参数:

  • value: TypedDataComponent

说明:

TODO

public <T> T remove(DataComponentType<?extends T> type) @ L84

  • 方法名:remove
  • 源码定位:L84
  • 返回类型: T
  • 修饰符:public

参数:

  • type: DataComponentType<?extends T>

说明:

TODO

public void applyPatch(DataComponentPatch patch) @ L97

  • 方法名:applyPatch
  • 源码定位:L97
  • 返回类型:void
  • 修饰符:public

参数:

  • patch: DataComponentPatch

说明:

TODO

private void applyPatch(DataComponentType<?> type, Optional<?> value) @ L105

  • 方法名:applyPatch
  • 源码定位:L105
  • 返回类型:void
  • 修饰符:private

参数:

  • type: DataComponentType<?>
  • value: Optional<?>

说明:

TODO

public void restorePatch(DataComponentPatch patch) @ L120

  • 方法名:restorePatch
  • 源码定位:L120
  • 返回类型:void
  • 修饰符:public

参数:

  • patch: DataComponentPatch

说明:

TODO

public void clearPatch() @ L126

  • 方法名:clearPatch
  • 源码定位:L126
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void setAll(DataComponentMap components) @ L131

  • 方法名:setAll
  • 源码定位:L131
  • 返回类型:void
  • 修饰符:public

参数:

  • components: DataComponentMap

说明:

TODO

private void ensureMapOwnership() @ L137

  • 方法名:ensureMapOwnership
  • 源码定位:L137
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public Set<DataComponentType<?>> keySet() @ L144

  • 方法名:keySet
  • 源码定位:L144
  • 返回类型:Set<DataComponentType<?>>
  • 修饰符:public

参数:

说明:

TODO

public Iterator<TypedDataComponent<?>> iterator() @ L166

  • 方法名:iterator
  • 源码定位:L166
  • 返回类型:Iterator<TypedDataComponent<?>>
  • 修饰符:public

参数:

说明:

TODO

public int size() @ L191

  • 方法名:size
  • 源码定位:L191
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public DataComponentPatch asPatch() @ L206

  • 方法名:asPatch
  • 源码定位:L206
  • 返回类型:DataComponentPatch
  • 修饰符:public

参数:

说明:

TODO

public PatchedDataComponentMap copy() @ L215

  • 方法名:copy
  • 源码定位:L215
  • 返回类型:PatchedDataComponentMap
  • 修饰符:public

参数:

说明:

TODO

public DataComponentMap toImmutableMap() @ L220

  • 方法名:toImmutableMap
  • 源码定位:L220
  • 返回类型:DataComponentMap
  • 修饰符:public

参数:

说明:

TODO

public boolean equals(Object obj) @ L224

  • 方法名:equals
  • 源码定位:L224
  • 返回类型:boolean
  • 修饰符:public

参数:

  • obj: Object

说明:

TODO

public int hashCode() @ L231

  • 方法名:hashCode
  • 源码定位:L231
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public String toString() @ L236

  • 方法名:toString
  • 源码定位:L236
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

代码

public final class PatchedDataComponentMap implements DataComponentMap {
    private final DataComponentMap prototype;
    private Reference2ObjectMap<DataComponentType<?>, Optional<?>> patch;
    private boolean copyOnWrite;
 
    public PatchedDataComponentMap(DataComponentMap prototype) {
        this(prototype, Reference2ObjectMaps.emptyMap(), true);
    }
 
    private PatchedDataComponentMap(DataComponentMap prototype, Reference2ObjectMap<DataComponentType<?>, Optional<?>> patch, boolean copyOnWrite) {
        this.prototype = prototype;
        this.patch = patch;
        this.copyOnWrite = copyOnWrite;
    }
 
    public static PatchedDataComponentMap fromPatch(DataComponentMap prototype, DataComponentPatch patch) {
        if (isPatchSanitized(prototype, patch.map)) {
            return new PatchedDataComponentMap(prototype, patch.map, true);
        } else {
            PatchedDataComponentMap map = new PatchedDataComponentMap(prototype);
            map.applyPatch(patch);
            return map;
        }
    }
 
    private static boolean isPatchSanitized(DataComponentMap prototype, Reference2ObjectMap<DataComponentType<?>, Optional<?>> patch) {
        for (Entry<DataComponentType<?>, Optional<?>> entry : Reference2ObjectMaps.fastIterable(patch)) {
            Object defaultValue = prototype.get(entry.getKey());
            Optional<?> value = entry.getValue();
            if (value.isPresent() && value.get().equals(defaultValue)) {
                return false;
            }
 
            if (value.isEmpty() && defaultValue == null) {
                return false;
            }
        }
 
        return true;
    }
 
    @Override
    public <T> @Nullable T get(DataComponentType<? extends T> type) {
        return DataComponentPatch.getFromPatchAndPrototype(this.patch, this.prototype, type);
    }
 
    public boolean hasNonDefault(DataComponentType<?> type) {
        return this.patch.containsKey(type);
    }
 
    public <T> @Nullable T set(DataComponentType<T> type, @Nullable T value) {
        this.ensureMapOwnership();
        T defaultValue = this.prototype.get(type);
        Optional<T> lastValue;
        if (Objects.equals(value, defaultValue)) {
            lastValue = (Optional<T>)this.patch.remove(type);
        } else {
            lastValue = (Optional<T>)this.patch.put(type, Optional.ofNullable(value));
        }
 
        return lastValue != null ? lastValue.orElse(defaultValue) : defaultValue;
    }
 
    public <T> @Nullable T set(TypedDataComponent<T> value) {
        return this.set(value.type(), value.value());
    }
 
    public <T> @Nullable T remove(DataComponentType<? extends T> type) {
        this.ensureMapOwnership();
        T defaultValue = this.prototype.get(type);
        Optional<? extends T> lastValue;
        if (defaultValue != null) {
            lastValue = (Optional<? extends T>)this.patch.put(type, Optional.empty());
        } else {
            lastValue = (Optional<? extends T>)this.patch.remove(type);
        }
 
        return (T)(lastValue != null ? lastValue.orElse(null) : defaultValue);
    }
 
    public void applyPatch(DataComponentPatch patch) {
        this.ensureMapOwnership();
 
        for (Entry<DataComponentType<?>, Optional<?>> entry : Reference2ObjectMaps.fastIterable(patch.map)) {
            this.applyPatch(entry.getKey(), entry.getValue());
        }
    }
 
    private void applyPatch(DataComponentType<?> type, Optional<?> value) {
        Object defaultValue = this.prototype.get(type);
        if (value.isPresent()) {
            if (value.get().equals(defaultValue)) {
                this.patch.remove(type);
            } else {
                this.patch.put(type, value);
            }
        } else if (defaultValue != null) {
            this.patch.put(type, Optional.empty());
        } else {
            this.patch.remove(type);
        }
    }
 
    public void restorePatch(DataComponentPatch patch) {
        this.ensureMapOwnership();
        this.patch.clear();
        this.patch.putAll(patch.map);
    }
 
    public void clearPatch() {
        this.ensureMapOwnership();
        this.patch.clear();
    }
 
    public void setAll(DataComponentMap components) {
        for (TypedDataComponent<?> entry : components) {
            entry.applyTo(this);
        }
    }
 
    private void ensureMapOwnership() {
        if (this.copyOnWrite) {
            this.patch = new Reference2ObjectArrayMap<>(this.patch);
            this.copyOnWrite = false;
        }
    }
 
    @Override
    public Set<DataComponentType<?>> keySet() {
        if (this.patch.isEmpty()) {
            return this.prototype.keySet();
        } else {
            Set<DataComponentType<?>> components = new ReferenceArraySet<>(this.prototype.keySet());
 
            for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Optional<?>> entry : Reference2ObjectMaps.fastIterable(
                this.patch
            )) {
                Optional<?> value = entry.getValue();
                if (value.isPresent()) {
                    components.add(entry.getKey());
                } else {
                    components.remove(entry.getKey());
                }
            }
 
            return components;
        }
    }
 
    @Override
    public Iterator<TypedDataComponent<?>> iterator() {
        if (this.patch.isEmpty()) {
            return this.prototype.iterator();
        } else {
            List<TypedDataComponent<?>> components = new ArrayList<>(this.patch.size() + this.prototype.size());
 
            for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Optional<?>> entry : Reference2ObjectMaps.fastIterable(
                this.patch
            )) {
                if (entry.getValue().isPresent()) {
                    components.add(TypedDataComponent.createUnchecked(entry.getKey(), entry.getValue().get()));
                }
            }
 
            for (TypedDataComponent<?> component : this.prototype) {
                if (!this.patch.containsKey(component.type())) {
                    components.add(component);
                }
            }
 
            return components.iterator();
        }
    }
 
    @Override
    public int size() {
        int size = this.prototype.size();
 
        for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Optional<?>> entry : Reference2ObjectMaps.fastIterable(this.patch)) {
            boolean inPatch = entry.getValue().isPresent();
            boolean inPrototype = this.prototype.has(entry.getKey());
            if (inPatch != inPrototype) {
                size += inPatch ? 1 : -1;
            }
        }
 
        return size;
    }
 
    public DataComponentPatch asPatch() {
        if (this.patch.isEmpty()) {
            return DataComponentPatch.EMPTY;
        } else {
            this.copyOnWrite = true;
            return new DataComponentPatch(this.patch);
        }
    }
 
    public PatchedDataComponentMap copy() {
        this.copyOnWrite = true;
        return new PatchedDataComponentMap(this.prototype, this.patch, true);
    }
 
    public DataComponentMap toImmutableMap() {
        return (DataComponentMap)(this.patch.isEmpty() ? this.prototype : this.copy());
    }
 
    @Override
    public boolean equals(Object obj) {
        return this == obj
            ? true
            : obj instanceof PatchedDataComponentMap otherMap && this.prototype.equals(otherMap.prototype) && this.patch.equals(otherMap.patch);
    }
 
    @Override
    public int hashCode() {
        return this.prototype.hashCode() + this.patch.hashCode() * 31;
    }
 
    @Override
    public String toString() {
        return "{" + this.stream().map(TypedDataComponent::toString).collect(Collectors.joining(", ")) + "}";
    }
}

引用的其他类

  • DataComponentMap

    • 引用位置: 参数/字段/实现/返回值
  • DataComponentPatch

    • 引用位置: 参数/方法调用/构造调用/返回值
    • 关联成员: DataComponentPatch(), DataComponentPatch.getFromPatchAndPrototype()
  • DataComponentType

    • 引用位置: 参数/字段/返回值
  • TypedDataComponent

    • 引用位置: 参数/方法调用/返回值
    • 关联成员: TypedDataComponent.createUnchecked()