fix: fix adding/getting masks to/from MaskExtent when it is a processor (#2351)

- fixes #2350
Dieser Commit ist enthalten in:
Jordan 2023-07-15 16:40:29 +01:00 committet von GitHub
Ursprung 8da530ee80
Commit f4da4b0287
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 67 neuen und 9 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,44 @@
package com.fastasyncworldedit.core.util;
import com.fastasyncworldedit.core.extent.processor.BatchProcessorHolder;
import com.fastasyncworldedit.core.extent.processor.MultiBatchProcessor;
import com.fastasyncworldedit.core.queue.IBatchProcessor;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayDeque;
import java.util.Queue;
public class ProcessorTraverser<T extends IBatchProcessor> {
private static final Logger LOGGER = LogManagerCompat.getLogger();
private final T root;
public ProcessorTraverser(@Nonnull T root) {
this.root = root;
}
public <U extends IBatchProcessor> @Nullable U find(Class<U> clazz) {
try {
Queue<IBatchProcessor> processors = new ArrayDeque<>();
IBatchProcessor processor = root;
do {
if (clazz.isAssignableFrom(processor.getClass())) {
return clazz.cast(processor);
} else if (processor instanceof MultiBatchProcessor multiProcessor) {
processors.addAll(multiProcessor.getBatchProcessors());
} else if (processor instanceof BatchProcessorHolder holder) {
processors.add(holder.getProcessor());
}
} while ((processor = processors.poll()) != null);
return null;
} catch (Throwable e) {
LOGGER.error("Error traversing processors", e);
return null;
}
}
}

Datei anzeigen

@ -30,6 +30,7 @@ import com.fastasyncworldedit.core.extent.ResettableExtent;
import com.fastasyncworldedit.core.extent.SingleRegionExtent; import com.fastasyncworldedit.core.extent.SingleRegionExtent;
import com.fastasyncworldedit.core.extent.SourceMaskExtent; import com.fastasyncworldedit.core.extent.SourceMaskExtent;
import com.fastasyncworldedit.core.extent.clipboard.WorldCopyClipboard; import com.fastasyncworldedit.core.extent.clipboard.WorldCopyClipboard;
import com.fastasyncworldedit.core.extent.processor.ExtentBatchProcessorHolder;
import com.fastasyncworldedit.core.extent.processor.lighting.NullRelighter; import com.fastasyncworldedit.core.extent.processor.lighting.NullRelighter;
import com.fastasyncworldedit.core.extent.processor.lighting.Relighter; import com.fastasyncworldedit.core.extent.processor.lighting.Relighter;
import com.fastasyncworldedit.core.function.SurfaceRegionFunction; import com.fastasyncworldedit.core.function.SurfaceRegionFunction;
@ -55,6 +56,7 @@ import com.fastasyncworldedit.core.queue.implementation.preloader.Preloader;
import com.fastasyncworldedit.core.util.ExtentTraverser; import com.fastasyncworldedit.core.util.ExtentTraverser;
import com.fastasyncworldedit.core.util.MaskTraverser; import com.fastasyncworldedit.core.util.MaskTraverser;
import com.fastasyncworldedit.core.util.MathMan; import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.ProcessorTraverser;
import com.fastasyncworldedit.core.util.TaskManager; import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.collection.BlockVector3Set; import com.fastasyncworldedit.core.util.collection.BlockVector3Set;
import com.fastasyncworldedit.core.util.task.RunnableVal; import com.fastasyncworldedit.core.util.task.RunnableVal;
@ -524,9 +526,15 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
* @return mask, may be null * @return mask, may be null
*/ */
public Mask getMask() { public Mask getMask() {
//FAWE start - ExtendTraverser & MaskingExtents //FAWE start - ExtentTraverser & MaskingExtents
ExtentTraverser<MaskingExtent> maskingExtent = new ExtentTraverser<>(getExtent()).find(MaskingExtent.class); MaskingExtent maskingExtent = new ExtentTraverser<>(getExtent()).findAndGet(MaskingExtent.class);
return maskingExtent != null ? maskingExtent.get().getMask() : null; if (maskingExtent == null) {
ExtentTraverser<ExtentBatchProcessorHolder> processorExtent =
new ExtentTraverser<>(getExtent()).find(ExtentBatchProcessorHolder.class);
maskingExtent =
new ProcessorTraverser<>(processorExtent.get().getProcessor()).find(MaskingExtent.class);
}
return maskingExtent != null ? maskingExtent.getMask() : null;
//FAWE end //FAWE end
} }
@ -609,23 +617,29 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
//FAWE start - use MaskingExtent & ExtentTraverser //FAWE start - use MaskingExtent & ExtentTraverser
/** /**
* Set a mask. * Set a mask. Combines with any existing masks, set null to clear existing masks.
* *
* @param mask mask or null * @param mask mask or null
*/ */
public void setMask(Mask mask) { public void setMask(@Nullable Mask mask) {
if (mask == null) { if (mask == null) {
mask = Masks.alwaysTrue(); mask = Masks.alwaysTrue();
} else { } else {
new MaskTraverser(mask).reset(this); new MaskTraverser(mask).reset(this);
} }
ExtentTraverser<MaskingExtent> maskingExtent = new ExtentTraverser<>(getExtent()).find(MaskingExtent.class); MaskingExtent maskingExtent = new ExtentTraverser<>(getExtent()).findAndGet(MaskingExtent.class);
if (maskingExtent != null && maskingExtent.get() != null) { if (maskingExtent == null && mask != Masks.alwaysTrue()) {
Mask oldMask = maskingExtent.get().getMask(); ExtentTraverser<ExtentBatchProcessorHolder> processorExtent =
new ExtentTraverser<>(getExtent()).find(ExtentBatchProcessorHolder.class);
maskingExtent =
new ProcessorTraverser<>(processorExtent.get().getProcessor()).find(MaskingExtent.class);
}
if (maskingExtent != null) {
Mask oldMask = maskingExtent.getMask();
if (oldMask instanceof ResettableMask) { if (oldMask instanceof ResettableMask) {
((ResettableMask) oldMask).reset(); ((ResettableMask) oldMask).reset();
} }
maskingExtent.get().setMask(mask); maskingExtent.setMask(mask);
} else if (mask != Masks.alwaysTrue()) { } else if (mask != Masks.alwaysTrue()) {
addProcessor(new MaskingExtent(getExtent(), mask)); addProcessor(new MaskingExtent(getExtent(), mask));
} }