| 1 | package net.minecraft.world.entity.ai.memory;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Iterators;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import java.util.Iterator;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.stream.Collectors;
|
| 10 | import java.util.stream.Stream;
|
| 11 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public final class MemoryMap implements Iterable<MemoryMap.Value<?>> {
|
| 15 | private static final Codec<MemoryModuleType<?>> SERIALIZABLE_MEMORY_MODULE_CODEC = BuiltInRegistries.MEMORY_MODULE_TYPE
|
| 16 | .byNameCodec()
|
| 17 | .validate(type -> type.canSerialize() ? DataResult.success(type) : DataResult.error(() -> "Memory module " + type + " cannot be encoded"));
|
| 18 | public static final Codec<MemoryMap> CODEC = Codec.<MemoryModuleType<?>, ExpirableValue<?>>dispatchedMap(
|
| 19 | SERIALIZABLE_MEMORY_MODULE_CODEC, type -> type.getCodec().orElseThrow()
|
| 20 | )
|
| 21 | .xmap(MemoryMap::new, m -> m.memories);
|
| 22 | public static final MemoryMap EMPTY = new MemoryMap(Map.of());
|
| 23 | private final Map<MemoryModuleType<?>, ExpirableValue<?>> memories;
|
| 24 |
|
| 25 | private MemoryMap(final Map<MemoryModuleType<?>, ExpirableValue<?>> memories) {
|
| 26 | this.memories = Map.copyOf(memories);
|
| 27 | }
|
| 28 |
|
| 29 | public static MemoryMap of(final Stream<MemoryMap.Value<?>> memories) {
|
| 30 | return new MemoryMap(memories.collect(Collectors.toMap(MemoryMap.Value::type, MemoryMap.Value::value)));
|
| 31 | }
|
| 32 |
|
| 33 | public <U> @Nullable ExpirableValue<U> get(final MemoryModuleType<U> type) {
|
| 34 | return (ExpirableValue<U>)this.memories.get(type);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public boolean equals(final Object obj) {
|
| 39 | return obj instanceof MemoryMap map && this.memories.equals(map.memories);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public int hashCode() {
|
| 44 | return this.memories.hashCode();
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public String toString() {
|
| 49 | return this.memories.toString();
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public Iterator<MemoryMap.Value<?>> iterator() {
|
| 54 | return Iterators.transform(this.memories.entrySet().iterator(), entry -> MemoryMap.Value.createUnchecked(entry.getKey(), entry.getValue()));
|
| 55 | }
|
| 56 |
|
| 57 | public static class Builder {
|
| 58 | private final ImmutableMap.Builder<MemoryModuleType<?>, ExpirableValue<?>> builder = ImmutableMap.builder();
|
| 59 |
|
| 60 | public <U> MemoryMap.Builder add(final MemoryModuleType<U> type, final ExpirableValue<U> value) {
|
| 61 | this.builder.put(type, value);
|
| 62 | return this;
|
| 63 | }
|
| 64 |
|
| 65 | public MemoryMap build() {
|
| 66 | return new MemoryMap(this.builder.buildOrThrow());
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public record Value<U>(MemoryModuleType<U> type, ExpirableValue<U> value) {
|
| 71 | public static <U> MemoryMap.Value<U> createUnchecked(final MemoryModuleType<U> type, final ExpirableValue<?> value) {
|
| 72 | return new MemoryMap.Value<>(type, (ExpirableValue<U>)value);
|
| 73 | }
|
| 74 | }
|
| 75 | }
|
| 76 |
|