|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using MinecraftClient.Mapping; |
| 6 | + |
| 7 | +namespace MinecraftClient.Commands |
| 8 | +{ |
| 9 | + public class Chunk : Command |
| 10 | + { |
| 11 | + public override string CmdName { get { return "chunk"; } } |
| 12 | + public override string CmdUsage { get { return "chunk status [chunkX chunkZ|locationX locationY locationZ]"; } } |
| 13 | + public override string CmdDesc { get { return "cmd.chunk.desc"; } } |
| 14 | + |
| 15 | + public override string Run(McClient handler, string command, Dictionary<string, object>? localVars) |
| 16 | + { |
| 17 | + if (hasArg(command)) |
| 18 | + { |
| 19 | + string[] args = getArgs(command); |
| 20 | + if (args.Length > 0) |
| 21 | + { |
| 22 | + if (args[0] == "status") |
| 23 | + { |
| 24 | + World world = handler.GetWorld(); |
| 25 | + Location current = handler.GetCurrentLocation(); |
| 26 | + |
| 27 | + Tuple<int, int>? markedChunkPos = ParseChunkPos(args); |
| 28 | + (int markChunkX, int markChunkZ) = markedChunkPos ?? (new(current.ChunkX, current.ChunkZ)); |
| 29 | + |
| 30 | + StringBuilder sb = new(); |
| 31 | + |
| 32 | + sb.Append(getChunkLoadingStatus(handler.GetWorld())); |
| 33 | + sb.Append('\n'); |
| 34 | + |
| 35 | + sb.Append(String.Format("Current location:{0}, chunk: ({1}, {2}).\n", current, current.ChunkX, current.ChunkZ)); |
| 36 | + if (markedChunkPos != null) |
| 37 | + { |
| 38 | + sb.Append("Marked location: "); |
| 39 | + if (args.Length == 1 + 3) |
| 40 | + sb.Append(String.Format("X:{0:0.00} Y:{1:0.00} Z:{2:0.00}, ", double.Parse(args[1]), double.Parse(args[2]), double.Parse(args[3]))); |
| 41 | + sb.Append(String.Format("chunk: ({0}, {1}).\n", markChunkX, markChunkZ)); |
| 42 | + } |
| 43 | + |
| 44 | + int consoleHeight = Math.Max(Console.BufferHeight - 2, 25); |
| 45 | + if (consoleHeight % 2 == 0) |
| 46 | + --consoleHeight; |
| 47 | + |
| 48 | + int consoleWidth = Math.Max(Console.BufferWidth / 2, 17); |
| 49 | + if (consoleWidth % 2 == 0) |
| 50 | + --consoleWidth; |
| 51 | + |
| 52 | + int startZ = current.ChunkZ - consoleHeight, endZ = current.ChunkZ + consoleHeight; |
| 53 | + int startX = current.ChunkX - consoleWidth, endX = current.ChunkX + consoleWidth; |
| 54 | + |
| 55 | + int leftMost = endX, rightMost = startX, topMost = endZ, bottomMost = startZ; |
| 56 | + for (int z = startZ; z <= endZ; z++) |
| 57 | + { |
| 58 | + for (int x = startX; x <= endX; ++x) |
| 59 | + { |
| 60 | + if (world[x, z] != null) |
| 61 | + { |
| 62 | + leftMost = Math.Min(leftMost, x); |
| 63 | + rightMost = Math.Max(rightMost, x); |
| 64 | + topMost = Math.Min(topMost, z); |
| 65 | + bottomMost = Math.Max(bottomMost, z); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Include the player's location |
| 71 | + topMost = Math.Min(topMost, current.ChunkZ); |
| 72 | + bottomMost = Math.Max(bottomMost, current.ChunkZ); |
| 73 | + leftMost = Math.Min(leftMost, current.ChunkX); |
| 74 | + rightMost = Math.Max(rightMost, current.ChunkX); |
| 75 | + |
| 76 | + // Empty one row and one column each |
| 77 | + --leftMost; ++rightMost; --topMost; ++bottomMost; |
| 78 | + |
| 79 | + // Resize according to limitations |
| 80 | + if ((bottomMost - topMost + 1) > consoleHeight) |
| 81 | + { |
| 82 | + int delta = (bottomMost - topMost + 1) - consoleHeight; |
| 83 | + if (bottomMost - (delta + 1) / 2 < current.ChunkZ + 1) |
| 84 | + { |
| 85 | + int bottomReduce = bottomMost - (current.ChunkZ + 1); |
| 86 | + bottomMost -= bottomReduce; |
| 87 | + topMost += delta - bottomReduce; |
| 88 | + } |
| 89 | + else if (topMost + delta / 2 > current.ChunkZ - 1) |
| 90 | + { |
| 91 | + int topAdd = topMost - (current.ChunkZ - 1); |
| 92 | + topMost += topAdd; |
| 93 | + bottomMost -= delta - topAdd; |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + topMost += delta / 2; |
| 98 | + bottomMost -= (delta + 1) / 2; |
| 99 | + } |
| 100 | + } |
| 101 | + if ((rightMost - leftMost + 1) > consoleWidth) |
| 102 | + { |
| 103 | + int delta = (rightMost - leftMost + 1) - consoleWidth; |
| 104 | + if (rightMost - (delta + 1) / 2 < current.ChunkX + 1) |
| 105 | + { |
| 106 | + int rightReduce = rightMost - (current.ChunkX + 1); |
| 107 | + rightMost -= rightReduce; |
| 108 | + leftMost += delta - rightReduce; |
| 109 | + } |
| 110 | + else if (leftMost + delta / 2 > current.ChunkX - 1) |
| 111 | + { |
| 112 | + int leftAdd = leftMost - (current.ChunkX - 1); |
| 113 | + leftMost += leftAdd; |
| 114 | + rightMost -= delta - leftAdd; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + leftMost += delta / 2; |
| 119 | + rightMost -= (delta + 1) / 2; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Try to include the marker chunk |
| 124 | + if (markedChunkPos != null && |
| 125 | + (((Math.Max(bottomMost, markChunkZ) - Math.Min(topMost, markChunkZ) + 1) > consoleHeight) || |
| 126 | + ((Math.Max(rightMost, markChunkX) - Math.Min(leftMost, markChunkX) + 1) > consoleWidth))) |
| 127 | + sb.Append("§x§0Since the marked chunk is outside the graph, it will not be displayed!§r\n"); |
| 128 | + else |
| 129 | + { |
| 130 | + topMost = Math.Min(topMost, markChunkZ); |
| 131 | + bottomMost = Math.Max(bottomMost, markChunkZ); |
| 132 | + leftMost = Math.Min(leftMost, markChunkX); |
| 133 | + rightMost = Math.Max(rightMost, markChunkX); |
| 134 | + } |
| 135 | + |
| 136 | + // Output |
| 137 | + for (int z = topMost; z <= bottomMost; ++z) |
| 138 | + { |
| 139 | + for (int x = leftMost; x <= rightMost; ++x) |
| 140 | + { |
| 141 | + if (z == current.ChunkZ && x == current.ChunkX) |
| 142 | + sb.Append("§z"); // Player Location: background gray |
| 143 | + else if (z == markChunkZ && x == markChunkX) |
| 144 | + sb.Append("§w"); // Marked chunk: background red |
| 145 | + |
| 146 | + ChunkColumn? chunkColumn = world[x, z]; |
| 147 | + if (chunkColumn == null) |
| 148 | + sb.Append("\ud83d\udd33"); // "🔳" white hollow square |
| 149 | + else if (chunkColumn.FullyLoaded) |
| 150 | + sb.Append("\ud83d\udfe9"); // "🟩" green |
| 151 | + else |
| 152 | + sb.Append("\ud83d\udfe8"); // "🟨" yellow |
| 153 | + |
| 154 | + if ((z == current.ChunkZ && x == current.ChunkX) || (z == markChunkZ && x == markChunkX)) |
| 155 | + sb.Append("§r"); // Reset background color |
| 156 | + } |
| 157 | + sb.Append('\n'); |
| 158 | + } |
| 159 | + |
| 160 | + sb.Append("Player:§z §r, MarkedChunk:§w §r, NotReceived:\ud83d\udd33, Loading:\ud83d\udfe8, Loaded:\ud83d\udfe9"); |
| 161 | + return sb.ToString(); |
| 162 | + } |
| 163 | + else if (args[0] == "setloading") // For debugging |
| 164 | + { |
| 165 | + Tuple<int, int>? chunkPos = ParseChunkPos(args); |
| 166 | + if (chunkPos != null) |
| 167 | + { |
| 168 | + handler.Log.Info("§x§0This command is used for debugging, make sure you know what you are doing.§r"); |
| 169 | + World world = handler.GetWorld(); |
| 170 | + (int chunkX, int chunkZ) = chunkPos; |
| 171 | + ChunkColumn? chunkColumn = world[chunkX, chunkZ]; |
| 172 | + if (chunkColumn != null) |
| 173 | + chunkColumn.FullyLoaded = false; |
| 174 | + return (chunkColumn == null) ? "Fail: chunk dosen't exist!" : |
| 175 | + String.Format("Successfully marked chunk ({0}, {1}) as loading.", chunkX, chunkZ); |
| 176 | + } |
| 177 | + else |
| 178 | + return GetCmdDescTranslated(); |
| 179 | + } |
| 180 | + else if (args[0] == "setloaded") // For debugging |
| 181 | + { |
| 182 | + Tuple<int, int>? chunkPos = ParseChunkPos(args); |
| 183 | + if (chunkPos != null) |
| 184 | + { |
| 185 | + handler.Log.Info("§x§0This command is used for debugging, make sure you know what you are doing.§r"); |
| 186 | + World world = handler.GetWorld(); |
| 187 | + (int chunkX, int chunkZ) = chunkPos; |
| 188 | + ChunkColumn? chunkColumn = world[chunkX, chunkZ]; |
| 189 | + if (chunkColumn != null) |
| 190 | + chunkColumn.FullyLoaded = true; |
| 191 | + return (chunkColumn == null) ? "Fail: chunk dosen't exist!" : |
| 192 | + String.Format("Successfully marked chunk ({0}, {1}) as loaded.", chunkX, chunkZ); |
| 193 | + } |
| 194 | + else |
| 195 | + return GetCmdDescTranslated(); |
| 196 | + } |
| 197 | + else if (args[0] == "delete") // For debugging |
| 198 | + { |
| 199 | + Tuple<int, int>? chunkPos = ParseChunkPos(args); |
| 200 | + if (chunkPos != null) |
| 201 | + { |
| 202 | + handler.Log.Info("§x§0This command is used for debugging, make sure you know what you are doing.§r"); |
| 203 | + World world = handler.GetWorld(); |
| 204 | + (int chunkX, int chunkZ) = chunkPos; |
| 205 | + world[chunkX, chunkZ] = null; |
| 206 | + return String.Format("Successfully deleted chunk ({0}, {1}).", chunkX, chunkZ); |
| 207 | + } |
| 208 | + else |
| 209 | + return GetCmdDescTranslated(); |
| 210 | + } |
| 211 | + else |
| 212 | + return GetCmdDescTranslated(); |
| 213 | + } |
| 214 | + else |
| 215 | + return GetCmdDescTranslated(); |
| 216 | + } |
| 217 | + else |
| 218 | + return GetCmdDescTranslated(); |
| 219 | + } |
| 220 | + |
| 221 | + private Tuple<int, int>? ParseChunkPos(string[] args) |
| 222 | + { |
| 223 | + try |
| 224 | + { |
| 225 | + int chunkX, chunkZ; |
| 226 | + if (args.Length == 1 + 3) |
| 227 | + { |
| 228 | + Location pos = new(double.Parse(args[1]), double.Parse(args[2]), double.Parse(args[3])); |
| 229 | + chunkX = pos.ChunkX; |
| 230 | + chunkZ = pos.ChunkZ; |
| 231 | + } |
| 232 | + else if (args.Length == 1 + 2) |
| 233 | + { |
| 234 | + chunkX = int.Parse(args[1]); |
| 235 | + chunkZ = int.Parse(args[2]); |
| 236 | + } |
| 237 | + else |
| 238 | + return null; |
| 239 | + return new(chunkX, chunkZ); |
| 240 | + } |
| 241 | + catch (FormatException) |
| 242 | + { |
| 243 | + return null; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private string getChunkLoadingStatus(World world) |
| 248 | + { |
| 249 | + double chunkLoadedRatio; |
| 250 | + if (world.chunkCnt == 0) |
| 251 | + chunkLoadedRatio = 0; |
| 252 | + else |
| 253 | + chunkLoadedRatio = (world.chunkCnt - world.chunkLoadNotCompleted) / (double)world.chunkCnt; |
| 254 | + |
| 255 | + string status = Translations.Get("cmd.move.chunk_loading_status", |
| 256 | + chunkLoadedRatio, world.chunkCnt - world.chunkLoadNotCompleted, world.chunkCnt); |
| 257 | + |
| 258 | + return status; |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments