| 1 | package net.minecraft.core.component;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap;
|
| 4 | import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
| 5 | import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
|
| 6 | import it.unimi.dsi.fastutil.objects.ReferenceArraySet;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.Map.Entry;
|
| 13 | import java.util.stream.Collectors;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public final class PatchedDataComponentMap implements DataComponentMap {
|
| 17 | private final DataComponentMap prototype;
|
| 18 | private Reference2ObjectMap<DataComponentType<?>, Object> patch;
|
| 19 | private boolean copyOnWrite;
|
| 20 |
|
| 21 | public PatchedDataComponentMap(final DataComponentMap prototype) {
|
| 22 | this(prototype, Reference2ObjectMaps.emptyMap(), true);
|
| 23 | }
|
| 24 |
|
| 25 | private PatchedDataComponentMap(final DataComponentMap prototype, final Reference2ObjectMap<DataComponentType<?>, Object> patch, final boolean copyOnWrite) {
|
| 26 | this.prototype = prototype;
|
| 27 | this.patch = patch;
|
| 28 | this.copyOnWrite = copyOnWrite;
|
| 29 | }
|
| 30 |
|
| 31 | public static PatchedDataComponentMap fromPatch(final DataComponentMap prototype, final DataComponentPatch patch) {
|
| 32 | if (isPatchSanitized(prototype, patch.map)) {
|
| 33 | return new PatchedDataComponentMap(prototype, patch.map, true);
|
| 34 | }
|
| 35 |
|
| 36 | PatchedDataComponentMap map = new PatchedDataComponentMap(prototype);
|
| 37 | map.applyPatch(patch);
|
| 38 | return map;
|
| 39 | }
|
| 40 |
|
| 41 | private static boolean isPatchSanitized(final DataComponentMap prototype, final Reference2ObjectMap<DataComponentType<?>, Object> patch) {
|
| 42 | for (Entry<DataComponentType<?>, Object> entry : Reference2ObjectMaps.fastIterable(patch)) {
|
| 43 | Object defaultValue = prototype.get(entry.getKey());
|
| 44 | Object value = entry.getValue();
|
| 45 | if (value.equals(defaultValue)) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | if (Removed.isRemoved(value) && defaultValue == null) {
|
| 50 | return false;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | return true;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 59 | return DataComponentPatch.getFromPatchAndPrototype(this.patch, this.prototype, type);
|
| 60 | }
|
| 61 |
|
| 62 | public boolean hasNonDefault(final DataComponentType<?> type) {
|
| 63 | return this.patch.containsKey(type);
|
| 64 | }
|
| 65 |
|
| 66 | public <T> @Nullable T set(final DataComponentType<T> type, final @Nullable T value) {
|
| 67 | this.ensureMapOwnership();
|
| 68 | T defaultValue = this.prototype.get(type);
|
| 69 | Object lastValue;
|
| 70 | if (Objects.equals(value, defaultValue)) {
|
| 71 | lastValue = this.patch.remove(type);
|
| 72 | } else {
|
| 73 | lastValue = this.patch.put(type, Removed.nullToRemoved(value));
|
| 74 | }
|
| 75 |
|
| 76 | return lastValue != null ? Objects.requireNonNullElse((T)Removed.removedToNull(lastValue), defaultValue) : defaultValue;
|
| 77 | }
|
| 78 |
|
| 79 | public <T> @Nullable T set(final TypedDataComponent<T> value) {
|
| 80 | return this.set(value.type(), value.value());
|
| 81 | }
|
| 82 |
|
| 83 | public <T> T remove(final DataComponentType<? extends T> type) {
|
| 84 | this.ensureMapOwnership();
|
| 85 | T defaultValue = this.prototype.get(type);
|
| 86 | Object lastValue;
|
| 87 | if (defaultValue != null) {
|
| 88 | lastValue = this.patch.put(type, Removed.INSTANCE);
|
| 89 | } else {
|
| 90 | lastValue = this.patch.remove(type);
|
| 91 | }
|
| 92 |
|
| 93 | return (T)(lastValue != null ? Removed.removedToNull(lastValue) : defaultValue);
|
| 94 | }
|
| 95 |
|
| 96 | public void applyPatch(final DataComponentPatch patch) {
|
| 97 | this.ensureMapOwnership();
|
| 98 |
|
| 99 | for (Entry<DataComponentType<?>, Object> entry : Reference2ObjectMaps.fastIterable(patch.map)) {
|
| 100 | this.applyPatch(entry.getKey(), entry.getValue());
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | private void applyPatch(final DataComponentType<?> type, final Object value) {
|
| 105 | Object defaultValue = this.prototype.get(type);
|
| 106 | if (Removed.isNotRemoved(value)) {
|
| 107 | if (value.equals(defaultValue)) {
|
| 108 | this.patch.remove(type);
|
| 109 | } else {
|
| 110 | this.patch.put(type, value);
|
| 111 | }
|
| 112 | } else if (defaultValue != null) {
|
| 113 | this.patch.put(type, Removed.INSTANCE);
|
| 114 | } else {
|
| 115 | this.patch.remove(type);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | public void restorePatch(final DataComponentPatch patch) {
|
| 120 | this.ensureMapOwnership();
|
| 121 | this.patch.clear();
|
| 122 | this.patch.putAll(patch.map);
|
| 123 | }
|
| 124 |
|
| 125 | public void clearPatch() {
|
| 126 | this.ensureMapOwnership();
|
| 127 | this.patch.clear();
|
| 128 | }
|
| 129 |
|
| 130 | public void setAll(final DataComponentMap components) {
|
| 131 | for (TypedDataComponent<?> entry : components) {
|
| 132 | entry.applyTo(this);
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | private void ensureMapOwnership() {
|
| 137 | if (this.copyOnWrite) {
|
| 138 | this.patch = new Reference2ObjectArrayMap<>(this.patch);
|
| 139 | this.copyOnWrite = false;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public Set<DataComponentType<?>> keySet() {
|
| 145 | if (this.patch.isEmpty()) {
|
| 146 | return this.prototype.keySet();
|
| 147 | }
|
| 148 |
|
| 149 | Set<DataComponentType<?>> components = new ReferenceArraySet<>(this.prototype.keySet());
|
| 150 |
|
| 151 | for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Object> entry : Reference2ObjectMaps.fastIterable(this.patch)) {
|
| 152 | Object value = entry.getValue();
|
| 153 | if (Removed.isNotRemoved(value)) {
|
| 154 | components.add(entry.getKey());
|
| 155 | } else {
|
| 156 | components.remove(entry.getKey());
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | return components;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public Iterator<TypedDataComponent<?>> iterator() {
|
| 165 | if (this.patch.isEmpty()) {
|
| 166 | return this.prototype.iterator();
|
| 167 | }
|
| 168 |
|
| 169 | List<TypedDataComponent<?>> components = new ArrayList<>(this.patch.size() + this.prototype.size());
|
| 170 |
|
| 171 | for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Object> entry : Reference2ObjectMaps.fastIterable(this.patch)) {
|
| 172 | Object value = entry.getValue();
|
| 173 | if (Removed.isNotRemoved(value)) {
|
| 174 | components.add(TypedDataComponent.createUnchecked(entry.getKey(), value));
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | for (TypedDataComponent<?> component : this.prototype) {
|
| 179 | if (!this.patch.containsKey(component.type())) {
|
| 180 | components.add(component);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | return components.iterator();
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public int size() {
|
| 189 | int size = this.prototype.size();
|
| 190 |
|
| 191 | for (it.unimi.dsi.fastutil.objects.Reference2ObjectMap.Entry<DataComponentType<?>, Object> entry : Reference2ObjectMaps.fastIterable(this.patch)) {
|
| 192 | boolean inPatch = Removed.isNotRemoved(entry.getValue());
|
| 193 | boolean inPrototype = this.prototype.has(entry.getKey());
|
| 194 | if (inPatch != inPrototype) {
|
| 195 | size += inPatch ? 1 : -1;
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | return size;
|
| 200 | }
|
| 201 |
|
| 202 | public DataComponentPatch asPatch() {
|
| 203 | if (this.patch.isEmpty()) {
|
| 204 | return DataComponentPatch.EMPTY;
|
| 205 | }
|
| 206 |
|
| 207 | this.copyOnWrite = true;
|
| 208 | return new DataComponentPatch(this.patch);
|
| 209 | }
|
| 210 |
|
| 211 | public PatchedDataComponentMap copy() {
|
| 212 | this.copyOnWrite = true;
|
| 213 | return new PatchedDataComponentMap(this.prototype, this.patch, true);
|
| 214 | }
|
| 215 |
|
| 216 | public DataComponentMap toImmutableMap() {
|
| 217 | return this.patch.isEmpty() ? this.prototype : this.copy();
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public boolean equals(final Object obj) {
|
| 222 | return this == obj ? true : obj instanceof PatchedDataComponentMap otherMap && this.prototype.equals(otherMap.prototype) && this.patch.equals(otherMap.patch);
|
| 223 | }
|
| 224 |
|
| 225 | @Override
|
| 226 | public int hashCode() {
|
| 227 | return this.prototype.hashCode() + this.patch.hashCode() * 31;
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public String toString() {
|
| 232 | return "{" + this.stream().map(TypedDataComponent::toString).collect(Collectors.joining(", ")) + "}";
|
| 233 | }
|
| 234 | }
|
| 235 |
|