| 1 | package net.minecraft.core.component;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMultimap;
|
| 4 | import com.google.common.collect.Multimap;
|
| 5 | import com.google.common.collect.ImmutableMultimap.Builder;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Map.Entry;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class DataComponentLookup<T> {
|
| 16 | private final Iterable<? extends Holder<T>> elements;
|
| 17 | private volatile Map<DataComponentType<?>, DataComponentLookup.ComponentStorage<?, T>> cache = Map.of();
|
| 18 |
|
| 19 | public DataComponentLookup(final Iterable<? extends Holder<T>> elements) {
|
| 20 | this.elements = elements;
|
| 21 | }
|
| 22 |
|
| 23 | private <C> DataComponentLookup.@Nullable ComponentStorage<C, T> getFromCache(final DataComponentType<C> type) {
|
| 24 | return (DataComponentLookup.ComponentStorage<C, T>)this.cache.get(type);
|
| 25 | }
|
| 26 |
|
| 27 | private <C> DataComponentLookup.ComponentStorage<C, T> getOrCreateStorage(final DataComponentType<C> type) {
|
| 28 | DataComponentLookup.ComponentStorage<C, T> existingStorage = this.getFromCache(type);
|
| 29 | if (existingStorage != null) {
|
| 30 | return existingStorage;
|
| 31 | }
|
| 32 |
|
| 33 | DataComponentLookup.ComponentStorage<C, T> newStorage = this.scanForComponents(type);
|
| 34 | synchronized (this) {
|
| 35 | DataComponentLookup.ComponentStorage<C, T> foreignStorage = this.getFromCache(type);
|
| 36 | if (foreignStorage != null) {
|
| 37 | return foreignStorage;
|
| 38 | }
|
| 39 |
|
| 40 | this.cache = Util.copyAndPut(this.cache, type, newStorage);
|
| 41 | return newStorage;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | private <C> DataComponentLookup.ComponentStorage<C, T> scanForComponents(final DataComponentType<C> type) {
|
| 46 | Builder<C, Holder<T>> results = ImmutableMultimap.builder();
|
| 47 |
|
| 48 | for (Holder<T> element : this.elements) {
|
| 49 | C componentValue = element.components().get(type);
|
| 50 | if (componentValue != null) {
|
| 51 | results.put(componentValue, element);
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | return new DataComponentLookup.ComponentStorage<>(results.build());
|
| 56 | }
|
| 57 |
|
| 58 | public <C> Stream<Holder<T>> findMatching(final DataComponentType<C> type, final Predicate<C> predicate) {
|
| 59 | return this.getOrCreateStorage(type).findMatching(predicate);
|
| 60 | }
|
| 61 |
|
| 62 | public <C> Collection<Holder<T>> findAll(final DataComponentType<C> type, final C value) {
|
| 63 | return this.getOrCreateStorage(type).findAll(value);
|
| 64 | }
|
| 65 |
|
| 66 | public <C> Collection<Holder<T>> findAll(final DataComponentType<C> type) {
|
| 67 | return this.getOrCreateStorage(type).valueToComponent.values();
|
| 68 | }
|
| 69 |
|
| 70 | private record ComponentStorage<C, T>(Multimap<C, Holder<T>> valueToComponent) {
|
| 71 | public Collection<Holder<T>> findAll(final C value) {
|
| 72 | return this.valueToComponent.get(value);
|
| 73 | }
|
| 74 |
|
| 75 | public Stream<Holder<T>> findMatching(final Predicate<C> predicate) {
|
| 76 | return this.valueToComponent.isEmpty()
|
| 77 | ? Stream.empty()
|
| 78 | : this.valueToComponent.entries().stream().filter(e -> predicate.test(e.getKey())).map(Entry::getValue);
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|