-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
LocalSession.java
1930 lines (1754 loc) · 60.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.configuration.Settings;
import com.fastasyncworldedit.core.extent.ResettableExtent;
import com.fastasyncworldedit.core.extent.clipboard.DiskOptimizedClipboard;
import com.fastasyncworldedit.core.extent.clipboard.MultiClipboardHolder;
import com.fastasyncworldedit.core.history.DiskStorageHistory;
import com.fastasyncworldedit.core.internal.exception.FaweClipboardVersionMismatchException;
import com.fastasyncworldedit.core.internal.io.FaweInputStream;
import com.fastasyncworldedit.core.internal.io.FaweOutputStream;
import com.fastasyncworldedit.core.limit.FaweLimit;
import com.fastasyncworldedit.core.util.BrushCache;
import com.fastasyncworldedit.core.util.MainUtil;
import com.fastasyncworldedit.core.util.MaskTraverser;
import com.fastasyncworldedit.core.util.StringMan;
import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.TextureHolder;
import com.fastasyncworldedit.core.util.TextureUtil;
import com.fastasyncworldedit.core.wrappers.WorldWrapper;
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.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.command.tool.BlockTool;
import com.sk89q.worldedit.command.tool.BrushTool;
import com.sk89q.worldedit.command.tool.InvalidToolBindException;
import com.sk89q.worldedit.command.tool.NavigationWand;
import com.sk89q.worldedit.command.tool.SelectionWand;
import com.sk89q.worldedit.command.tool.SinglePickaxe;
import com.sk89q.worldedit.command.tool.Tool;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Locatable;
import com.sk89q.worldedit.extent.NullExtent;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.ChangeSetExecutor;
import com.sk89q.worldedit.history.changeset.ChangeSet;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.internal.cui.CUIRegion;
import com.sk89q.worldedit.internal.cui.SelectionShapeEvent;
import com.sk89q.worldedit.internal.cui.ServerCUIHandler;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.regions.selector.RegionSelectorType;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.Countable;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.Identifiable;
import com.sk89q.worldedit.util.SideEffectSet;
import com.sk89q.worldedit.util.nbt.CompoundBinaryTag;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.snapshot.experimental.Snapshot;
import com.zaxxer.sparsebits.SparseBitSet;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Stores session information.
*/
public class LocalSession implements TextureHolder {
public static int MAX_HISTORY_SIZE = 15;
private static final int CUI_VERSION_UNINITIALIZED = -1;
// Non-session related fields
private transient LocalConfiguration config;
private final transient AtomicBoolean dirty = new AtomicBoolean();
// Single-connection lifetime fields
private transient int failedCuiAttempts = 0;
private transient boolean hasCUISupport = false;
private transient int cuiVersion = CUI_VERSION_UNINITIALIZED;
// Session related
//FAWE start - allow saving to session store
private RegionSelector selector = new CuboidRegionSelector();
//FAWE end
private transient boolean placeAtPos1 = false;
//FAWE start
private final transient List<Object> history = Collections.synchronizedList(new LinkedList<>() {
@Override
public Object get(int index) {
Object value = super.get(index);
if (value instanceof Integer) {
value = getChangeSet(value);
set(index, value);
}
return value;
}
@Override
public Object remove(int index) {
return getChangeSet(super.remove(index));
}
});
private transient volatile Integer historyNegativeIndex;
private transient final ReentrantLock historyWriteLock = new ReentrantLock(true);
private final transient Int2ObjectOpenHashMap<Tool> tools = new Int2ObjectOpenHashMap<>(0);
private transient Mask sourceMask;
private transient TextureUtil texture;
private transient ResettableExtent transform = null;
private transient World currentWorld;
//FAWE end
private transient ClipboardHolder clipboard;
private transient final Object clipboardLock = new Object();
private transient boolean superPickaxe = false;
private transient BlockTool pickaxeMode = new SinglePickaxe();
private transient int maxBlocksChanged = -1;
private transient int maxTimeoutTime;
private transient boolean useInventory;
private transient com.sk89q.worldedit.world.snapshot.Snapshot snapshot;
private transient Snapshot snapshotExperimental;
private transient SideEffectSet sideEffectSet = SideEffectSet.defaults();
private transient Mask mask;
private transient ZoneId timezone = ZoneId.systemDefault();
private transient UUID uuid;
private transient volatile long historySize = 0;
private transient BlockVector3 cuiTemporaryBlock;
@SuppressWarnings("unused")
private final transient EditSession.ReorderMode reorderMode = EditSession.ReorderMode.MULTI_STAGE;
private transient List<Countable<BlockState>> lastDistribution;
private transient World worldOverride;
private transient boolean tickingWatchdog = false;
private transient boolean hasBeenToldVersion;
private transient boolean tracingActions;
// Saved properties
private String lastScript;
private RegionSelectorType defaultSelector;
private boolean useServerCUI = false; // Save this to not annoy players.
private ItemType wandItem;
private ItemType navWandItem;
/**
* Construct the object.
*
* <p>{@link #setConfiguration(LocalConfiguration)} should be called
* later with configuration.</p>
*/
public LocalSession() {
}
/**
* Construct the object.
*
* @param config the configuration
*/
public LocalSession(@Nullable LocalConfiguration config) {
this.config = config;
}
/**
* Set the configuration.
*
* @param config the configuration
*/
public void setConfiguration(LocalConfiguration config) {
checkNotNull(config);
this.config = config;
}
/**
* Called on post load of the session from persistent storage.
*/
public void postLoad() {
if (defaultSelector != null) {
this.selector = defaultSelector.createSelector();
}
//FAWE start
if (worldOverride != null) {
this.selector.setWorld(worldOverride);
} else {
this.selector.setWorld(currentWorld);
}
//FAWE end
}
//FAWE start
public boolean loadSessionHistoryFromDisk(UUID uuid, World world) {
if (world == null || uuid == null) {
return false;
}
if (Settings.settings().HISTORY.USE_DISK) {
MAX_HISTORY_SIZE = Integer.MAX_VALUE;
}
world = WorldWrapper.unwrap(world);
if (!world.equals(currentWorld)) {
this.uuid = uuid;
// Save history
saveHistoryNegativeIndex(uuid, currentWorld);
history.clear();
currentWorld = world;
// Load history
if (loadHistoryChangeSets(uuid, currentWorld)) {
loadHistoryNegativeIndex(uuid, currentWorld);
return true;
}
historyNegativeIndex = 0;
}
return false;
}
private boolean loadHistoryChangeSets(UUID uuid, World world) {
SparseBitSet set = new SparseBitSet();
final File folder = MainUtil.getFile(
Fawe.platform().getDirectory(),
Settings.settings().PATHS.HISTORY + File.separator + world.getName() + File.separator + uuid
);
if (folder.isDirectory()) {
folder.listFiles(pathname -> {
String name = pathname.getName();
Integer val = null;
if (pathname.isDirectory()) {
val = StringMan.toInteger(name, 0, name.length());
} else {
int i = name.lastIndexOf('.');
if (i != -1) {
val = StringMan.toInteger(name, 0, i);
}
}
if (val != null) {
set.set(val);
}
return false;
});
}
if (!set.isEmpty()) {
historySize = MainUtil.getTotalSize(folder.toPath());
for (int index = set.nextSetBit(0); index != -1; index = set.nextSetBit(index + 1)) {
history.add(index);
}
} else {
historySize = 0;
}
return !set.isEmpty();
}
private void loadHistoryNegativeIndex(UUID uuid, World world) {
if (!Settings.settings().HISTORY.USE_DISK) {
return;
}
File file = MainUtil.getFile(
Fawe.platform().getDirectory(),
Settings.settings().PATHS.HISTORY + File.separator + world.getName() + File.separator + uuid + File.separator + "index"
);
if (file.exists()) {
try (FaweInputStream is = new FaweInputStream(new FileInputStream(file))) {
historyNegativeIndex = Math.min(Math.max(0, is.readInt()), history.size());
} catch (IOException e) {
e.printStackTrace();
}
} else {
historyNegativeIndex = 0;
}
}
private void saveHistoryNegativeIndex(UUID uuid, World world) {
if (world == null || !Settings.settings().HISTORY.USE_DISK) {
return;
}
File file = MainUtil.getFile(
Fawe.platform().getDirectory(),
// Use "unsafe" method here as world does not need to be loaded to save this information.
Settings.settings().PATHS.HISTORY + File.separator + world.getNameUnsafe() + File.separator + uuid + File.separator + "index"
);
if (getHistoryNegativeIndex() != 0) {
try {
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
try (FaweOutputStream os = new FaweOutputStream(new FileOutputStream(file))) {
os.writeInt(getHistoryNegativeIndex());
}
} catch (IOException e) {
e.printStackTrace();
}
} else if (file.exists()) {
file.delete();
}
}
//FAWE end
/**
* Get whether this session is "dirty" and has changes that needs to
* be committed.
*
* @return true if dirty
*/
public boolean isDirty() {
return dirty.get();
}
/**
* Set this session as dirty.
*/
private void setDirty() {
dirty.set(true);
}
//FAWE start
public int getHistoryIndex() {
return history.size() - 1 - (historyNegativeIndex == null ? 0 : historyNegativeIndex);
}
public int getHistoryNegativeIndex() {
return (historyNegativeIndex == null ? historyNegativeIndex = 0 : historyNegativeIndex);
}
public List<ChangeSet> getHistory() {
return history.stream().map(this::getChangeSet).collect(Collectors.toList());
}
public boolean save() {
saveHistoryNegativeIndex(uuid, currentWorld);
if (defaultSelector == RegionSelectorType.CUBOID) {
defaultSelector = null;
}
return lastScript != null || defaultSelector != null;
}
//FAWE end
/**
* Get whether this session is "dirty" and has changes that needs to
* be committed, and reset it to {@code false}.
*
* @return true if the dirty value was {@code true}
*/
public boolean compareAndResetDirty() {
return dirty.compareAndSet(true, false);
}
/**
* Get the session's timezone.
*
* @return the timezone
*/
public ZoneId getTimeZone() {
return timezone;
}
/**
* Set the session's timezone.
*
* @param timezone the user's timezone
*/
public void setTimezone(ZoneId timezone) {
checkNotNull(timezone);
this.timezone = timezone;
}
/**
* Clear history.
*/
public void clearHistory() {
//FAWE start
boolean mainThread = Fawe.isMainThread();
if (mainThread && !historyWriteLock.tryLock()) {
// Do not make main thread wait if we cannot immediately clear history (on player logout usually)
TaskManager.taskManager().async(this::clearHistoryTask);
return;
}
try {
clearHistoryTask();
} finally {
// only if we are on the main thread, we ever called tryLock -> need to unlock again
if (mainThread) {
historyWriteLock.unlock();
}
}
}
private void clearHistoryTask() {
historyWriteLock.lock();
try {
// Ensure that changesets are properly removed
for (Object item : history) {
getChangeSet(item).delete();
}
history.clear();
} finally {
historyWriteLock.unlock();
}
historyNegativeIndex = 0;
save();
historySize = 0;
currentWorld = null;
}
//FAWE end
/**
* Remember an edit session for the undo history. If the history maximum
* size is reached, old edit sessions will be discarded.
*
* @param editSession the edit session
*/
public void remember(EditSession editSession) {
checkNotNull(editSession);
// Don't store anything if no changes were made
if (editSession.size() == 0) {
return;
}
//FAWE start
Actor actor = editSession.getActor();
int limit = actor == null ? Integer.MAX_VALUE : actor.getLimit().MAX_HISTORY;
remember(editSession, true, limit);
}
private ChangeSet getChangeSet(Object o) {
if (o instanceof ChangeSet) {
ChangeSet cs = (ChangeSet) o;
try {
cs.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return cs;
}
if (o instanceof Integer) {
File folder = MainUtil.getFile(
Fawe.platform().getDirectory(),
Settings.settings().PATHS.HISTORY + File.separator + currentWorld.getName() + File.separator + uuid
);
File specific = new File(folder, o.toString());
if (specific.isDirectory()) {
// TODO NOT IMPLEMENTED
// return new AnvilHistory(currentWorld.getName(), specific);
} else {
return new DiskStorageHistory(currentWorld, this.uuid, (Integer) o);
}
}
return null;
}
public void remember(Identifiable player, World world, ChangeSet changeSet, FaweLimit limit) {
historyWriteLock.lock();
try {
if (Settings.settings().HISTORY.USE_DISK) {
LocalSession.MAX_HISTORY_SIZE = Integer.MAX_VALUE;
}
if (changeSet.size() == 0) {
return;
}
loadSessionHistoryFromDisk(player.getUniqueId(), world);
if (changeSet instanceof ChangeSet) {
ListIterator<Object> iter = history.listIterator();
int i = 0;
int cutoffIndex = history.size() - getHistoryNegativeIndex();
while (iter.hasNext()) {
Object item = iter.next();
if (++i > cutoffIndex) {
ChangeSet oldChangeSet;
if (item instanceof ChangeSet) {
oldChangeSet = (ChangeSet) item;
} else {
oldChangeSet = getChangeSet(item);
}
historySize -= MainUtil.getSize(oldChangeSet);
iter.remove();
}
}
}
historySize += MainUtil.getSize(changeSet);
history.add(changeSet);
if (getHistoryNegativeIndex() != 0) {
setDirty();
historyNegativeIndex = 0;
}
if (limit != null) {
int limitMb = limit.MAX_HISTORY;
while (((!Settings.settings().HISTORY.USE_DISK && history.size() > MAX_HISTORY_SIZE) || (historySize >> 20) > limitMb) && history
.size() > 1) {
ChangeSet item = (ChangeSet) history.remove(0);
item.delete();
long size = MainUtil.getSize(item);
historySize -= size;
}
}
} finally {
historyWriteLock.unlock();
}
}
public void remember(EditSession editSession, boolean append, int limitMb) {
historyWriteLock.lock();
try {
if (Settings.settings().HISTORY.USE_DISK) {
LocalSession.MAX_HISTORY_SIZE = Integer.MAX_VALUE;
}
// It should have already been flushed, but just in case!
editSession.flushQueue();
if (editSession.getChangeSet() == null || limitMb == 0 || historySize >> 20 > limitMb && !append) {
return;
}
ChangeSet changeSet = editSession.getChangeSet();
if (changeSet.isEmpty()) {
return;
}
Actor actor = editSession.getActor();
if (actor != null) {
loadSessionHistoryFromDisk(actor.getUniqueId(), editSession.getWorld());
}
// Destroy any sessions after this undo point
if (append) {
ListIterator<Object> iter = history.listIterator();
int i = 0;
int cutoffIndex = history.size() - getHistoryNegativeIndex();
while (iter.hasNext()) {
Object item = iter.next();
if (++i > cutoffIndex) {
ChangeSet oldChangeSet;
if (item instanceof ChangeSet) {
oldChangeSet = (ChangeSet) item;
} else {
oldChangeSet = getChangeSet(item);
}
historySize -= MainUtil.getSize(oldChangeSet);
iter.remove();
}
}
}
historySize += MainUtil.getSize(changeSet);
if (append) {
history.add(changeSet);
if (getHistoryNegativeIndex() != 0) {
setDirty();
historyNegativeIndex = 0;
}
} else {
history.add(0, changeSet);
}
while (((!Settings.settings().HISTORY.USE_DISK && history.size() > MAX_HISTORY_SIZE) || (historySize >> 20) > limitMb) && history
.size() > 1) {
ChangeSet item = (ChangeSet) history.remove(0);
item.delete();
long size = MainUtil.getSize(item);
historySize -= size;
}
// free the mask from any remaining references to e.g. extents
// if used again
new MaskTraverser(mask).reset(NullExtent.INSTANCE);
} finally {
historyWriteLock.unlock();
}
}
//FAWE end
/**
* Performs an undo.
*
* @param newBlockBag a new block bag
* @param actor the actor
* @return whether anything was undone
*/
public EditSession undo(@Nullable BlockBag newBlockBag, Actor actor) {
checkNotNull(actor);
//FAWE start - use our logic
World world = (actor instanceof Player) ? ((Player) actor).getWorldForEditing() : getWorldOverride();
if (world == null) {
throw new MissingWorldException();
}
loadSessionHistoryFromDisk(actor.getUniqueId(), world);
if (getHistoryNegativeIndex() < history.size()) {
ChangeSet changeSet = getChangeSet(history.get(getHistoryIndex()));
EditSessionBuilder builder = WorldEdit.getInstance().newEditSessionBuilder().world(world)
.checkMemory(false)
.changeSetNull()
.fastMode(false)
.limitUnprocessed(actor)
.actor(actor);
if (actor instanceof Player) {
builder = builder.blockBag(getBlockBag((Player) actor));
}
if (!actor.getLimit().RESTRICT_HISTORY_TO_REGIONS) {
builder = builder.allowedRegionsEverywhere();
}
try (EditSession newEditSession = builder.build()) {
newEditSession.setBlocks(changeSet, ChangeSetExecutor.Type.UNDO);
setDirty();
historyNegativeIndex++;
return newEditSession;
}
} else {
int size = history.size();
if (getHistoryNegativeIndex() != size) {
historyNegativeIndex = history.size();
setDirty();
}
return null;
}
//FAWE end
}
/**
* Performs a redo
*
* @param newBlockBag a new block bag
* @param actor the actor
* @return whether anything was redone
*/
//FAWE start - use our logic
public EditSession redo(@Nullable BlockBag newBlockBag, Actor actor) {
checkNotNull(actor);
World world = (actor instanceof Player) ? ((Player) actor).getWorldForEditing() : getWorldOverride();
if (world == null) {
throw new MissingWorldException();
}
loadSessionHistoryFromDisk(actor.getUniqueId(), world);
if (getHistoryNegativeIndex() > 0) {
setDirty();
historyNegativeIndex--;
ChangeSet changeSet = getChangeSet(history.get(getHistoryIndex()));
EditSessionBuilder builder = WorldEdit.getInstance().newEditSessionBuilder().world(world)
.checkMemory(false)
.changeSetNull()
.fastMode(false)
.limitUnprocessed(actor)
.actor(actor);
if (actor instanceof Player) {
builder = builder.blockBag(getBlockBag((Player) actor));
}
if (!actor.getLimit().RESTRICT_HISTORY_TO_REGIONS) {
builder = builder.allowedRegionsEverywhere();
}
try (EditSession newEditSession = builder.build()) {
newEditSession.setBlocks(changeSet, ChangeSetExecutor.Type.REDO);
return newEditSession;
}
}
//FAWE end
return null;
}
public boolean hasWorldOverride() {
return this.worldOverride != null;
}
@Nullable
public World getWorldOverride() {
return this.worldOverride;
}
public void setWorldOverride(@Nullable World worldOverride) {
this.worldOverride = worldOverride;
}
public boolean isTickingWatchdog() {
return tickingWatchdog;
}
public void setTickingWatchdog(boolean tickingWatchdog) {
this.tickingWatchdog = tickingWatchdog;
}
public boolean isTracingActions() {
return tracingActions;
}
public void setTracingActions(boolean tracingActions) {
this.tracingActions = tracingActions;
}
/**
* Get the default region selector.
*
* @return the default region selector
*/
public RegionSelectorType getDefaultRegionSelector() {
return defaultSelector;
}
/**
* Set the default region selector.
*
* @param defaultSelector the default region selector
*/
public void setDefaultRegionSelector(RegionSelectorType defaultSelector) {
checkNotNull(defaultSelector);
this.defaultSelector = defaultSelector;
setDirty();
}
/**
* Get the region selector for defining the selection. If the selection
* was defined for a different world, the old selection will be discarded.
*
* @param world the world
* @return position the position
*/
public RegionSelector getRegionSelector(World world) {
checkNotNull(world);
if (selector.getWorld() == null || !selector.getWorld().equals(world)) {
selector.setWorld(world);
selector.clear();
if (hasWorldOverride() && !world.equals(getWorldOverride())) {
setWorldOverride(null);
}
}
return selector;
}
/**
* Set the region selector.
*
* @param world the world
* @param selector the selector
*/
public void setRegionSelector(World world, RegionSelector selector) {
checkNotNull(world);
checkNotNull(selector);
selector.setWorld(world);
this.selector = selector;
setDirty();
if (hasWorldOverride() && !world.equals(getWorldOverride())) {
setWorldOverride(null);
}
}
/**
* Returns true if the region is fully defined for the specified world.
*
* @param world the world
* @return true if a region selection is defined
*/
public boolean isSelectionDefined(World world) {
checkNotNull(world);
if (selector.getIncompleteRegion().getWorld() == null || !selector.getIncompleteRegion().getWorld().equals(world)) {
return false;
}
return selector.isDefined();
}
/**
* Get the selection region. If you change the region, you should
* call learnRegionChanges(). If the selection is not fully defined,
* the {@code IncompleteRegionException} exception will be thrown.
*
* <p>Note that this method will return a region in the current selection world,
* which is not guaranteed to be the player's world or even the current world
* override. If you require a specific world, use the
* {@link LocalSession#getSelection(World)} overload instead.
*
* @return the selected region
* @throws IncompleteRegionException if the region is not fully defined
*/
public Region getSelection() throws IncompleteRegionException {
return getSelection(getSelectionWorld());
}
/**
* Get the selection region. If you change the region, you should
* call learnRegionChanges(). If the selection is defined in
* a different world, or the selection isn't fully defined,
* the {@code IncompleteRegionException} exception will be thrown.
*
* @param world the world
* @return a region
* @throws IncompleteRegionException if no region is selected, or the provided world is null
*/
public Region getSelection(@Nullable World world) throws IncompleteRegionException {
if (world == null || selector.getIncompleteRegion().getWorld() == null
|| !selector.getIncompleteRegion().getWorld().equals(world)) {
throw new IncompleteRegionException() {
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
};
}
return selector.getRegion();
}
/**
* Get the selection world.
*
* @return the the world of the selection
*/
@Nullable
public World getSelectionWorld() {
World world = selector.getIncompleteRegion().getWorld();
if (world instanceof WorldWrapper) {
return ((WorldWrapper) world).getParent();
}
return world;
}
/**
* Gets the clipboard.
*
* @return clipboard
* @throws EmptyClipboardException thrown if no clipboard is set
*/
public ClipboardHolder getClipboard() throws EmptyClipboardException {
//FAWE start
synchronized (clipboardLock) {
if (clipboard == null) {
throw new EmptyClipboardException();
}
//FAWE end
return clipboard;
}
}
//FAWE start
@Nullable
public ClipboardHolder getExistingClipboard() {
synchronized (clipboardLock) {
if (clipboard == null) {
return null;
}
return clipboard;
}
}
public void addClipboard(@Nonnull MultiClipboardHolder toAppend) {
checkNotNull(toAppend);
ClipboardHolder existing = getExistingClipboard();
MultiClipboardHolder multi;
if (existing instanceof MultiClipboardHolder) {
multi = (MultiClipboardHolder) existing;
for (ClipboardHolder holder : toAppend.getHolders()) {
multi.add(holder);
}
} else {
multi = toAppend;
if (existing != null) {
multi.add(existing);
}
}
setClipboard(multi);
}
/**
* Ensure the player's clipboard is flushed. (will only do something with clipboard-on-disk)
*/
public void flushClipboard() {
synchronized (clipboardLock) {
if (this.clipboard != null) {
try {
this.clipboard.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Load a clipboard from disk and into this localsession. Synchronises with other clipboard setting/getting to and from
* this session
*
* @param file Clipboard file to load
* @throws FaweClipboardVersionMismatchException in clipboard version mismatch (between saved and internal, expected, version)
* @throws ExecutionException if the computation threw an exception
* @throws InterruptedException if the current thread was interrupted while waiting
*/
public void loadClipboardFromDisk(File file) throws FaweClipboardVersionMismatchException, ExecutionException,
InterruptedException {
synchronized (clipboardLock) {
if (file.exists() && file.length() > 5) {
try {
if (getClipboard() != null) {
return;
}
} catch (EmptyClipboardException ignored) {
}
DiskOptimizedClipboard doc = Fawe.instance().getClipboardExecutor().submit(
uuid,
() -> DiskOptimizedClipboard.loadFromFile(file)
).get();
Clipboard clip = doc.toClipboard();
ClipboardHolder holder = new ClipboardHolder(clip);
setClipboard(holder);
}
}
}
public void deleteClipboardOnDisk() {
synchronized (clipboardLock) {
ClipboardHolder holder = getExistingClipboard();
if (holder != null) {
for (Clipboard clipboard : holder.getClipboards()) {
DiskOptimizedClipboard doc;
if (clipboard instanceof DiskOptimizedClipboard) {
doc = (DiskOptimizedClipboard) clipboard;
} else if (clipboard instanceof BlockArrayClipboard && ((BlockArrayClipboard) clipboard).getParent() instanceof DiskOptimizedClipboard) {
doc = (DiskOptimizedClipboard) ((BlockArrayClipboard) clipboard).getParent();
} else {
continue;
}
Fawe.instance().getClipboardExecutor().submit(uuid, () -> {
doc.close(); // Ensure closed before deletion
doc.getFile().delete();
});
}
}
}
}
//FAWE end
/**
* Sets the clipboard.
*
* <p>Pass {@code null} to clear the clipboard.</p>
*
* @param clipboard the clipboard, or null if the clipboard is to be cleared
*/
public void setClipboard(@Nullable ClipboardHolder clipboard) {
//FAWE start
synchronized (clipboardLock) {
if (this.clipboard == clipboard) {
return;
}
if (this.clipboard != null) {
if (clipboard == null || !clipboard.contains(this.clipboard.getClipboard())) {
this.clipboard.close();
}
}
//FAWE end
this.clipboard = clipboard;
}
}
/**
* @return true always - see deprecation notice
* @deprecated The wand is now a tool that can be bound/unbound.
*/
@Deprecated
public boolean isToolControlEnabled() {
return true;
}
/**
* @param toolControl unused - see deprecation notice
* @deprecated The wand is now a tool that can be bound/unbound.
*/