Fix source masks using EditSession instead of clipboard. (#515)

This commit is contained in:
wizjany 2019-08-10 11:54:22 -04:00 committed by GitHub
parent f83de2a703
commit 61bc012f6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 91 additions and 21 deletions

View File

@ -48,6 +48,7 @@ import com.sk89q.worldedit.function.mask.BlockTypeMask;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.annotation.ClipboardMask;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.factory.RegionFactory; import com.sk89q.worldedit.regions.factory.RegionFactory;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
@ -156,9 +157,13 @@ public class BrushCommands {
@Switch(name = 'b', desc = "Paste biomes if available") @Switch(name = 'b', desc = "Paste biomes if available")
boolean pasteBiomes, boolean pasteBiomes,
@ArgFlag(name = 'm', desc = "Skip blocks matching this mask in the clipboard", def = "") @ArgFlag(name = 'm', desc = "Skip blocks matching this mask in the clipboard", def = "")
@ClipboardMask
Mask sourceMask) throws WorldEditException { Mask sourceMask) throws WorldEditException {
ClipboardHolder holder = session.getClipboard(); ClipboardHolder holder = session.getClipboard();
Clipboard clipboard = holder.getClipboard(); Clipboard clipboard = holder.getClipboard();
ClipboardHolder newHolder = new ClipboardHolder(clipboard);
newHolder.setTransform(holder.getTransform());
BlockVector3 size = clipboard.getDimensions(); BlockVector3 size = clipboard.getDimensions();
@ -167,7 +172,7 @@ public class BrushCommands {
worldEdit.checkMaxBrushRadius(size.getBlockZ() / 2D - 1); worldEdit.checkMaxBrushRadius(size.getBlockZ() / 2D - 1);
BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType()); BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType());
tool.setBrush(new ClipboardBrush(holder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, sourceMask), "worldedit.brush.clipboard"); tool.setBrush(new ClipboardBrush(newHolder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, sourceMask), "worldedit.brush.clipboard");
player.print("Clipboard brush shape equipped."); player.print("Clipboard brush shape equipped.");
} }

View File

@ -35,6 +35,7 @@ import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.annotation.ClipboardMask;
import com.sk89q.worldedit.internal.annotation.Direction; import com.sk89q.worldedit.internal.annotation.Direction;
import com.sk89q.worldedit.internal.annotation.Selection; import com.sk89q.worldedit.internal.annotation.Selection;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
@ -143,6 +144,7 @@ public class ClipboardCommands {
@Switch(name = 'b', desc = "Paste biomes if available") @Switch(name = 'b', desc = "Paste biomes if available")
boolean pasteBiomes, boolean pasteBiomes,
@ArgFlag(name = 'm', desc = "Only paste blocks matching this mask", def = "") @ArgFlag(name = 'm', desc = "Only paste blocks matching this mask", def = "")
@ClipboardMask
Mask sourceMask) throws WorldEditException { Mask sourceMask) throws WorldEditException {
ClipboardHolder holder = session.getClipboard(); ClipboardHolder holder = session.getClipboard();

View File

@ -19,6 +19,7 @@
package com.sk89q.worldedit.command.argument; package com.sk89q.worldedit.command.argument;
import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItem;
@ -27,9 +28,14 @@ import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.extent.transform.BlockTransformExtent;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.annotation.ClipboardMask;
import com.sk89q.worldedit.internal.registry.AbstractFactory; import com.sk89q.worldedit.internal.registry.AbstractFactory;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
@ -41,30 +47,53 @@ import org.enginehub.piston.converter.SuccessfulConversion;
import org.enginehub.piston.inject.InjectedValueAccess; import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.Key; import org.enginehub.piston.inject.Key;
import javax.annotation.Nullable;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
public class FactoryConverter<T> implements ArgumentConverter<T> { public class FactoryConverter<T> implements ArgumentConverter<T> {
public static void register(WorldEdit worldEdit, CommandManager commandManager) { public static void register(WorldEdit worldEdit, CommandManager commandManager) {
commandManager.registerConverter(Key.of(Pattern.class), commandManager.registerConverter(Key.of(Pattern.class),
new FactoryConverter<>(worldEdit, WorldEdit::getPatternFactory, "pattern")); new FactoryConverter<>(worldEdit, WorldEdit::getPatternFactory, "pattern", null));
commandManager.registerConverter(Key.of(Mask.class), commandManager.registerConverter(Key.of(Mask.class),
new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask")); new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask", null));
commandManager.registerConverter(Key.of(BaseItem.class), commandManager.registerConverter(Key.of(BaseItem.class),
new FactoryConverter<>(worldEdit, WorldEdit::getItemFactory, "item")); new FactoryConverter<>(worldEdit, WorldEdit::getItemFactory, "item", null));
commandManager.registerConverter(Key.of(Mask.class, ClipboardMask.class),
new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask",
context -> {
try {
ClipboardHolder holder = context.getSession().getClipboard();
Transform transform = holder.getTransform();
Extent target;
if (transform.isIdentity()) {
target = holder.getClipboard();
} else {
target = new BlockTransformExtent(holder.getClipboard(), transform);
}
context.setExtent(target);
} catch (EmptyClipboardException e) {
throw new IllegalStateException(e);
}
}));
} }
private final WorldEdit worldEdit; private final WorldEdit worldEdit;
private final Function<WorldEdit, AbstractFactory<T>> factoryExtractor; private final Function<WorldEdit, AbstractFactory<T>> factoryExtractor;
private final String description; private final String description;
@Nullable private final Consumer<ParserContext> contextTweaker;
private FactoryConverter(WorldEdit worldEdit, private FactoryConverter(WorldEdit worldEdit,
Function<WorldEdit, AbstractFactory<T>> factoryExtractor, Function<WorldEdit, AbstractFactory<T>> factoryExtractor,
String description) { String description,
@Nullable Consumer<ParserContext> contextTweaker) {
this.worldEdit = worldEdit; this.worldEdit = worldEdit;
this.factoryExtractor = factoryExtractor; this.factoryExtractor = factoryExtractor;
this.description = description; this.description = description;
this.contextTweaker = contextTweaker;
} }
@Override @Override
@ -80,10 +109,15 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
if (extent instanceof World) { if (extent instanceof World) {
parserContext.setWorld((World) extent); parserContext.setWorld((World) extent);
} }
parserContext.setExtent(new RequestExtent());
} }
parserContext.setSession(session); parserContext.setSession(session);
parserContext.setRestricted(true); parserContext.setRestricted(true);
if (contextTweaker != null) {
contextTweaker.accept(parserContext);
}
try { try {
return SuccessfulConversion.fromSingle( return SuccessfulConversion.fromSingle(
factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext) factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext)

View File

@ -28,7 +28,6 @@ import com.sk89q.worldedit.function.mask.BiomeMask2D;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Masks; import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeType;
import java.util.Arrays; import java.util.Arrays;
@ -78,6 +77,6 @@ public class BiomeMaskParser extends InputParser<Mask> {
biomes.add(biome); biomes.add(biome);
} }
return Masks.asMask(new BiomeMask2D(new RequestExtent(), biomes)); return Masks.asMask(new BiomeMask2D(context.getExtent(), biomes));
} }
} }

View File

@ -26,7 +26,6 @@ import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.BlockCategoryMask; import com.sk89q.worldedit.function.mask.BlockCategoryMask;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.world.block.BlockCategory; import com.sk89q.worldedit.world.block.BlockCategory;
import java.util.Locale; import java.util.Locale;
@ -54,7 +53,7 @@ public class BlockCategoryMaskParser extends InputParser<Mask> {
if (category == null) { if (category == null) {
throw new InputParseException("Unrecognised tag '" + input.substring(2) + '\''); throw new InputParseException("Unrecognised tag '" + input.substring(2) + '\'');
} else { } else {
return new BlockCategoryMask(new RequestExtent(), category); return new BlockCategoryMask(context.getExtent(), category);
} }
} }
} }

View File

@ -26,7 +26,6 @@ import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.BlockStateMask; import com.sk89q.worldedit.function.mask.BlockStateMask;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -53,7 +52,7 @@ public class BlockStateMaskParser extends InputParser<Mask> {
boolean strict = input.charAt(1) == '='; boolean strict = input.charAt(1) == '=';
String states = input.substring(2 + (strict ? 1 : 0), input.length() - 1); String states = input.substring(2 + (strict ? 1 : 0), input.length() - 1);
try { try {
return new BlockStateMask(new RequestExtent(), return new BlockStateMask(context.getExtent(),
Splitter.on(',').omitEmptyStrings().trimResults().withKeyValueSeparator('=').split(states), Splitter.on(',').omitEmptyStrings().trimResults().withKeyValueSeparator('=').split(states),
strict); strict);
} catch (Exception e) { } catch (Exception e) {

View File

@ -26,7 +26,6 @@ import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.BlockMask; import com.sk89q.worldedit.function.mask.BlockMask;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BaseBlock;
import java.util.Set; import java.util.Set;
@ -56,7 +55,7 @@ public class BlocksMaskParser extends InputParser<Mask> {
if (holders.isEmpty()) { if (holders.isEmpty()) {
return null; return null;
} }
return new BlockMask(new RequestExtent(), holders); return new BlockMask(context.getExtent(), holders);
} catch (NoMatchException e) { } catch (NoMatchException e) {
return null; return null;
} }

View File

@ -25,7 +25,6 @@ import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.ExistingBlockMask; import com.sk89q.worldedit.function.mask.ExistingBlockMask;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser; import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import java.util.List; import java.util.List;
@ -44,6 +43,6 @@ public class ExistingMaskParser extends SimpleInputParser<Mask> {
@Override @Override
public Mask parseFromSimpleInput(String input, ParserContext context) { public Mask parseFromSimpleInput(String input, ParserContext context) {
return new ExistingBlockMask(new RequestExtent()); return new ExistingBlockMask(context.getExtent());
} }
} }

View File

@ -30,7 +30,6 @@ import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment; import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment;
import com.sk89q.worldedit.session.SessionOwner; import com.sk89q.worldedit.session.SessionOwner;
import com.sk89q.worldedit.session.request.RequestExtent;
import java.util.function.IntSupplier; import java.util.function.IntSupplier;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -58,7 +57,7 @@ public class ExpressionMaskParser extends InputParser<Mask> {
try { try {
Expression exp = Expression.compile(input.substring(1), "x", "y", "z"); Expression exp = Expression.compile(input.substring(1), "x", "y", "z");
WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment( WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment(
new RequestExtent(), Vector3.ONE, Vector3.ZERO); context.getExtent(), Vector3.ONE, Vector3.ZERO);
exp.setEnvironment(env); exp.setEnvironment(env);
if (context.getActor() != null) { if (context.getActor() != null) {
SessionOwner owner = context.getActor(); SessionOwner owner = context.getActor();

View File

@ -29,7 +29,6 @@ import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.function.mask.OffsetMask; import com.sk89q.worldedit.function.mask.OffsetMask;
import com.sk89q.worldedit.internal.registry.InputParser; import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.session.request.RequestExtent;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -62,7 +61,7 @@ public class OffsetMaskParser extends InputParser<Mask> {
if (input.length() > 1) { if (input.length() > 1) {
submask = worldEdit.getMaskFactory().parseFromInput(input.substring(1), context); submask = worldEdit.getMaskFactory().parseFromInput(input.substring(1), context);
} else { } else {
submask = new ExistingBlockMask(new RequestExtent()); submask = new ExistingBlockMask(context.getExtent());
} }
OffsetMask offsetMask = new OffsetMask(submask, BlockVector3.at(0, firstChar == '>' ? -1 : 1, 0)); OffsetMask offsetMask = new OffsetMask(submask, BlockVector3.at(0, firstChar == '>' ? -1 : 1, 0));
return new MaskIntersection(offsetMask, Masks.negate(submask)); return new MaskIntersection(offsetMask, Masks.negate(submask));

View File

@ -25,7 +25,6 @@ import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.SolidBlockMask; import com.sk89q.worldedit.function.mask.SolidBlockMask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser; import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import java.util.List; import java.util.List;
@ -44,6 +43,6 @@ public class SolidMaskParser extends SimpleInputParser<Mask> {
@Override @Override
public Mask parseFromSimpleInput(String input, ParserContext context) { public Mask parseFromSimpleInput(String input, ParserContext context) {
return new SolidBlockMask(new RequestExtent()); return new SolidBlockMask(context.getExtent());
} }
} }

View File

@ -0,0 +1,37 @@
/*
* 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 Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.internal.annotation;
import com.sk89q.worldedit.function.mask.Mask;
import org.enginehub.piston.inject.InjectAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotates a {@link Mask} parameter to use the clipboard as the extent instead of target World/EditSession.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@InjectAnnotation
public @interface ClipboardMask {
}