Plex-FAWE/src/main/java/com/sk89q/worldedit/regions/CuboidRegionSelector.java

202 lines
5.8 KiB
Java
Raw Normal View History

2011-02-20 01:44:39 +00:00
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.regions;
import java.util.ArrayList;
import java.util.List;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalPlayer;
2011-03-12 06:43:02 +00:00
import com.sk89q.worldedit.LocalSession;
2011-12-13 03:20:31 +00:00
import com.sk89q.worldedit.LocalWorld;
2011-02-20 01:44:39 +00:00
import com.sk89q.worldedit.Vector;
2011-03-12 06:43:02 +00:00
import com.sk89q.worldedit.cui.CUIPointBasedRegion;
import com.sk89q.worldedit.cui.SelectionPointEvent;
2011-02-20 01:44:39 +00:00
/**
* Selector for cuboids.
*
* @author sk89q
*/
2011-03-12 06:43:02 +00:00
public class CuboidRegionSelector implements RegionSelector, CUIPointBasedRegion {
2011-02-20 01:44:39 +00:00
protected BlockVector pos1;
protected BlockVector pos2;
2011-12-13 03:20:31 +00:00
protected CuboidRegion region;
public CuboidRegionSelector(LocalWorld world) {
region = new CuboidRegion(world, new Vector(), new Vector());
}
2011-02-20 01:44:39 +00:00
public CuboidRegionSelector() {
2011-12-13 03:20:31 +00:00
this((LocalWorld)null);
}
public CuboidRegionSelector(RegionSelector oldSelector) {
2011-12-13 03:20:31 +00:00
region = new CuboidRegion(oldSelector.getIncompleteRegion().getWorld(), new Vector(), new Vector());
if (oldSelector instanceof CuboidRegionSelector) {
final CuboidRegionSelector cuboidRegionSelector = (CuboidRegionSelector) oldSelector;
pos1 = cuboidRegionSelector.pos1;
pos2 = cuboidRegionSelector.pos2;
} else {
final Region oldRegion;
try {
oldRegion = oldSelector.getRegion();
} catch (IncompleteRegionException e) {
return;
}
pos1 = oldRegion.getMinimumPoint().toBlockVector();
pos2 = oldRegion.getMaximumPoint().toBlockVector();
}
region.setPos1(pos1);
region.setPos2(pos2);
}
2011-02-20 01:44:39 +00:00
public boolean selectPrimary(Vector pos) {
if (pos.equals(pos1)) {
2011-02-20 01:44:39 +00:00
return false;
}
2011-02-20 01:44:39 +00:00
pos1 = pos.toBlockVector();
2011-11-23 01:29:48 +00:00
region.setPos1(pos1);
2011-02-20 01:44:39 +00:00
return true;
}
public boolean selectSecondary(Vector pos) {
if (pos.equals(pos2)) {
2011-02-20 01:44:39 +00:00
return false;
}
2011-02-20 01:44:39 +00:00
pos2 = pos.toBlockVector();
region.setPos2(pos2);
return true;
}
public void explainPrimarySelection(LocalPlayer player, LocalSession session, Vector pos) {
2011-02-20 01:44:39 +00:00
if (pos1 != null && pos2 != null) {
player.print("First position set to " + pos1 + " (" + region.getArea() + ").");
2011-02-20 01:44:39 +00:00
} else {
player.print("First position set to " + pos1 + ".");
}
2011-11-23 01:29:48 +00:00
2011-09-24 19:24:10 +00:00
session.dispatchCUIEvent(player, new SelectionPointEvent(0, pos, getArea()));
2011-02-20 01:44:39 +00:00
}
public void explainSecondarySelection(LocalPlayer player, LocalSession session, Vector pos) {
2011-02-20 01:44:39 +00:00
if (pos1 != null && pos2 != null) {
player.print("Second position set to " + pos2 + " (" + region.getArea() + ").");
2011-02-20 01:44:39 +00:00
} else {
player.print("Second position set to " + pos2 + ".");
2011-02-20 01:44:39 +00:00
}
2011-11-23 01:29:48 +00:00
2011-09-24 19:24:10 +00:00
session.dispatchCUIEvent(player, new SelectionPointEvent(1, pos, getArea()));
2011-02-20 01:44:39 +00:00
}
public void explainRegionAdjust(LocalPlayer player, LocalSession session) {
if (pos1 != null) {
2011-09-24 19:24:10 +00:00
session.dispatchCUIEvent(player, new SelectionPointEvent(0, pos1, getArea()));
}
if (pos2 != null) {
2011-09-24 19:24:10 +00:00
session.dispatchCUIEvent(player, new SelectionPointEvent(1, pos2, getArea()));
}
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
public BlockVector getPrimaryPosition() throws IncompleteRegionException {
if (pos1 == null) {
throw new IncompleteRegionException();
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
return pos1;
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
public boolean isDefined() {
return pos1 != null && pos2 != null;
}
2011-04-03 18:03:57 +00:00
public CuboidRegion getRegion() throws IncompleteRegionException {
2011-02-20 01:44:39 +00:00
if (pos1 == null || pos2 == null) {
throw new IncompleteRegionException();
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
return region;
}
2011-04-03 18:03:57 +00:00
public CuboidRegion getIncompleteRegion() {
return region;
}
2011-02-20 01:44:39 +00:00
public void learnChanges() {
pos1 = region.getPos1().toBlockVector();
pos2 = region.getPos2().toBlockVector();
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
public void clear() {
pos1 = null;
pos2 = null;
}
public String getTypeName() {
return "cuboid";
}
public List<String> getInformationLines() {
final List<String> lines = new ArrayList<String>();
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
if (pos1 != null) {
lines.add("Position 1: " + pos1);
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
if (pos2 != null) {
lines.add("Position 2: " + pos2);
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
return lines;
}
2011-03-12 06:43:02 +00:00
public String getTypeId() {
return "cuboid";
}
2011-09-24 17:45:03 +00:00
public void describeCUI(LocalPlayer player) {
2011-09-24 19:24:10 +00:00
if (pos1 != null) {
player.dispatchCUIEvent(new SelectionPointEvent(0, pos1, getArea()));
}
2011-09-24 19:24:10 +00:00
if (pos2 != null) {
player.dispatchCUIEvent(new SelectionPointEvent(1, pos2, getArea()));
}
2011-03-12 06:43:02 +00:00
}
public int getArea() {
if (pos1 == null) {
return -1;
}
if (pos2 == null) {
return -1;
2011-03-12 06:43:02 +00:00
}
2011-11-23 01:29:48 +00:00
return region.getArea();
2011-03-12 06:43:02 +00:00
}
2011-02-20 01:44:39 +00:00
}