-
-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
899d6c8
commit 3f1652b
Showing
2 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
worldedit-core/src/main/java/com/sk89q/worldedit/extent/buffer/internal/BatchingExtent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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.extent.buffer.internal; | ||
|
||
import com.google.common.base.Throwables; | ||
import com.sk89q.worldedit.WorldEditException; | ||
import com.sk89q.worldedit.extent.AbstractBufferingExtent; | ||
import com.sk89q.worldedit.extent.Extent; | ||
import com.sk89q.worldedit.function.operation.Operation; | ||
import com.sk89q.worldedit.function.operation.RunContext; | ||
import com.sk89q.worldedit.math.BlockVector3; | ||
import com.sk89q.worldedit.util.collection.BlockMap; | ||
import com.sk89q.worldedit.world.block.BaseBlock; | ||
import com.sk89q.worldedit.world.block.BlockStateHolder; | ||
|
||
/** | ||
* An extent that buffers all changes until completed. | ||
*/ | ||
public class BatchingExtent extends AbstractBufferingExtent { | ||
|
||
private final BlockMap<BaseBlock> blockMap = BlockMap.createForBaseBlock(); | ||
private boolean enabled; | ||
|
||
public BatchingExtent(Extent extent) { | ||
this(extent, true); | ||
} | ||
|
||
public BatchingExtent(Extent extent, boolean enabled) { | ||
super(extent); | ||
this.enabled = enabled; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public boolean commitRequired() { | ||
return enabled; | ||
} | ||
|
||
@Override | ||
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException { | ||
if (!enabled) { | ||
return setDelegateBlock(location, block); | ||
} | ||
blockMap.put(location, block.toBaseBlock()); | ||
return true; | ||
} | ||
|
||
@Override | ||
protected BaseBlock getBufferedFullBlock(BlockVector3 position) { | ||
if (!enabled) { | ||
// Early exit if we're not enabled. | ||
return null; | ||
} | ||
return blockMap.get(position); | ||
} | ||
|
||
@Override | ||
protected Operation commitBefore() { | ||
if (!commitRequired()) { | ||
return null; | ||
} | ||
return new Operation() { | ||
|
||
@Override | ||
public Operation resume(RunContext run) throws WorldEditException { | ||
try { | ||
blockMap.forEach((position, block) -> { | ||
try { | ||
getExtent().setBlock(position, block); | ||
} catch (WorldEditException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} catch (RuntimeException e) { | ||
Throwables.throwIfInstanceOf(e.getCause(), WorldEditException.class); | ||
throw e; | ||
} | ||
blockMap.clear(); | ||
return null; | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
} | ||
}; | ||
} | ||
|
||
} |