HashedPatchMap .java

net.minecraft.network.HashedPatchMap (record, 61 lines): members: addedComponents, removedComponents, HashGenerator, matches, create, STREAM_CODEC.

net.minecraft.networkrecord61 linescommon2.2 KB

API Surface (3)

record HashedPatchMap(Map<DataComponentType<?>, Integer> addedComponents, Set<DataComponentType<?>> removedComponents)
HashedPatchMap create(final DataComponentPatch patch, final HashedPatchMap.HashGenerator hasher)
boolean matches(final DataComponentPatch patch, final HashedPatchMap.HashGenerator hasher)

Fields & Constants (1)

STREAM_CODEC

Full Source

HashedPatchMap.java61 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network;
2
3
import java.util.HashMap;
4
import java.util.HashSet;
5
import java.util.IdentityHashMap;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.function.Function;
9
import net.minecraft.core.component.DataComponentPatch;
10
import net.minecraft.core.component.DataComponentType;
11
import net.minecraft.core.component.TypedDataComponent;
12
import net.minecraft.core.registries.Registries;
13
import net.minecraft.network.codec.ByteBufCodecs;
14
import net.minecraft.network.codec.StreamCodec;
15
16
public record HashedPatchMap(Map<DataComponentType<?>, Integer> addedComponents, Set<DataComponentType<?>> removedComponents) {
17
	public static final StreamCodec<RegistryFriendlyByteBuf, HashedPatchMap> STREAM_CODEC = StreamCodec.composite(
18
		ByteBufCodecs.map(HashMap::new, ByteBufCodecs.registry(Registries.DATA_COMPONENT_TYPE), ByteBufCodecs.INT, 256),
19
		HashedPatchMap::addedComponents,
20
		ByteBufCodecs.collection(HashSet::new, ByteBufCodecs.registry(Registries.DATA_COMPONENT_TYPE), 256),
21
		HashedPatchMap::removedComponents,
22
		HashedPatchMap::new
23
	);
24
25
	public static HashedPatchMap create(final DataComponentPatch patch, final HashedPatchMap.HashGenerator hasher) {
26
		DataComponentPatch.SplitResult split = patch.split();
27
		Map<DataComponentType<?>, Integer> setComponentHashes = new IdentityHashMap<>(split.added().size());
28
		split.added().forEach(e -> setComponentHashes.put(e.type(), hasher.apply((TypedDataComponent<?>)e)));
29
		return new HashedPatchMap(setComponentHashes, split.removed());
30
	}
31
32
	public boolean matches(final DataComponentPatch patch, final HashedPatchMap.HashGenerator hasher) {
33
		DataComponentPatch.SplitResult split = patch.split();
34
		if (!split.removed().equals(this.removedComponents)) {
35
			return false;
36
		}
37
38
		if (this.addedComponents.size() != split.added().size()) {
39
			return false;
40
		}
41
42
		for (TypedDataComponent<?> typedDataComponent : split.added()) {
43
			Integer expectedHash = this.addedComponents.get(typedDataComponent.type());
44
			if (expectedHash == null) {
45
				return false;
46
			}
47
48
			Integer actualHash = hasher.apply(typedDataComponent);
49
			if (!actualHash.equals(expectedHash)) {
50
				return false;
51
			}
52
		}
53
54
		return true;
55
	}
56
57
	@FunctionalInterface
58
	public interface HashGenerator extends Function<TypedDataComponent<?>, Integer> {
59
	}
60
}
61

Decompiled Minecraft 26.3 source of HashedPatchMap (net/minecraft/network/HashedPatchMap.java) with Mojang official mappings, stored verbatim on the site.