Use block change limit for //copy and //cut (#1536)

* Use block change limit for //copy

* Use a cuboid region for accuracy

* Add a bounding box method to Region and use that

* Update worldedit-core/src/main/java/com/sk89q/worldedit/regions/Region.java

Co-authored-by: Octavia Togami <octavia.togami@gmail.com>

Co-authored-by: Octavia Togami <octavia.togami@gmail.com>
This commit is contained in:
Matthew Miller 2020-09-27 15:40:50 +10:00 committed by GitHub
parent a095b284c8
commit fd8dbdd7b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -22,6 +22,7 @@
import com.google.common.collect.Lists;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
@ -66,6 +67,20 @@
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
public class ClipboardCommands {
/**
* Throws if the region would allocate a clipboard larger than the block change limit.
*
* @param region The region to check
* @param session The session
* @throws MaxChangedBlocksException if the volume exceeds the limit
*/
private void checkRegionBounds(Region region, LocalSession session) throws MaxChangedBlocksException {
int limit = session.getBlockChangeLimit();
if (region.getBoundingBox().getVolume() > limit) {
throw new MaxChangedBlocksException(limit);
}
}
@Command(
name = "/copy",
desc = "Copy the selection to the clipboard"
@ -79,6 +94,7 @@ public void copy(Actor actor, LocalSession session, EditSession editSession,
boolean copyBiomes,
@ArgFlag(name = 'm', desc = "Set the include mask, non-matching blocks become air")
Mask mask) throws WorldEditException {
checkRegionBounds(region, session);
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(session.getPlacementPosition(actor));
ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint());
@ -109,7 +125,7 @@ public void cut(Actor actor, LocalSession session, EditSession editSession,
boolean copyBiomes,
@ArgFlag(name = 'm', desc = "Set the exclude mask, non-matching blocks become air")
Mask mask) throws WorldEditException {
checkRegionBounds(region, session);
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(session.getPlacementPosition(actor));
ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint());

View File

@ -163,6 +163,11 @@ public BlockVector3 getMaximumPoint() {
return pos1.getMaximum(pos2);
}
@Override
public CuboidRegion getBoundingBox() {
return this;
}
@Override
public int getMinimumY() {
return Math.min(pos1.getBlockY(), pos2.getBlockY());

View File

@ -49,6 +49,15 @@ public interface Region extends Iterable<BlockVector3>, Cloneable {
*/
BlockVector3 getMaximumPoint();
/**
* Get the bounding box of this region as a {@link CuboidRegion}.
*
* @return the bounding box
*/
default CuboidRegion getBoundingBox() {
return new CuboidRegion(getMinimumPoint(), getMaximumPoint());
}
/**
* Get the center point of a region.
* Note: Coordinates will not be integers