|
| 1 | +package com.hlysine.create_power_loader.command; |
| 2 | + |
| 3 | +import com.hlysine.create_power_loader.content.*; |
| 4 | +import com.hlysine.create_power_loader.content.trains.CarriageChunkLoader; |
| 5 | +import com.hlysine.create_power_loader.content.trains.StationChunkLoader; |
| 6 | +import com.hlysine.create_power_loader.content.trains.TrainChunkLoader; |
| 7 | +import com.mojang.brigadier.Command; |
| 8 | +import com.mojang.brigadier.builder.ArgumentBuilder; |
| 9 | +import com.simibubi.create.foundation.utility.Components; |
| 10 | +import net.minecraft.ChatFormatting; |
| 11 | +import net.minecraft.commands.CommandSourceStack; |
| 12 | +import net.minecraft.commands.Commands; |
| 13 | +import net.minecraft.commands.arguments.DimensionArgument; |
| 14 | +import net.minecraft.network.chat.Component; |
| 15 | +import net.minecraft.network.chat.MutableComponent; |
| 16 | +import net.minecraft.resources.ResourceLocation; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.LinkedList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.BiConsumer; |
| 24 | +import java.util.function.Consumer; |
| 25 | + |
| 26 | +public class SummaryCommand { |
| 27 | + |
| 28 | + public static ArgumentBuilder<CommandSourceStack, ?> register() { |
| 29 | + return Commands.literal("summary") |
| 30 | + .requires(cs -> cs.hasPermission(2)) |
| 31 | + .then(Commands.argument("dimension", DimensionArgument.dimension()) |
| 32 | + .executes(ctx -> { |
| 33 | + CommandSourceStack source = ctx.getSource(); |
| 34 | + fillReport(ctx.getArgument("dimension", ResourceLocation.class), |
| 35 | + (s, f) -> source.sendSuccess(() -> Components.literal(s).withStyle(st -> st.withColor(f)), false), |
| 36 | + (c) -> source.sendSuccess(() -> c, false)); |
| 37 | + return Command.SINGLE_SUCCESS; |
| 38 | + }) |
| 39 | + ) |
| 40 | + .executes(ctx -> { |
| 41 | + CommandSourceStack source = ctx.getSource(); |
| 42 | + fillReport(null, |
| 43 | + (s, f) -> source.sendSuccess(() -> Components.literal(s).withStyle(st -> st.withColor(f)), false), |
| 44 | + (c) -> source.sendSuccess(() -> c, false)); |
| 45 | + return Command.SINGLE_SUCCESS; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + private static int white = ChatFormatting.WHITE.getColor(); |
| 50 | + private static int gray = ChatFormatting.GRAY.getColor(); |
| 51 | + private static int blue = 0xD3DEDC; |
| 52 | + |
| 53 | + private static void fillReport(@Nullable ResourceLocation dimension, |
| 54 | + BiConsumer<String, Integer> chat, |
| 55 | + Consumer<Component> chatRaw) { |
| 56 | + List<ChunkLoader> loaders = new LinkedList<>(); |
| 57 | + for (WeakCollection<ChunkLoader> list : ChunkLoadManager.allLoaders.values()) { |
| 58 | + loaders.addAll(list); |
| 59 | + } |
| 60 | + if (dimension != null) { |
| 61 | + loaders.removeIf(loader -> !loader.getLocation().getFirst().equals(dimension)); |
| 62 | + } |
| 63 | + |
| 64 | + chat.accept("", white); |
| 65 | + chat.accept("-+------<< Chunk Loader Summary >>------+-", white); |
| 66 | + if (dimension != null) |
| 67 | + chat.accept("For " + dimension, gray); |
| 68 | + genSummary(null, loaders, chat, chatRaw); |
| 69 | + chat.accept("", white); |
| 70 | + chat.accept("-+------------------------------------+-", white); |
| 71 | + } |
| 72 | + |
| 73 | + private static void genSummary(@Nullable LoaderMode mode, |
| 74 | + List<ChunkLoader> loaders, |
| 75 | + BiConsumer<String, Integer> chat, |
| 76 | + Consumer<Component> chatRaw) { |
| 77 | + if (mode == LoaderMode.STATIC || mode == null) { |
| 78 | + chat.accept("", white); |
| 79 | + chat.accept("Static chunk loaders", white); |
| 80 | + int staticLoaders = 0; |
| 81 | + int functional = 0; |
| 82 | + int brass = 0; |
| 83 | + int andesite = 0; |
| 84 | + Set<ChunkLoadManager.LoadedChunkPos> chunks = new HashSet<>(); |
| 85 | + for (ChunkLoader loader : loaders) { |
| 86 | + if (loader instanceof AbstractChunkLoaderBlockEntity be) { |
| 87 | + staticLoaders++; |
| 88 | + if (be.getForcedChunks().size() > 0) functional++; |
| 89 | + if (be.type == LoaderType.BRASS) brass++; |
| 90 | + else if (be.type == LoaderType.ANDESITE) andesite++; |
| 91 | + chunks.addAll(be.getForcedChunks()); |
| 92 | + } |
| 93 | + } |
| 94 | + chatRaw.accept(line("Total blocks", staticLoaders)); |
| 95 | + chatRaw.accept(line("Functional blocks", functional)); |
| 96 | + chatRaw.accept(line("Brass loaders", brass)); |
| 97 | + chatRaw.accept(line("Andesite loaders", andesite)); |
| 98 | + chatRaw.accept(line("Loaded chunks", chunks.size())); |
| 99 | + } |
| 100 | + if (mode == LoaderMode.CONTRAPTION || mode == null) { |
| 101 | + chat.accept("", white); |
| 102 | + chat.accept("Contraption chunk loaders", white); |
| 103 | + int contraptions = 0; |
| 104 | + int nonTrain = 0; |
| 105 | + Set<ChunkLoadManager.LoadedChunkPos> chunks = new HashSet<>(); |
| 106 | + for (ChunkLoader loader : loaders) { |
| 107 | + if (loader instanceof ChunkLoaderMovementBehaviour.SavedState state) { |
| 108 | + contraptions++; |
| 109 | + if (!state.isTrain) nonTrain++; |
| 110 | + chunks.addAll(state.forcedChunks); |
| 111 | + } |
| 112 | + } |
| 113 | + chatRaw.accept(line("Total contraptions", contraptions)); |
| 114 | + chatRaw.accept(line("Non-train contraptions", nonTrain)); |
| 115 | + chatRaw.accept(line("Loaded chunks", chunks.size())); |
| 116 | + } |
| 117 | + if (mode == LoaderMode.TRAIN || mode == null) { |
| 118 | + chat.accept("", white); |
| 119 | + chat.accept("Train chunk loaders", white); |
| 120 | + int trains = 0; |
| 121 | + int carriages = 0; |
| 122 | + int functionalTrains = 0; |
| 123 | + int functionalCarriages = 0; |
| 124 | + int unknown = 0; |
| 125 | + int brassAndAndesite = 0; |
| 126 | + int brass = 0; |
| 127 | + int andesite = 0; |
| 128 | + Set<ChunkLoadManager.LoadedChunkPos> chunks = new HashSet<>(); |
| 129 | + for (ChunkLoader loader : loaders) { |
| 130 | + if (loader instanceof TrainChunkLoader train) { |
| 131 | + trains++; |
| 132 | + carriages += train.carriageLoaders.size(); |
| 133 | + if (train.getForcedChunks().size() > 0) functionalTrains++; |
| 134 | + for (CarriageChunkLoader carriage : train.carriageLoaders) { |
| 135 | + if (carriage.getForcedChunks().size() > 0) functionalCarriages++; |
| 136 | + if (!carriage.known) unknown++; |
| 137 | + else if (carriage.brass && carriage.andesite) brassAndAndesite++; |
| 138 | + else if (carriage.brass) brass++; |
| 139 | + else if (carriage.andesite) andesite++; |
| 140 | + chunks.addAll(carriage.getForcedChunks()); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + chatRaw.accept(line("Total trains", trains)); |
| 145 | + chatRaw.accept(line("Total carriages", carriages)); |
| 146 | + chatRaw.accept(line("Functional trains", functionalTrains)); |
| 147 | + chatRaw.accept(line("Functional carriages", functionalCarriages)); |
| 148 | + chatRaw.accept(line("Unknown carriages", unknown)); |
| 149 | + chatRaw.accept(line("Brass+Andesite carriages", brassAndAndesite)); |
| 150 | + chatRaw.accept(line("Brass carriages", brass)); |
| 151 | + chatRaw.accept(line("Andesite carriages", andesite)); |
| 152 | + chatRaw.accept(line("Loaded chunks", chunks.size())); |
| 153 | + } |
| 154 | + if (mode == LoaderMode.STATION || mode == null) { |
| 155 | + chat.accept("", white); |
| 156 | + chat.accept("Station chunk loaders", white); |
| 157 | + int stations = 0; |
| 158 | + int attachments = 0; |
| 159 | + int activeStations = 0; |
| 160 | + int activeAttachments = 0; |
| 161 | + int brass = 0; |
| 162 | + int andesite = 0; |
| 163 | + Set<ChunkLoadManager.LoadedChunkPos> chunks = new HashSet<>(); |
| 164 | + for (ChunkLoader loader : loaders) { |
| 165 | + if (loader instanceof StationChunkLoader station) { |
| 166 | + stations++; |
| 167 | + attachments += station.attachments.size(); |
| 168 | + if (station.getForcedChunks().size() > 0) { |
| 169 | + activeStations++; |
| 170 | + activeAttachments += station.attachments.size(); |
| 171 | + } |
| 172 | + for (StationChunkLoader.AttachedLoader attachment : station.attachments) { |
| 173 | + if (attachment.type() == LoaderType.ANDESITE) andesite++; |
| 174 | + else if (attachment.type() == LoaderType.BRASS) brass++; |
| 175 | + } |
| 176 | + chunks.addAll(station.getForcedChunks()); |
| 177 | + } |
| 178 | + } |
| 179 | + chatRaw.accept(line("Total stations", stations)); |
| 180 | + chatRaw.accept(line("Total attachments", attachments)); |
| 181 | + chatRaw.accept(line("Active stations", activeStations)); |
| 182 | + chatRaw.accept(line("Active attachments", activeAttachments)); |
| 183 | + chatRaw.accept(line("Brass attachments", brass)); |
| 184 | + chatRaw.accept(line("Andesite attachments", andesite)); |
| 185 | + chatRaw.accept(line("Loaded chunks", chunks.size())); |
| 186 | + } |
| 187 | + if (mode == null) { |
| 188 | + chat.accept("", white); |
| 189 | + chat.accept("All chunk loaders", white); |
| 190 | + chatRaw.accept(line("Total units", loaders.size())); |
| 191 | + Set<ChunkLoadManager.LoadedChunkPos> allChunks = new HashSet<>(); |
| 192 | + loaders.forEach(loader -> allChunks.addAll(loader.getForcedChunks())); |
| 193 | + chatRaw.accept(line("Total chunks", allChunks.size())); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private static MutableComponent text(String text, int color) { |
| 198 | + return Components.literal(text).withStyle(style -> style.withColor(color)); |
| 199 | + } |
| 200 | + |
| 201 | + private static MutableComponent line(String label, Object value) { |
| 202 | + return text(" ", gray).append(text(label + ": ", gray)).append(text(String.valueOf(value), blue)); |
| 203 | + } |
| 204 | +} |
0 commit comments