Skip to content

Commit

Permalink
feat: implement graceful world bounds check where appropriate (#2804)
Browse files Browse the repository at this point in the history
- closes #2494
  • Loading branch information
dordsor21 authored Jun 28, 2024
1 parent 99a58f6 commit 4853a65
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fastasyncworldedit.core.exception;

import com.sk89q.worldedit.WorldEditException;

public class OutsideWorldBoundsException extends WorldEditException {

private final int y;

public OutsideWorldBoundsException(int y) {
this.y = y;
}

public int y() {
return y;
}

}
16 changes: 16 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/worldedit/WorldEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.exception.BrushRadiusLimitException;
import com.fastasyncworldedit.core.exception.OutsideWorldBoundsException;
import com.fastasyncworldedit.core.exception.RadiusLimitException;
import com.fastasyncworldedit.core.extension.factory.TransformFactory;
import com.fastasyncworldedit.core.extent.ResettableExtent;
Expand All @@ -46,6 +47,7 @@
import com.sk89q.worldedit.extension.platform.Locatable;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.PlatformManager;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.pattern.Pattern;
Expand Down Expand Up @@ -523,6 +525,20 @@ public void checkMaxBrushRadius(Expression expression, Actor actor) {
throw new BrushRadiusLimitException(max);
}
}

/**
* Check if the given position is contained by the extent's min/max height
*
* @param position Position to check
* @param extent Extent to check in
* @throws OutsideWorldBoundsException If the position is outside the world height limits
* @since TODO
*/
public void checkExtentHeightBounds(BlockVector3 position, Extent extent) {
if (position.y() < extent.getMinY() || position.y() > extent.getMaxY()) {
throw new OutsideWorldBoundsException(position.y());
}
}
//FAWE end

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public int fill(
we.checkMaxRadius(depth, actor);

BlockVector3 pos = session.getPlacementPosition(actor);
we.checkExtentHeightBounds(pos, editSession);
int affected = editSession.fillDirection(pos, pattern, radius, depth, direction);
actor.print(Caption.of("worldedit.fill.created", TextComponent.of(affected)));
return affected;
Expand Down Expand Up @@ -330,6 +331,7 @@ public int fillr(
we.checkMaxRadius(radius, actor);

BlockVector3 pos = session.getPlacementPosition(actor);
we.checkExtentHeightBounds(pos, editSession);
int affected = editSession.fillXZ(pos, pattern, radius, depth, true);
actor.print(Caption.of("worldedit.fillr.created", TextComponent.of(affected)));
return affected;
Expand Down Expand Up @@ -357,7 +359,9 @@ public int drain(
double radius = radiusExp.evaluate();
radius = Math.max(0, radius);
we.checkMaxRadius(radius, actor);
int affected = editSession.drainArea(session.getPlacementPosition(actor), radius, waterlogged, plants);
BlockVector3 pos = session.getPlacementPosition(actor);
we.checkExtentHeightBounds(pos, editSession);
int affected = editSession.drainArea(pos, radius, waterlogged, plants);
actor.print(Caption.of("worldedit.drain.drained", TextComponent.of(affected)));
return affected;
}
Expand All @@ -376,7 +380,9 @@ public int fixLava(
) throws WorldEditException {
radius = Math.max(0, radius);
we.checkMaxRadius(radius, actor);
int affected = editSession.fixLiquid(session.getPlacementPosition(actor), radius, BlockTypes.LAVA);
BlockVector3 pos = session.getPlacementPosition(actor);
we.checkExtentHeightBounds(pos, editSession);
int affected = editSession.fixLiquid(pos, radius, BlockTypes.LAVA);
actor.print(Caption.of("worldedit.fixlava.fixed", TextComponent.of(affected)));
return affected;
}
Expand All @@ -395,7 +401,9 @@ public int fixWater(
) throws WorldEditException {
radius = Math.max(0, radius);
we.checkMaxRadius(radius, actor);
int affected = editSession.fixLiquid(session.getPlacementPosition(actor), radius, BlockTypes.WATER);
BlockVector3 pos = session.getPlacementPosition(actor);
we.checkExtentHeightBounds(pos, editSession);
int affected = editSession.fixLiquid(pos, radius, BlockTypes.WATER);
actor.print(Caption.of("worldedit.fixwater.fixed", TextComponent.of(affected)));
return affected;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.exception.BrushRadiusLimitException;
import com.fastasyncworldedit.core.exception.OutsideWorldBoundsException;
import com.fastasyncworldedit.core.exception.RadiusLimitException;
import com.fastasyncworldedit.core.internal.exception.FaweException;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -146,6 +147,11 @@ public void convert(BrushRadiusLimitException e) throws CommandException {
public void convert(RadiusLimitException e) throws CommandException {
throw newCommandException(Caption.of("fawe.error.limit.max-radius", TextComponent.of(e.getMaxRadius())), e);
}

@ExceptionMatch
public void convert(OutsideWorldBoundsException e) throws CommandException {
throw newCommandException(Caption.of("fawe.cancel.reason.world.limit", TextComponent.of(e.y())), e);
}
//FAWE end

@ExceptionMatch
Expand Down
1 change: 1 addition & 0 deletions worldedit-core/src/main/resources/lang/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
"fawe.cancel.reason.no.region.not.added": "Not added to region",
"fawe.cancel.reason.player-only": "This operation requires a player, and cannot be executed from console, or without an actor.",
"fawe.cancel.reason.actor-required": "This operation requires an actor.",
"fawe.cancel.reason.world.limit": "This operation cannot be performed at y={0} as it is outside world limits.",
"fawe.cancel.worldedit.failed.load.chunk": "Skipped loading chunk: {0};{1}. Try increasing chunk-wait.",
"fawe.navigation.no.block": "No block in sight! (or too far)",
"fawe.selection.sel.max": "{0} points maximum.",
Expand Down

0 comments on commit 4853a65

Please sign in to comment.