RideCommand.java
net.minecraft.server.commands.RideCommand
信息
- 全限定名:net.minecraft.server.commands.RideCommand
- 类型:public class
- 包:net.minecraft.server.commands
- 源码路径:src/main/java/net/minecraft/server/commands/RideCommand.java
- 起始行号:L15
- 职责:
TODO
字段/常量
-
ERROR_NOT_RIDING- 类型:
DynamicCommandExceptionType - 修饰符:
private static final - 源码定位:
L16 - 说明:
TODO
- 类型:
-
ERROR_ALREADY_RIDING- 类型:
Dynamic2CommandExceptionType - 修饰符:
private static final - 源码定位:
L19 - 说明:
TODO
- 类型:
-
ERROR_MOUNT_FAILED- 类型:
Dynamic2CommandExceptionType - 修饰符:
private static final - 源码定位:
L22 - 说明:
TODO
- 类型:
-
ERROR_MOUNTING_PLAYER- 类型:
SimpleCommandExceptionType - 修饰符:
private static final - 源码定位:
L25 - 说明:
TODO
- 类型:
-
ERROR_MOUNTING_LOOP- 类型:
SimpleCommandExceptionType - 修饰符:
private static final - 源码定位:
L28 - 说明:
TODO
- 类型:
-
ERROR_WRONG_DIMENSION- 类型:
SimpleCommandExceptionType - 修饰符:
private static final - 源码定位:
L31 - 说明:
TODO
- 类型:
内部类/嵌套类型
- 无
构造器
- 无
方法
下面的方法块按源码顺序生成。
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L35
- 方法名:register
- 源码定位:L35
- 返回类型:void
- 修饰符:public static
参数:
- dispatcher: CommandDispatcher
说明:
TODO
private static int mount(CommandSourceStack source, Entity target, Entity vehicle) @ L53
- 方法名:mount
- 源码定位:L53
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
- vehicle: Entity
说明:
TODO
private static int dismount(CommandSourceStack source, Entity target) @ L71
- 方法名:dismount
- 源码定位:L71
- 返回类型:int
- 修饰符:private static
参数:
- source: CommandSourceStack
- target: Entity
说明:
TODO
代码
public class RideCommand {
private static final DynamicCommandExceptionType ERROR_NOT_RIDING = new DynamicCommandExceptionType(
entity -> Component.translatableEscape("commands.ride.not_riding", entity)
);
private static final Dynamic2CommandExceptionType ERROR_ALREADY_RIDING = new Dynamic2CommandExceptionType(
(entity, vehicle) -> Component.translatableEscape("commands.ride.already_riding", entity, vehicle)
);
private static final Dynamic2CommandExceptionType ERROR_MOUNT_FAILED = new Dynamic2CommandExceptionType(
(entity, vehicle) -> Component.translatableEscape("commands.ride.mount.failure.generic", entity, vehicle)
);
private static final SimpleCommandExceptionType ERROR_MOUNTING_PLAYER = new SimpleCommandExceptionType(
Component.translatable("commands.ride.mount.failure.cant_ride_players")
);
private static final SimpleCommandExceptionType ERROR_MOUNTING_LOOP = new SimpleCommandExceptionType(
Component.translatable("commands.ride.mount.failure.loop")
);
private static final SimpleCommandExceptionType ERROR_WRONG_DIMENSION = new SimpleCommandExceptionType(
Component.translatable("commands.ride.mount.failure.wrong_dimension")
);
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(
Commands.literal("ride")
.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
.then(
Commands.argument("target", EntityArgument.entity())
.then(
Commands.literal("mount")
.then(
Commands.argument("vehicle", EntityArgument.entity())
.executes(c -> mount(c.getSource(), EntityArgument.getEntity(c, "target"), EntityArgument.getEntity(c, "vehicle")))
)
)
.then(Commands.literal("dismount").executes(c -> dismount(c.getSource(), EntityArgument.getEntity(c, "target"))))
)
);
}
private static int mount(CommandSourceStack source, Entity target, Entity vehicle) throws CommandSyntaxException {
Entity currentVehicle = target.getVehicle();
if (currentVehicle != null) {
throw ERROR_ALREADY_RIDING.create(target.getDisplayName(), currentVehicle.getDisplayName());
} else if (vehicle.is(EntityType.PLAYER)) {
throw ERROR_MOUNTING_PLAYER.create();
} else if (target.getSelfAndPassengers().anyMatch(e -> e == vehicle)) {
throw ERROR_MOUNTING_LOOP.create();
} else if (target.level() != vehicle.level()) {
throw ERROR_WRONG_DIMENSION.create();
} else if (!target.startRiding(vehicle, true, true)) {
throw ERROR_MOUNT_FAILED.create(target.getDisplayName(), vehicle.getDisplayName());
} else {
source.sendSuccess(() -> Component.translatable("commands.ride.mount.success", target.getDisplayName(), vehicle.getDisplayName()), true);
return 1;
}
}
private static int dismount(CommandSourceStack source, Entity target) throws CommandSyntaxException {
Entity vehicle = target.getVehicle();
if (vehicle == null) {
throw ERROR_NOT_RIDING.create(target.getDisplayName());
} else {
target.stopRiding();
source.sendSuccess(() -> Component.translatable("commands.ride.dismount.success", target.getDisplayName(), vehicle.getDisplayName()), true);
return 1;
}
}
}引用的其他类
-
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Commands.argument(), Commands.hasPermission(), Commands.literal()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
EntityArgument.entity(), EntityArgument.getEntity()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Component.translatable(), Component.translatableEscape()
- 引用位置:
-
- 引用位置:
参数
- 引用位置: