| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 6 | import net.minecraft.commands.CommandSourceStack;
|
| 7 | import net.minecraft.commands.Commands;
|
| 8 | import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.world.entity.Mob;
|
| 12 | import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
| 13 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 14 | import net.minecraft.world.level.pathfinder.Path;
|
| 15 |
|
| 16 | public class DebugPathCommand {
|
| 17 | private static final SimpleCommandExceptionType ERROR_NOT_MOB = new SimpleCommandExceptionType(Component.literal("Source is not a mob"));
|
| 18 | private static final SimpleCommandExceptionType ERROR_NO_PATH = new SimpleCommandExceptionType(Component.literal("Path not found"));
|
| 19 | private static final SimpleCommandExceptionType ERROR_NOT_COMPLETE = new SimpleCommandExceptionType(Component.literal("Target not reached"));
|
| 20 |
|
| 21 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 22 | dispatcher.register(
|
| 23 | Commands.literal("debugpath")
|
| 24 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 25 | .then(Commands.argument("to", BlockPosArgument.blockPos()).executes(c -> fillBlocks(c.getSource(), BlockPosArgument.getLoadedBlockPos(c, "to"))))
|
| 26 | );
|
| 27 | }
|
| 28 |
|
| 29 | private static int fillBlocks(final CommandSourceStack source, final BlockPos target) throws CommandSyntaxException {
|
| 30 | if (source.getEntity() instanceof Mob mob) {
|
| 31 | PathNavigation pathNavigation = new GroundPathNavigation(mob, source.getLevel());
|
| 32 | Path path = pathNavigation.createPath(target, 0);
|
| 33 | if (path == null) {
|
| 34 | throw ERROR_NO_PATH.create();
|
| 35 | }
|
| 36 |
|
| 37 | if (!path.canReach()) {
|
| 38 | throw ERROR_NOT_COMPLETE.create();
|
| 39 | }
|
| 40 |
|
| 41 | source.sendSuccess(() -> Component.literal("Made path"), true);
|
| 42 | return 1;
|
| 43 | } else {
|
| 44 | throw ERROR_NOT_MOB.create();
|
| 45 | }
|
| 46 | }
|
| 47 | }
|
| 48 |
|