From 57b47d24515bea4ff90e7c011c1872b5b12146a3 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Tue, 21 Sep 2021 14:59:49 +0100 Subject: [PATCH] Use a MaskTraverser to set extent where it will be a WorldWrapper otherwise --- .../core/util/MaskTraverser.java | 53 +++++++++++++++++++ .../worldedit/command/GenerationCommands.java | 10 ++-- .../worldedit/command/RegionCommands.java | 23 +++----- .../worldedit/command/SelectionCommands.java | 6 +-- .../worldedit/command/UtilityCommands.java | 10 ++-- 5 files changed, 68 insertions(+), 34 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MaskTraverser.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MaskTraverser.java index af20ac5b8..416cae7b5 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MaskTraverser.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MaskTraverser.java @@ -73,4 +73,57 @@ public class MaskTraverser { } } + public void setNewExtent(Extent newExtent) { + setNewExtent(mask, newExtent); + } + + private void setNewExtent(Mask mask, Extent newExtent) { + if (mask == null) { + return; + } + Class current = mask.getClass(); + while (current.getSuperclass() != null) { + if (mask instanceof AbstractExtentMask) { + AbstractExtentMask mask1 = (AbstractExtentMask) mask; + mask1.setExtent(newExtent); + } else { + try { + Field field = current.getDeclaredField("extent"); + field.setAccessible(true); + field.set(mask, newExtent); + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + } + if (mask instanceof MaskIntersection) { + MaskIntersection mask1 = (MaskIntersection) mask; + try { + Field field = mask1.getClass().getDeclaredField("masks"); + field.setAccessible(true); + Collection masks = (Collection) field.get(mask); + for (Mask next : masks) { + setNewExtent(next, newExtent); + } + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + } + try { + Field field = current.getDeclaredField("mask"); + field.setAccessible(true); + Mask next = (Mask) field.get(mask); + setNewExtent(next, newExtent); + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + try { + Field field = current.getDeclaredField("masks"); + field.setAccessible(true); + Collection masks = (Collection) field.get(mask); + for (Mask next : masks) { + setNewExtent(next, newExtent); + } + } catch (NoSuchFieldException | IllegalAccessException ignored) { + } + current = current.getSuperclass(); + } + } + } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java index bf602be13..54f78e983 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java @@ -23,6 +23,7 @@ import com.fastasyncworldedit.core.Fawe; import com.fastasyncworldedit.core.configuration.Caption; import com.fastasyncworldedit.core.function.generator.CavesGen; import com.fastasyncworldedit.core.util.MainUtil; +import com.fastasyncworldedit.core.util.MaskTraverser; import com.fastasyncworldedit.core.util.MathMan; import com.fastasyncworldedit.core.util.TextureUtil; import com.fastasyncworldedit.core.util.image.ImageUtil; @@ -37,7 +38,6 @@ import com.sk89q.worldedit.command.util.annotation.Confirm; import com.sk89q.worldedit.command.util.annotation.Preload; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; -import com.sk89q.worldedit.function.mask.AbstractExtentMask; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.pattern.Pattern; @@ -560,9 +560,7 @@ public class GenerationCommands { @Selection Region region, @Arg(desc = "Mask") Mask mask ) throws WorldEditException { - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); editSession.addOres(region, mask); actor.print(Caption.of("fawe.worldedit.visitor.visitor.block", editSession.getBlockChangeCount())); } @@ -633,9 +631,7 @@ public class GenerationCommands { @Arg(desc = "Ore vein min y", def = "0") int minY, @Arg(desc = "Ore vein max y", def = "63") int maxY ) throws WorldEditException { - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); checkCommandArgument(minY >= editSession.getMinY(), Caption.of("fawe.error.outside-range-lower", "miny", editSession.getMinY() )); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java index ea2635a6f..f3cd5c0c1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java @@ -24,6 +24,7 @@ import com.fastasyncworldedit.core.FaweCache; import com.fastasyncworldedit.core.configuration.Caption; import com.fastasyncworldedit.core.extent.processor.lighting.RelightMode; import com.fastasyncworldedit.core.object.FaweLimit; +import com.fastasyncworldedit.core.util.MaskTraverser; import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.LocalSession; @@ -37,7 +38,6 @@ import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.function.GroundFunction; import com.sk89q.worldedit.function.generator.FloraGenerator; -import com.sk89q.worldedit.function.mask.AbstractExtentMask; import com.sk89q.worldedit.function.mask.ExistingBlockMask; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.MaskIntersection; @@ -321,9 +321,8 @@ public class RegionCommands { if (from == null) { from = new ExistingBlockMask(editSession); //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow - } else if (from instanceof AbstractExtentMask) { - ((AbstractExtentMask) from).setExtent(editSession); - //FAWE end + } else { + new MaskTraverser(from).setNewExtent(editSession); } int affected = editSession.replaceBlocks(region, from, to); actor.print(Caption.of("worldedit.replace.replaced", TextComponent.of(affected))); @@ -467,9 +466,7 @@ public class RegionCommands { Mask mask ) throws WorldEditException { //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow) - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); //FAWE end BlockVector3 min = region.getMinimumPoint(); BlockVector3 max = region.getMaximumPoint(); @@ -582,9 +579,7 @@ public class RegionCommands { checkCommandArgument(count >= 1, "Count must be >= 1"); //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow) - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); //FAWE end Mask combinedMask; if (ignoreAirBlocks) { @@ -664,9 +659,7 @@ public class RegionCommands { ) throws WorldEditException { //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow) - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); //FAWE end Mask combinedMask; if (ignoreAirBlocks) { @@ -842,9 +835,7 @@ public class RegionCommands { //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow) Mask finalMask; if (mask != null) { - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); finalMask = mask; } else { finalMask = new SolidBlockMask(editSession); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java index 4bbe1068c..a248fae3b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java @@ -24,6 +24,7 @@ import com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder; import com.fastasyncworldedit.core.function.mask.IdMask; import com.fastasyncworldedit.core.regions.selector.FuzzyRegionSelector; import com.fastasyncworldedit.core.regions.selector.PolyhedralRegionSelector; +import com.fastasyncworldedit.core.util.MaskTraverser; import com.google.common.base.Strings; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.LocalSession; @@ -42,7 +43,6 @@ import com.sk89q.worldedit.extension.platform.Locatable; import com.sk89q.worldedit.extension.platform.permission.ActorSelectorLimits; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.function.block.BlockDistributionCounter; -import com.sk89q.worldedit.function.mask.AbstractExtentMask; import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.visitor.RegionVisitor; @@ -576,9 +576,7 @@ public class SelectionCommands { Mask mask ) throws WorldEditException { //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow) - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); //FAWE end int count = editSession.countBlocks(session.getSelection(world), mask); actor.print(Caption.of("worldedit.count.counted", TextComponent.of(count))); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java index 15d4b3bc2..d6d61dc23 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/UtilityCommands.java @@ -24,6 +24,7 @@ import com.fastasyncworldedit.core.configuration.Caption; import com.fastasyncworldedit.core.configuration.Settings; import com.fastasyncworldedit.core.function.QuadFunction; import com.fastasyncworldedit.core.util.MainUtil; +import com.fastasyncworldedit.core.util.MaskTraverser; import com.fastasyncworldedit.core.util.TaskManager; import com.fastasyncworldedit.core.util.image.ImageUtil; import com.fastasyncworldedit.core.util.task.DelegateConsumer; @@ -49,7 +50,6 @@ import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; import com.sk89q.worldedit.function.EntityFunction; -import com.sk89q.worldedit.function.mask.AbstractExtentMask; import com.sk89q.worldedit.function.mask.BlockTypeMask; import com.sk89q.worldedit.function.mask.ExistingBlockMask; import com.sk89q.worldedit.function.mask.Mask; @@ -462,9 +462,7 @@ public class UtilityCommands { int radius ) throws WorldEditException { //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow) - if (mask instanceof AbstractExtentMask) { - ((AbstractExtentMask) mask).setExtent(editSession); - } + new MaskTraverser(mask).setNewExtent(editSession); //FAWE end radius = Math.max(1, radius); we.checkMaxRadius(radius); @@ -491,9 +489,7 @@ public class UtilityCommands { Pattern to ) throws WorldEditException { //FAWE start > the mask will have been initialised with a WorldWrapper extent (very bad/slow) - if (from instanceof AbstractExtentMask) { - ((AbstractExtentMask) from).setExtent(editSession); - } + new MaskTraverser(from).setNewExtent(editSession); //FAWE end radius = Math.max(1, radius); we.checkMaxRadius(radius);