| 1 | package net.minecraft.server.commands.data;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterables;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.brigadier.arguments.DoubleArgumentType;
|
| 6 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 7 | import com.mojang.brigadier.builder.ArgumentBuilder;
|
| 8 | import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
| 9 | import com.mojang.brigadier.context.CommandContext;
|
| 10 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 11 | import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
|
| 12 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 13 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 14 | import java.util.ArrayList;
|
| 15 | import java.util.Collection;
|
| 16 | import java.util.Collections;
|
| 17 | import java.util.Iterator;
|
| 18 | import java.util.List;
|
| 19 | import java.util.function.BiConsumer;
|
| 20 | import net.minecraft.commands.CommandBuildContext;
|
| 21 | import net.minecraft.commands.CommandSourceStack;
|
| 22 | import net.minecraft.commands.Commands;
|
| 23 | import net.minecraft.commands.arguments.CompoundTagArgument;
|
| 24 | import net.minecraft.commands.arguments.NbtPathArgument;
|
| 25 | import net.minecraft.commands.arguments.NbtTagArgument;
|
| 26 | import net.minecraft.commands.arguments.ResourceOrIdArgument;
|
| 27 | import net.minecraft.core.Holder;
|
| 28 | import net.minecraft.nbt.CollectionTag;
|
| 29 | import net.minecraft.nbt.CompoundTag;
|
| 30 | import net.minecraft.nbt.EndTag;
|
| 31 | import net.minecraft.nbt.FloatTag;
|
| 32 | import net.minecraft.nbt.IntTag;
|
| 33 | import net.minecraft.nbt.NumericTag;
|
| 34 | import net.minecraft.nbt.PrimitiveTag;
|
| 35 | import net.minecraft.nbt.StringTag;
|
| 36 | import net.minecraft.nbt.Tag;
|
| 37 | import net.minecraft.network.chat.Component;
|
| 38 | import net.minecraft.server.commands.ArgProvider;
|
| 39 | import net.minecraft.server.commands.LootContextSources;
|
| 40 | import net.minecraft.util.Mth;
|
| 41 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 42 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProvider;
|
| 43 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
|
| 44 |
|
| 45 | public class DataCommands {
|
| 46 | private static final SimpleCommandExceptionType ERROR_MERGE_UNCHANGED = new SimpleCommandExceptionType(Component.translatable("commands.data.merge.failed"));
|
| 47 | private static final DynamicCommandExceptionType ERROR_GET_NOT_NUMBER = new DynamicCommandExceptionType(
|
| 48 | path -> Component.translatableEscape("commands.data.get.invalid", path)
|
| 49 | );
|
| 50 | private static final DynamicCommandExceptionType ERROR_GET_NON_EXISTENT = new DynamicCommandExceptionType(
|
| 51 | path -> Component.translatableEscape("commands.data.get.unknown", path)
|
| 52 | );
|
| 53 | private static final SimpleCommandExceptionType ERROR_MULTIPLE_TAGS = new SimpleCommandExceptionType(Component.translatable("commands.data.get.multiple"));
|
| 54 | private static final DynamicCommandExceptionType ERROR_EXPECTED_OBJECT = new DynamicCommandExceptionType(
|
| 55 | node -> Component.translatableEscape("commands.data.modify.expected_object", node)
|
| 56 | );
|
| 57 | private static final DynamicCommandExceptionType ERROR_EXPECTED_VALUE = new DynamicCommandExceptionType(
|
| 58 | node -> Component.translatableEscape("commands.data.modify.expected_value", node)
|
| 59 | );
|
| 60 | private static final Dynamic2CommandExceptionType ERROR_INVALID_SUBSTRING = new Dynamic2CommandExceptionType(
|
| 61 | (start, end) -> Component.translatableEscape("commands.data.modify.invalid_substring", start, end)
|
| 62 | );
|
| 63 | public static final List<ArgProvider.Factory<DataAccessor>> ALL_PROVIDERS = List.of(
|
| 64 | EntityDataAccessor.PROVIDER, BlockDataAccessor.PROVIDER, StorageDataAccessor.PROVIDER
|
| 65 | );
|
| 66 | public static final List<ArgProvider<DataAccessor>> TARGET_PROVIDERS = ArgProvider.buildList("target", ALL_PROVIDERS);
|
| 67 | public static final List<ArgProvider<DataAccessor>> SOURCE_PROVIDERS = ArgProvider.buildList("source", ALL_PROVIDERS);
|
| 68 |
|
| 69 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext buildContext) {
|
| 70 | LiteralArgumentBuilder<CommandSourceStack> root = Commands.literal("data").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS));
|
| 71 |
|
| 72 | for (ArgProvider<DataAccessor> targetProvider : TARGET_PROVIDERS) {
|
| 73 | root.then(
|
| 74 | targetProvider.wrap(
|
| 75 | Commands.literal("merge"),
|
| 76 | p -> p.then(
|
| 77 | Commands.argument("nbt", CompoundTagArgument.compoundTag())
|
| 78 | .executes(c -> mergeData(c.getSource(), targetProvider.access(c), CompoundTagArgument.getCompoundTag(c, "nbt")))
|
| 79 | )
|
| 80 | )
|
| 81 | )
|
| 82 | .then(
|
| 83 | targetProvider.wrap(
|
| 84 | Commands.literal("get"),
|
| 85 | p -> p.executes(c -> getData(c.getSource(), targetProvider.access(c)))
|
| 86 | .then(
|
| 87 | Commands.argument("path", NbtPathArgument.nbtPath())
|
| 88 | .executes(c -> getData(c.getSource(), targetProvider.access(c), NbtPathArgument.getPath(c, "path")))
|
| 89 | .then(
|
| 90 | Commands.argument("scale", DoubleArgumentType.doubleArg())
|
| 91 | .executes(c -> getNumeric(c.getSource(), targetProvider.access(c), NbtPathArgument.getPath(c, "path"), DoubleArgumentType.getDouble(c, "scale")))
|
| 92 | )
|
| 93 | )
|
| 94 | )
|
| 95 | )
|
| 96 | .then(
|
| 97 | targetProvider.wrap(
|
| 98 | Commands.literal("remove"),
|
| 99 | p -> p.then(
|
| 100 | Commands.argument("path", NbtPathArgument.nbtPath())
|
| 101 | .executes(c -> removeData(c.getSource(), targetProvider.access(c), NbtPathArgument.getPath(c, "path")))
|
| 102 | )
|
| 103 | )
|
| 104 | )
|
| 105 | .then(
|
| 106 | decorateModification(
|
| 107 | buildContext,
|
| 108 | (parent, rest) -> parent.then(
|
| 109 | Commands.literal("insert")
|
| 110 | .then(
|
| 111 | Commands.argument("index", IntegerArgumentType.integer())
|
| 112 | .then(rest.create((context, target, targetPath, source) -> targetPath.insert(IntegerArgumentType.getInteger(context, "index"), target, source)))
|
| 113 | )
|
| 114 | )
|
| 115 | .then(Commands.literal("prepend").then(rest.create((context, target, targetPath, source) -> targetPath.insert(0, target, source))))
|
| 116 | .then(Commands.literal("append").then(rest.create((context, target, targetPath, source) -> targetPath.insert(-1, target, source))))
|
| 117 | .then(Commands.literal("set").then(rest.create((context, target, targetPath, source) -> targetPath.set(target, Iterables.getLast(source)))))
|
| 118 | .then(Commands.literal("merge").then(rest.create((context, target, targetPath, source) -> {
|
| 119 | CompoundTag combinedSources = new CompoundTag();
|
| 120 |
|
| 121 | for (Tag sourceTag : source) {
|
| 122 | if (NbtPathArgument.NbtPath.isTooDeep(sourceTag, 0)) {
|
| 123 | throw NbtPathArgument.ERROR_DATA_TOO_DEEP.create();
|
| 124 | }
|
| 125 |
|
| 126 | if (!(sourceTag instanceof CompoundTag tag)) {
|
| 127 | throw ERROR_EXPECTED_OBJECT.create(sourceTag);
|
| 128 | }
|
| 129 |
|
| 130 | combinedSources.merge(tag);
|
| 131 | }
|
| 132 |
|
| 133 | Collection<Tag> targets = targetPath.getOrCreate(target, CompoundTag::new);
|
| 134 | int changedCount = 0;
|
| 135 |
|
| 136 | for (Tag targetTag : targets) {
|
| 137 | if (!(targetTag instanceof CompoundTag targetObject)) {
|
| 138 | throw ERROR_EXPECTED_OBJECT.create(targetTag);
|
| 139 | }
|
| 140 |
|
| 141 | CompoundTag originalTarget = targetObject.copy();
|
| 142 | targetObject.merge(combinedSources);
|
| 143 | changedCount += originalTarget.equals(targetObject) ? 0 : 1;
|
| 144 | }
|
| 145 |
|
| 146 | return changedCount;
|
| 147 | })))
|
| 148 | )
|
| 149 | );
|
| 150 | }
|
| 151 |
|
| 152 | dispatcher.register(root);
|
| 153 | }
|
| 154 |
|
| 155 | private static String getAsText(final Tag tag) throws CommandSyntaxException {
|
| 156 | return switch (tag) {
|
| 157 | case StringTag(String value) -> value;
|
| 158 | case PrimitiveTag primitiveTag -> primitiveTag.toString();
|
| 159 | default -> throw ERROR_EXPECTED_VALUE.create(tag);
|
| 160 | };
|
| 161 | }
|
| 162 |
|
| 163 | private static List<Tag> stringifyTagList(final List<Tag> source, final DataCommands.StringProcessor stringProcessor) throws CommandSyntaxException {
|
| 164 | List<Tag> result = new ArrayList<>(source.size());
|
| 165 |
|
| 166 | for (Tag tag : source) {
|
| 167 | String text = getAsText(tag);
|
| 168 | result.add(StringTag.valueOf(stringProcessor.process(text)));
|
| 169 | }
|
| 170 |
|
| 171 | return result;
|
| 172 | }
|
| 173 |
|
| 174 | private static ArgumentBuilder<CommandSourceStack, ?> decorateModification(
|
| 175 | final CommandBuildContext buildContext, final BiConsumer<ArgumentBuilder<CommandSourceStack, ?>, DataCommands.DataManipulatorDecorator> nodeSupplier
|
| 176 | ) {
|
| 177 | LiteralArgumentBuilder<CommandSourceStack> modify = Commands.literal("modify");
|
| 178 |
|
| 179 | for (ArgProvider<DataAccessor> targetProvider : TARGET_PROVIDERS) {
|
| 180 | targetProvider.wrap(
|
| 181 | modify,
|
| 182 | t -> {
|
| 183 | ArgumentBuilder<CommandSourceStack, ?> targetPathNode = Commands.argument("targetPath", NbtPathArgument.nbtPath());
|
| 184 |
|
| 185 | for (ArgProvider<DataAccessor> sourceProvider : SOURCE_PROVIDERS) {
|
| 186 | nodeSupplier.accept(
|
| 187 | targetPathNode,
|
| 188 | manipulator -> sourceProvider.wrap(
|
| 189 | Commands.literal("from"),
|
| 190 | s -> s.executes(c -> manipulateData(c, targetProvider, manipulator, getSingletonSource(c, sourceProvider)))
|
| 191 | .then(
|
| 192 | Commands.argument("sourcePath", NbtPathArgument.nbtPath())
|
| 193 | .executes(c -> manipulateData(c, targetProvider, manipulator, resolveSourcePath(c, sourceProvider)))
|
| 194 | )
|
| 195 | )
|
| 196 | );
|
| 197 | nodeSupplier.accept(
|
| 198 | targetPathNode,
|
| 199 | manipulator -> sourceProvider.wrap(
|
| 200 | Commands.literal("string"),
|
| 201 | s -> s.executes(c -> manipulateData(c, targetProvider, manipulator, stringifyTagList(getSingletonSource(c, sourceProvider), str -> str)))
|
| 202 | .then(
|
| 203 | Commands.argument("sourcePath", NbtPathArgument.nbtPath())
|
| 204 | .executes(c -> manipulateData(c, targetProvider, manipulator, stringifyTagList(resolveSourcePath(c, sourceProvider), str -> str)))
|
| 205 | .then(
|
| 206 | Commands.argument("start", IntegerArgumentType.integer())
|
| 207 | .executes(
|
| 208 | c -> manipulateData(
|
| 209 | c,
|
| 210 | targetProvider,
|
| 211 | manipulator,
|
| 212 | stringifyTagList(resolveSourcePath(c, sourceProvider), str -> substring(str, IntegerArgumentType.getInteger(c, "start")))
|
| 213 | )
|
| 214 | )
|
| 215 | .then(
|
| 216 | Commands.argument("end", IntegerArgumentType.integer())
|
| 217 | .executes(
|
| 218 | c -> manipulateData(
|
| 219 | c,
|
| 220 | targetProvider,
|
| 221 | manipulator,
|
| 222 | stringifyTagList(
|
| 223 | resolveSourcePath(c, sourceProvider),
|
| 224 | str -> substring(str, IntegerArgumentType.getInteger(c, "start"), IntegerArgumentType.getInteger(c, "end"))
|
| 225 | )
|
| 226 | )
|
| 227 | )
|
| 228 | )
|
| 229 | )
|
| 230 | )
|
| 231 | )
|
| 232 | );
|
| 233 | }
|
| 234 |
|
| 235 | nodeSupplier.accept(targetPathNode, manipulator -> LootContextSources.addContextSources(Commands.literal("compute"), (contextDecorator, output) -> {
|
| 236 | output.accept(Commands.literal("float").then(Commands.argument("provider", ResourceOrIdArgument.floatProvider(buildContext)).executes(c -> {
|
| 237 | LootContext lootContext = contextDecorator.createContext(c);
|
| 238 | Holder<ContextFloatProvider> provider = ResourceOrIdArgument.getFloatProvider(c, "provider");
|
| 239 | float value = provider.value().getFloat(lootContext);
|
| 240 | return manipulateData(c, targetProvider, manipulator, List.of(FloatTag.valueOf(value)));
|
| 241 | })));
|
| 242 | output.accept(Commands.literal("integer").then(Commands.argument("provider", ResourceOrIdArgument.intProvider(buildContext)).executes(c -> {
|
| 243 | LootContext lootContext = contextDecorator.createContext(c);
|
| 244 | Holder<ContextIntProvider> provider = ResourceOrIdArgument.getIntProvider(c, "provider");
|
| 245 | int value = provider.value().getInt(lootContext);
|
| 246 | return manipulateData(c, targetProvider, manipulator, List.of(IntTag.valueOf(value)));
|
| 247 | })));
|
| 248 | }));
|
| 249 | nodeSupplier.accept(targetPathNode, manipulator -> Commands.literal("value").then(Commands.argument("value", NbtTagArgument.nbtTag()).executes(c -> {
|
| 250 | List<Tag> source = Collections.singletonList(NbtTagArgument.getNbtTag(c, "value"));
|
| 251 | return manipulateData(c, targetProvider, manipulator, source);
|
| 252 | })));
|
| 253 | return t.then(targetPathNode);
|
| 254 | }
|
| 255 | );
|
| 256 | }
|
| 257 |
|
| 258 | return modify;
|
| 259 | }
|
| 260 |
|
| 261 | private static String validatedSubstring(final String input, final int start, final int end) throws CommandSyntaxException {
|
| 262 | if (start >= 0 && end <= input.length() && start <= end) {
|
| 263 | return input.substring(start, end);
|
| 264 | } else {
|
| 265 | throw ERROR_INVALID_SUBSTRING.create(start, end);
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | private static String substring(final String input, final int start, final int end) throws CommandSyntaxException {
|
| 270 | int length = input.length();
|
| 271 | int absoluteStart = getOffset(start, length);
|
| 272 | int absoluteEnd = getOffset(end, length);
|
| 273 | return validatedSubstring(input, absoluteStart, absoluteEnd);
|
| 274 | }
|
| 275 |
|
| 276 | private static String substring(final String input, final int start) throws CommandSyntaxException {
|
| 277 | int length = input.length();
|
| 278 | return validatedSubstring(input, getOffset(start, length), length);
|
| 279 | }
|
| 280 |
|
| 281 | private static int getOffset(final int index, final int length) {
|
| 282 | return index >= 0 ? index : length + index;
|
| 283 | }
|
| 284 |
|
| 285 | private static List<Tag> getSingletonSource(final CommandContext<CommandSourceStack> context, final ArgProvider<DataAccessor> sourceProvider) throws CommandSyntaxException {
|
| 286 | DataAccessor source = sourceProvider.access(context);
|
| 287 | return Collections.singletonList(source.getData());
|
| 288 | }
|
| 289 |
|
| 290 | private static List<Tag> resolveSourcePath(final CommandContext<CommandSourceStack> context, final ArgProvider<DataAccessor> sourceProvider) throws CommandSyntaxException {
|
| 291 | DataAccessor source = sourceProvider.access(context);
|
| 292 | NbtPathArgument.NbtPath sourcePath = NbtPathArgument.getPath(context, "sourcePath");
|
| 293 | return sourcePath.get(source.getData());
|
| 294 | }
|
| 295 |
|
| 296 | private static int manipulateData(
|
| 297 | final CommandContext<CommandSourceStack> context,
|
| 298 | final ArgProvider<DataAccessor> targetProvider,
|
| 299 | final DataCommands.DataManipulator manipulator,
|
| 300 | final List<Tag> source
|
| 301 | ) throws CommandSyntaxException {
|
| 302 | DataAccessor target = targetProvider.access(context);
|
| 303 | NbtPathArgument.NbtPath targetPath = NbtPathArgument.getPath(context, "targetPath");
|
| 304 | CompoundTag targetData = target.getData();
|
| 305 | int result = manipulator.modify(context, targetData, targetPath, source);
|
| 306 | if (result == 0) {
|
| 307 | throw ERROR_MERGE_UNCHANGED.create();
|
| 308 | }
|
| 309 |
|
| 310 | target.setData(targetData);
|
| 311 | context.getSource().sendSuccess(() -> target.getModifiedSuccess(), true);
|
| 312 | return result;
|
| 313 | }
|
| 314 |
|
| 315 | private static int removeData(final CommandSourceStack source, final DataAccessor accessor, final NbtPathArgument.NbtPath path) throws CommandSyntaxException {
|
| 316 | CompoundTag result = accessor.getData();
|
| 317 | int count = path.remove(result);
|
| 318 | if (count == 0) {
|
| 319 | throw ERROR_MERGE_UNCHANGED.create();
|
| 320 | }
|
| 321 |
|
| 322 | accessor.setData(result);
|
| 323 | source.sendSuccess(() -> accessor.getModifiedSuccess(), true);
|
| 324 | return count;
|
| 325 | }
|
| 326 |
|
| 327 | public static Tag getSingleTag(final NbtPathArgument.NbtPath path, final DataAccessor accessor) throws CommandSyntaxException {
|
| 328 | Collection<Tag> tags = path.get(accessor.getData());
|
| 329 | Iterator<Tag> iterator = tags.iterator();
|
| 330 | Tag result = iterator.next();
|
| 331 | if (iterator.hasNext()) {
|
| 332 | throw ERROR_MULTIPLE_TAGS.create();
|
| 333 | } else {
|
| 334 | return result;
|
| 335 | }
|
| 336 | }
|
| 337 |
|
| 338 | private static int getData(final CommandSourceStack source, final DataAccessor accessor, final NbtPathArgument.NbtPath path) throws CommandSyntaxException {
|
| 339 | Tag tag = getSingleTag(path, accessor);
|
| 340 |
|
| 341 | int result = switch (tag) {
|
| 342 | case NumericTag numericTag -> Mth.floor(numericTag.doubleValue());
|
| 343 | case CollectionTag collectionTag -> collectionTag.size();
|
| 344 | case CompoundTag compoundTag -> compoundTag.size();
|
| 345 | case StringTag(String value) -> value.length();
|
| 346 | case EndTag ignored -> throw ERROR_GET_NON_EXISTENT.create(path.toString());
|
| 347 | default -> throw new MatchException(null, null);
|
| 348 | };
|
| 349 | source.sendSuccess(() -> accessor.getPrintSuccess(tag), false);
|
| 350 | return result;
|
| 351 | }
|
| 352 |
|
| 353 | private static int getNumeric(final CommandSourceStack source, final DataAccessor accessor, final NbtPathArgument.NbtPath path, final double scale) throws CommandSyntaxException {
|
| 354 | if (getSingleTag(path, accessor) instanceof NumericTag numericTag) {
|
| 355 | int result = Mth.floor(numericTag.doubleValue() * scale);
|
| 356 | source.sendSuccess(() -> accessor.getPrintSuccess(path, scale, result), false);
|
| 357 | return result;
|
| 358 | } else {
|
| 359 | throw ERROR_GET_NOT_NUMBER.create(path.toString());
|
| 360 | }
|
| 361 | }
|
| 362 |
|
| 363 | private static int getData(final CommandSourceStack source, final DataAccessor accessor) throws CommandSyntaxException {
|
| 364 | CompoundTag data = accessor.getData();
|
| 365 | source.sendSuccess(() -> accessor.getPrintSuccess(data), false);
|
| 366 | return 1;
|
| 367 | }
|
| 368 |
|
| 369 | private static int mergeData(final CommandSourceStack source, final DataAccessor accessor, final CompoundTag nbt) throws CommandSyntaxException {
|
| 370 | CompoundTag old = accessor.getData();
|
| 371 | if (NbtPathArgument.NbtPath.isTooDeep(nbt, 0)) {
|
| 372 | throw NbtPathArgument.ERROR_DATA_TOO_DEEP.create();
|
| 373 | }
|
| 374 |
|
| 375 | CompoundTag result = old.copy().merge(nbt);
|
| 376 | if (old.equals(result)) {
|
| 377 | throw ERROR_MERGE_UNCHANGED.create();
|
| 378 | }
|
| 379 |
|
| 380 | accessor.setData(result);
|
| 381 | source.sendSuccess(() -> accessor.getModifiedSuccess(), true);
|
| 382 | return 1;
|
| 383 | }
|
| 384 |
|
| 385 | @FunctionalInterface
|
| 386 | private interface DataManipulator {
|
| 387 | int modify(CommandContext<CommandSourceStack> context, CompoundTag targetData, NbtPathArgument.NbtPath targetPath, List<Tag> source) throws CommandSyntaxException;
|
| 388 | }
|
| 389 |
|
| 390 | @FunctionalInterface
|
| 391 | private interface DataManipulatorDecorator {
|
| 392 | ArgumentBuilder<CommandSourceStack, ?> create(DataCommands.DataManipulator manipulator);
|
| 393 | }
|
| 394 |
|
| 395 | @FunctionalInterface
|
| 396 | private interface StringProcessor {
|
| 397 | String process(String string) throws CommandSyntaxException;
|
| 398 | }
|
| 399 | }
|
| 400 |
|