| 1 | package net.minecraft.server.commands.data;
|
| 2 |
|
| 3 | import com.mojang.brigadier.context.CommandContext;
|
| 4 | import com.mojang.brigadier.suggestion.SuggestionProvider;
|
| 5 | import java.util.Locale;
|
| 6 | import net.minecraft.commands.CommandSourceStack;
|
| 7 | import net.minecraft.commands.Commands;
|
| 8 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 9 | import net.minecraft.commands.arguments.IdentifierArgument;
|
| 10 | import net.minecraft.commands.arguments.NbtPathArgument;
|
| 11 | import net.minecraft.nbt.CompoundTag;
|
| 12 | import net.minecraft.nbt.NbtUtils;
|
| 13 | import net.minecraft.nbt.Tag;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.server.commands.ArgProvider;
|
| 17 | import net.minecraft.world.level.storage.CommandStorage;
|
| 18 |
|
| 19 | public class StorageDataAccessor implements DataAccessor {
|
| 20 | private static final SuggestionProvider<CommandSourceStack> SUGGEST_STORAGE = (c, p) -> SharedSuggestionProvider.suggestResource(getGlobalTags(c).keys(), p);
|
| 21 | public static final ArgProvider.Factory<DataAccessor> PROVIDER = arg -> ArgProvider.create(
|
| 22 | "storage",
|
| 23 | () -> Commands.argument(arg, IdentifierArgument.id()).suggests(SUGGEST_STORAGE),
|
| 24 | c -> new StorageDataAccessor(getGlobalTags(c), IdentifierArgument.getId(c, arg))
|
| 25 | );
|
| 26 | private final CommandStorage storage;
|
| 27 | private final Identifier id;
|
| 28 |
|
| 29 | private static CommandStorage getGlobalTags(final CommandContext<CommandSourceStack> context) {
|
| 30 | return context.getSource().getServer().getCommandStorage();
|
| 31 | }
|
| 32 |
|
| 33 | private StorageDataAccessor(final CommandStorage storage, final Identifier id) {
|
| 34 | this.storage = storage;
|
| 35 | this.id = id;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void setData(final CompoundTag tag) {
|
| 40 | this.storage.set(this.id, tag);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public CompoundTag getData() {
|
| 45 | return this.storage.get(this.id);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public Component getModifiedSuccess() {
|
| 50 | return Component.translatable("commands.data.storage.modified", Component.translationArg(this.id));
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public Component getPrintSuccess(final Tag data) {
|
| 55 | return Component.translatable("commands.data.storage.query", Component.translationArg(this.id), NbtUtils.toPrettyComponent(data));
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public Component getPrintSuccess(final NbtPathArgument.NbtPath path, final double scale, final int value) {
|
| 60 | return Component.translatable(
|
| 61 | "commands.data.storage.get", path.asString(), Component.translationArg(this.id), String.format(Locale.ROOT, "%.2f", scale), value
|
| 62 | );
|
| 63 | }
|
| 64 | }
|
| 65 |
|