| 1 | package net.minecraft.world.level.storage;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.stream.Stream;
|
| 8 | import net.minecraft.nbt.CompoundTag;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.util.ExtraCodecs;
|
| 11 | import net.minecraft.util.datafix.DataFixTypes;
|
| 12 | import net.minecraft.world.level.saveddata.SavedData;
|
| 13 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public class CommandStorage {
|
| 17 | private static final String COMMAND_STORAGE = "command_storage";
|
| 18 | private final Map<String, CommandStorage.Container> namespaces = new HashMap<>();
|
| 19 | private final SavedDataStorage savedDataStorage;
|
| 20 |
|
| 21 | public CommandStorage(final SavedDataStorage savedDataStorage) {
|
| 22 | this.savedDataStorage = savedDataStorage;
|
| 23 | }
|
| 24 |
|
| 25 | public CompoundTag get(final Identifier id) {
|
| 26 | CommandStorage.Container container = this.getContainer(id.getNamespace());
|
| 27 | return container != null ? container.get(id.getPath()) : new CompoundTag();
|
| 28 | }
|
| 29 |
|
| 30 | private CommandStorage.@Nullable Container getContainer(final String namespace) {
|
| 31 | CommandStorage.Container container = this.namespaces.get(namespace);
|
| 32 | if (container != null) {
|
| 33 | return container;
|
| 34 | }
|
| 35 |
|
| 36 | CommandStorage.Container newContainer = this.savedDataStorage.get(CommandStorage.Container.type(namespace));
|
| 37 | if (newContainer != null) {
|
| 38 | this.namespaces.put(namespace, newContainer);
|
| 39 | }
|
| 40 |
|
| 41 | return newContainer;
|
| 42 | }
|
| 43 |
|
| 44 | private CommandStorage.Container getOrCreateContainer(final String namespace) {
|
| 45 | CommandStorage.Container container = this.namespaces.get(namespace);
|
| 46 | if (container != null) {
|
| 47 | return container;
|
| 48 | }
|
| 49 |
|
| 50 | CommandStorage.Container newContainer = this.savedDataStorage.computeIfAbsent(CommandStorage.Container.type(namespace));
|
| 51 | this.namespaces.put(namespace, newContainer);
|
| 52 | return newContainer;
|
| 53 | }
|
| 54 |
|
| 55 | public void set(final Identifier id, final CompoundTag contents) {
|
| 56 | this.getOrCreateContainer(id.getNamespace()).put(id.getPath(), contents);
|
| 57 | }
|
| 58 |
|
| 59 | public Stream<Identifier> keys() {
|
| 60 | return this.namespaces.entrySet().stream().flatMap(e -> e.getValue().getKeys(e.getKey()));
|
| 61 | }
|
| 62 |
|
| 63 | private static class Container extends SavedData {
|
| 64 | public static final Codec<CommandStorage.Container> CODEC = RecordCodecBuilder.create(
|
| 65 | i -> i.group(Codec.unboundedMap(ExtraCodecs.RESOURCE_PATH_CODEC, CompoundTag.CODEC).fieldOf("contents").forGetter(container -> container.storage))
|
| 66 | .apply(i, CommandStorage.Container::new)
|
| 67 | );
|
| 68 | private final Map<String, CompoundTag> storage;
|
| 69 |
|
| 70 | private Container(final Map<String, CompoundTag> storage) {
|
| 71 | this.storage = new HashMap<>(storage);
|
| 72 | }
|
| 73 |
|
| 74 | private Container() {
|
| 75 | this(new HashMap<>());
|
| 76 | }
|
| 77 |
|
| 78 | public static SavedDataType<CommandStorage.Container> type(final String namespace) {
|
| 79 | return new SavedDataType<>(
|
| 80 | Identifier.fromNamespaceAndPath(namespace, "command_storage"), CommandStorage.Container::new, CODEC, DataFixTypes.SAVED_DATA_COMMAND_STORAGE
|
| 81 | );
|
| 82 | }
|
| 83 |
|
| 84 | public CompoundTag get(final String id) {
|
| 85 | CompoundTag result = this.storage.get(id);
|
| 86 | return result != null ? result : new CompoundTag();
|
| 87 | }
|
| 88 |
|
| 89 | public void put(final String id, final CompoundTag contents) {
|
| 90 | if (contents.isEmpty()) {
|
| 91 | this.storage.remove(id);
|
| 92 | } else {
|
| 93 | this.storage.put(id, contents);
|
| 94 | }
|
| 95 |
|
| 96 | this.setDirty();
|
| 97 | }
|
| 98 |
|
| 99 | public Stream<Identifier> getKeys(final String namespace) {
|
| 100 | return this.storage.keySet().stream().map(p -> Identifier.fromNamespaceAndPath(namespace, p));
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|