/
LocalSession.java
429 lines (386 loc) · 9.9 KB
/
LocalSession.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// $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/>.
*/
package com.sk89q.worldedit;
import java.util.LinkedList;
import com.sk89q.worldedit.snapshots.Snapshot;
import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.CuboidRegion;
/**
*
* @author sk89q
*/
public class LocalSession {
/**
* List of super pick axe modes.
*/
public static enum SuperPickaxeMode {
SINGLE,
SAME_TYPE_RECURSIVE,
SAME_TYPE_AREA
};
/**
* List of tools.
*/
public static enum Tool {
NONE,
INFO,
TREE,
}
public static final int MAX_HISTORY_SIZE = 15;
private boolean placeAtPos1 = false;
private Vector pos1, pos2;
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;
private SuperPickaxeMode superPickaxeMode = SuperPickaxeMode.SINGLE;
private Tool tool = Tool.NONE;
private int superPickaxeRange = 3;
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;
}
/**
* Checks to make sure that position 1 is defined.
*
* @throws IncompleteRegionException
*/
private void checkPos1() throws IncompleteRegionException {
if (pos1 == null) {
throw new IncompleteRegionException();
}
}
/**
* 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;
}
/**
* Gets defined position 1.
*
* @return position 1
* @throws IncompleteRegionException
*/
public Vector getPos1() throws IncompleteRegionException {
checkPos1();
return pos1;
}
/**
* Sets position 1.
*
* @param pt
*/
public void setPos1(Vector pt) {
pos1 = pt;
if (pos1 != null && pos2 != null) {
region = new CuboidRegion(pos1, pos2);
}
}
/**
* Gets position 2.
*
* @return position 2
* @throws IncompleteRegionException
*/
public Vector getPos2() throws IncompleteRegionException {
checkPos2();
return pos2;
}
/**
* Sets position 2.
*
* @param pt
*/
public void setPos2(Vector pt) {
pos2 = pt;
if (pos1 != null && pos2 != null) {
region = new CuboidRegion(pos1, pos2);
}
}
/**
* 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
* call learnRegionChanges().
*
* @return region
* @throws IncompleteRegionException
*/
public Region getRegion() throws IncompleteRegionException {
if (region == null) {
throw new IncompleteRegionException();
}
return region;
}
/**
* Gets the clipboard.
*
* @return clipboard, may be null
* @throws EmptyClipboardException
*/
public CuboidClipboard getClipboard() throws EmptyClipboardException {
if (clipboard == null) {
throw new EmptyClipboardException();
}
return clipboard;
}
/**
* Sets the clipboard.
*
* @param clipboard
*/
public void setClipboard(CuboidClipboard clipboard) {
this.clipboard = clipboard;
}
/**
* See if tool control is enabled.
*
* @return true if enabled
*/
public boolean isToolControlEnabled() {
return toolControl;
}
/**
* Change tool control setting.
*
* @param toolControl
*/
public void setToolControl(boolean toolControl) {
this.toolControl = toolControl;
}
/**
* Get the maximum number of blocks that can be changed in an edit session.
*
* @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;
}
/**
* Checks whether the super pick axe is enabled.
*
* @return status
*/
public boolean hasSuperPickAxe() {
return superPickAxe;
}
/**
* Enable super pick axe.
*
* @param superPickAxe
*/
public void enableSuperPickAxe() {
superPickAxe = true;
}
/**
* Disable super pick axe.
*
* @param superPickAxe
*/
public void disableSuperPickAxe() {
superPickAxe = false;
}
/**
* Toggle the super pick axe.
*
* @return status
*/
public boolean toggleSuperPickAxe() {
superPickAxe = !superPickAxe;
return superPickAxe;
}
/**
* @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;
}
return player.getInventoryBlockBag();
}
/**
* @return the snapshotName
*/
public Snapshot getSnapshot() {
return snapshot;
}
/**
* @param snapshot
*/
public void setSnapshot(Snapshot snapshot) {
this.snapshot = snapshot;
}
/**
* @return the superPickaxeMode
*/
public SuperPickaxeMode getSuperPickaxeMode() {
return superPickaxeMode;
}
/**
* @param superPickaxeMode the superPickaxeMode to set
*/
public void setSuperPickaxeMode(SuperPickaxeMode superPickaxeMode) {
this.superPickaxeMode = superPickaxeMode;
}
/**
* @return the superPickaxeRange
*/
public int getSuperPickaxeRange() {
return superPickaxeRange;
}
/**
* @param superPickaxeRange the superPickaxeRange to set
*/
public void setSuperPickaxeRange(int superPickaxeRange) {
this.superPickaxeRange = superPickaxeRange;
}
/**
* @return the tool
*/
public Tool getTool() {
return tool;
}
/**
* @param tool the tool to set
*/
public void setTool(Tool tool) {
this.tool = tool;
}
/**
* @return the useInventory
*/
public boolean isUsingInventory() {
return useInventory;
}
/**
* @param useInventory the useInventory to set
*/
public void setUseInventory(boolean useInventory) {
this.useInventory = useInventory;
}
}