Added selection setting API.

This commit is contained in:
sk89q 2011-04-03 11:03:57 -07:00
parent fd2de1d264
commit fc3531ce89
9 changed files with 177 additions and 14 deletions

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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<BlockVector2D> 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<BlockVector2D> getNativePoints() {
return poly2d.getPoints();
return Collections.unmodifiableList(poly2d.getPoints());
}
}

View File

@ -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

View File

@ -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.
*

View File

@ -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();

View File

@ -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.

View File

@ -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();
}
}

View File

@ -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.
*