|
| 1 | +package net.coreprotect.command; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import org.bukkit.Location; |
| 9 | +import org.bukkit.Material; |
| 10 | +import org.bukkit.command.CommandSender; |
| 11 | + |
| 12 | +import net.coreprotect.command.parser.ActionParser; |
| 13 | +import net.coreprotect.command.parser.LocationParser; |
| 14 | +import net.coreprotect.command.parser.MaterialParser; |
| 15 | +import net.coreprotect.command.parser.TimeParser; |
| 16 | +import net.coreprotect.command.parser.UserParser; |
| 17 | +import net.coreprotect.command.parser.WorldParser; |
| 18 | + |
| 19 | +/** |
| 20 | + * Main parser class for CoreProtect commands. |
| 21 | + * Delegates to specialized parser classes for specific functionality. |
| 22 | + */ |
| 23 | +public class CommandParser { |
| 24 | + |
| 25 | + /** |
| 26 | + * Parse page number from command arguments |
| 27 | + * |
| 28 | + * @param argumentArray |
| 29 | + * The command arguments |
| 30 | + * @return The modified argument array |
| 31 | + */ |
| 32 | + protected static String[] parsePage(String[] argumentArray) { |
| 33 | + return ActionParser.parsePage(argumentArray); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Parse action type from command arguments |
| 38 | + * |
| 39 | + * @param inputArguments |
| 40 | + * The command arguments |
| 41 | + * @return A list of action type integers |
| 42 | + */ |
| 43 | + protected static List<Integer> parseAction(String[] inputArguments) { |
| 44 | + return ActionParser.parseAction(inputArguments); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Parse coordinates from command arguments |
| 49 | + * |
| 50 | + * @param location |
| 51 | + * The base location |
| 52 | + * @param inputArguments |
| 53 | + * The command arguments |
| 54 | + * @param worldId |
| 55 | + * The world ID |
| 56 | + * @return The parsed location |
| 57 | + */ |
| 58 | + protected static Location parseCoordinates(Location location, String[] inputArguments, int worldId) { |
| 59 | + return LocationParser.parseCoordinates(location, inputArguments, worldId); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Parse count flag from command arguments |
| 64 | + * |
| 65 | + * @param inputArguments |
| 66 | + * The command arguments |
| 67 | + * @return true if the count flag is present |
| 68 | + */ |
| 69 | + protected static boolean parseCount(String[] inputArguments) { |
| 70 | + return ActionParser.parseCount(inputArguments); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Parse excluded materials and entities from command arguments |
| 75 | + * |
| 76 | + * @param player |
| 77 | + * The command sender |
| 78 | + * @param inputArguments |
| 79 | + * The command arguments |
| 80 | + * @param argAction |
| 81 | + * The list of actions to include |
| 82 | + * @return A map of excluded materials and entities |
| 83 | + */ |
| 84 | + protected static Map<Object, Boolean> parseExcluded(CommandSender player, String[] inputArguments, List<Integer> argAction) { |
| 85 | + return MaterialParser.parseExcluded(player, inputArguments, argAction); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Parse excluded users from command arguments |
| 90 | + * |
| 91 | + * @param player |
| 92 | + * The command sender |
| 93 | + * @param inputArguments |
| 94 | + * The command arguments |
| 95 | + * @return A list of excluded users |
| 96 | + */ |
| 97 | + protected static List<String> parseExcludedUsers(CommandSender player, String[] inputArguments) { |
| 98 | + return UserParser.parseExcludedUsers(player, inputArguments); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Parse force global flag from command arguments |
| 103 | + * |
| 104 | + * @param inputArguments |
| 105 | + * The command arguments |
| 106 | + * @return true if global search should be forced |
| 107 | + */ |
| 108 | + protected static boolean parseForceGlobal(String[] inputArguments) { |
| 109 | + return WorldParser.parseForceGlobal(inputArguments); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Parse location from command sender and command arguments |
| 114 | + * |
| 115 | + * @param user |
| 116 | + * The command sender |
| 117 | + * @param argumentArray |
| 118 | + * The command arguments |
| 119 | + * @return The parsed location |
| 120 | + */ |
| 121 | + protected static Location parseLocation(CommandSender user, String[] argumentArray) { |
| 122 | + return LocationParser.parseLocation(user, argumentArray); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Parse noisy/verbose flag from command arguments |
| 127 | + * |
| 128 | + * @param inputArguments |
| 129 | + * The command arguments |
| 130 | + * @return 1 if noisy/verbose mode is enabled, 0 otherwise |
| 131 | + */ |
| 132 | + protected static int parseNoisy(String[] inputArguments) { |
| 133 | + return ActionParser.parseNoisy(inputArguments); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Parse preview flag from command arguments |
| 138 | + * |
| 139 | + * @param inputArguments |
| 140 | + * The command arguments |
| 141 | + * @return 1 for preview, 2 for preview cancel, 0 otherwise |
| 142 | + */ |
| 143 | + protected static int parsePreview(String[] inputArguments) { |
| 144 | + return ActionParser.parsePreview(inputArguments); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Parse radius from command arguments |
| 149 | + * |
| 150 | + * @param inputArguments |
| 151 | + * The command arguments |
| 152 | + * @param user |
| 153 | + * The command sender |
| 154 | + * @param location |
| 155 | + * The base location |
| 156 | + * @return The parsed radius |
| 157 | + */ |
| 158 | + protected static Integer[] parseRadius(String[] inputArguments, CommandSender user, Location location) { |
| 159 | + Integer[] result = LocationParser.parseRadius(inputArguments, user, location); |
| 160 | + |
| 161 | + // Handle WorldEdit case which LocationParser returned as null |
| 162 | + if (result == null && WorldParser.parseWorldEdit(inputArguments)) { |
| 163 | + if (user.getServer().getPluginManager().getPlugin("WorldEdit") != null) { |
| 164 | + Integer[] worldEditResult = WorldEditHandler.runWorldEditCommand(user); |
| 165 | + if (worldEditResult != null) { |
| 166 | + result = worldEditResult; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return result; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Parse restricted materials and entities from command arguments |
| 176 | + * |
| 177 | + * @param player |
| 178 | + * The command sender |
| 179 | + * @param inputArguments |
| 180 | + * The command arguments |
| 181 | + * @param argAction |
| 182 | + * The list of actions to include |
| 183 | + * @return A list of restricted materials and entities |
| 184 | + */ |
| 185 | + protected static List<Object> parseRestricted(CommandSender player, String[] inputArguments, List<Integer> argAction) { |
| 186 | + return MaterialParser.parseRestricted(player, inputArguments, argAction); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Parse time from command arguments |
| 191 | + * |
| 192 | + * @param inputArguments |
| 193 | + * The command arguments |
| 194 | + * @return An array of two longs - [time1, time2] |
| 195 | + */ |
| 196 | + protected static long[] parseTime(String[] inputArguments) { |
| 197 | + return TimeParser.parseTime(inputArguments); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Parse time string from command arguments for display |
| 202 | + * |
| 203 | + * @param inputArguments |
| 204 | + * The command arguments |
| 205 | + * @return A formatted time string |
| 206 | + */ |
| 207 | + protected static String parseTimeString(String[] inputArguments) { |
| 208 | + return TimeParser.parseTimeString(inputArguments); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Parse rows from command arguments |
| 213 | + * |
| 214 | + * @param inputArguments |
| 215 | + * The command arguments |
| 216 | + * @return The number of rows |
| 217 | + */ |
| 218 | + protected static int parseRows(String[] inputArguments) { |
| 219 | + return TimeParser.parseRows(inputArguments); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Parse world from command arguments |
| 224 | + * |
| 225 | + * @param inputArguments |
| 226 | + * The command arguments |
| 227 | + * @param processWorldEdit |
| 228 | + * Whether to process WorldEdit arguments |
| 229 | + * @param requireLoaded |
| 230 | + * Whether the world must be loaded |
| 231 | + * @return The world ID |
| 232 | + */ |
| 233 | + protected static int parseWorld(String[] inputArguments, boolean processWorldEdit, boolean requireLoaded) { |
| 234 | + return WorldParser.parseWorld(inputArguments, processWorldEdit, requireLoaded); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Parse whether to use WorldEdit for radius |
| 239 | + * |
| 240 | + * @param inputArguments |
| 241 | + * The command arguments |
| 242 | + * @return true if WorldEdit should be used |
| 243 | + */ |
| 244 | + protected static boolean parseWorldEdit(String[] inputArguments) { |
| 245 | + return WorldParser.parseWorldEdit(inputArguments); |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * Parse world name from command arguments |
| 250 | + * |
| 251 | + * @param inputArguments |
| 252 | + * The command arguments |
| 253 | + * @param processWorldEdit |
| 254 | + * Whether to process WorldEdit arguments |
| 255 | + * @return The world name |
| 256 | + */ |
| 257 | + protected static String parseWorldName(String[] inputArguments, boolean processWorldEdit) { |
| 258 | + return WorldParser.parseWorldName(inputArguments, processWorldEdit); |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Get a map of block tags and their associated materials |
| 263 | + * |
| 264 | + * @return A map of block tags and their associated materials |
| 265 | + */ |
| 266 | + protected static Map<String, Set<Material>> getTags() { |
| 267 | + return MaterialParser.getTags(); |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Check if an argument matches a block tag |
| 272 | + * |
| 273 | + * @param argument |
| 274 | + * The argument to check |
| 275 | + * @return true if the argument matches a block tag |
| 276 | + */ |
| 277 | + protected static boolean checkTags(String argument) { |
| 278 | + return MaterialParser.checkTags(argument); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * Check if an argument matches a block tag and add the associated materials to the list |
| 283 | + * |
| 284 | + * @param argument |
| 285 | + * The argument to check |
| 286 | + * @param list |
| 287 | + * The list to add the associated materials to |
| 288 | + * @return true if the argument matches a block tag |
| 289 | + */ |
| 290 | + protected static boolean checkTags(String argument, Map<Object, Boolean> list) { |
| 291 | + return MaterialParser.checkTags(argument, list); |
| 292 | + } |
| 293 | + |
| 294 | + /** |
| 295 | + * Check if an argument matches a block tag and add the associated materials to the list |
| 296 | + * |
| 297 | + * @param argument |
| 298 | + * The argument to check |
| 299 | + * @param list |
| 300 | + * The list to add the associated materials to |
| 301 | + * @return true if the argument matches a block tag |
| 302 | + */ |
| 303 | + protected static boolean checkTags(String argument, List<Object> list) { |
| 304 | + return MaterialParser.checkTags(argument, list); |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * Parse users from command arguments |
| 309 | + * |
| 310 | + * @param inputArguments |
| 311 | + * The command arguments |
| 312 | + * @return A list of parsed users |
| 313 | + */ |
| 314 | + protected static List<String> parseUsers(String[] inputArguments) { |
| 315 | + return UserParser.parseUsers(inputArguments); |
| 316 | + } |
| 317 | + |
| 318 | + /** |
| 319 | + * Helper method for formatting BigDecimal values |
| 320 | + * |
| 321 | + * @param input |
| 322 | + * The BigDecimal value to format |
| 323 | + * @return The formatted string |
| 324 | + */ |
| 325 | + private static String timeString(BigDecimal input) { |
| 326 | + return input.stripTrailingZeros().toPlainString(); |
| 327 | + } |
| 328 | + |
| 329 | +} |
0 commit comments