Skip to content

Commit 774298b

Browse files
authored
feat: apply coord limits to more commands (#4710)
- fixes #4375
1 parent 6fc25bc commit 774298b

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

Core/src/main/java/com/plotsquared/core/command/MainCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public CompletableFuture<Boolean> execute(
275275
private CompletableFuture<Optional<CommandExecutionData>> prepareArguments(CommandExecutionData data) {
276276
if (data.args().length >= 2) {
277277
PlotArea area = data.player().getApplicablePlotArea();
278-
Plot newPlot = Plot.fromString(area, data.args()[0]);
278+
Plot newPlot = Plot.fromString(area, data.args()[0], data.player());
279279
return preparePlotArgument(newPlot, data, area)
280280
.thenApply(d -> d.flatMap(x -> prepareFlagArgument(x, area)));
281281
} else {

Core/src/main/java/com/plotsquared/core/plot/Plot.java

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ public Plot(
321321
}
322322

323323
/**
324-
* Get the plot from a string.
324+
* Get the plot from a string. Performs a check to ensure Plot#getBottomAbs is not outside world bounds
325+
* (x/z +/- 30,000,000) to prevent crashes
325326
*
326327
* @param player Provides a context for what world to search in. Prefixing the term with 'world_name;' will override this context.
327328
* @param arg The search term
@@ -332,6 +333,31 @@ public Plot(
332333
final @Nullable PlotPlayer<?> player,
333334
final @Nullable String arg,
334335
final boolean message
336+
) {
337+
Plot plot = getPlotFromStringUnchecked(player, arg, message);
338+
if (plot != null && !WorldUtil.isValidLocation(plot.getBottomAbs())) {
339+
if (message) {
340+
(player == null ? ConsolePlayer.getConsole() : player).sendMessage(TranslatableCaption.of(
341+
"invalid.world_location_plot"));
342+
}
343+
return null;
344+
}
345+
return plot;
346+
}
347+
348+
/**
349+
* Get the plot from a string. Does not perform a check on world bounds.
350+
*
351+
* @param player Provides a context for what world to search in. Prefixing the term with 'world_name;' will override this context.
352+
* @param arg The search term
353+
* @param message If a message should be sent to the player if a plot cannot be found
354+
* @return The plot if only 1 result is found, or null
355+
* @since TODO
356+
*/
357+
public static @Nullable Plot getPlotFromStringUnchecked(
358+
final @Nullable PlotPlayer<?> player,
359+
final @Nullable String arg,
360+
final boolean message
335361
) {
336362
if (arg == null) {
337363
if (player == null) {
@@ -389,13 +415,51 @@ public Plot(
389415
}
390416

391417
/**
392-
* Gets a plot from a string e.g. [area];[id]
418+
* Gets a plot from a string e.g. [area];[id]. Performs a check to ensure Plot#getBottomAbs is not outside world bounds
419+
* (x/z +/- 30,000,000) to prevent crashes
393420
*
394421
* @param defaultArea if no area is specified
395422
* @param string plot id/area + id
396423
* @return New or existing plot object
397424
*/
398425
public static @Nullable Plot fromString(final @Nullable PlotArea defaultArea, final @NonNull String string) {
426+
return fromString(defaultArea, string, null);
427+
}
428+
429+
/**
430+
* Gets a plot from a string e.g. [area];[id]. Performs a check to ensure Plot#getBottomAbs is not outside world bounds
431+
* (x/z +/- 30,000,000) to prevent crashes
432+
*
433+
* @param defaultArea if no area is specified
434+
* @param string plot id/area + id
435+
* @param player {@link PlotPlayer} player to notify if plot is invalid (outside bounds)
436+
* @return New or existing plot object
437+
* @since TODO
438+
*/
439+
public static @Nullable Plot fromString(
440+
final @Nullable PlotArea defaultArea,
441+
final @NonNull String string,
442+
final @Nullable PlotPlayer<?> player
443+
) {
444+
Plot plot = fromStringUnchecked(defaultArea, string);
445+
if (plot != null && !WorldUtil.isValidLocation(plot.getBottomAbs())) {
446+
if (player != null) {
447+
player.sendMessage(TranslatableCaption.of("invalid.world_location_plot"));
448+
}
449+
return null;
450+
}
451+
return plot;
452+
}
453+
454+
/**
455+
* Gets a plot from a string e.g. [area];[id]. Does not perform a check on world bounds.
456+
*
457+
* @param defaultArea if no area is specified
458+
* @param string plot id/area + id
459+
* @return New or existing plot object
460+
* @since TODO
461+
*/
462+
public static @Nullable Plot fromStringUnchecked(final @Nullable PlotArea defaultArea, final @NonNull String string) {
399463
final String[] split = string.split("[;,]");
400464
if (split.length == 2) {
401465
if (defaultArea != null) {
@@ -419,7 +483,8 @@ public Plot(
419483
}
420484

421485
/**
422-
* Return a new/cached plot object at a given location.
486+
* Return a new/cached plot object at a given location. Does not check world bounds for potential crashes, these should be
487+
* performed before (or after) this method is used.
423488
*
424489
* <p>
425490
* Use {@link PlotPlayer#getCurrentPlot()} if a player is expected here.

Core/src/main/resources/lang/messages_en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
"invalid.not_valid_number": "<prefix><red>That's not a valid number within the range: </red><gray><value></gray>",
235235
"invalid.not_valid_plot_id": "<prefix><red>That's not a valid plot ID.</red>",
236236
"invalid.origin_cant_be_target": "<prefix><red>The origin and target location cannot be the same.</red>",
237+
"invalid.world_location_plot": "<prefix><red>The target plot is invalid.</red>",
237238
"invalid.found_no_plots": "<prefix><red>Found no plots with your search query.</red>",
238239
"invalid.number_not_in_range": "<prefix><red>That's not a valid number within the range: <gray>(<min>, <max>)</gray></red>",
239240
"invalid.number_not_positive": "<red>That's not a positive number: <gray><value></gray></red>",

0 commit comments

Comments
 (0)