Skip to content
Permalink
Browse files

Fix source masks using EditSession instead of clipboard.

Eventually this entire system (Request) needs to be ripped out.
  • Loading branch information...
wizjany committed Aug 7, 2019
1 parent 11a532b commit c461c608f1febaadf39c24bbd43d83faf98b89b6
@@ -48,6 +48,7 @@
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.annotation.ClipboardMask;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.factory.RegionFactory;
import com.sk89q.worldedit.session.ClipboardHolder;
@@ -156,6 +157,7 @@ public void clipboardBrush(Player player, LocalSession session,
@Switch(name = 'b', desc = "Paste biomes if available")
boolean pasteBiomes,
@ArgFlag(name = 'm', desc = "Skip blocks matching this mask in the clipboard", def = "")
@ClipboardMask
Mask sourceMask) throws WorldEditException {
ClipboardHolder holder = session.getClipboard();
Clipboard clipboard = holder.getClipboard();
@@ -35,6 +35,7 @@
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
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.Selection;
import com.sk89q.worldedit.math.BlockVector3;
@@ -143,6 +144,7 @@ public void paste(Player player, LocalSession session, EditSession editSession,
@Switch(name = 'b', desc = "Paste biomes if available")
boolean pasteBiomes,
@ArgFlag(name = 'm', desc = "Only paste blocks matching this mask", def = "")
@ClipboardMask
Mask sourceMask) throws WorldEditException {

ClipboardHolder holder = session.getClipboard();
@@ -19,6 +19,8 @@

package com.sk89q.worldedit.command.argument;

import com.google.auto.value.AutoAnnotation;
import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BaseItem;
@@ -29,7 +31,9 @@
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.Mask;
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.session.request.RequestExtent;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.World;
@@ -42,29 +46,48 @@
import org.enginehub.piston.inject.Key;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class FactoryConverter<T> implements ArgumentConverter<T> {

@AutoAnnotation
private static ClipboardMask clipboardMask() {
return new AutoAnnotation_FactoryConverter_clipboardMask();
}

public static void register(WorldEdit worldEdit, CommandManager commandManager) {
commandManager.registerConverter(Key.of(Pattern.class),
new FactoryConverter<>(worldEdit, WorldEdit::getPatternFactory, "pattern"));
new FactoryConverter<>(worldEdit, WorldEdit::getPatternFactory, "pattern", c -> {}));
commandManager.registerConverter(Key.of(Mask.class),
new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask"));
new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask", c -> {}));
commandManager.registerConverter(Key.of(BaseItem.class),
new FactoryConverter<>(worldEdit, WorldEdit::getItemFactory, "item"));
new FactoryConverter<>(worldEdit, WorldEdit::getItemFactory, "item", c -> {}));

commandManager.registerConverter(Key.of(Mask.class, clipboardMask()),
new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask",
context -> {
try {
context.setExtent(context.getSession().getClipboard().getClipboard());
} catch (EmptyClipboardException e) {
throw new IllegalStateException(e);
}
}));
}

private final WorldEdit worldEdit;
private final Function<WorldEdit, AbstractFactory<T>> factoryExtractor;
private final String description;
private final Consumer<ParserContext> contextTweaker;

private FactoryConverter(WorldEdit worldEdit,
Function<WorldEdit, AbstractFactory<T>> factoryExtractor,
String description) {
String description,
Consumer<ParserContext> contextTweaker) {
this.worldEdit = worldEdit;
this.factoryExtractor = factoryExtractor;
this.description = description;
this.contextTweaker = contextTweaker;
}

@Override
@@ -80,10 +103,13 @@ private FactoryConverter(WorldEdit worldEdit,
if (extent instanceof World) {
parserContext.setWorld((World) extent);
}
parserContext.setExtent(new RequestExtent());
}
parserContext.setSession(session);
parserContext.setRestricted(true);

contextTweaker.accept(parserContext);

try {
return SuccessfulConversion.fromSingle(
factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext)
@@ -28,7 +28,6 @@
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.world.biome.BiomeType;

import java.util.Arrays;
@@ -78,6 +77,6 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
biomes.add(biome);
}

return Masks.asMask(new BiomeMask2D(new RequestExtent(), biomes));
return Masks.asMask(new BiomeMask2D(context.getExtent(), biomes));
}
}
@@ -26,7 +26,6 @@
import com.sk89q.worldedit.function.mask.BlockCategoryMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.world.block.BlockCategory;

import java.util.Locale;
@@ -54,7 +53,7 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
if (category == null) {
throw new InputParseException("Unrecognised tag '" + input.substring(2) + '\'');
} else {
return new BlockCategoryMask(new RequestExtent(), category);
return new BlockCategoryMask(context.getExtent(), category);
}
}
}
@@ -26,7 +26,6 @@
import com.sk89q.worldedit.function.mask.BlockStateMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;

import java.util.stream.Stream;

@@ -53,7 +52,7 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
boolean strict = input.charAt(1) == '=';
String states = input.substring(2 + (strict ? 1 : 0), input.length() - 1);
try {
return new BlockStateMask(new RequestExtent(),
return new BlockStateMask(context.getExtent(),
Splitter.on(',').omitEmptyStrings().trimResults().withKeyValueSeparator('=').split(states),
strict);
} catch (Exception e) {
@@ -26,7 +26,6 @@
import com.sk89q.worldedit.function.mask.BlockMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.world.block.BaseBlock;

import java.util.Set;
@@ -56,7 +55,7 @@ public Mask parseFromInput(String component, ParserContext context) throws Input
if (holders.isEmpty()) {
return null;
}
return new BlockMask(new RequestExtent(), holders);
return new BlockMask(context.getExtent(), holders);
} catch (NoMatchException e) {
return null;
}
@@ -25,7 +25,6 @@
import com.sk89q.worldedit.function.mask.ExistingBlockMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.request.RequestExtent;

import java.util.List;

@@ -44,6 +43,6 @@ public ExistingMaskParser(WorldEdit worldEdit) {

@Override
public Mask parseFromSimpleInput(String input, ParserContext context) {
return new ExistingBlockMask(new RequestExtent());
return new ExistingBlockMask(context.getExtent());
}
}
@@ -30,7 +30,6 @@
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment;
import com.sk89q.worldedit.session.SessionOwner;
import com.sk89q.worldedit.session.request.RequestExtent;

import java.util.function.IntSupplier;
import java.util.stream.Stream;
@@ -58,7 +57,7 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
try {
Expression exp = Expression.compile(input.substring(1), "x", "y", "z");
WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment(
new RequestExtent(), Vector3.ONE, Vector3.ZERO);
context.getExtent(), Vector3.ONE, Vector3.ZERO);
exp.setEnvironment(env);
if (context.getActor() != null) {
SessionOwner owner = context.getActor();
@@ -29,7 +29,6 @@
import com.sk89q.worldedit.function.mask.OffsetMask;
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.session.request.RequestExtent;

import java.util.stream.Stream;

@@ -62,7 +61,7 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
if (input.length() > 1) {
submask = worldEdit.getMaskFactory().parseFromInput(input.substring(1), context);
} else {
submask = new ExistingBlockMask(new RequestExtent());
submask = new ExistingBlockMask(context.getExtent());
}
OffsetMask offsetMask = new OffsetMask(submask, BlockVector3.at(0, firstChar == '>' ? -1 : 1, 0));
return new MaskIntersection(offsetMask, Masks.negate(submask));
@@ -25,7 +25,6 @@
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.SolidBlockMask;
import com.sk89q.worldedit.internal.registry.SimpleInputParser;
import com.sk89q.worldedit.session.request.RequestExtent;

import java.util.List;

@@ -44,6 +43,6 @@ public SolidMaskParser(WorldEdit worldEdit) {

@Override
public Mask parseFromSimpleInput(String input, ParserContext context) {
return new SolidBlockMask(new RequestExtent());
return new SolidBlockMask(context.getExtent());
}
}
@@ -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 {
}

0 comments on commit c461c60

Please sign in to comment.
You can’t perform that action at this time.