From 7fd5d1b61c4707610fdd84e72935f9f4707c20b1 Mon Sep 17 00:00:00 2001 From: aumgn Date: Wed, 29 Aug 2012 10:00:26 +0200 Subject: [PATCH] Add /center commmand --- .../java/com/sk89q/worldedit/EditSession.java | 21 +++++++++++++++++++ .../worldedit/commands/RegionCommands.java | 16 ++++++++++++++ .../worldedit/regions/AbstractRegion.java | 5 +++++ .../worldedit/regions/CylinderRegion.java | 1 + .../worldedit/regions/EllipsoidRegion.java | 1 + .../com/sk89q/worldedit/regions/Region.java | 9 ++++++++ 6 files changed, 53 insertions(+) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index ff6d39566..157c4c5e3 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -1352,6 +1352,27 @@ public class EditSession { return affected; } + public int center(Region region, Pattern pattern) + throws MaxChangedBlocksException { + Vector center = region.getCenter(); + int x2 = center.getBlockX(); + int y2 = center.getBlockY(); + int z2 = center.getBlockZ(); + + int affected = 0; + for (int x = (int) center.getX(); x <= x2; x++) { + for (int y = (int) center.getY(); y <= y2; y++) { + for (int z = (int) center.getZ(); z <= z2; z++) { + if (setBlock(new Vector(x, y, z), pattern)) { + affected++; + } + } + } + } + + return affected; + } + /** * Make faces of the region (as if it was a cuboid if it's not). * diff --git a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java index a33669404..64a6cf5f6 100644 --- a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java @@ -144,6 +144,22 @@ public class RegionCommands { player.print(affected + " block(s) have been overlayed."); } + @Command( + aliases = { "/center", "/middle" }, + usage = "", + desc = "Set the center block(s)", + min = 1, + max = 1 + ) + public void center(CommandContext args, LocalSession session, LocalPlayer player, + EditSession editSession) throws WorldEditException { + Pattern pattern = we.getBlockPattern(player, args.getString(0)); + Region region = session.getSelection(player.getWorld()); + + int affected = editSession.center(region, pattern); + player.print("Center set ("+ affected + " blocks changed)"); + } + @Command( aliases = { "/naturalize" }, usage = "", diff --git a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java index 96507ab2e..948bc881a 100644 --- a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java @@ -35,6 +35,11 @@ public abstract class AbstractRegion implements Region { this.world = world; } + @Override + public Vector getCenter() { + return getMinimumPoint().add(getMaximumPoint()).divide(2); + } + /** * Get the iterator. * diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index 3beb99803..536316c9f 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -87,6 +87,7 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { * * @return */ + @Override public Vector getCenter() { return center.toVector((maxY + minY) / 2); } diff --git a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java index 77664a902..42e671f9a 100644 --- a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java @@ -175,6 +175,7 @@ public class EllipsoidRegion extends AbstractRegion { * * @return center */ + @Override public Vector getCenter() { return center; } diff --git a/src/main/java/com/sk89q/worldedit/regions/Region.java b/src/main/java/com/sk89q/worldedit/regions/Region.java index 309bf17b6..ccfad2b9b 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Region.java +++ b/src/main/java/com/sk89q/worldedit/regions/Region.java @@ -44,6 +44,15 @@ public interface Region extends Iterable, Cloneable { */ public Vector getMaximumPoint(); + /** + * Get the center point of a region. + * Note: Coordinates will not be integers + * if the corresponding lengths are even. + * + * @return center point + */ + public Vector getCenter(); + /** * Get the number of blocks in the region. *