Plex-FAWE/src/com/sk89q/worldedit/LocalSession.java

416 lines
9.8 KiB
Java
Raw Normal View History

// $Id$
/*
* WorldEdit
* Copyright (C) 2010 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/>.
*/
2011-01-01 01:40:07 +00:00
package com.sk89q.worldedit;
import java.util.LinkedList;
import com.sk89q.worldedit.snapshots.Snapshot;
import com.sk89q.worldedit.superpickaxe.SinglePickaxe;
import com.sk89q.worldedit.superpickaxe.SuperPickaxeMode;
import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.CuboidRegion;
/**
*
* @author sk89q
*/
public class LocalSession {
public static final int MAX_HISTORY_SIZE = 15;
private boolean placeAtPos1 = false;
private Vector pos1, pos2;
2010-10-13 04:41:06 +00:00
private Region region;
private LinkedList<EditSession> history = new LinkedList<EditSession>();
private int historyPointer = 0;
private CuboidClipboard clipboard;
private boolean toolControl = true;
private boolean superPickaxe = false;
2011-01-09 19:14:55 +00:00
private SuperPickaxeMode leftClickMode = new SinglePickaxe();
private SuperPickaxeMode armSwingMode;
private SuperPickaxeMode rightClickMode;
private int maxBlocksChanged = -1;
private boolean useInventory;
private Snapshot snapshot;
/**
* Clear history.
*/
public void clearHistory() {
history.clear();
historyPointer = 0;
}
/**
* Get the edit session.
*/
public void remember(EditSession editSession) {
// Don't store anything if no changes were made
if (editSession.size() == 0) { return; }
// Destroy any sessions after this undo point
while (historyPointer < history.size()) {
history.remove(historyPointer);
}
history.add(editSession);
while (history.size() > MAX_HISTORY_SIZE) {
history.remove(0);
}
historyPointer = history.size();
}
/**
* Undo.
*
* @return whether anything was undone
*/
public EditSession undo(BlockBag newBlockBag) {
historyPointer--;
if (historyPointer >= 0) {
EditSession editSession = history.get(historyPointer);
editSession.setBlockBag(newBlockBag);
editSession.undo();
return editSession;
} else {
historyPointer = 0;
return null;
}
}
/**
* Redo.
*
* @return whether anything was redone
*/
public EditSession redo(BlockBag newBlockBag) {
if (historyPointer < history.size()) {
EditSession editSession = history.get(historyPointer);
editSession.setBlockBag(newBlockBag);
editSession.redo();
historyPointer++;
return editSession;
}
return null;
}
2010-10-03 19:43:30 +00:00
/**
* Checks to make sure that position 1 is defined.
*
* @throws IncompleteRegionException
*/
private void checkPos1() throws IncompleteRegionException {
if (pos1 == null) {
throw new IncompleteRegionException();
}
}
2010-10-03 19:43:30 +00:00
/**
* Checks to make sure that position 2 is defined.
*
* @throws IncompleteRegionException
*/
private void checkPos2() throws IncompleteRegionException {
if (pos2 == null) {
throw new IncompleteRegionException();
}
}
/**
* Returns true if the region is fully defined.
*
* @throws IncompleteRegionException
*/
public boolean isRegionDefined() {
return pos1 != null && pos2 != null;
}
2010-10-03 19:43:30 +00:00
/**
* Gets defined position 1.
*
2010-10-11 15:56:19 +00:00
* @return position 1
2010-10-03 19:43:30 +00:00
* @throws IncompleteRegionException
*/
public Vector getPos1() throws IncompleteRegionException {
checkPos1();
return pos1;
}
2010-10-03 19:43:30 +00:00
/**
* Sets position 1.
*
* @param pt
2010-10-03 19:43:30 +00:00
*/
public void setPos1(Vector pt) {
pos1 = pt;
2010-10-13 04:41:06 +00:00
if (pos1 != null && pos2 != null) {
region = new CuboidRegion(pos1, pos2);
}
}
2010-10-03 19:43:30 +00:00
/**
* Gets position 2.
*
2010-10-11 15:56:19 +00:00
* @return position 2
2010-10-03 19:43:30 +00:00
* @throws IncompleteRegionException
*/
public Vector getPos2() throws IncompleteRegionException {
checkPos2();
return pos2;
}
2010-10-03 19:43:30 +00:00
/**
* Sets position 2.
*
* @param pt
2010-10-03 19:43:30 +00:00
*/
public void setPos2(Vector pt) {
pos2 = pt;
2010-10-13 04:41:06 +00:00
if (pos1 != null && pos2 != null) {
region = new CuboidRegion(pos1, pos2);
}
}
/**
2010-10-13 04:41:06 +00:00
* Update session position 1/2 based on the currently set region,
* provided that the region is of a cuboid.
*/
public void learnRegionChanges() {
if (region instanceof CuboidRegion) {
CuboidRegion cuboidRegion = (CuboidRegion)region;
pos1 = cuboidRegion.getPos1();
pos2 = cuboidRegion.getPos2();
}
}
/**
* Get the region. If you change the region, you should
2010-10-13 04:41:06 +00:00
* call learnRegionChanges().
2010-10-03 19:43:30 +00:00
*
2010-10-11 15:56:19 +00:00
* @return region
* @throws IncompleteRegionException
2010-10-03 19:43:30 +00:00
*/
public Region getRegion() throws IncompleteRegionException {
if (region == null) {
throw new IncompleteRegionException();
}
2010-10-13 04:41:06 +00:00
return region;
}
2010-10-02 23:11:44 +00:00
/**
2010-10-03 19:43:30 +00:00
* Gets the clipboard.
*
2010-10-11 15:56:19 +00:00
* @return clipboard, may be null
* @throws EmptyClipboardException
2010-10-02 23:11:44 +00:00
*/
public CuboidClipboard getClipboard() throws EmptyClipboardException {
if (clipboard == null) {
throw new EmptyClipboardException();
}
2010-10-02 23:11:44 +00:00
return clipboard;
}
/**
2010-10-03 19:43:30 +00:00
* Sets the clipboard.
*
2010-10-02 23:11:44 +00:00
* @param clipboard
*/
public void setClipboard(CuboidClipboard clipboard) {
2010-10-02 23:11:44 +00:00
this.clipboard = clipboard;
}
/**
* See if tool control is enabled.
*
2010-10-11 15:56:19 +00:00
* @return true if enabled
*/
public boolean isToolControlEnabled() {
return toolControl;
}
/**
* Change tool control setting.
*
2010-10-11 15:56:19 +00:00
* @param toolControl
*/
public void setToolControl(boolean toolControl) {
this.toolControl = toolControl;
}
/**
* Get the maximum number of blocks that can be changed in an edit session.
2010-10-11 15:56:19 +00:00
*
* @return block change limit
*/
public int getBlockChangeLimit() {
return maxBlocksChanged;
}
/**
* Set the maximum number of blocks that can be changed.
*
* @param maxBlocksChanged
*/
public void setBlockChangeLimit(int maxBlocksChanged) {
this.maxBlocksChanged = maxBlocksChanged;
}
2010-10-13 18:26:07 +00:00
/**
* Checks whether the super pick axe is enabled.
*
* @return status
*/
public boolean hasSuperPickAxe() {
return superPickaxe;
2010-10-13 18:26:07 +00:00
}
/**
* Enable super pick axe.
*
* @param superPickaxe
2010-10-13 18:26:07 +00:00
*/
public void enableSuperPickAxe() {
superPickaxe = true;
2010-10-13 18:26:07 +00:00
}
/**
* Disable super pick axe.
*
* @param superPickaxe
2010-10-13 18:26:07 +00:00
*/
public void disableSuperPickAxe() {
superPickaxe = false;
2010-10-13 18:26:07 +00:00
}
/**
* Toggle the super pick axe.
*
* @return status
*/
public boolean toggleSuperPickAxe() {
superPickaxe = !superPickaxe;
return superPickaxe;
2010-10-13 18:26:07 +00:00
}
/**
* @return position
* @throws IncompleteRegionException
*/
public Vector getPlacementPosition(LocalPlayer player)
throws IncompleteRegionException {
if (!placeAtPos1) {
return player.getBlockIn();
}
checkPos1();
return pos1;
}
/**
* Toggle placement position;
*/
public boolean togglePlacementPosition() {
placeAtPos1 = !placeAtPos1;
return placeAtPos1;
}
/**
* Get a block bag for a player.
*
* @param player
* @return
*/
public BlockBag getBlockBag(LocalPlayer player) {
if (!useInventory) {
return null;
}
2011-01-01 01:06:42 +00:00
return player.getInventoryBlockBag();
}
/**
* @return the snapshotName
*/
public Snapshot getSnapshot() {
return snapshot;
}
/**
* @param snapshot
*/
public void setSnapshot(Snapshot snapshot) {
this.snapshot = snapshot;
}
/**
* @return the superPickaxeMode
*/
2011-01-09 19:14:55 +00:00
public SuperPickaxeMode getLeftClickMode() {
return leftClickMode;
}
/**
* @param superPickaxeMode the superPickaxeMode to set
*/
2011-01-09 19:14:55 +00:00
public void setLeftClickMode(SuperPickaxeMode leftClickMode) {
this.leftClickMode = leftClickMode;
}
/**
* @return the tool
*/
2011-01-09 19:14:55 +00:00
public SuperPickaxeMode getRightClickMode() {
return rightClickMode;
}
/**
* @param tool the tool to set
*/
2011-01-09 19:14:55 +00:00
public void setRightClickMode(SuperPickaxeMode rightClickMode) {
this.rightClickMode = rightClickMode;
}
/**
* @return the arm swing mode
*/
public SuperPickaxeMode getArmSwingMode() {
return armSwingMode;
}
/**
* @param rightClickMode the tool to set
*/
public void setArmSwingMode(SuperPickaxeMode armSwingMode) {
this.armSwingMode = armSwingMode;
}
/**
* @return the useInventory
*/
public boolean isUsingInventory() {
return useInventory;
}
/**
* @param useInventory the useInventory to set
*/
public void setUseInventory(boolean useInventory) {
this.useInventory = useInventory;
}
}