| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 7 | import com.mojang.datafixers.util.Pair;
|
| 8 | import com.mojang.logging.LogUtils;
|
| 9 | import java.time.Duration;
|
| 10 | import java.util.Optional;
|
| 11 | import net.minecraft.ChatFormatting;
|
| 12 | import net.minecraft.commands.CommandBuildContext;
|
| 13 | import net.minecraft.commands.CommandSourceStack;
|
| 14 | import net.minecraft.commands.Commands;
|
| 15 | import net.minecraft.commands.arguments.ResourceOrTagArgument;
|
| 16 | import net.minecraft.commands.arguments.ResourceOrTagKeyArgument;
|
| 17 | import net.minecraft.core.BlockPos;
|
| 18 | import net.minecraft.core.Holder;
|
| 19 | import net.minecraft.core.HolderSet;
|
| 20 | import net.minecraft.core.Registry;
|
| 21 | import net.minecraft.core.registries.Registries;
|
| 22 | import net.minecraft.network.chat.ClickEvent;
|
| 23 | import net.minecraft.network.chat.Component;
|
| 24 | import net.minecraft.network.chat.ComponentUtils;
|
| 25 | import net.minecraft.network.chat.HoverEvent;
|
| 26 | import net.minecraft.resources.ResourceKey;
|
| 27 | import net.minecraft.server.level.ServerLevel;
|
| 28 | import net.minecraft.util.Mth;
|
| 29 | import net.minecraft.util.Util;
|
| 30 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 31 | import net.minecraft.world.entity.ai.village.poi.PoiType;
|
| 32 | import net.minecraft.world.level.biome.Biome;
|
| 33 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 34 | import org.slf4j.Logger;
|
| 35 |
|
| 36 | public class LocateCommand {
|
| 37 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 38 | private static final DynamicCommandExceptionType ERROR_STRUCTURE_NOT_FOUND = new DynamicCommandExceptionType(
|
| 39 | value -> Component.translatableEscape("commands.locate.structure.not_found", value)
|
| 40 | );
|
| 41 | private static final DynamicCommandExceptionType ERROR_STRUCTURE_INVALID = new DynamicCommandExceptionType(
|
| 42 | value -> Component.translatableEscape("commands.locate.structure.invalid", value)
|
| 43 | );
|
| 44 | private static final DynamicCommandExceptionType ERROR_BIOME_NOT_FOUND = new DynamicCommandExceptionType(
|
| 45 | value -> Component.translatableEscape("commands.locate.biome.not_found", value)
|
| 46 | );
|
| 47 | private static final DynamicCommandExceptionType ERROR_POI_NOT_FOUND = new DynamicCommandExceptionType(
|
| 48 | value -> Component.translatableEscape("commands.locate.poi.not_found", value)
|
| 49 | );
|
| 50 | private static final int MAX_STRUCTURE_SEARCH_RADIUS = 100;
|
| 51 | private static final int MAX_BIOME_SEARCH_RADIUS = 6400;
|
| 52 | private static final int BIOME_SAMPLE_RESOLUTION_HORIZONTAL = 32;
|
| 53 | private static final int BIOME_SAMPLE_RESOLUTION_VERTICAL = 64;
|
| 54 | private static final int POI_SEARCH_RADIUS = 256;
|
| 55 |
|
| 56 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
|
| 57 | dispatcher.register(
|
| 58 | Commands.literal("locate")
|
| 59 | .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
| 60 | .then(
|
| 61 | Commands.literal("structure")
|
| 62 | .then(
|
| 63 | Commands.argument("structure", ResourceOrTagKeyArgument.resourceOrTagKey(Registries.STRUCTURE))
|
| 64 | .executes(
|
| 65 | c -> locateStructure(c.getSource(), ResourceOrTagKeyArgument.getResourceOrTagKey(c, "structure", Registries.STRUCTURE, ERROR_STRUCTURE_INVALID))
|
| 66 | )
|
| 67 | )
|
| 68 | )
|
| 69 | .then(
|
| 70 | Commands.literal("biome")
|
| 71 | .then(
|
| 72 | Commands.argument("biome", ResourceOrTagArgument.resourceOrTag(context, Registries.BIOME))
|
| 73 | .executes(c -> locateBiome(c.getSource(), ResourceOrTagArgument.getResourceOrTag(c, "biome", Registries.BIOME)))
|
| 74 | )
|
| 75 | )
|
| 76 | .then(
|
| 77 | Commands.literal("poi")
|
| 78 | .then(
|
| 79 | Commands.argument("poi", ResourceOrTagArgument.resourceOrTag(context, Registries.POINT_OF_INTEREST_TYPE))
|
| 80 | .executes(c -> locatePoi(c.getSource(), ResourceOrTagArgument.getResourceOrTag(c, "poi", Registries.POINT_OF_INTEREST_TYPE)))
|
| 81 | )
|
| 82 | )
|
| 83 | );
|
| 84 | }
|
| 85 |
|
| 86 | private static Optional<? extends HolderSet.ListBacked<Structure>> getHolders(
|
| 87 | final ResourceOrTagKeyArgument.Result<Structure> resourceOrTag, final Registry<Structure> registry
|
| 88 | ) {
|
| 89 | return resourceOrTag.unwrap().map(id -> registry.get((ResourceKey<Structure>)id).map(xva$0 -> HolderSet.direct(xva$0)), registry::get);
|
| 90 | }
|
| 91 |
|
| 92 | private static int locateStructure(final CommandSourceStack source, final ResourceOrTagKeyArgument.Result<Structure> resourceOrTag) throws CommandSyntaxException {
|
| 93 | Registry<Structure> registry = source.getLevel().registryAccess().lookupOrThrow(Registries.STRUCTURE);
|
| 94 | HolderSet<Structure> target = (HolderSet<Structure>)getHolders(resourceOrTag, registry)
|
| 95 | .orElseThrow(() -> ERROR_STRUCTURE_INVALID.create(resourceOrTag.asPrintable()));
|
| 96 | BlockPos sourcePos = BlockPos.containing(source.getPosition());
|
| 97 | ServerLevel serverLevel = source.getLevel();
|
| 98 | Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
|
| 99 | Pair<BlockPos, Holder<Structure>> nearest = serverLevel.getChunkSource().getGenerator().findNearestMapStructure(serverLevel, target, sourcePos, 100, false);
|
| 100 | stopwatch.stop();
|
| 101 | if (nearest == null) {
|
| 102 | throw ERROR_STRUCTURE_NOT_FOUND.create(resourceOrTag.asPrintable());
|
| 103 | } else {
|
| 104 | return showLocateResult(source, resourceOrTag, sourcePos, nearest, "commands.locate.structure.success", false, stopwatch.elapsed());
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private static int locateBiome(final CommandSourceStack source, final ResourceOrTagArgument.Result<Biome> elementOrTag) throws CommandSyntaxException {
|
| 109 | BlockPos sourcePos = BlockPos.containing(source.getPosition());
|
| 110 | Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
|
| 111 | Pair<BlockPos, Holder<Biome>> nearest = source.getLevel().findClosestBiome3d(elementOrTag, sourcePos, 6400, 32, 64);
|
| 112 | stopwatch.stop();
|
| 113 | if (nearest == null) {
|
| 114 | throw ERROR_BIOME_NOT_FOUND.create(elementOrTag.asPrintable());
|
| 115 | } else {
|
| 116 | return showLocateResult(source, elementOrTag, sourcePos, nearest, "commands.locate.biome.success", true, stopwatch.elapsed());
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | private static int locatePoi(final CommandSourceStack source, final ResourceOrTagArgument.Result<PoiType> resourceOrTag) throws CommandSyntaxException {
|
| 121 | BlockPos sourcePos = BlockPos.containing(source.getPosition());
|
| 122 | ServerLevel serverLevel = source.getLevel();
|
| 123 | Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
|
| 124 | Optional<Pair<Holder<PoiType>, BlockPos>> closestWithType = serverLevel.getPoiManager()
|
| 125 | .findClosestWithType(resourceOrTag, sourcePos, 256, PoiManager.Occupancy.ANY);
|
| 126 | stopwatch.stop();
|
| 127 | if (closestWithType.isEmpty()) {
|
| 128 | throw ERROR_POI_NOT_FOUND.create(resourceOrTag.asPrintable());
|
| 129 | } else {
|
| 130 | return showLocateResult(source, resourceOrTag, sourcePos, closestWithType.get().swap(), "commands.locate.poi.success", false, stopwatch.elapsed());
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | public static int showLocateResult(
|
| 135 | final CommandSourceStack source,
|
| 136 | final ResourceOrTagArgument.Result<?> name,
|
| 137 | final BlockPos sourcePos,
|
| 138 | final Pair<BlockPos, ? extends Holder<?>> found,
|
| 139 | final String successMessageKey,
|
| 140 | final boolean includeY,
|
| 141 | final Duration taskDuration
|
| 142 | ) {
|
| 143 | String foundName = name.unwrap().map(element -> name.asPrintable(), tag -> name.asPrintable() + " (" + found.getSecond().getRegisteredName() + ")");
|
| 144 | return showLocateResult(source, sourcePos, found, successMessageKey, includeY, foundName, taskDuration);
|
| 145 | }
|
| 146 |
|
| 147 | public static int showLocateResult(
|
| 148 | final CommandSourceStack source,
|
| 149 | final ResourceOrTagKeyArgument.Result<?> name,
|
| 150 | final BlockPos sourcePos,
|
| 151 | final Pair<BlockPos, ? extends Holder<?>> found,
|
| 152 | final String successMessageKey,
|
| 153 | final boolean includeY,
|
| 154 | final Duration taskDuration
|
| 155 | ) {
|
| 156 | String foundName = name.unwrap()
|
| 157 | .map(element -> element.identifier().toString(), tag -> "#" + tag.location() + " (" + found.getSecond().getRegisteredName() + ")");
|
| 158 | return showLocateResult(source, sourcePos, found, successMessageKey, includeY, foundName, taskDuration);
|
| 159 | }
|
| 160 |
|
| 161 | private static int showLocateResult(
|
| 162 | final CommandSourceStack source,
|
| 163 | final BlockPos sourcePos,
|
| 164 | final Pair<BlockPos, ? extends Holder<?>> found,
|
| 165 | final String successMessageKey,
|
| 166 | final boolean includeY,
|
| 167 | final String foundName,
|
| 168 | final Duration taskDuration
|
| 169 | ) {
|
| 170 | BlockPos foundPos = found.getFirst();
|
| 171 | int distance = includeY
|
| 172 | ? Mth.floor(Mth.sqrt((float)sourcePos.distSqr(foundPos)))
|
| 173 | : Mth.floor(dist(sourcePos.getX(), sourcePos.getZ(), foundPos.getX(), foundPos.getZ()));
|
| 174 | String displayedY = includeY ? String.valueOf(foundPos.getY()) : "~";
|
| 175 | Component coordinates = ComponentUtils.wrapInSquareBrackets(Component.translatable("chat.coordinates", foundPos.getX(), displayedY, foundPos.getZ()))
|
| 176 | .withStyle(
|
| 177 | s -> s.withColor(ChatFormatting.GREEN)
|
| 178 | .withClickEvent(new ClickEvent.SuggestCommand("/tp @s " + foundPos.getX() + " " + displayedY + " " + foundPos.getZ()))
|
| 179 | .withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.coordinates.tooltip")))
|
| 180 | );
|
| 181 | source.sendSuccess(() -> Component.translatable(successMessageKey, foundName, coordinates, distance), false);
|
| 182 | LOGGER.info("Locating element {} took {} ms", foundName, taskDuration.toMillis());
|
| 183 | return distance;
|
| 184 | }
|
| 185 |
|
| 186 | private static float dist(final int x1, final int z1, final int x2, final int z2) {
|
| 187 | int dx = x2 - x1;
|
| 188 | int dz = z2 - z1;
|
| 189 | return Mth.sqrt(dx * dx + dz * dz);
|
| 190 | }
|
| 191 | }
|
| 192 |
|