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 <octavia.togami@gmail.com>

Co-authored-by: Octavia Togami <octavia.togami@gmail.com>
This commit is contained in:
Maddy Miller 2022-09-18 20:38:03 +10:00 committed by GitHub
parent 3f343b6b91
commit 267bbb307f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 6 deletions

View File

@ -207,6 +207,7 @@ public String getDisplayName() {
private final MultiStageReorder reorderExtent;
private final MaskingExtent maskingExtent;
private final BlockChangeLimiter changeLimiter;
private @Nullable ChangeSetExtent changeSetExtent;
private final List<WatchdogTickingExtent> watchdogExtents = new ArrayList<>(2);
private final Extent bypassReorderHistory;
@ -284,7 +285,7 @@ public String getDisplayName() {
this.bypassHistory = traceIfNeeded(new DataValidatorExtent(extent, world));
// These extents can be skipped by calling smartSetBlock()
extent = traceIfNeeded(new ChangeSetExtent(extent, changeSet));
extent = traceIfNeeded(changeSetExtent = new ChangeSetExtent(extent, changeSet));
extent = traceIfNeeded(maskingExtent = new MaskingExtent(extent, Masks.alwaysTrue()));
extent = traceIfNeeded(changeLimiter = new BlockChangeLimiter(extent, maxBlocks));
extent = wrapExtent(extent, eventBus, event, Stage.BEFORE_HISTORY);
@ -864,6 +865,28 @@ public void redo(EditSession editSession) {
editSession.internalFlushSession();
}
/**
* Gets whether this EditSession will track history.
*
* @return whether history is tracked
*/
public boolean isTrackingHistory() {
return changeSetExtent != null && changeSetExtent.isEnabled();
}
/**
* Sets whether this EditSession will track history.
*
* @param trackHistory whether to track history
*/
public void setTrackingHistory(boolean trackHistory) {
if (changeSetExtent != null) {
changeSetExtent.setEnabled(trackHistory);
} else if (trackHistory) {
throw new IllegalStateException("No ChangeSetExtent is available");
}
}
/**
* Get the number of changed blocks.
*

View File

@ -46,6 +46,7 @@
public class ChangeSetExtent extends AbstractDelegateExtent {
private final ChangeSet changeSet;
private boolean enabled;
/**
* Create a new instance.
@ -54,22 +55,56 @@ public class ChangeSetExtent extends AbstractDelegateExtent {
* @param changeSet the change set
*/
public ChangeSetExtent(Extent extent, ChangeSet changeSet) {
this(extent, changeSet, true);
}
/**
* Create a new instance.
*
* @param extent the extent
* @param changeSet the change set
* @param enabled if the extent is enabled
*/
public ChangeSetExtent(Extent extent, ChangeSet changeSet, boolean enabled) {
super(extent);
checkNotNull(changeSet);
this.changeSet = changeSet;
this.enabled = true;
}
/**
* If this extent is enabled and should perform change tracking.
*
* @return if enabled
*/
public boolean isEnabled() {
return this.enabled;
}
/**
* Sets whether this extent is enabled and should perform change tracking.
*
* @param enabled whether to enable
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
BaseBlock previous = getFullBlock(location);
changeSet.add(new BlockChange(location, previous, block));
if (enabled) {
BaseBlock previous = getFullBlock(location);
changeSet.add(new BlockChange(location, previous, block));
}
return super.setBlock(location, block);
}
@Override
public boolean setBiome(BlockVector3 position, BiomeType biome) {
BiomeType previous = getBiome(position);
changeSet.add(new BiomeChange3D(position, previous, biome));
if (enabled) {
BiomeType previous = getBiome(position);
changeSet.add(new BiomeChange3D(position, previous, biome));
}
return super.setBiome(position, biome);
}
@ -77,7 +112,7 @@ public boolean setBiome(BlockVector3 position, BiomeType biome) {
@Override
public Entity createEntity(Location location, BaseEntity state) {
Entity entity = super.createEntity(location, state);
if (entity != null) {
if (enabled && entity != null) {
changeSet.add(new EntityCreate(location, state, entity));
}
return entity;
@ -94,6 +129,9 @@ public List<? extends Entity> getEntities(Region region) {
}
private List<? extends Entity> wrapEntities(List<? extends Entity> entities) {
if (!enabled) {
return entities;
}
List<Entity> newList = new ArrayList<>(entities.size());
for (Entity entity : entities) {
newList.add(new TrackedEntity(entity));