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

704 lines
18 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.Calendar;
import java.util.HashMap;
2011-01-01 01:40:07 +00:00
import java.util.LinkedList;
import java.util.Map;
import java.util.TimeZone;
import com.sk89q.jchronic.Chronic;
import com.sk89q.jchronic.Options;
import com.sk89q.jchronic.utils.Span;
import com.sk89q.jchronic.utils.Time;
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.SelectionShapeEvent;
import com.sk89q.worldedit.masks.Mask;
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;
public static int EXPIRATION_GRACE = 600000;
2011-01-31 08:58:29 +00:00
private LocalConfiguration config;
private long expirationTime = 0;
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();
2011-09-24 19:24:10 +00:00
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;
private boolean fastMode = false;
private Mask mask;
private TimeZone timezone = TimeZone.getDefault();
private Boolean jumptoBlock = true;
2011-01-31 08:58:29 +00:00
/**
* Construct the object.
*
* @param config
*/
public LocalSession(LocalConfiguration config) {
this.config = config;
}
/**
* Get the session's timezone.
*
* @return
*/
public TimeZone getTimeZone() {
return timezone;
}
/**
* Set the session's timezone.
*
* @param timezone
*/
public void setTimezone(TimeZone timezone) {
this.timezone = timezone;
}
/**
* 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
2011-09-24 19:24:10 +00:00
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();
newEditSession.setFastMode(fastMode);
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();
newEditSession.setFastMode(fastMode);
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("worldedit.brush.sphere");
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 == 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 (selector != null) {
dispatchCUISelection(player);
}
}
/**
* Send the selection information.
*
* @param player
*/
public void dispatchCUISelection(LocalPlayer player) {
if (!hasCUISupport) {
return;
}
2011-09-24 19:24:10 +00:00
player.dispatchCUIEvent(new SelectionShapeEvent(selector.getTypeId()));
if (selector instanceof CUIPointBasedRegion) {
2011-09-24 17:45:03 +00:00
((CUIPointBasedRegion) selector).describeCUI(player);
2011-03-12 06:43:02 +00:00
}
}
/**
* Gets the status of CUI support.
*
* @return
*/
public boolean hasCUISupport() {
return hasCUISupport;
}
2011-03-12 06:43:02 +00:00
/**
* Sets the status of CUI support.
*
* @param support
*/
public void setCUISupport(boolean support) {
hasCUISupport = true;
}
/**
* Detect date from a user's input.
*
* @param input
* @return
*/
public Calendar detectDate(String input) {
Time.setTimeZone(getTimeZone());
Options opt = new com.sk89q.jchronic.Options();
opt.setNow(Calendar.getInstance(getTimeZone()));
Span date = Chronic.parse(input, opt);
if (date == null) {
return null;
} else {
return date.getBeginCalendar();
}
}
/**
* Update the last update time for calculating expiration.
*/
public void update() {
expirationTime = System.currentTimeMillis();
}
/**
* Returns whether this session has expired.
*
* @return
*/
public boolean hasExpired() {
return System.currentTimeMillis() - expirationTime > EXPIRATION_GRACE;
}
/**
* Construct a new edit session.
*
* @param player
* @return
*/
public EditSession createEditSession(LocalPlayer player) {
BlockBag blockBag = getBlockBag(player);
// Create an edit session
EditSession editSession =
new EditSession(player.getWorld(),
getBlockChangeLimit(), blockBag);
editSession.setFastMode(fastMode);
editSession.setMask(mask);
return editSession;
}
/**
* Checks if the session has fast mode enabled.
*
* @return
*/
public boolean hasFastMode() {
return fastMode;
}
/**
* Set fast mode.
*
* @param fastMode
*/
public void setFastMode(boolean fastMode) {
this.fastMode = fastMode;
}
/**
* Get the mask.
*
* @return mask, may be null
*/
public Mask getMask() {
return mask;
}
/**
* Set a mask.
*
* @param mask mask or null
*/
public void setMask(Mask mask) {
this.mask = mask;
}
/**
* This is used as a workaround for a bug.
* It blocks the compass from using the jumpto function after the thru function
*/
public void toggleJumptoBlock() {
this.jumptoBlock = !jumptoBlock;
}
/**
* This is used as a workaround for a bug.
* @return true if the compass's jumpto function can be used again
*/
public Boolean canUseJumpto() {
return jumptoBlock;
}
}