| 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.Dynamic2CommandExceptionType;
|
| 6 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import net.minecraft.commands.CommandSourceStack;
|
| 9 | import net.minecraft.commands.Commands;
|
| 10 | import net.minecraft.commands.arguments.EntityArgument;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.EntityTypes;
|
| 14 |
|
| 15 | public class RideCommand {
|
| 16 | private static final DynamicCommandExceptionType ERROR_NOT_RIDING = new DynamicCommandExceptionType(
|
| 17 | entity -> Component.translatableEscape("commands.ride.not_riding", entity)
|
| 18 | );
|
| 19 | private static final Dynamic2CommandExceptionType ERROR_ALREADY_RIDING = new Dynamic2CommandExceptionType(
|
| 20 | (entity, vehicle) -> Component.translatableEscape("commands.ride.already_riding", entity, vehicle)
|
| 21 | );
|
| 22 | private static final Dynamic2CommandExceptionType ERROR_MOUNT_FAILED = new Dynamic2CommandExceptionType(
|
| 23 | (entity, vehicle) -> Component.translatableEscape("commands.ride.mount.failure.generic", entity, vehicle)
|
| 24 | );
|
| 25 | private static final SimpleCommandExceptionType ERROR_MOUNTING_PLAYER = new SimpleCommandExceptionType(
|
| 26 | Component.translatable("commands.ride.mount.failure.cant_ride_players")
|
| 27 | );
|
| 28 | private static final SimpleCommandExceptionType ERROR_MOUNTING_LOOP = new SimpleCommandExceptionType(
|
| 29 | Component.translatable("commands.ride.mount.failure.loop")
|
| 30 | );
|
| 31 | private static final SimpleCommandExceptionType ERROR_WRONG_DIMENSION = new SimpleCommandExceptionType(
|
| 32 | Component.translatable("commands.ride.mount.failure.wrong_dimension")
|
| 33 | );
|
| 34 |
|
| 35 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 36 | dispatcher.register(
|
| 37 | Commands.literal("ride")
|
| 38 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 39 | .then(
|
| 40 | Commands.argument("target", EntityArgument.entity())
|
| 41 | .then(
|
| 42 | Commands.literal("mount")
|
| 43 | .then(
|
| 44 | Commands.argument("vehicle", EntityArgument.entity())
|
| 45 | .executes(c -> mount(c.getSource(), EntityArgument.getEntity(c, "target"), EntityArgument.getEntity(c, "vehicle")))
|
| 46 | )
|
| 47 | )
|
| 48 | .then(Commands.literal("dismount").executes(c -> dismount(c.getSource(), EntityArgument.getEntity(c, "target"))))
|
| 49 | )
|
| 50 | );
|
| 51 | }
|
| 52 |
|
| 53 | private static int mount(final CommandSourceStack source, final Entity target, final Entity vehicle) throws CommandSyntaxException {
|
| 54 | Entity currentVehicle = target.getVehicle();
|
| 55 | if (currentVehicle != null) {
|
| 56 | throw ERROR_ALREADY_RIDING.create(target.getDisplayName(), currentVehicle.getDisplayName());
|
| 57 | }
|
| 58 |
|
| 59 | if (vehicle.is(EntityTypes.PLAYER)) {
|
| 60 | throw ERROR_MOUNTING_PLAYER.create();
|
| 61 | }
|
| 62 |
|
| 63 | if (target.getSelfAndPassengers().anyMatch(e -> e == vehicle)) {
|
| 64 | throw ERROR_MOUNTING_LOOP.create();
|
| 65 | }
|
| 66 |
|
| 67 | if (target.level() != vehicle.level()) {
|
| 68 | throw ERROR_WRONG_DIMENSION.create();
|
| 69 | }
|
| 70 |
|
| 71 | if (!target.startRiding(vehicle, true, true)) {
|
| 72 | throw ERROR_MOUNT_FAILED.create(target.getDisplayName(), vehicle.getDisplayName());
|
| 73 | }
|
| 74 |
|
| 75 | source.sendSuccess(() -> Component.translatable("commands.ride.mount.success", target.getDisplayName(), vehicle.getDisplayName()), true);
|
| 76 | return 1;
|
| 77 | }
|
| 78 |
|
| 79 | private static int dismount(final CommandSourceStack source, final Entity target) throws CommandSyntaxException {
|
| 80 | Entity vehicle = target.getVehicle();
|
| 81 | if (vehicle == null) {
|
| 82 | throw ERROR_NOT_RIDING.create(target.getDisplayName());
|
| 83 | }
|
| 84 |
|
| 85 | target.stopRiding();
|
| 86 | source.sendSuccess(() -> Component.translatable("commands.ride.dismount.success", target.getDisplayName(), vehicle.getDisplayName()), true);
|
| 87 | return 1;
|
| 88 | }
|
| 89 | }
|
| 90 |
|