From fc3531ce89f6af91ffe5599fda632e8eb468a7b3 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 3 Apr 2011 11:03:57 -0700 Subject: [PATCH] Added selection setting API. --- .../worldedit/bukkit/WorldEditPlugin.java | 34 ++++++++++++++++- .../bukkit/selections/CuboidSelection.java | 38 +++++++++++++++++-- .../selections/Polygonal2DSelection.java | 34 +++++++++++++++-- .../bukkit/selections/RegionSelection.java | 29 ++++++++++++-- .../bukkit/selections/Selection.java | 8 ++++ .../regions/CuboidRegionSelector.java | 6 ++- .../worldedit/regions/Polygonal2DRegion.java | 22 +++++++++++ .../regions/Polygonal2DRegionSelector.java | 13 ++++++- .../worldedit/regions/RegionSelector.java | 7 ++++ 9 files changed, 177 insertions(+), 14 deletions(-) diff --git a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 3442b6ba0..d0387e51b 100644 --- a/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/src/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -353,6 +353,13 @@ public class WorldEditPlugin extends JavaPlugin { * @return the selection or null if there was none */ public Selection getSelection(Player player) { + if (player == null) { + throw new IllegalArgumentException("Null player not allowed"); + } + if (!player.isOnline()) { + throw new IllegalArgumentException("Offline player not allowed"); + } + LocalSession session = controller.getSession(wrapPlayer(player)); RegionSelector selector = session.getRegionSelector(); @@ -361,9 +368,9 @@ public class WorldEditPlugin extends JavaPlugin { World world = ((BukkitWorld) session.getSelectionWorld()).getWorld(); if (region instanceof CuboidRegion) { - return new CuboidSelection(world, (CuboidRegion)region); + return new CuboidSelection(world, selector, (CuboidRegion)region); } else if (region instanceof Polygonal2DRegion) { - return new Polygonal2DSelection(world, (Polygonal2DRegion)region); + return new Polygonal2DSelection(world, selector, (Polygonal2DRegion)region); } else { return null; } @@ -371,4 +378,27 @@ public class WorldEditPlugin extends JavaPlugin { return null; } } + + /** + * Sets the region selection for a player. + * + * @param player + * @param selection + */ + public void setSelection(Player player, Selection selection) { + if (player == null) { + throw new IllegalArgumentException("Null player not allowed"); + } + if (!player.isOnline()) { + throw new IllegalArgumentException("Offline player not allowed"); + } + if (selection == null) { + throw new IllegalArgumentException("Null selection not allowed"); + } + + LocalSession session = controller.getSession(wrapPlayer(player)); + RegionSelector sel = selection.getRegionSelector(); + session.setRegionSelector(new BukkitWorld(player.getWorld()), sel); + session.dispatchCUISelection(wrapPlayer(player)); + } } diff --git a/src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java b/src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java index fbb81b6f9..16f2b665f 100644 --- a/src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java +++ b/src/com/sk89q/worldedit/bukkit/selections/CuboidSelection.java @@ -19,16 +19,48 @@ package com.sk89q.worldedit.bukkit.selections; +import org.bukkit.Location; import org.bukkit.World; +import com.sk89q.worldedit.IncompleteRegionException; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.bukkit.BukkitUtil; import com.sk89q.worldedit.regions.*; public class CuboidSelection extends RegionSelection { protected CuboidRegion cuboid; - public CuboidSelection(World world, CuboidRegion region) { - super(world, region); - this.world = world; + public CuboidSelection(World world, Location pt1, Location pt2) { + this(world, BukkitUtil.toVector(pt1), BukkitUtil.toVector(pt2)); + } + + public CuboidSelection(World world, Vector pt1, Vector pt2) { + super(world); + + if (pt1 == null) { + throw new IllegalArgumentException("Null point 1 not permitted"); + } + + if (pt2 == null) { + throw new IllegalArgumentException("Null point 2 not permitted"); + } + + CuboidRegionSelector sel = new CuboidRegionSelector(); + sel.selectPrimary(pt1); + sel.selectSecondary(pt2); + + try { + cuboid = sel.getRegion(); + } catch (IncompleteRegionException e) { + throw new RuntimeException("IncompleteRegionException unexpectedly thrown"); + } + + setRegionSelector(sel); + setRegion(cuboid); + } + + public CuboidSelection(World world, RegionSelector sel, CuboidRegion region) { + super(world, sel, region); this.cuboid = region; } } diff --git a/src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java b/src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java index 817b59850..3d73316d3 100644 --- a/src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java +++ b/src/com/sk89q/worldedit/bukkit/selections/Polygonal2DSelection.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.bukkit.selections; +import java.util.Collections; import java.util.List; import org.bukkit.World; import com.sk89q.worldedit.BlockVector2D; @@ -28,13 +29,38 @@ public class Polygonal2DSelection extends RegionSelection { protected Polygonal2DRegion poly2d; - public Polygonal2DSelection(World world, Polygonal2DRegion region) { - super(world, region); - this.world = world; + public Polygonal2DSelection(World world, RegionSelector sel, Polygonal2DRegion region) { + super(world, sel, region); this.poly2d = region; } + public Polygonal2DSelection(World world, List points, int minY, int maxY) { + super(world); + + minY = Math.min(Math.max(0, minY), 127); + maxY = Math.min(Math.max(0, maxY), 127); + + Polygonal2DRegionSelector sel = new Polygonal2DRegionSelector(); + poly2d = sel.getIncompleteRegion(); + + for (BlockVector2D pt : points) { + if (pt == null) { + throw new IllegalArgumentException("Null point not permitted"); + } + + poly2d.addPoint(pt); + } + + poly2d.setMinimumY(minY); + poly2d.setMaximumY(maxY); + + sel.learnChanges(); + + setRegionSelector(sel); + setRegion(poly2d); + } + public List getNativePoints() { - return poly2d.getPoints(); + return Collections.unmodifiableList(poly2d.getPoints()); } } diff --git a/src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java b/src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java index a26916db1..d1c597b99 100644 --- a/src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java +++ b/src/com/sk89q/worldedit/bukkit/selections/RegionSelection.java @@ -25,15 +25,38 @@ import org.bukkit.Location; import org.bukkit.World; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.regions.RegionSelector; public abstract class RegionSelection implements Selection { - protected World world; - protected Region region; + private World world; + private RegionSelector selector; + private Region region; - public RegionSelection(World world, Region region) { + public RegionSelection(World world) { + this.world = world; + } + + public RegionSelection(World world, RegionSelector selector, Region region) { this.world = world; this.region = region; + this.selector = selector; + } + + protected Region getRegion() { + return region; + } + + protected void setRegion(Region region) { + this.region = region; + } + + public RegionSelector getRegionSelector() { + return selector; + } + + protected void setRegionSelector(RegionSelector selector) { + this.selector = selector; } @Override diff --git a/src/com/sk89q/worldedit/bukkit/selections/Selection.java b/src/com/sk89q/worldedit/bukkit/selections/Selection.java index 2b9ac6460..a0f366182 100644 --- a/src/com/sk89q/worldedit/bukkit/selections/Selection.java +++ b/src/com/sk89q/worldedit/bukkit/selections/Selection.java @@ -22,6 +22,7 @@ package com.sk89q.worldedit.bukkit.selections; import org.bukkit.Location; import org.bukkit.World; import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.regions.RegionSelector; public interface Selection { /** @@ -52,6 +53,13 @@ public interface Selection { */ public Vector getNativeMaximumPoint(); + /** + * Get the region selector. This is for internal use. + * + * @return + */ + public RegionSelector getRegionSelector(); + /** * Get the world. * diff --git a/src/com/sk89q/worldedit/regions/CuboidRegionSelector.java b/src/com/sk89q/worldedit/regions/CuboidRegionSelector.java index 77c59ed49..f9b991be9 100644 --- a/src/com/sk89q/worldedit/regions/CuboidRegionSelector.java +++ b/src/com/sk89q/worldedit/regions/CuboidRegionSelector.java @@ -106,7 +106,7 @@ public class CuboidRegionSelector implements RegionSelector, CUIPointBasedRegion return pos1 != null && pos2 != null; } - public Region getRegion() throws IncompleteRegionException { + public CuboidRegion getRegion() throws IncompleteRegionException { if (pos1 == null || pos2 == null) { throw new IncompleteRegionException(); } @@ -114,6 +114,10 @@ public class CuboidRegionSelector implements RegionSelector, CUIPointBasedRegion return region; } + public CuboidRegion getIncompleteRegion() { + return region; + } + public void learnChanges() { pos1 = region.getPos1().toBlockVector(); pos2 = region.getPos2().toBlockVector(); diff --git a/src/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/com/sk89q/worldedit/regions/Polygonal2DRegion.java index 2daa7b259..957216d85 100644 --- a/src/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -142,6 +142,28 @@ public class Polygonal2DRegion implements Region { points.add(new BlockVector2D(pt.getBlockX(), pt.getBlockZ())); recalculate(); } + + /** + * Set the minimum Y. + * + * @param y + */ + public void setMinimumY(int y) { + hasY = true; + minY = y; + recalculate(); + } + + /** + * Se the maximum Y. + * + * @param y + */ + public void setMaximumY(int y) { + hasY = true; + maxY = y; + recalculate(); + } /** * Get the lower point of a region. diff --git a/src/com/sk89q/worldedit/regions/Polygonal2DRegionSelector.java b/src/com/sk89q/worldedit/regions/Polygonal2DRegionSelector.java index ca49e19e5..74b773afe 100644 --- a/src/com/sk89q/worldedit/regions/Polygonal2DRegionSelector.java +++ b/src/com/sk89q/worldedit/regions/Polygonal2DRegionSelector.java @@ -87,7 +87,7 @@ public class Polygonal2DRegionSelector implements RegionSelector { return pos1; } - public Region getRegion() throws IncompleteRegionException { + public Polygonal2DRegion getRegion() throws IncompleteRegionException { if (!isDefined()) { throw new IncompleteRegionException(); } @@ -95,11 +95,18 @@ public class Polygonal2DRegionSelector implements RegionSelector { return region; } + public Polygonal2DRegion getIncompleteRegion() { + return region; + } + public boolean isDefined() { return region.size() > 2; } public void learnChanges() { + BlockVector2D pt = region.getPoints().get(0); + pos1 = new BlockVector(pt.getBlockX(), + region.getMinimumPoint().getBlockY(), pt.getBlockZ()); } public void clear() { @@ -124,5 +131,9 @@ public class Polygonal2DRegionSelector implements RegionSelector { public int getArea() { return region.getArea(); } + + public int getPointCount() { + return region.getPoints().size(); + } } diff --git a/src/com/sk89q/worldedit/regions/RegionSelector.java b/src/com/sk89q/worldedit/regions/RegionSelector.java index a66fa52c3..6fb6e3cec 100644 --- a/src/com/sk89q/worldedit/regions/RegionSelector.java +++ b/src/com/sk89q/worldedit/regions/RegionSelector.java @@ -93,6 +93,13 @@ public interface RegionSelector { */ public Region getRegion() throws IncompleteRegionException; + /** + * Get the region even if it's not fully defined. + * + * @return + */ + public Region getIncompleteRegion(); + /** * Returns whether the region has been fully defined. *