| 1 | package net.minecraft.core.component;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import it.unimi.dsi.fastutil.objects.ObjectIterable;
|
| 7 | import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap;
|
| 8 | import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
| 9 | import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.Map.Entry;
|
| 12 | import java.util.function.Predicate;
|
| 13 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 14 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 15 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 16 | import net.minecraft.network.codec.StreamCodec;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public final class DataComponentPatch {
|
| 21 | public static final DataComponentPatch EMPTY = new DataComponentPatch(Reference2ObjectMaps.emptyMap());
|
| 22 | public static final Codec<DataComponentPatch> CODEC = Codec.dispatchedMap(DataComponentPatch.PatchKey.CODEC, DataComponentPatch.PatchKey::valueCodec)
|
| 23 | .xmap(data -> {
|
| 24 | if (data.isEmpty()) {
|
| 25 | return EMPTY;
|
| 26 | }
|
| 27 |
|
| 28 | Reference2ObjectMap<DataComponentType<?>, Object> map = new Reference2ObjectArrayMap<>(data.size());
|
| 29 |
|
| 30 | for (Entry<DataComponentPatch.PatchKey, ?> entry : data.entrySet()) {
|
| 31 | map.put(entry.getKey().type(), entry.getValue());
|
| 32 | }
|
| 33 |
|
| 34 | return new DataComponentPatch(map);
|
| 35 | }, patch -> {
|
| 36 | Reference2ObjectMap<DataComponentPatch.PatchKey, Object> map = new Reference2ObjectArrayMap<>(patch.map.size());
|
| 37 |
|
| 38 | for (Entry<DataComponentType<?>, Object> entry : Reference2ObjectMaps.fastIterable(patch.map)) {
|
| 39 | DataComponentType<?> type = entry.getKey();
|
| 40 | if (!type.isTransient()) {
|
| 41 | Object value = entry.getValue();
|
| 42 | map.put(new DataComponentPatch.PatchKey(type, Removed.isRemoved(value)), value);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | return map;
|
| 47 | });
|
| 48 | public static final StreamCodec<RegistryFriendlyByteBuf, DataComponentPatch> STREAM_CODEC = createStreamCodec(new DataComponentPatch.CodecGetter() {
|
| 49 | @Override
|
| 50 | public <T> StreamCodec<RegistryFriendlyByteBuf, T> apply(final DataComponentType<T> type) {
|
| 51 | return type.streamCodec().cast();
|
| 52 | }
|
| 53 | });
|
| 54 | public static final StreamCodec<RegistryFriendlyByteBuf, DataComponentPatch> DELIMITED_STREAM_CODEC = createStreamCodec(new DataComponentPatch.CodecGetter() {
|
| 55 | @Override
|
| 56 | public <T> StreamCodec<RegistryFriendlyByteBuf, T> apply(final DataComponentType<T> type) {
|
| 57 | StreamCodec<RegistryFriendlyByteBuf, T> original = type.streamCodec().cast();
|
| 58 | return original.apply(ByteBufCodecs.registryFriendlyLengthPrefixed(Integer.MAX_VALUE));
|
| 59 | }
|
| 60 | });
|
| 61 | private static final String REMOVED_PREFIX = "!";
|
| 62 | final Reference2ObjectMap<DataComponentType<?>, Object> map;
|
| 63 |
|
| 64 | private static StreamCodec<RegistryFriendlyByteBuf, DataComponentPatch> createStreamCodec(final DataComponentPatch.CodecGetter codecGetter) {
|
| 65 | return new StreamCodec<RegistryFriendlyByteBuf, DataComponentPatch>() {
|
| 66 | public DataComponentPatch decode(final RegistryFriendlyByteBuf input) {
|
| 67 | int positiveCount = input.readVarInt();
|
| 68 | int negativeCount = input.readVarInt();
|
| 69 | if (positiveCount == 0 && negativeCount == 0) {
|
| 70 | return DataComponentPatch.EMPTY;
|
| 71 | }
|
| 72 |
|
| 73 | int expectedSize = positiveCount + negativeCount;
|
| 74 | Reference2ObjectMap<DataComponentType<?>, Object> map = new Reference2ObjectArrayMap<>(Math.min(expectedSize, 65536));
|
| 75 |
|
| 76 | for (int i = 0; i < positiveCount; i++) {
|
| 77 | DataComponentType<?> type = DataComponentType.STREAM_CODEC.decode(input);
|
| 78 | Object value = codecGetter.apply(type).decode(input);
|
| 79 | map.put(type, value);
|
| 80 | }
|
| 81 |
|
| 82 | for (int i = 0; i < negativeCount; i++) {
|
| 83 | DataComponentType<?> type = DataComponentType.STREAM_CODEC.decode(input);
|
| 84 | map.put(type, Removed.INSTANCE);
|
| 85 | }
|
| 86 |
|
| 87 | return new DataComponentPatch(map);
|
| 88 | }
|
| 89 |
|
| 90 | public void encode(final RegistryFriendlyByteBuf output, final DataComponentPatch patch) {
|
| 91 | if (patch.isEmpty()) {
|
| 92 | output.writeVarInt(0);
|
| 93 | output.writeVarInt(0);
|
| 94 | } else {
|
| 95 | ObjectIterable<it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Object>> fastEntries = Reference2ObjectMaps.fastIterable(
|
| 96 | patch.map
|
| 97 | );
|
| 98 | int positiveCount = 0;
|
| 99 | int negativeCount = 0;
|
| 100 |
|
| 101 | for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Object> entry : fastEntries) {
|
| 102 | if (Removed.isNotRemoved(entry.getValue())) {
|
| 103 | positiveCount++;
|
| 104 | } else {
|
| 105 | negativeCount++;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | output.writeVarInt(positiveCount);
|
| 110 | output.writeVarInt(negativeCount);
|
| 111 |
|
| 112 | for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Object> entry : fastEntries) {
|
| 113 | Object value = entry.getValue();
|
| 114 | if (Removed.isNotRemoved(value)) {
|
| 115 | DataComponentType<?> type = entry.getKey();
|
| 116 | DataComponentType.STREAM_CODEC.encode(output, type);
|
| 117 | this.encodeComponent(output, type, value);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Object> entry : fastEntries) {
|
| 122 | if (Removed.isRemoved(entry.getValue())) {
|
| 123 | DataComponentType<?> type = entry.getKey();
|
| 124 | DataComponentType.STREAM_CODEC.encode(output, type);
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | private <T> void encodeComponent(final RegistryFriendlyByteBuf output, final DataComponentType<T> type, final Object value) {
|
| 131 | codecGetter.apply(type).encode(output, (T)value);
|
| 132 | }
|
| 133 | };
|
| 134 | }
|
| 135 |
|
| 136 | DataComponentPatch(final Reference2ObjectMap<DataComponentType<?>, Object> map) {
|
| 137 | this.map = map;
|
| 138 | }
|
| 139 |
|
| 140 | public static DataComponentPatch.Builder builder() {
|
| 141 | return new DataComponentPatch.Builder();
|
| 142 | }
|
| 143 |
|
| 144 | public <T> @Nullable T get(final DataComponentGetter prototype, final DataComponentType<? extends T> type) {
|
| 145 | return getFromPatchAndPrototype(this.map, prototype, type);
|
| 146 | }
|
| 147 |
|
| 148 | static <T> T getFromPatchAndPrototype(
|
| 149 | final Reference2ObjectMap<DataComponentType<?>, Object> patch, final DataComponentGetter prototype, final DataComponentType<? extends T> type
|
| 150 | ) {
|
| 151 | Object value = patch.get(type);
|
| 152 | return (T)(value != null ? Removed.removedToNull(value) : prototype.get(type));
|
| 153 | }
|
| 154 |
|
| 155 | public int size() {
|
| 156 | return this.map.size();
|
| 157 | }
|
| 158 |
|
| 159 | public DataComponentPatch forget(final Predicate<DataComponentType<?>> test) {
|
| 160 | if (this.isEmpty()) {
|
| 161 | return EMPTY;
|
| 162 | }
|
| 163 |
|
| 164 | Reference2ObjectMap<DataComponentType<?>, Object> newMap = new Reference2ObjectArrayMap<>(this.map);
|
| 165 | newMap.keySet().removeIf(test);
|
| 166 | return newMap.isEmpty() ? EMPTY : new DataComponentPatch(newMap);
|
| 167 | }
|
| 168 |
|
| 169 | public boolean isEmpty() {
|
| 170 | return this.map.isEmpty();
|
| 171 | }
|
| 172 |
|
| 173 | public DataComponentPatch.SplitResult split() {
|
| 174 | if (this.isEmpty()) {
|
| 175 | return DataComponentPatch.SplitResult.EMPTY;
|
| 176 | }
|
| 177 |
|
| 178 | DataComponentMap.Builder added = DataComponentMap.builder();
|
| 179 | Set<DataComponentType<?>> removed = Sets.newIdentityHashSet();
|
| 180 | this.map.forEach((type, value) -> {
|
| 181 | if (Removed.isNotRemoved(value)) {
|
| 182 | added.setUnchecked((DataComponentType<?>)type, value);
|
| 183 | } else {
|
| 184 | removed.add((DataComponentType<?>)type);
|
| 185 | }
|
| 186 | });
|
| 187 | return new DataComponentPatch.SplitResult(added.build(), removed);
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public boolean equals(final Object obj) {
|
| 192 | return this == obj ? true : obj instanceof DataComponentPatch patch && this.map.equals(patch.map);
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public int hashCode() {
|
| 197 | return this.map.hashCode();
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public String toString() {
|
| 202 | return toString(this.map);
|
| 203 | }
|
| 204 |
|
| 205 | static String toString(final Reference2ObjectMap<DataComponentType<?>, Object> map) {
|
| 206 | StringBuilder builder = new StringBuilder();
|
| 207 | builder.append('{');
|
| 208 | boolean first = true;
|
| 209 |
|
| 210 | for (Entry<DataComponentType<?>, Object> entry : Reference2ObjectMaps.fastIterable(map)) {
|
| 211 | if (first) {
|
| 212 | first = false;
|
| 213 | } else {
|
| 214 | builder.append(", ");
|
| 215 | }
|
| 216 |
|
| 217 | Object value = entry.getValue();
|
| 218 | if (Removed.isNotRemoved(value)) {
|
| 219 | builder.append(entry.getKey());
|
| 220 | builder.append("=>");
|
| 221 | builder.append(value);
|
| 222 | } else {
|
| 223 | builder.append("!");
|
| 224 | builder.append(entry.getKey());
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | builder.append('}');
|
| 229 | return builder.toString();
|
| 230 | }
|
| 231 |
|
| 232 | public static class Builder {
|
| 233 | private final Reference2ObjectMap<DataComponentType<?>, Object> map = new Reference2ObjectArrayMap<>();
|
| 234 |
|
| 235 | private Builder() {
|
| 236 | }
|
| 237 |
|
| 238 | public <T> DataComponentPatch.Builder set(final DataComponentType<T> type, final T value) {
|
| 239 | this.map.put(type, value);
|
| 240 | return this;
|
| 241 | }
|
| 242 |
|
| 243 | public <T> DataComponentPatch.Builder remove(final DataComponentType<T> type) {
|
| 244 | this.map.put(type, Removed.INSTANCE);
|
| 245 | return this;
|
| 246 | }
|
| 247 |
|
| 248 | public <T> DataComponentPatch.Builder set(final TypedDataComponent<T> component) {
|
| 249 | return this.set(component.type(), component.value());
|
| 250 | }
|
| 251 |
|
| 252 | public <T> DataComponentPatch.Builder set(final Iterable<TypedDataComponent<?>> components) {
|
| 253 | for (TypedDataComponent<?> component : components) {
|
| 254 | this.set(component);
|
| 255 | }
|
| 256 |
|
| 257 | return this;
|
| 258 | }
|
| 259 |
|
| 260 | public DataComponentPatch build() {
|
| 261 | return this.map.isEmpty() ? DataComponentPatch.EMPTY : new DataComponentPatch(this.map);
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | @FunctionalInterface
|
| 266 | private interface CodecGetter {
|
| 267 | <T> StreamCodec<? super RegistryFriendlyByteBuf, T> apply(DataComponentType<T> type);
|
| 268 | }
|
| 269 |
|
| 270 | private record PatchKey(DataComponentType<?> type, boolean removed) {
|
| 271 | public static final Codec<DataComponentPatch.PatchKey> CODEC = Codec.STRING
|
| 272 | .flatXmap(
|
| 273 | string -> {
|
| 274 | boolean removed = string.startsWith("!");
|
| 275 | if (removed) {
|
| 276 | string = string.substring("!".length());
|
| 277 | }
|
| 278 |
|
| 279 | Identifier id = Identifier.tryParse(string);
|
| 280 | DataComponentType<?> type = BuiltInRegistries.DATA_COMPONENT_TYPE.getValue(id);
|
| 281 | if (type == null) {
|
| 282 | return DataResult.error(() -> "No component with type: '" + id + "'");
|
| 283 | } else {
|
| 284 | return type.isTransient()
|
| 285 | ? DataResult.error(() -> "'" + id + "' is not a persistent component")
|
| 286 | : DataResult.success(new DataComponentPatch.PatchKey(type, removed));
|
| 287 | }
|
| 288 | },
|
| 289 | key -> {
|
| 290 | DataComponentType<?> type = key.type();
|
| 291 | Identifier id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(type);
|
| 292 | return id == null ? DataResult.error(() -> "Unregistered component: " + type) : DataResult.success(key.removed() ? "!" + id : id.toString());
|
| 293 | }
|
| 294 | );
|
| 295 |
|
| 296 | public Codec<?> valueCodec() {
|
| 297 | return this.removed ? Removed.CODEC : this.type.codecOrThrow();
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | public record SplitResult(DataComponentMap added, Set<DataComponentType<?>> removed) {
|
| 302 | public static final DataComponentPatch.SplitResult EMPTY = new DataComponentPatch.SplitResult(DataComponentMap.EMPTY, Set.of());
|
| 303 | }
|
| 304 | }
|
| 305 |
|