From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Wed, 29 Jan 2025 00:54:19 +0300 Subject: [PATCH] Async locate command diff --git a/net/minecraft/server/commands/LocateCommand.java b/net/minecraft/server/commands/LocateCommand.java index 13bcd8653d766cd0b754a22e9aab261fbc62b0a5..0bc2d17e9bad62f62ab171feb2a92b87e1c3ba6a 100644 --- a/net/minecraft/server/commands/LocateCommand.java +++ b/net/minecraft/server/commands/LocateCommand.java @@ -103,44 +103,77 @@ public class LocateCommand { } private static int locateStructure(CommandSourceStack source, ResourceOrTagKeyArgument.Result structure) throws CommandSyntaxException { - Registry registry = source.getLevel().registryAccess().lookupOrThrow(Registries.STRUCTURE); - HolderSet holderSet = (HolderSet)getHolders(structure, registry) - .orElseThrow(() -> ERROR_STRUCTURE_INVALID.create(structure.asPrintable())); - BlockPos blockPos = BlockPos.containing(source.getPosition()); - ServerLevel level = source.getLevel(); - Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER); - Pair> pair = level.getChunkSource().getGenerator().findNearestMapStructure(level, holderSet, blockPos, 100, false); - stopwatch.stop(); - if (pair == null) { - throw ERROR_STRUCTURE_NOT_FOUND.create(structure.asPrintable()); - } else { - return showLocateResult(source, structure, blockPos, pair, "commands.locate.structure.success", false, stopwatch.elapsed()); - } + // DivineMC start - Async structure locate + io.papermc.paper.util.MCUtil.scheduleAsyncTask(() -> { + Registry registry = source.getLevel().registryAccess().lookupOrThrow(Registries.STRUCTURE); + HolderSet holderSet; + try { + holderSet = getHolders(structure, registry) + .orElseThrow(() -> ERROR_STRUCTURE_INVALID.create(structure.asPrintable())); + } catch (CommandSyntaxException e) { + source.sendFailure(Component.literal(e.getMessage())); + return; + } + BlockPos blockPos = BlockPos.containing(source.getPosition()); + ServerLevel level = source.getLevel(); + Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER); + Pair> pair = level.getChunkSource().getGenerator().findNearestMapStructure(level, holderSet, blockPos, 100, false); + stopwatch.stop(); + if (pair == null) { + try { + throw ERROR_STRUCTURE_NOT_FOUND.create(structure.asPrintable()); + } catch (CommandSyntaxException e) { + source.sendFailure(Component.literal(e.getMessage())); + } + } else { + showLocateResult(source, structure, blockPos, pair, "commands.locate.structure.success", false, stopwatch.elapsed()); + } + }); + return 0; + // DivineMC end - Async structure locate } private static int locateBiome(CommandSourceStack source, ResourceOrTagArgument.Result biome) throws CommandSyntaxException { - BlockPos blockPos = BlockPos.containing(source.getPosition()); - Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER); - Pair> pair = source.getLevel().findClosestBiome3d(biome, blockPos, 6400, 32, 64); - stopwatch.stop(); - if (pair == null) { - throw ERROR_BIOME_NOT_FOUND.create(biome.asPrintable()); - } else { - return showLocateResult(source, biome, blockPos, pair, "commands.locate.biome.success", true, stopwatch.elapsed()); - } + // DivineMC start - Async biome locate + io.papermc.paper.util.MCUtil.scheduleAsyncTask(() -> { + BlockPos blockPos = BlockPos.containing(source.getPosition()); + Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER); + Pair> pair = source.getLevel().findClosestBiome3d(biome, blockPos, 6400, 32, 64); + stopwatch.stop(); + if (pair == null) { + try { + throw ERROR_BIOME_NOT_FOUND.create(biome.asPrintable()); + } catch (CommandSyntaxException e) { + source.sendFailure(Component.literal(e.getMessage())); + } + } else { + showLocateResult(source, biome, blockPos, pair, "commands.locate.biome.success", true, stopwatch.elapsed()); + } + }); + return 0; + // DivineMC end - Async biome locate } private static int locatePoi(CommandSourceStack source, ResourceOrTagArgument.Result poiType) throws CommandSyntaxException { - BlockPos blockPos = BlockPos.containing(source.getPosition()); - ServerLevel level = source.getLevel(); - Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER); - Optional, BlockPos>> optional = level.getPoiManager().findClosestWithType(poiType, blockPos, 256, PoiManager.Occupancy.ANY); - stopwatch.stop(); - if (optional.isEmpty()) { - throw ERROR_POI_NOT_FOUND.create(poiType.asPrintable()); - } else { - return showLocateResult(source, poiType, blockPos, optional.get().swap(), "commands.locate.poi.success", false, stopwatch.elapsed()); - } + // DivineMC start - Async poi locate + io.papermc.paper.util.MCUtil.scheduleAsyncTask(() -> { + BlockPos blockPos = BlockPos.containing(source.getPosition()); + ServerLevel level = source.getLevel(); + Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER); + Optional, BlockPos>> optional = level.getPoiManager().findClosestWithType(poiType, blockPos, 256, PoiManager.Occupancy.ANY); + stopwatch.stop(); + if (optional.isEmpty()) { + try { + throw ERROR_POI_NOT_FOUND.create(poiType.asPrintable()); + } catch (CommandSyntaxException e) { + source.sendFailure(Component.literal(e.getMessage())); + } + } else { + showLocateResult(source, poiType, blockPos, optional.get().swap(), "commands.locate.poi.success", false, stopwatch.elapsed()); + } + }); + return 0; + // DivineMC end - Async poi locate } public static int showLocateResult( @@ -195,7 +228,7 @@ public class LocateCommand { .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.translatable("chat.coordinates.tooltip"))) ); source.sendSuccess(() -> Component.translatable(translationKey, elementName, component, i), false); - LOGGER.info("Locating element " + elementName + " took " + duration.toMillis() + " ms"); + LOGGER.info("Locating element {} on Thread:{} took {} ms", elementName, Thread.currentThread().getName(), duration.toMillis()); // DivineMC - Log thread name return i; }