Skip to content

Commit 779b1ac

Browse files
committed
Split up CommandHandler class
1 parent 5bb0080 commit 779b1ac

File tree

13 files changed

+1784
-1204
lines changed

13 files changed

+1784
-1204
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ Desktop.ini
172172
## Misc.
173173
#############
174174

175+
.cursorrules
176+
175177
# Compiled class file
176178
*.class
177179

src/main/java/net/coreprotect/command/CommandHandler.java

Lines changed: 0 additions & 1158 deletions
Large diffs are not rendered by default.
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
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+
}

src/main/java/net/coreprotect/command/LookupCommand.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,30 @@
4545
import net.coreprotect.utility.ItemUtils;
4646
import net.coreprotect.utility.MaterialUtils;
4747
import net.coreprotect.utility.StringUtils;
48-
import net.coreprotect.utility.Util;
4948
import net.coreprotect.utility.WorldUtils;
5049

5150
public class LookupCommand {
5251
protected static void runCommand(CommandSender player, Command command, boolean permission, String[] args) {
5352
int resultc = args.length;
54-
args = CommandHandler.parsePage(args);
55-
Location lo = CommandHandler.parseLocation(player, args);
53+
args = CommandParser.parsePage(args);
54+
Location lo = CommandParser.parseLocation(player, args);
5655
// List<String> arg_uuids = new ArrayList<String>();
57-
List<String> argUsers = CommandHandler.parseUsers(args);
58-
Integer[] argRadius = CommandHandler.parseRadius(args, player, lo);
59-
int argNoisy = CommandHandler.parseNoisy(args);
60-
List<Integer> argAction = CommandHandler.parseAction(args);
61-
List<Object> argBlocks = CommandHandler.parseRestricted(player, args, argAction);
62-
Map<Object, Boolean> argExclude = CommandHandler.parseExcluded(player, args, argAction);
63-
List<String> argExcludeUsers = CommandHandler.parseExcludedUsers(player, args);
64-
String ts = CommandHandler.parseTimeString(args);
65-
long[] argTime = CommandHandler.parseTime(args);
56+
List<String> argUsers = CommandParser.parseUsers(args);
57+
Integer[] argRadius = CommandParser.parseRadius(args, player, lo);
58+
int argNoisy = CommandParser.parseNoisy(args);
59+
List<Integer> argAction = CommandParser.parseAction(args);
60+
List<Object> argBlocks = CommandParser.parseRestricted(player, args, argAction);
61+
Map<Object, Boolean> argExclude = CommandParser.parseExcluded(player, args, argAction);
62+
List<String> argExcludeUsers = CommandParser.parseExcludedUsers(player, args);
63+
String ts = CommandParser.parseTimeString(args);
64+
long[] argTime = CommandParser.parseTime(args);
6665
long startTime = argTime[0];
6766
long endTime = argTime[1];
68-
int argWid = CommandHandler.parseWorld(args, true, true);
69-
int parseRows = CommandHandler.parseRows(args);
70-
boolean count = CommandHandler.parseCount(args);
71-
boolean worldedit = CommandHandler.parseWorldEdit(args);
72-
boolean forceglobal = CommandHandler.parseForceGlobal(args);
67+
int argWid = CommandParser.parseWorld(args, true, true);
68+
int parseRows = CommandParser.parseRows(args);
69+
boolean count = CommandParser.parseCount(args);
70+
boolean worldedit = CommandParser.parseWorldEdit(args);
71+
boolean forceglobal = CommandParser.parseForceGlobal(args);
7372
boolean pageLookup = false;
7473

7574
if (argBlocks == null || argExclude == null || argExcludeUsers == null) {
@@ -126,7 +125,7 @@ else if (!argAction.contains(3)) {
126125
}
127126

128127
if (argWid == -1) {
129-
String worldName = CommandHandler.parseWorldName(args, true);
128+
String worldName = CommandParser.parseWorldName(args, true);
130129
Chat.sendMessage(player, new ChatMessage(Phrase.build(Phrase.WORLD_NOT_FOUND, worldName)).build());
131130
return;
132131
}

0 commit comments

Comments
 (0)