Skip to content

Commit

Permalink
Added "fast mode corrections"
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Oct 15, 2018
1 parent aff05d6 commit 98e6de3
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,17 @@ public boolean setBlock(Vector position, BlockStateHolder block, boolean notifyA
}
}

@Override
public boolean notifyAndLightBlock(Vector position, com.sk89q.worldedit.world.block.BlockState previousType) throws WorldEditException {
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
if (adapter != null) {
adapter.notifyAndLightBlock(BukkitAdapter.adapt(getWorld(), position), previousType);
return true;
}

return false;
}

@Override
public BaseBlock getFullBlock(Vector position) {
BukkitImplAdapter adapter = WorldEditPlugin.getInstance().getBukkitImplAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import org.bukkit.Location;
Expand Down Expand Up @@ -78,6 +79,15 @@ public interface BukkitImplAdapter {
*/
boolean setBlock(Location location, BlockStateHolder state, boolean notifyAndLight);

/**
* Notifies the simulation that the block at the given location has
* been changed and it must be re-lighted (and issue other events).
*
* @param position position of the block
* @param previousType the type of the previous block that was there
*/
void notifyAndLightBlock(Location position, BlockState previousType);

/**
* Get the state for the given entity.
*
Expand Down
Binary file not shown.
Binary file not shown.
27 changes: 18 additions & 9 deletions worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import static com.sk89q.worldedit.regions.Regions.maximumBlockY;
import static com.sk89q.worldedit.regions.Regions.minimumBlockY;

import com.sk89q.worldedit.function.block.BlockDistributionCounter;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.event.extent.EditSessionEvent;
Expand All @@ -48,6 +46,7 @@
import com.sk89q.worldedit.extent.world.SurvivalModeExtent;
import com.sk89q.worldedit.function.GroundFunction;
import com.sk89q.worldedit.function.RegionMaskingFilter;
import com.sk89q.worldedit.function.block.BlockDistributionCounter;
import com.sk89q.worldedit.function.block.BlockReplace;
import com.sk89q.worldedit.function.block.Counter;
import com.sk89q.worldedit.function.block.Naturalizer;
Expand Down Expand Up @@ -113,8 +112,6 @@
import com.sk89q.worldedit.world.registry.LegacyMapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -169,6 +166,8 @@ public enum Stage {
private final Extent bypassHistory;
private final Extent bypassNone;

private final boolean useFastModeCorrections;

private Mask oldMask;

/**
Expand All @@ -186,12 +185,13 @@ public enum Stage {
checkNotNull(event);

this.world = world;
this.useFastModeCorrections = false;

if (world != null) {
Extent extent;

// These extents are ALWAYS used
extent = fastModeExtent = new FastModeExtent(world, false);
extent = fastModeExtent = new FastModeExtent(world, useFastModeCorrections);
extent = survivalExtent = new SurvivalModeExtent(extent, world);
extent = quirkExtent = new BlockQuirkExtent(extent, world);
extent = chunkLoadingExtent = new ChunkLoadingExtent(extent, world);
Expand Down Expand Up @@ -287,14 +287,16 @@ public void setBlockChangeLimit(int limit) {
* @return whether the queue is enabled
*/
public boolean isQueueEnabled() {
return reorderExtent.isEnabled();
return !useFastModeCorrections && reorderExtent.isEnabled();
}

/**
* Queue certain types of block for better reproduction of those blocks.
*/
public void enableQueue() {
reorderExtent.setEnabled(true);
if (!useFastModeCorrections) {
reorderExtent.setEnabled(true);
}
}

/**
Expand All @@ -304,7 +306,7 @@ public void disableQueue() {
if (isQueueEnabled()) {
flushQueue();
}
reorderExtent.setEnabled(true);
reorderExtent.setEnabled(false);
}

/**
Expand Down Expand Up @@ -349,7 +351,14 @@ public SurvivalModeExtent getSurvivalExtent() {
*/
public void setFastMode(boolean enabled) {
if (fastModeExtent != null) {
fastModeExtent.setEnabled(enabled);
// If fast mode corrections are enabled, we're using fast mode for
// multipass support. Thus, we do not actually ever turn the fast mode
// extent off, we instead toggle post edit simulation
if (useFastModeCorrections) {
fastModeExtent.setPostEditSimulationEnabled(!enabled);
} else {
fastModeExtent.setEnabled(enabled);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import com.sk89q.worldedit.function.operation.RunContext;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;

/**
Expand All @@ -40,8 +43,10 @@
public class FastModeExtent extends AbstractDelegateExtent {

private final World world;
private final Queue<Vector> positions = new ArrayDeque<>();
private final Set<BlockVector2D> dirtyChunks = new HashSet<>();
private boolean enabled = true;
private boolean postEditSimulation;

/**
* Create a new instance with fast mode enabled.
Expand All @@ -63,6 +68,9 @@ public FastModeExtent(World world, boolean enabled) {
checkNotNull(world);
this.world = world;
this.enabled = enabled;
if (enabled) {
this.postEditSimulation = true;
}
}

/**
Expand All @@ -83,11 +91,27 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public boolean isPostEditSimulationEnabled() {
return postEditSimulation;
}

public void setPostEditSimulationEnabled(boolean enabled) {
this.postEditSimulation = enabled;
}

@Override
public boolean setBlock(Vector location, BlockStateHolder block) throws WorldEditException {
if (enabled) {
dirtyChunks.add(new BlockVector2D(location.getBlockX() >> 4, location.getBlockZ() >> 4));
return world.setBlock(location, block, false);

if (world.setBlock(location, block, false)) {
if (postEditSimulation) {
positions.offer(location);
}
return true;
}

return false;
} else {
return world.setBlock(location, block, true);
}
Expand All @@ -101,6 +125,16 @@ public Operation resume(RunContext run) throws WorldEditException {
if (!dirtyChunks.isEmpty()) {
world.fixAfterFastMode(dirtyChunks);
}

if (postEditSimulation) {
while (run.shouldContinue() && !positions.isEmpty()) {
Vector position = positions.poll(); // Remove from queue
world.notifyAndLightBlock(position, BlockTypes.AIR.getDefaultState());
}

return !positions.isEmpty() ? this : null;
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface ChangeSet {
* @return whether or not the ChangeSet is set to record changes
*/
boolean isRecordingChanges();

/**
* Tell the change set whether to record changes or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public boolean setBlock(Vector position, BlockStateHolder block, boolean notifyA
return false;
}

@Override
public boolean notifyAndLightBlock(Vector position, BlockState previousType) throws WorldEditException {
return false;
}

@Override
public int getBlockLightLevel(Vector position) {
return 0;
Expand Down
11 changes: 11 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/worldedit/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.weather.WeatherType;
Expand Down Expand Up @@ -94,6 +95,16 @@ public interface World extends Extent {
*/
boolean setBlock(Vector position, BlockStateHolder block, boolean notifyAndLight) throws WorldEditException;

/**
* Notifies the simulation that the block at the given location has
* been changed and it must be re-lighted (and issue other events).
*
* @param position position of the block
* @param previousType the type of the previous block that was there
* @return true if the block was successfully notified
*/
boolean notifyAndLightBlock(Vector position, BlockState previousType) throws WorldEditException;

/**
* Get the light level at the given block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ public boolean setBlock(Vector position, BlockStateHolder block, boolean notifyA
return successful;
}

@Override
public boolean notifyAndLightBlock(Vector position, BlockState previousType) throws WorldEditException {
// TODO Implement
return false;
}

// Can't get the "Object" to be right for withProperty w/o this
@SuppressWarnings({ "rawtypes", "unchecked" })
private IBlockState applyProperties(BlockStateContainer stateContainer, IBlockState newState, Map<Property<?>, Object> states) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public boolean setBlock(Vector position, BlockStateHolder block, boolean notifyA
return true;
}

@Override
public boolean notifyAndLightBlock(Vector position, com.sk89q.worldedit.world.block.BlockState previousType) throws WorldEditException {
// TODO Move this to adapter
return false;
}

@Override
public boolean regenerate(Region region, EditSession editSession) {
return false;
Expand Down

0 comments on commit 98e6de3

Please sign in to comment.