Skip to content

Commit c6770c5

Browse files
me4502octylFractal
authored andcommitted
Add API to disable history tracking in EditSessions (#2189)
* Add API to disable history tracking in EditSessions * Update worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java Co-authored-by: Octavia Togami <[email protected]> Co-authored-by: Octavia Togami <[email protected]>
1 parent 8591aab commit c6770c5

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,40 @@ public void redo(EditSession editSession) {
12051205
}
12061206
//FAWE end
12071207

1208+
/**
1209+
* Gets whether this EditSession will track history.
1210+
*
1211+
* @return whether history is tracked
1212+
*/
1213+
public boolean isTrackingHistory() {
1214+
//FAWE start
1215+
return history;
1216+
//FAWE end
1217+
}
1218+
1219+
/**
1220+
* Sets whether this EditSession will track history.
1221+
*
1222+
* @param trackHistory whether to track history
1223+
*/
1224+
public void setTrackingHistory(boolean trackHistory) {
1225+
//FAWE start
1226+
if (trackHistory) {
1227+
if (this.history) {
1228+
if (this.changeSet == null) {
1229+
throw new IllegalStateException("No ChangeSetExtent is available");
1230+
}
1231+
enableHistory(this.changeSet);
1232+
}
1233+
} else {
1234+
if (this.history) {
1235+
disableHistory();
1236+
this.history = false;
1237+
}
1238+
}
1239+
//FAWE end
1240+
}
1241+
12081242
/**
12091243
* Get the number of changed blocks.
12101244
*

worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
public class ChangeSetExtent extends AbstractDelegateExtent {
4848

4949
private final ChangeSet changeSet;
50+
private boolean enabled;
5051

5152
/**
5253
* Create a new instance.
@@ -55,31 +56,65 @@ public class ChangeSetExtent extends AbstractDelegateExtent {
5556
* @param changeSet the change set
5657
*/
5758
public ChangeSetExtent(Extent extent, ChangeSet changeSet) {
59+
this(extent, changeSet, true);
60+
}
61+
62+
/**
63+
* Create a new instance.
64+
*
65+
* @param extent the extent
66+
* @param changeSet the change set
67+
* @param enabled if the extent is enabled
68+
*/
69+
public ChangeSetExtent(Extent extent, ChangeSet changeSet, boolean enabled) {
5870
super(extent);
5971
checkNotNull(changeSet);
6072
this.changeSet = changeSet;
73+
this.enabled = true;
74+
}
75+
76+
/**
77+
* If this extent is enabled and should perform change tracking.
78+
*
79+
* @return if enabled
80+
*/
81+
public boolean isEnabled() {
82+
return this.enabled;
83+
}
84+
85+
/**
86+
* Sets whether this extent is enabled and should perform change tracking.
87+
*
88+
* @param enabled whether to enable
89+
*/
90+
public void setEnabled(boolean enabled) {
91+
this.enabled = enabled;
6192
}
6293

6394
@Override
6495
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
65-
BaseBlock previous = getFullBlock(location);
66-
changeSet.add(new BlockChange(location, previous, block));
96+
if (enabled) {
97+
BaseBlock previous = getFullBlock(location);
98+
changeSet.add(new BlockChange(location, previous, block));
99+
}
67100
return super.setBlock(location, block);
68101
}
69102

70103
@Override
71104
public boolean setBiome(BlockVector3 position, BiomeType biome) {
72-
BiomeType previous = getBiome(position);
73-
changeSet.add(new BiomeChange3D(position, previous, biome));
105+
if (enabled) {
106+
BiomeType previous = getBiome(position);
107+
changeSet.add(new BiomeChange3D(position, previous, biome));
108+
}
74109
return super.setBiome(position, biome);
75110
}
76111

77112
@Nullable
78113
@Override
79114
public Entity createEntity(Location location, BaseEntity state) {
80115
Entity entity = super.createEntity(location, state);
81-
if (entity != null) {
82-
changeSet.add(new EntityCreate(location, entity.getState(), entity));
116+
if (enabled && entity != null) {
117+
changeSet.add(new EntityCreate(location, state, entity));
83118
}
84119
return entity;
85120
}
@@ -89,7 +124,7 @@ public Entity createEntity(Location location, BaseEntity state) {
89124
@Nullable
90125
public Entity createEntity(Location location, BaseEntity state, UUID uuid) {
91126
Entity entity = super.createEntity(location, state, uuid);
92-
if (entity != null) {
127+
if (enabled && entity != null) {
93128
changeSet.add(new EntityCreate(location, entity.getState(), entity));
94129
}
95130
return entity;
@@ -107,6 +142,9 @@ public List<? extends Entity> getEntities(Region region) {
107142
}
108143

109144
private List<? extends Entity> wrapEntities(List<? extends Entity> entities) {
145+
if (!enabled) {
146+
return entities;
147+
}
110148
List<Entity> newList = new ArrayList<>(entities.size());
111149
for (Entity entity : entities) {
112150
newList.add(new TrackedEntity(entity));

0 commit comments

Comments
 (0)