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.
Dieser Commit ist enthalten in:
wizjany 2019-02-11 17:40:30 -05:00 committet von Wizjany
Ursprung 76400e533d
Commit 19796aa3be
3 geänderte Dateien mit 65 neuen und 29 gelöschten Zeilen

Datei anzeigen

@ -28,31 +28,53 @@ import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.pattern.ClipboardPattern; import com.sk89q.worldedit.function.pattern.ClipboardPattern;
import com.sk89q.worldedit.function.pattern.Pattern; 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 com.sk89q.worldedit.session.ClipboardHolder;
import java.util.List; import java.util.List;
public class ClipboardPatternParser extends SimpleInputParser<Pattern> { public class ClipboardPatternParser extends InputParser<Pattern> {
public ClipboardPatternParser(WorldEdit worldEdit) { public ClipboardPatternParser(WorldEdit worldEdit) {
super(worldEdit); super(worldEdit);
} }
@Override @Override
public List<String> getMatchedAliases() { public List<String> getSuggestions() {
return Lists.newArrayList("#clipboard", "#copy"); return Lists.newArrayList("#clipboard", "#copy");
} }
@Override @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(); 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) { if (session != null) {
try { try {
ClipboardHolder holder = session.getClipboard(); ClipboardHolder holder = session.getClipboard();
Clipboard clipboard = holder.getClipboard(); Clipboard clipboard = holder.getClipboard();
return new ClipboardPattern(clipboard); return new ClipboardPattern(clipboard, offset);
} catch (EmptyClipboardException e) { } catch (EmptyClipboardException e) {
throw new InputParseException("To use #clipboard, please first copy something to your clipboard"); throw new InputParseException("To use #clipboard, please first copy something to your clipboard");
} }

Datei anzeigen

@ -19,19 +19,13 @@
package com.sk89q.worldedit.function.pattern; 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.extent.clipboard.Clipboard;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
/** /**
* A pattern that reads from {@link Clipboard}. * A pattern that reads from {@link Clipboard}.
*/ */
public class ClipboardPattern extends AbstractPattern { public class ClipboardPattern extends RepeatingExtentPattern {
private final Clipboard clipboard;
private final BlockVector3 size;
/** /**
* Create a new clipboard pattern. * Create a new clipboard pattern.
@ -39,18 +33,16 @@ public class ClipboardPattern extends AbstractPattern {
* @param clipboard the clipboard * @param clipboard the clipboard
*/ */
public ClipboardPattern(Clipboard clipboard) { public ClipboardPattern(Clipboard clipboard) {
checkNotNull(clipboard); this(clipboard, BlockVector3.ZERO);
this.clipboard = clipboard;
this.size = clipboard.getMaximumPoint().subtract(clipboard.getMinimumPoint()).add(1, 1, 1);
} }
@Override /**
public BaseBlock apply(BlockVector3 position) { * Create a new clipboard pattern.
int xp = Math.abs(position.getBlockX()) % size.getBlockX(); *
int yp = Math.abs(position.getBlockY()) % size.getBlockY(); * @param clipboard the clipboard
int zp = Math.abs(position.getBlockZ()) % size.getBlockZ(); * @param offset the offset
*/
return clipboard.getFullBlock(clipboard.getMinimumPoint().add(xp, yp, zp)); public ClipboardPattern(Clipboard clipboard, BlockVector3 offset) {
super(clipboard, clipboard.getMinimumPoint(), offset);
} }
} }

Datei anzeigen

@ -30,7 +30,9 @@ import com.sk89q.worldedit.world.block.BaseBlock;
*/ */
public class RepeatingExtentPattern extends AbstractPattern { public class RepeatingExtentPattern extends AbstractPattern {
private final BlockVector3 size;
private Extent extent; private Extent extent;
private BlockVector3 origin;
private BlockVector3 offset; private BlockVector3 offset;
/** /**
@ -39,9 +41,11 @@ public class RepeatingExtentPattern extends AbstractPattern {
* @param extent the extent * @param extent the extent
* @param offset the offset * @param offset the offset
*/ */
public RepeatingExtentPattern(Extent extent, BlockVector3 offset) { public RepeatingExtentPattern(Extent extent, BlockVector3 origin, BlockVector3 offset) {
setExtent(extent); setExtent(extent);
setOrigin(origin);
setOffset(offset); setOffset(offset);
size = extent.getMaximumPoint().subtract(extent.getMinimumPoint()).add(1, 1, 1);
} }
/** /**
@ -82,14 +86,32 @@ public class RepeatingExtentPattern extends AbstractPattern {
this.offset = offset; 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 @Override
public BaseBlock apply(BlockVector3 position) { public BaseBlock apply(BlockVector3 position) {
BlockVector3 base = position.add(offset); BlockVector3 base = position.add(offset);
BlockVector3 size = extent.getMaximumPoint().subtract(extent.getMinimumPoint()).add(1, 1, 1); int x = Math.abs(base.getBlockX()) % size.getBlockX();
int x = base.getBlockX() % size.getBlockX(); int y = Math.abs(base.getBlockY()) % size.getBlockY();
int y = base.getBlockY() % size.getBlockY(); int z = Math.abs(base.getBlockZ()) % size.getBlockZ();
int z = base.getBlockZ() % size.getBlockZ(); return extent.getFullBlock(BlockVector3.at(x, y, z).add(origin));
return extent.getFullBlock(BlockVector3.at(x, y, z));
} }
} }