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

576 lines
15 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.HashMap;
2011-01-01 01:40:07 +00:00
import java.util.LinkedList;
import java.util.Map;
import com.sk89q.worldedit.snapshots.Snapshot;
import com.sk89q.worldedit.tools.BrushTool;
import com.sk89q.worldedit.tools.SinglePickaxe;
import com.sk89q.worldedit.tools.BlockTool;
import com.sk89q.worldedit.tools.Tool;
import com.sk89q.worldedit.bags.BlockBag;
2011-03-12 06:43:02 +00:00
import com.sk89q.worldedit.cui.CUIPointBasedRegion;
import com.sk89q.worldedit.cui.CUIEvent;
import com.sk89q.worldedit.cui.SelectionPointEvent;
import com.sk89q.worldedit.cui.SelectionShapeEvent;
2011-02-20 01:44:39 +00:00
import com.sk89q.worldedit.regions.CuboidRegionSelector;
import com.sk89q.worldedit.regions.Region;
2011-02-20 01:44:39 +00:00
import com.sk89q.worldedit.regions.RegionSelector;
/**
* An instance of this represents the WorldEdit session of a user. A session
* stores history and settings. Sessions are not tied particularly to any
* player and can be shuffled between players, saved, and loaded.
*
* @author sk89q
*/
public class LocalSession {
public static int MAX_HISTORY_SIZE = 15;
2011-01-31 08:58:29 +00:00
private LocalConfiguration config;
2011-02-20 01:44:39 +00:00
private LocalWorld selectionWorld;
2011-03-12 06:43:02 +00:00
private RegionSelector selector = new CuboidRegionSelector();
private boolean placeAtPos1 = false;
private LinkedList<EditSession> history = new LinkedList<EditSession>();
private int historyPointer = 0;
private CuboidClipboard clipboard;
private boolean toolControl = true;
private boolean superPickaxe = false;
private BlockTool pickaxeMode = new SinglePickaxe();
private Map<Integer, Tool> tools
= new HashMap<Integer, Tool>();
private int maxBlocksChanged = -1;
private boolean useInventory;
private Snapshot snapshot;
2011-01-23 10:09:51 +00:00
private String lastScript;
2011-01-31 08:58:29 +00:00
private boolean beenToldVersion = false;
2011-03-12 06:43:02 +00:00
private boolean hasCUISupport = false;
2011-01-31 08:58:29 +00:00
/**
* Construct the object.
*
* @param config
*/
public LocalSession(LocalConfiguration config) {
this.config = config;
}
/**
* Clear history.
*/
public void clearHistory() {
history.clear();
historyPointer = 0;
}
/**
* Remember an edit session for the undo history. If the history maximum
* size is reached, old edit sessions will be discarded.
*
* @param editSession
*/
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();
}
/**
* Performs an undo.
*
* @param newBlockBag
* @return whether anything was undone
*/
public EditSession undo(BlockBag newBlockBag) {
historyPointer--;
if (historyPointer >= 0) {
EditSession editSession = history.get(historyPointer);
EditSession newEditSession =
new EditSession(editSession.getWorld(), -1, newBlockBag);
newEditSession.enableQueue();
editSession.undo(newEditSession);
return editSession;
} else {
historyPointer = 0;
return null;
}
}
/**
* Performs a redo
*
* @param newBlockBag
* @return whether anything was redone
*/
public EditSession redo(BlockBag newBlockBag) {
if (historyPointer < history.size()) {
EditSession editSession = history.get(historyPointer);
EditSession newEditSession =
new EditSession(editSession.getWorld(), -1, newBlockBag);
newEditSession.enableQueue();
editSession.redo(newEditSession);
historyPointer++;
return editSession;
}
return null;
}
2010-10-03 19:43:30 +00:00
/**
2011-02-20 01:44:39 +00:00
* Get the region selector for defining the selection. If the selection
* was defined for a different world, the old selection will be discarded.
2010-10-03 19:43:30 +00:00
*
2011-02-20 01:44:39 +00:00
* @param world
* @return position
2010-10-03 19:43:30 +00:00
*/
2011-02-20 01:44:39 +00:00
public RegionSelector getRegionSelector(LocalWorld world) {
if (selectionWorld == null) {
selectionWorld = world;
} else if (!selectionWorld.equals(world)) {
selectionWorld = world;
2011-03-12 06:43:02 +00:00
selector.clear();
}
2011-03-12 06:43:02 +00:00
return selector;
}
2010-10-03 19:43:30 +00:00
/**
2011-02-20 01:44:39 +00:00
* Get the region selector. This won't check worlds so make sure that
* this region selector isn't used blindly.
*
* @return position
2010-10-03 19:43:30 +00:00
*/
2011-02-20 01:44:39 +00:00
public RegionSelector getRegionSelector() {
2011-03-12 06:43:02 +00:00
return selector;
}
/**
2011-02-20 01:44:39 +00:00
* Set the region selector.
*
2011-02-20 01:44:39 +00:00
* @param world
* @param selector
*/
2011-02-20 01:44:39 +00:00
public void setRegionSelector(LocalWorld world, RegionSelector selector) {
selectionWorld = world;
2011-03-12 06:43:02 +00:00
this.selector = selector;
}
2010-10-03 19:43:30 +00:00
/**
2011-02-20 01:44:39 +00:00
* Returns true if the region is fully defined.
2010-10-03 19:43:30 +00:00
*
2011-02-20 01:44:39 +00:00
* @return
2010-10-03 19:43:30 +00:00
*/
2011-02-20 01:44:39 +00:00
@Deprecated
public boolean isRegionDefined() {
2011-03-12 06:43:02 +00:00
return selector.isDefined();
}
2010-10-03 19:43:30 +00:00
/**
2011-02-20 01:44:39 +00:00
* Returns true if the region is fully defined for the specified world.
*
* @param world
* @return
2010-10-03 19:43:30 +00:00
*/
2011-02-20 01:44:39 +00:00
public boolean isSelectionDefined(LocalWorld world) {
if (selectionWorld == null || !selectionWorld.equals(world)) {
return false;
2010-10-13 04:41:06 +00:00
}
2011-03-12 06:43:02 +00:00
return selector.isDefined();
}
2010-10-03 19:43:30 +00:00
/**
2011-02-20 01:44:39 +00:00
* Use <code>getSelection()</code>.
2010-10-03 19:43:30 +00:00
*
2011-02-20 01:44:39 +00:00
* @return region
2010-10-03 19:43:30 +00:00
* @throws IncompleteRegionException
*/
2011-02-20 01:44:39 +00:00
@Deprecated
public Region getRegion() throws IncompleteRegionException {
2011-03-12 06:43:02 +00:00
return selector.getRegion();
2010-10-13 04:41:06 +00:00
}
/**
2011-02-20 01:44:39 +00:00
* Get the selection region. If you change the region, you should
* call learnRegionChanges(). If the selection is defined in
* a different world, the <code>IncompleteRegionException</code>
* exception will be thrown.
2010-10-03 19:43:30 +00:00
*
2011-02-20 01:44:39 +00:00
* @param world
2010-10-11 15:56:19 +00:00
* @return region
* @throws IncompleteRegionException
2010-10-03 19:43:30 +00:00
*/
2011-02-20 01:44:39 +00:00
public Region getSelection(LocalWorld world) throws IncompleteRegionException {
if (selectionWorld == null || !selectionWorld.equals(world)) {
throw new IncompleteRegionException();
}
2011-03-12 06:43:02 +00:00
return selector.getRegion();
}
/**
* Get the selection world.
*
* @return
*/
public LocalWorld getSelectionWorld() {
return selectionWorld;
}
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.
*/
public void enableSuperPickAxe() {
superPickaxe = true;
2010-10-13 18:26:07 +00:00
}
/**
* Disable super pick axe.
*/
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
}
/**
* Get the placement position.
*
* @param player
* @return position
* @throws IncompleteRegionException
*/
public Vector getPlacementPosition(LocalPlayer player)
throws IncompleteRegionException {
if (!placeAtPos1) {
return player.getBlockIn();
}
2011-03-12 06:43:02 +00:00
return selector.getPrimaryPosition();
}
/**
* Toggle placement position.
*
* @return
*/
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();
}
/**
* Get the snapshot that has been selected.
*
* @return the snapshot
*/
public Snapshot getSnapshot() {
return snapshot;
}
/**
* Select a snapshot.
*
* @param snapshot
*/
public void setSnapshot(Snapshot snapshot) {
this.snapshot = snapshot;
}
/**
* @return the superPickaxeMode
*/
public BlockTool getSuperPickaxe() {
return pickaxeMode;
}
/**
* Set the super pickaxe tool.
*
* @param tool
*/
public void setSuperPickaxe(BlockTool tool) {
this.pickaxeMode = tool;
}
/**
* Get the tool assigned to the item.
*
* @param item
* @return the tool
*/
public Tool getTool(int item) {
return tools.get(item);
2011-01-09 19:14:55 +00:00
}
/**
* Get the brush tool assigned to the item. If there is no tool assigned
* or the tool is not assigned, the slot will be replaced with the
* brush tool.
*
* @param item
* @return the tool
2011-02-19 04:50:40 +00:00
* @throws InvalidToolBindException
2011-01-09 19:14:55 +00:00
*/
public BrushTool getBrushTool(int item) throws InvalidToolBindException {
Tool tool = getTool(item);
if (tool == null || !(tool instanceof BrushTool)) {
tool = new BrushTool();
setTool(item, tool);
}
return (BrushTool)tool;
2011-01-09 19:14:55 +00:00
}
/**
* Set the tool.
*
* @param item
* @param tool the tool to set
2011-02-19 04:50:40 +00:00
* @throws InvalidToolBindException
*/
public void setTool(int item, Tool tool) throws InvalidToolBindException {
if (item > 0 && item < 255) {
throw new InvalidToolBindException(item, "Blocks can't be used");
} else if (item == 263 || item == 348) {
throw new InvalidToolBindException(item, "Item is not usuable");
} else if (item == config.wandItem) {
throw new InvalidToolBindException(item, "Already used for the wand");
} else if (item == config.navigationWand) {
throw new InvalidToolBindException(item, "Already used for the navigation wand");
2011-02-19 04:50:40 +00:00
}
this.tools.put(item, tool);
}
/**
* Returns whether inventory usage is enabled for this session.
*
* @return the useInventory
*/
public boolean isUsingInventory() {
return useInventory;
}
/**
* Set the state of inventory usage.
*
* @param useInventory the useInventory to set
*/
public void setUseInventory(boolean useInventory) {
this.useInventory = useInventory;
}
2011-01-23 10:09:51 +00:00
/**
* Get the last script used.
*
2011-01-23 10:09:51 +00:00
* @return the lastScript
*/
public String getLastScript() {
return lastScript;
}
/**
* Set the last script used.
*
2011-01-23 10:09:51 +00:00
* @param lastScript the lastScript to set
*/
public void setLastScript(String lastScript) {
this.lastScript = lastScript;
}
2011-01-31 08:58:29 +00:00
/**
* Tell the player the WorldEdit version.
*
* @param player
2011-01-31 08:58:29 +00:00
*/
public void tellVersion(LocalPlayer player) {
if (config.showFirstUseVersion) {
if (!beenToldVersion) {
player.printRaw("\u00A78WorldEdit ver. " + WorldEdit.getVersion()
+ " (http://sk89q.com/projects/worldedit/)");
beenToldVersion = true;
}
}
}
2011-03-12 06:43:02 +00:00
/**
* Dispatch a CUI event but only if the player has CUI support.
*
* @param player
* @param event
*/
public void dispatchCUIEvent(LocalPlayer player, CUIEvent event) {
if (hasCUISupport) {
player.dispatchCUIEvent(event);
}
}
/**
* Dispatch the initial setup CUI messages.
*
* @param player
*/
public void dispatchCUISetup(LocalPlayer player) {
if (!hasCUISupport) {
return;
}
if (selector != null) {
dispatchCUISelection(player);
}
}
/**
* Send the selection information.
*
* @param player
*/
public void dispatchCUISelection(LocalPlayer player) {
if (!hasCUISupport) {
return;
}
player.dispatchCUIEvent(
new SelectionShapeEvent(selector.getTypeId()));
if (selector instanceof CUIPointBasedRegion) {
Vector[] points = ((CUIPointBasedRegion) selector).getCUIPoints();
int size = selector.getArea();
2011-03-12 06:43:02 +00:00
int i = 0;
for (Vector pt : points) {
if (pt != null) {
player.dispatchCUIEvent(
new SelectionPointEvent(i, pt, size));
2011-03-12 06:43:02 +00:00
}
i++;
2011-03-12 06:43:02 +00:00
}
}
}
/**
* Sets the status of CUI support.
*
* @param support
*/
public void setCUISupport(boolean support) {
hasCUISupport = true;
}
}