Permalink
Browse files

Added an option to switch reorder modes

  • Loading branch information...
me4502 committed Dec 4, 2018
1 parent 0e48590 commit ee242ae89239694a52027c43ecb0e329da8d6aa6
@@ -149,6 +149,38 @@
BEFORE_CHANGE
}

/**
* Reorder mode for {@link EditSession#setReorderMode(ReorderMode)}.
*
* MULTI_STAGE = Multi stage reorder, may not be great with mods.
* FAST = Use the fast mode. Good for mods.
* NONE = Place blocks without worrying about placement order.
*/
public enum ReorderMode {
MULTI_STAGE("multi"),
FAST("fast"),
NONE("none");

private String name;

ReorderMode(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public static ReorderMode getFromName(String name) {
for (ReorderMode mode : values()) {
if (mode.getName().equalsIgnoreCase(name)) {
return mode;
}
}
return null;
}
}

@SuppressWarnings("ProtectedField")
protected final World world;
private final ChangeSet changeSet = new BlockOptimizedHistory();
@@ -170,7 +202,7 @@
private final Extent bypassHistory;
private final Extent bypassNone;

private final boolean useFastModeCorrections;
private ReorderMode reorderMode = ReorderMode.MULTI_STAGE;

private Mask oldMask;

@@ -189,13 +221,12 @@
checkNotNull(event);

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

if (world != null) {
Extent extent;

// These extents are ALWAYS used
extent = fastModeExtent = new FastModeExtent(world, useFastModeCorrections);
extent = fastModeExtent = new FastModeExtent(world, false);
extent = survivalExtent = new SurvivalModeExtent(extent, world);
extent = quirkExtent = new BlockQuirkExtent(extent, world);
extent = chunkLoadingExtent = new ChunkLoadingExtent(extent, world);
@@ -240,28 +271,74 @@ private Extent wrapExtent(Extent extent, EventBus eventBus, EditSessionEvent eve

// pkg private for TracedEditSession only, may later become public API
boolean commitRequired() {
if (isQueueEnabled() && reorderExtent.commitRequired()) {
if (reorderExtent.commitRequired()) {
return true;
}
if (isBatchingChunks() && chunkBatchingExtent.commitRequired()) {
return true;
}
if (hasFastMode() && fastModeExtent.commitRequired()) {
if (fastModeExtent != null && fastModeExtent.commitRequired()) {
return true;
}
return false;
}

/**
* Turns on specific features for a normal WorldEdit session, such as
* {@link #enableQueue() queuing} and {@link #setBatchingChunks(boolean)
* {@link #setReorderMode(ReorderMode)} reordering} and {@link #setBatchingChunks(boolean)
* chunk batching}.
*/
public void enableStandardMode() {
enableQueue();
setReorderMode(ReorderMode.MULTI_STAGE);
setBatchingChunks(true);
}

/**
* Sets the {@link ReorderMode} of this EditSession.
*
* @param reorderMode The reorder mode
*/
public void setReorderMode(ReorderMode reorderMode) {
if (reorderMode == ReorderMode.FAST && fastModeExtent == null) {
throw new IllegalArgumentException("An EditSession without a fast mode tried to use it for reordering!");
}
if (reorderMode == ReorderMode.MULTI_STAGE && reorderExtent == null) {
throw new IllegalArgumentException("An EditSession without a reorder extent tried to use it for reordering!");
}
this.reorderMode = reorderMode;
switch (reorderMode) {
case MULTI_STAGE:
if (fastModeExtent != null) {
fastModeExtent.setPostEditSimulationEnabled(false);
}
reorderExtent.setEnabled(true);
break;
case FAST:
fastModeExtent.setPostEditSimulationEnabled(true);
if (reorderExtent != null) {
reorderExtent.setEnabled(false);
}
break;
case NONE:
if (fastModeExtent != null) {
fastModeExtent.setPostEditSimulationEnabled(false);
}
if (reorderExtent != null) {
reorderExtent.setEnabled(false);
}
break;
}
}

/**
* Get the reorder mode.
*
* @return the reorder mode
*/
public ReorderMode getReorderMode() {
return reorderMode;
}

/**
* Get the world.
*
@@ -305,26 +382,31 @@ public void setBlockChangeLimit(int limit) {
* @return whether the queue is enabled
*/
public boolean isQueueEnabled() {
return !useFastModeCorrections && reorderExtent.isEnabled();
return reorderMode == ReorderMode.MULTI_STAGE && reorderExtent.isEnabled();
}

/**
* Queue certain types of block for better reproduction of those blocks.
*
* Uses {@link ReorderMode#MULTI_STAGE}
* @deprecated Use {@link EditSession#setReorderMode(ReorderMode)} with MULTI_STAGE instead.
*/
@Deprecated
public void enableQueue() {
if (!useFastModeCorrections) {
reorderExtent.setEnabled(true);
}
setReorderMode(ReorderMode.MULTI_STAGE);
}

/**
* Disable the queue. This will {@linkplain #flushSession() flush the session}.
*
* @deprecated Use {@link EditSession#setReorderMode(ReorderMode)} with another mode instead.
*/
@Deprecated
public void disableQueue() {
if (isQueueEnabled()) {
flushSession();
}
reorderExtent.setEnabled(false);
setReorderMode(ReorderMode.NONE);
}

/**
@@ -369,14 +451,7 @@ public SurvivalModeExtent getSurvivalExtent() {
*/
public void setFastMode(boolean enabled) {
if (fastModeExtent != null) {
// 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);
}
fastModeExtent.setEnabled(enabled);
}
}

@@ -451,16 +526,15 @@ public void setBatchingChunks(boolean batchingChunks) {
/**
* Disable all buffering extents.
*
* @see #disableQueue()
* @see #setReorderMode(ReorderMode)
* @see #setBatchingChunks(boolean)
*/
public void disableBuffering() {
// We optimize here to avoid repeated calls to flushSession.
boolean needsFlush = isQueueEnabled() || isBatchingChunks();
if (needsFlush) {
if (commitRequired()) {
flushSession();
}
reorderExtent.setEnabled(false);
setReorderMode(ReorderMode.NONE);
if (chunkBatchingExtent != null) {
chunkBatchingExtent.setEnabled(false);
}
@@ -93,6 +93,7 @@
private transient Mask mask;
private transient TimeZone timezone = TimeZone.getDefault();
private transient BlockVector3 cuiTemporaryBlock;
private transient EditSession.ReorderMode reorderMode = EditSession.ReorderMode.MULTI_STAGE;

// Saved properties
private String lastScript;
@@ -877,6 +878,24 @@ public void setFastMode(boolean fastMode) {
this.fastMode = fastMode;
}

/**
* Gets the reorder mode of the session.
*
* @return The reorder mode
*/
public EditSession.ReorderMode getReorderMode() {
return reorderMode;
}

/**
* Sets the reorder mode of the session.
*
* @param reorderMode The reorder mode
*/
public void setReorderMode(EditSession.ReorderMode reorderMode) {
this.reorderMode = reorderMode;
}

/**
* Get the mask.
*
@@ -115,6 +115,30 @@ public void fast(Player player, LocalSession session, EditSession editSession, C
}
}

@Command(
aliases = { "/reorder" },
usage = "[multi|fast|none]",
desc = "Sets the reorder mode of WorldEdit",
min = 0,
max = 1
)
@CommandPermissions("worldedit.reorder")
public void reorderMode(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
String newState = args.getString(0, null);
if (newState == null) {
player.print("The reorder mode is " + session.getReorderMode().getName());
} else {
EditSession.ReorderMode reorderMode = EditSession.ReorderMode.getFromName(newState);
if (reorderMode == null) {
player.printError("Unknown reorder mode!");
return;
}

session.setReorderMode(reorderMode);
player.print("The reorder mode is now " + session.getReorderMode().getName());
}
}

@Command(
aliases = { "/drawsel" },
usage = "[on|off]",
@@ -99,7 +99,7 @@ public void setEnabled(boolean enabled) {
}

public boolean commitRequired() {
return stages.stream().anyMatch(stage -> stage.size() > 0);
return enabled && stages.stream().anyMatch(stage -> stage.size() > 0);
}

/**
@@ -67,9 +67,6 @@ public FastModeExtent(World world, boolean enabled) {
checkNotNull(world);
this.world = world;
this.enabled = enabled;
if (enabled) {
this.postEditSimulation = true;
}
}

/**
@@ -100,11 +97,11 @@ public void setPostEditSimulationEnabled(boolean enabled) {

@Override
public boolean setBlock(BlockVector3 location, BlockStateHolder block) throws WorldEditException {
if (enabled) {
if (enabled || postEditSimulation) {
dirtyChunks.add(BlockVector2.at(location.getBlockX() >> 4, location.getBlockZ() >> 4));

if (world.setBlock(location, block, false)) {
if (postEditSimulation) {
if (!enabled && postEditSimulation) {
positions.add(location);
}
return true;
@@ -129,7 +126,7 @@ public Operation resume(RunContext run) throws WorldEditException {
world.fixAfterFastMode(dirtyChunks);
}

if (postEditSimulation) {
if (!enabled && postEditSimulation) {
Iterator<BlockVector3> positionIterator = positions.iterator();
while (run.shouldContinue() && positionIterator.hasNext()) {
BlockVector3 position = positionIterator.next();

0 comments on commit ee242ae

Please sign in to comment.