LocateCommand.java

net.minecraft.server.commands.LocateCommand

信息

  • 全限定名:net.minecraft.server.commands.LocateCommand
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/LocateCommand.java
  • 起始行号:L36
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L37
    • 说明:

      TODO

  • ERROR_STRUCTURE_NOT_FOUND

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L38
    • 说明:

      TODO

  • ERROR_STRUCTURE_INVALID

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L41
    • 说明:

      TODO

  • ERROR_BIOME_NOT_FOUND

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L44
    • 说明:

      TODO

  • ERROR_POI_NOT_FOUND

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L47
    • 说明:

      TODO

  • MAX_STRUCTURE_SEARCH_RADIUS

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L50
    • 说明:

      TODO

  • MAX_BIOME_SEARCH_RADIUS

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L51
    • 说明:

      TODO

  • BIOME_SAMPLE_RESOLUTION_HORIZONTAL

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L52
    • 说明:

      TODO

  • BIOME_SAMPLE_RESOLUTION_VERTICAL

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L53
    • 说明:

      TODO

  • POI_SEARCH_RADIUS

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L54
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) @ L56

  • 方法名:register
  • 源码定位:L56
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static Optional<?extends HolderSet.ListBacked<Structure>> getHolders(ResourceOrTagKeyArgument.Result<Structure> resourceOrTag, Registry<Structure> registry) @ L89

  • 方法名:getHolders
  • 源码定位:L89
  • 返回类型:Optional<?extends HolderSet.ListBacked>
  • 修饰符:private static

参数:

  • resourceOrTag: ResourceOrTagKeyArgument.Result
  • registry: Registry

说明:

TODO

private static int locateStructure(CommandSourceStack source, ResourceOrTagKeyArgument.Result<Structure> resourceOrTag) @ L95

  • 方法名:locateStructure
  • 源码定位:L95
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • resourceOrTag: ResourceOrTagKeyArgument.Result

说明:

TODO

private static int locateBiome(CommandSourceStack source, ResourceOrTagArgument.Result<Biome> elementOrTag) @ L113

  • 方法名:locateBiome
  • 源码定位:L113
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • elementOrTag: ResourceOrTagArgument.Result

说明:

TODO

private static int locatePoi(CommandSourceStack source, ResourceOrTagArgument.Result<PoiType> resourceOrTag) @ L125

  • 方法名:locatePoi
  • 源码定位:L125
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • resourceOrTag: ResourceOrTagArgument.Result

说明:

TODO

public static int showLocateResult(CommandSourceStack source, ResourceOrTagArgument.Result<?> name, BlockPos sourcePos, Pair<BlockPos,?extends Holder<?>> found, String successMessageKey, boolean includeY, Duration taskDuration) @ L139

  • 方法名:showLocateResult
  • 源码定位:L139
  • 返回类型:int
  • 修饰符:public static

参数:

  • source: CommandSourceStack
  • name: ResourceOrTagArgument.Result<?>
  • sourcePos: BlockPos
  • found: Pair<BlockPos,?extends Holder<?>>
  • successMessageKey: String
  • includeY: boolean
  • taskDuration: Duration

说明:

TODO

public static int showLocateResult(CommandSourceStack source, ResourceOrTagKeyArgument.Result<?> name, BlockPos sourcePos, Pair<BlockPos,?extends Holder<?>> found, String successMessageKey, boolean includeY, Duration taskDuration) @ L152

  • 方法名:showLocateResult
  • 源码定位:L152
  • 返回类型:int
  • 修饰符:public static

参数:

  • source: CommandSourceStack
  • name: ResourceOrTagKeyArgument.Result<?>
  • sourcePos: BlockPos
  • found: Pair<BlockPos,?extends Holder<?>>
  • successMessageKey: String
  • includeY: boolean
  • taskDuration: Duration

说明:

TODO

private static int showLocateResult(CommandSourceStack source, BlockPos sourcePos, Pair<BlockPos,?extends Holder<?>> found, String successMessageKey, boolean includeY, String foundName, Duration taskDuration) @ L166

  • 方法名:showLocateResult
  • 源码定位:L166
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • sourcePos: BlockPos
  • found: Pair<BlockPos,?extends Holder<?>>
  • successMessageKey: String
  • includeY: boolean
  • foundName: String
  • taskDuration: Duration

说明:

TODO

private static float dist(int x1, int z1, int x2, int z2) @ L191

  • 方法名:dist
  • 源码定位:L191
  • 返回类型:float
  • 修饰符:private static

参数:

  • x1: int
  • z1: int
  • x2: int
  • z2: int

说明:

TODO

代码

public class LocateCommand {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final DynamicCommandExceptionType ERROR_STRUCTURE_NOT_FOUND = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("commands.locate.structure.not_found", value)
    );
    private static final DynamicCommandExceptionType ERROR_STRUCTURE_INVALID = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("commands.locate.structure.invalid", value)
    );
    private static final DynamicCommandExceptionType ERROR_BIOME_NOT_FOUND = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("commands.locate.biome.not_found", value)
    );
    private static final DynamicCommandExceptionType ERROR_POI_NOT_FOUND = new DynamicCommandExceptionType(
        value -> Component.translatableEscape("commands.locate.poi.not_found", value)
    );
    private static final int MAX_STRUCTURE_SEARCH_RADIUS = 100;
    private static final int MAX_BIOME_SEARCH_RADIUS = 6400;
    private static final int BIOME_SAMPLE_RESOLUTION_HORIZONTAL = 32;
    private static final int BIOME_SAMPLE_RESOLUTION_VERTICAL = 64;
    private static final int POI_SEARCH_RADIUS = 256;
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        dispatcher.register(
            Commands.literal("locate")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.literal("structure")
                        .then(
                            Commands.argument("structure", ResourceOrTagKeyArgument.resourceOrTagKey(Registries.STRUCTURE))
                                .executes(
                                    c -> locateStructure(
                                        c.getSource(),
                                        ResourceOrTagKeyArgument.getResourceOrTagKey(c, "structure", Registries.STRUCTURE, ERROR_STRUCTURE_INVALID)
                                    )
                                )
                        )
                )
                .then(
                    Commands.literal("biome")
                        .then(
                            Commands.argument("biome", ResourceOrTagArgument.resourceOrTag(context, Registries.BIOME))
                                .executes(c -> locateBiome(c.getSource(), ResourceOrTagArgument.getResourceOrTag(c, "biome", Registries.BIOME)))
                        )
                )
                .then(
                    Commands.literal("poi")
                        .then(
                            Commands.argument("poi", ResourceOrTagArgument.resourceOrTag(context, Registries.POINT_OF_INTEREST_TYPE))
                                .executes(c -> locatePoi(c.getSource(), ResourceOrTagArgument.getResourceOrTag(c, "poi", Registries.POINT_OF_INTEREST_TYPE)))
                        )
                )
        );
    }
 
    private static Optional<? extends HolderSet.ListBacked<Structure>> getHolders(
        ResourceOrTagKeyArgument.Result<Structure> resourceOrTag, Registry<Structure> registry
    ) {
        return resourceOrTag.unwrap().map(id -> registry.get((ResourceKey<Structure>)id).map(xva$0 -> HolderSet.direct(xva$0)), registry::get);
    }
 
    private static int locateStructure(CommandSourceStack source, ResourceOrTagKeyArgument.Result<Structure> resourceOrTag) throws CommandSyntaxException {
        Registry<Structure> registry = source.getLevel().registryAccess().lookupOrThrow(Registries.STRUCTURE);
        HolderSet<Structure> target = (HolderSet<Structure>)getHolders(resourceOrTag, registry)
            .orElseThrow(() -> ERROR_STRUCTURE_INVALID.create(resourceOrTag.asPrintable()));
        BlockPos sourcePos = BlockPos.containing(source.getPosition());
        ServerLevel serverLevel = source.getLevel();
        Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
        Pair<BlockPos, Holder<Structure>> nearest = serverLevel.getChunkSource()
            .getGenerator()
            .findNearestMapStructure(serverLevel, target, sourcePos, 100, false);
        stopwatch.stop();
        if (nearest == null) {
            throw ERROR_STRUCTURE_NOT_FOUND.create(resourceOrTag.asPrintable());
        } else {
            return showLocateResult(source, resourceOrTag, sourcePos, nearest, "commands.locate.structure.success", false, stopwatch.elapsed());
        }
    }
 
    private static int locateBiome(CommandSourceStack source, ResourceOrTagArgument.Result<Biome> elementOrTag) throws CommandSyntaxException {
        BlockPos sourcePos = BlockPos.containing(source.getPosition());
        Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
        Pair<BlockPos, Holder<Biome>> nearest = source.getLevel().findClosestBiome3d(elementOrTag, sourcePos, 6400, 32, 64);
        stopwatch.stop();
        if (nearest == null) {
            throw ERROR_BIOME_NOT_FOUND.create(elementOrTag.asPrintable());
        } else {
            return showLocateResult(source, elementOrTag, sourcePos, nearest, "commands.locate.biome.success", true, stopwatch.elapsed());
        }
    }
 
    private static int locatePoi(CommandSourceStack source, ResourceOrTagArgument.Result<PoiType> resourceOrTag) throws CommandSyntaxException {
        BlockPos sourcePos = BlockPos.containing(source.getPosition());
        ServerLevel serverLevel = source.getLevel();
        Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
        Optional<Pair<Holder<PoiType>, BlockPos>> closestWithType = serverLevel.getPoiManager()
            .findClosestWithType(resourceOrTag, sourcePos, 256, PoiManager.Occupancy.ANY);
        stopwatch.stop();
        if (closestWithType.isEmpty()) {
            throw ERROR_POI_NOT_FOUND.create(resourceOrTag.asPrintable());
        } else {
            return showLocateResult(source, resourceOrTag, sourcePos, closestWithType.get().swap(), "commands.locate.poi.success", false, stopwatch.elapsed());
        }
    }
 
    public static int showLocateResult(
        CommandSourceStack source,
        ResourceOrTagArgument.Result<?> name,
        BlockPos sourcePos,
        Pair<BlockPos, ? extends Holder<?>> found,
        String successMessageKey,
        boolean includeY,
        Duration taskDuration
    ) {
        String foundName = name.unwrap().map(element -> name.asPrintable(), tag -> name.asPrintable() + " (" + found.getSecond().getRegisteredName() + ")");
        return showLocateResult(source, sourcePos, found, successMessageKey, includeY, foundName, taskDuration);
    }
 
    public static int showLocateResult(
        CommandSourceStack source,
        ResourceOrTagKeyArgument.Result<?> name,
        BlockPos sourcePos,
        Pair<BlockPos, ? extends Holder<?>> found,
        String successMessageKey,
        boolean includeY,
        Duration taskDuration
    ) {
        String foundName = name.unwrap()
            .map(element -> element.identifier().toString(), tag -> "#" + tag.location() + " (" + found.getSecond().getRegisteredName() + ")");
        return showLocateResult(source, sourcePos, found, successMessageKey, includeY, foundName, taskDuration);
    }
 
    private static int showLocateResult(
        CommandSourceStack source,
        BlockPos sourcePos,
        Pair<BlockPos, ? extends Holder<?>> found,
        String successMessageKey,
        boolean includeY,
        String foundName,
        Duration taskDuration
    ) {
        BlockPos foundPos = found.getFirst();
        int distance = includeY
            ? Mth.floor(Mth.sqrt((float)sourcePos.distSqr(foundPos)))
            : Mth.floor(dist(sourcePos.getX(), sourcePos.getZ(), foundPos.getX(), foundPos.getZ()));
        String displayedY = includeY ? String.valueOf(foundPos.getY()) : "~";
        Component coordinates = ComponentUtils.wrapInSquareBrackets(Component.translatable("chat.coordinates", foundPos.getX(), displayedY, foundPos.getZ()))
            .withStyle(
                s -> s.withColor(ChatFormatting.GREEN)
                    .withClickEvent(new ClickEvent.SuggestCommand("/tp @s " + foundPos.getX() + " " + displayedY + " " + foundPos.getZ()))
                    .withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.coordinates.tooltip")))
            );
        source.sendSuccess(() -> Component.translatable(successMessageKey, foundName, coordinates, distance), false);
        LOGGER.info("Locating element {} took {} ms", foundName, taskDuration.toMillis());
        return distance;
    }
 
    private static float dist(int x1, int z1, int x2, int z2) {
        int dx = x2 - x1;
        int dz = z2 - z1;
        return Mth.sqrt(dx * dx + dz * dz);
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.hasPermission(), Commands.literal()
  • ResourceOrTagArgument

    • 引用位置: 参数/方法调用
    • 关联成员: ResourceOrTagArgument.getResourceOrTag(), ResourceOrTagArgument.resourceOrTag()
  • ResourceOrTagKeyArgument

    • 引用位置: 参数/方法调用
    • 关联成员: ResourceOrTagKeyArgument.getResourceOrTagKey(), ResourceOrTagKeyArgument.resourceOrTagKey()
  • BlockPos

    • 引用位置: 参数/方法调用
    • 关联成员: BlockPos.containing()
  • Holder

    • 引用位置: 参数
  • HolderSet

    • 引用位置: 方法调用/返回值
    • 关联成员: HolderSet.direct()
  • Registry

    • 引用位置: 参数
  • ClickEvent

    • 引用位置: 方法调用/构造调用
    • 关联成员: ClickEvent.SuggestCommand(), SuggestCommand()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable(), Component.translatableEscape()
  • ComponentUtils

    • 引用位置: 方法调用
    • 关联成员: ComponentUtils.wrapInSquareBrackets()
  • HoverEvent

    • 引用位置: 方法调用/构造调用
    • 关联成员: HoverEvent.ShowText(), ShowText()
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.floor(), Mth.sqrt()
  • Stopwatch

    • 引用位置: 方法调用
    • 关联成员: Stopwatch.createStarted()
  • PoiType

    • 引用位置: 参数
  • Biome

    • 引用位置: 参数
  • Structure

    • 引用位置: 参数/返回值