|
| 1 | +package com.hlysine.create_power_loader.command; |
| 2 | + |
| 3 | +import com.hlysine.create_power_loader.content.ChunkLoadManager; |
| 4 | +import com.hlysine.create_power_loader.content.ChunkLoader; |
| 5 | +import com.hlysine.create_power_loader.content.LoaderMode; |
| 6 | +import com.hlysine.create_power_loader.content.WeakCollection; |
| 7 | +import com.hlysine.create_power_loader.content.trains.CarriageChunkLoader; |
| 8 | +import com.hlysine.create_power_loader.content.trains.StationChunkLoader; |
| 9 | +import com.hlysine.create_power_loader.content.trains.TrainChunkLoader; |
| 10 | +import com.mojang.brigadier.Command; |
| 11 | +import com.mojang.brigadier.arguments.IntegerArgumentType; |
| 12 | +import com.mojang.brigadier.builder.ArgumentBuilder; |
| 13 | +import com.simibubi.create.foundation.utility.Components; |
| 14 | +import com.simibubi.create.foundation.utility.Pair; |
| 15 | +import net.minecraft.ChatFormatting; |
| 16 | +import net.minecraft.commands.CommandSourceStack; |
| 17 | +import net.minecraft.commands.Commands; |
| 18 | +import net.minecraft.core.BlockPos; |
| 19 | +import net.minecraft.core.registries.Registries; |
| 20 | +import net.minecraft.network.chat.ClickEvent; |
| 21 | +import net.minecraft.network.chat.Component; |
| 22 | +import net.minecraft.network.chat.HoverEvent; |
| 23 | +import net.minecraft.resources.ResourceKey; |
| 24 | +import net.minecraft.resources.ResourceLocation; |
| 25 | +import net.minecraft.server.MinecraftServer; |
| 26 | +import net.minecraft.server.level.ServerLevel; |
| 27 | +import net.minecraft.world.level.dimension.DimensionType; |
| 28 | +import net.minecraft.world.phys.Vec3; |
| 29 | +import net.minecraftforge.server.command.EnumArgument; |
| 30 | +import org.jetbrains.annotations.Nullable; |
| 31 | + |
| 32 | +import java.util.*; |
| 33 | +import java.util.function.BiConsumer; |
| 34 | +import java.util.function.Consumer; |
| 35 | +import java.util.function.Function; |
| 36 | + |
| 37 | +public class ListLoadersCommand { |
| 38 | + |
| 39 | + public static ArgumentBuilder<CommandSourceStack, ?> register() { |
| 40 | + return Commands.literal("list") |
| 41 | + .requires(cs -> cs.hasPermission(2)) |
| 42 | + .then( |
| 43 | + Commands.argument("type", EnumArgument.enumArgument(LoaderMode.class)) |
| 44 | + .then( |
| 45 | + Commands.literal("limit") |
| 46 | + .then( |
| 47 | + Commands.argument("limit", IntegerArgumentType.integer(1)) |
| 48 | + .executes(handler(true, true)) |
| 49 | + ) |
| 50 | + ) |
| 51 | + .executes(handler(true, false)) |
| 52 | + ) |
| 53 | + .then( |
| 54 | + Commands.literal("limit") |
| 55 | + .then( |
| 56 | + Commands.argument("limit", IntegerArgumentType.integer(1)) |
| 57 | + .executes(handler(false, true)) |
| 58 | + ) |
| 59 | + ) |
| 60 | + .executes(handler(false, false)); |
| 61 | + } |
| 62 | + |
| 63 | + private static Command<CommandSourceStack> handler(boolean hasMode, boolean hasLimit) { |
| 64 | + return ctx -> { |
| 65 | + CommandSourceStack source = ctx.getSource(); |
| 66 | + fillReport(source.getLevel(), source.getPosition(), |
| 67 | + hasMode ? ctx.getArgument("type", LoaderMode.class) : null, |
| 68 | + hasLimit ? ctx.getArgument("limit", Integer.class) : Integer.MAX_VALUE, |
| 69 | + (s, f) -> source.sendSuccess(() -> Components.literal(s).withStyle(st -> st.withColor(f)), false), |
| 70 | + (c) -> source.sendSuccess(() -> c, false)); |
| 71 | + return Command.SINGLE_SUCCESS; |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + private static void fillReport(ServerLevel level, Vec3 location, @Nullable LoaderMode mode, int limit, BiConsumer<String, Integer> chat, |
| 76 | + Consumer<Component> chatRaw) { |
| 77 | + int white = ChatFormatting.WHITE.getColor(); |
| 78 | + int blue = 0xD3DEDC; |
| 79 | + int bright = 0xFFEFEF; |
| 80 | + int orange = 0xFFAD60; |
| 81 | + |
| 82 | + List<ChunkLoader> loaders = new LinkedList<>(); |
| 83 | + if (mode == null) { |
| 84 | + for (WeakCollection<ChunkLoader> list : ChunkLoadManager.allLoaders.values()) { |
| 85 | + loaders.addAll(list); |
| 86 | + } |
| 87 | + } else { |
| 88 | + loaders.addAll(ChunkLoadManager.allLoaders.get(mode)); |
| 89 | + } |
| 90 | + loaders.removeIf(loader -> loader.getForcedChunks().size() == 0); |
| 91 | + |
| 92 | + Map<ResourceLocation, DimensionType> typeCache = new HashMap<>(); |
| 93 | + MinecraftServer server = level.getServer(); |
| 94 | + Function<ResourceLocation, DimensionType> computeType = key -> server.getLevel(ResourceKey.create(Registries.DIMENSION, key)).dimensionType(); |
| 95 | + List<Pair<ChunkLoader, Pair<ResourceLocation, Vec3>>> pairs = loaders.stream() |
| 96 | + .map(loader -> Pair.of(loader, loader.getLocation())) |
| 97 | + .map(pair -> Pair.of(pair.getFirst(), Pair.of(pair.getSecond().getFirst(), Vec3.atCenterOf(pair.getSecond().getSecond())))) |
| 98 | + .sorted(Comparator |
| 99 | + .<Pair<ChunkLoader, Pair<ResourceLocation, Vec3>>>comparingInt(p -> p.getSecond().getFirst().equals(level.dimension().location()) ? 0 : 1) |
| 100 | + .thenComparingDouble(p -> p.getSecond().getSecond() |
| 101 | + .scale(DimensionType.getTeleportationScale(typeCache.computeIfAbsent(p.getSecond().getFirst(), computeType), level.dimensionType())) |
| 102 | + .distanceToSqr(location)) |
| 103 | + ) |
| 104 | + .limit(limit) |
| 105 | + .toList(); |
| 106 | + |
| 107 | + chat.accept("", white); |
| 108 | + chat.accept("-+------<< Chunk Loader List >>------+-", white); |
| 109 | + chat.accept(pairs.size() + " out of " + loaders.size() + " nearest" + (mode != null ? " " + mode.getSerializedName() : "") + " loaders", blue); |
| 110 | + for (Pair<ChunkLoader, Pair<ResourceLocation, Vec3>> pair : pairs) { |
| 111 | + ChunkLoader loader = pair.getFirst(); |
| 112 | + ResourceLocation dimension = pair.getSecond().getFirst(); |
| 113 | + BlockPos pos = BlockPos.containing(pair.getSecond().getSecond()); |
| 114 | + |
| 115 | + chatRaw.accept(createTpButton(dimension, pos, |
| 116 | + (mode == null ? " " + loader.getLoaderMode().getSerializedName() + " " : "") |
| 117 | + + "[" + pos.toShortString() + "]" |
| 118 | + + (!dimension.equals(level.dimension().location()) ? " in " + dimension : "") |
| 119 | + , white)); |
| 120 | + |
| 121 | + chat.accept( |
| 122 | + " " |
| 123 | + + loader.getLoaderType().getSerializedName() + " - " |
| 124 | + + loader.getForcedChunks().size() + " chunks" |
| 125 | + , orange); |
| 126 | + if (loader instanceof TrainChunkLoader trainLoader) { |
| 127 | + for (int i = 0; i < trainLoader.carriageLoaders.size(); i++) { |
| 128 | + CarriageChunkLoader carriageLoader = trainLoader.carriageLoaders.get(i); |
| 129 | + if (carriageLoader.getForcedChunks().isEmpty()) continue; |
| 130 | + Pair<ResourceLocation, BlockPos> carriageLocation = carriageLoader.getLocation(); |
| 131 | + chatRaw.accept(createTpButton(carriageLocation.getFirst(), carriageLocation.getSecond(), |
| 132 | + " Carriage " + (i + 1) + " - " |
| 133 | + + "[" + carriageLocation.getSecond().toShortString() + "]" |
| 134 | + + (!carriageLocation.getFirst().equals(level.dimension().location()) ? " in " + carriageLocation.getFirst().toString() : "") |
| 135 | + , blue)); |
| 136 | + chat.accept( |
| 137 | + " " |
| 138 | + + carriageLoader.getLoaderType().getSerializedName() + " - " |
| 139 | + + carriageLoader.getForcedChunks().size() + " chunks" |
| 140 | + , orange); |
| 141 | + } |
| 142 | + } else if (loader instanceof StationChunkLoader stationLoader) { |
| 143 | + for (StationChunkLoader.AttachedLoader attachment : stationLoader.attachments) { |
| 144 | + chatRaw.accept(createTpButton(stationLoader.getLocation().getFirst(), attachment.pos(), |
| 145 | + " " |
| 146 | + + attachment.type().getSerializedName() |
| 147 | + + " - " |
| 148 | + + "[" + attachment.pos().toShortString() + "]" |
| 149 | + , blue)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + chat.accept("-+--------------------------------+-", white); |
| 154 | + } |
| 155 | + |
| 156 | + private static Component createTpButton(ResourceLocation dimension, BlockPos blockPos, String text, int color) { |
| 157 | + String teleport = "/execute in " + dimension.toString() + " run tp @s " + blockPos.getX() + " " + blockPos.getY() + " " + blockPos.getZ(); |
| 158 | + return Components.literal(text).withStyle((p_180514_) -> { |
| 159 | + return p_180514_.withColor(color) |
| 160 | + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, teleport)) |
| 161 | + .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, |
| 162 | + Components.literal("Click to teleport"))) |
| 163 | + .withInsertion(teleport); |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments