package net.minecraft.server.commands.data; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.suggestion.SuggestionProvider; import java.util.Locale; import java.util.function.Function; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.commands.arguments.NbtPathArgument; import net.minecraft.commands.arguments.ResourceLocationArgument; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtUtils; import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.storage.CommandStorage; public class StorageDataAccessor implements DataAccessor { static final SuggestionProvider SUGGEST_STORAGE = (commandContext, suggestionsBuilder) -> SharedSuggestionProvider.suggestResource( getGlobalTags(commandContext).keys(), suggestionsBuilder ); public static final Function PROVIDER = string -> new DataCommands.DataProvider() { @Override public DataAccessor access(CommandContext context) { return new StorageDataAccessor(StorageDataAccessor.getGlobalTags(context), ResourceLocationArgument.getId(context, string)); } @Override public ArgumentBuilder wrap( ArgumentBuilder builder, Function, ArgumentBuilder> action ) { return builder.then( Commands.literal("storage") .then( (ArgumentBuilder)action.apply( Commands.argument(string, ResourceLocationArgument.id()).suggests(StorageDataAccessor.SUGGEST_STORAGE) ) ) ); } }; private final CommandStorage storage; private final ResourceLocation id; static CommandStorage getGlobalTags(CommandContext context) { return context.getSource().getServer().getCommandStorage(); } StorageDataAccessor(CommandStorage storage, ResourceLocation id) { this.storage = storage; this.id = id; } @Override public void setData(CompoundTag other) { this.storage.set(this.id, other); } @Override public CompoundTag getData() { return this.storage.get(this.id); } @Override public Component getModifiedSuccess() { return Component.translatable("commands.data.storage.modified", Component.translationArg(this.id)); } @Override public Component getPrintSuccess(Tag nbt) { return Component.translatable("commands.data.storage.query", Component.translationArg(this.id), NbtUtils.toPrettyComponent(nbt)); } @Override public Component getPrintSuccess(NbtPathArgument.NbtPath path, double scale, int value) { return Component.translatable( "commands.data.storage.get", path.asString(), Component.translationArg(this.id), String.format(Locale.ROOT, "%.2f", scale), value ); } }