From 19796aa3beae72d3751bd941c671f6345b0e0e18 Mon Sep 17 00:00:00 2001 From: wizjany Date: Mon, 11 Feb 2019 17:40:30 -0500 Subject: [PATCH] Added offset to ClipboardPattern. Takes input like '//set #clipboard@-1,0,1' which shifts the pattern over. It also now extends RepeatingExtentPattern, which was previously unused. --- .../pattern/ClipboardPatternParser.java | 32 ++++++++++++++--- .../function/pattern/ClipboardPattern.java | 28 ++++++--------- .../pattern/RepeatingExtentPattern.java | 34 +++++++++++++++---- 3 files changed, 65 insertions(+), 29 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java index 9bf102f32..1d88aefa6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/ClipboardPatternParser.java @@ -28,31 +28,53 @@ import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.function.pattern.ClipboardPattern; import com.sk89q.worldedit.function.pattern.Pattern; -import com.sk89q.worldedit.internal.registry.SimpleInputParser; +import com.sk89q.worldedit.internal.registry.InputParser; +import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.session.ClipboardHolder; import java.util.List; -public class ClipboardPatternParser extends SimpleInputParser { +public class ClipboardPatternParser extends InputParser { public ClipboardPatternParser(WorldEdit worldEdit) { super(worldEdit); } @Override - public List getMatchedAliases() { + public List getSuggestions() { return Lists.newArrayList("#clipboard", "#copy"); } @Override - public Pattern parseFromSimpleInput(String input, ParserContext context) throws InputParseException { + public Pattern parseFromInput(String input, ParserContext context) throws InputParseException { + if (!input.startsWith("#clipboard") && !input.startsWith("#copy")) { + return null; + } LocalSession session = context.requireSession(); + int offsetPart; + BlockVector3 offset = BlockVector3.ZERO; + if ((offsetPart = input.indexOf('@')) >= 0) { + if (input.length() <= offsetPart + 1) { + throw new InputParseException("Clipboard offset coordinates not specified!"); + } + String offsetString = input.substring(offsetPart + 1); + String[] offsetCoords = offsetString.split(","); + if (offsetCoords.length != 3) { + throw new InputParseException("Clipboard offset needs x,y,z coordinates."); + } + offset = BlockVector3.at( + Integer.valueOf(offsetCoords[0]), + Integer.valueOf(offsetCoords[1]), + Integer.valueOf(offsetCoords[2]) + ); + } + if (session != null) { try { ClipboardHolder holder = session.getClipboard(); Clipboard clipboard = holder.getClipboard(); - return new ClipboardPattern(clipboard); + return new ClipboardPattern(clipboard, offset); } catch (EmptyClipboardException e) { throw new InputParseException("To use #clipboard, please first copy something to your clipboard"); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ClipboardPattern.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ClipboardPattern.java index f62328d0e..9f226a1ab 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ClipboardPattern.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/ClipboardPattern.java @@ -19,19 +19,13 @@ package com.sk89q.worldedit.function.pattern; -import static com.google.common.base.Preconditions.checkNotNull; - import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.world.block.BaseBlock; /** * A pattern that reads from {@link Clipboard}. */ -public class ClipboardPattern extends AbstractPattern { - - private final Clipboard clipboard; - private final BlockVector3 size; +public class ClipboardPattern extends RepeatingExtentPattern { /** * Create a new clipboard pattern. @@ -39,18 +33,16 @@ public class ClipboardPattern extends AbstractPattern { * @param clipboard the clipboard */ public ClipboardPattern(Clipboard clipboard) { - checkNotNull(clipboard); - this.clipboard = clipboard; - this.size = clipboard.getMaximumPoint().subtract(clipboard.getMinimumPoint()).add(1, 1, 1); + this(clipboard, BlockVector3.ZERO); } - @Override - public BaseBlock apply(BlockVector3 position) { - int xp = Math.abs(position.getBlockX()) % size.getBlockX(); - int yp = Math.abs(position.getBlockY()) % size.getBlockY(); - int zp = Math.abs(position.getBlockZ()) % size.getBlockZ(); - - return clipboard.getFullBlock(clipboard.getMinimumPoint().add(xp, yp, zp)); + /** + * Create a new clipboard pattern. + * + * @param clipboard the clipboard + * @param offset the offset + */ + public ClipboardPattern(Clipboard clipboard, BlockVector3 offset) { + super(clipboard, clipboard.getMinimumPoint(), offset); } - } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/RepeatingExtentPattern.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/RepeatingExtentPattern.java index 101771ab1..9b16fd439 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/RepeatingExtentPattern.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/RepeatingExtentPattern.java @@ -30,7 +30,9 @@ import com.sk89q.worldedit.world.block.BaseBlock; */ public class RepeatingExtentPattern extends AbstractPattern { + private final BlockVector3 size; private Extent extent; + private BlockVector3 origin; private BlockVector3 offset; /** @@ -39,9 +41,11 @@ public class RepeatingExtentPattern extends AbstractPattern { * @param extent the extent * @param offset the offset */ - public RepeatingExtentPattern(Extent extent, BlockVector3 offset) { + public RepeatingExtentPattern(Extent extent, BlockVector3 origin, BlockVector3 offset) { setExtent(extent); + setOrigin(origin); setOffset(offset); + size = extent.getMaximumPoint().subtract(extent.getMinimumPoint()).add(1, 1, 1); } /** @@ -82,14 +86,32 @@ public class RepeatingExtentPattern extends AbstractPattern { this.offset = offset; } + /** + * Get the origin. + * + * @return the origin + */ + public BlockVector3 getOrigin() { + return origin; + } + + /** + * Set the origin. + * + * @param origin the origin + */ + public void setOrigin(BlockVector3 origin) { + checkNotNull(origin); + this.origin = origin; + } + @Override public BaseBlock apply(BlockVector3 position) { BlockVector3 base = position.add(offset); - BlockVector3 size = extent.getMaximumPoint().subtract(extent.getMinimumPoint()).add(1, 1, 1); - int x = base.getBlockX() % size.getBlockX(); - int y = base.getBlockY() % size.getBlockY(); - int z = base.getBlockZ() % size.getBlockZ(); - return extent.getFullBlock(BlockVector3.at(x, y, z)); + int x = Math.abs(base.getBlockX()) % size.getBlockX(); + int y = Math.abs(base.getBlockY()) % size.getBlockY(); + int z = Math.abs(base.getBlockZ()) % size.getBlockZ(); + return extent.getFullBlock(BlockVector3.at(x, y, z).add(origin)); } }