From bd90df7635d3716f6945c5af5aa79a21afa707f3 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 28 Sep 2016 17:52:01 +1000 Subject: [PATCH] Finish new patterns --- .../object/pattern/Linear3DBlockPattern.java | 28 ++++++++++ .../object/pattern/LinearBlockPattern.java | 22 +++++--- .../factory/HashTagPatternParser.java | 52 ++++++++++++------- 3 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 core/src/main/java/com/boydti/fawe/object/pattern/Linear3DBlockPattern.java diff --git a/core/src/main/java/com/boydti/fawe/object/pattern/Linear3DBlockPattern.java b/core/src/main/java/com/boydti/fawe/object/pattern/Linear3DBlockPattern.java new file mode 100644 index 00000000..5c194886 --- /dev/null +++ b/core/src/main/java/com/boydti/fawe/object/pattern/Linear3DBlockPattern.java @@ -0,0 +1,28 @@ +package com.boydti.fawe.object.pattern; + +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.function.pattern.AbstractPattern; +import com.sk89q.worldedit.function.pattern.Pattern; +import java.util.Arrays; +import java.util.Collection; + +public class Linear3DBlockPattern extends AbstractPattern { + + private final Collection patterns; + private final Pattern[] patternsArray; + + public Linear3DBlockPattern(Pattern[] patterns) { + this.patternsArray = patterns; + this.patterns = Arrays.asList(patterns); + } + + @Override + public BaseBlock apply(Vector position) { + int index = (position.getBlockX() + position.getBlockY() + position.getBlockZ()) % patternsArray.length; + if (index < 0) { + index += patternsArray.length; + } + return patternsArray[index].apply(position); + } +} diff --git a/core/src/main/java/com/boydti/fawe/object/pattern/LinearBlockPattern.java b/core/src/main/java/com/boydti/fawe/object/pattern/LinearBlockPattern.java index a7f4b8ff..79e0463f 100644 --- a/core/src/main/java/com/boydti/fawe/object/pattern/LinearBlockPattern.java +++ b/core/src/main/java/com/boydti/fawe/object/pattern/LinearBlockPattern.java @@ -3,21 +3,31 @@ package com.boydti.fawe.object.pattern; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.function.pattern.AbstractPattern; +import com.sk89q.worldedit.function.pattern.Pattern; +import java.util.Arrays; +import java.util.Collection; -public class LinearBlockPattern extends AbstractPattern { +public class LinearBlockPattern extends AbstractPattern implements ResettablePattern { - private final BaseBlock[] blocks; + private final Collection patterns; + private final Pattern[] patternsArray; private int index; - public LinearBlockPattern(BaseBlock[] blocks) { - this.blocks = blocks; + public LinearBlockPattern(Pattern[] patterns) { + this.patternsArray = patterns; + this.patterns = Arrays.asList(patterns); } @Override public BaseBlock apply(Vector position) { - if (index == blocks.length) { + if (index == patternsArray.length) { index = 0; } - return blocks[index++]; + return patternsArray[index++].apply(position); + } + + @Override + public void reset() { + index = 0; } } diff --git a/core/src/main/java/com/sk89q/worldedit/extension/factory/HashTagPatternParser.java b/core/src/main/java/com/sk89q/worldedit/extension/factory/HashTagPatternParser.java index b39c4a9d..4287d7da 100644 --- a/core/src/main/java/com/sk89q/worldedit/extension/factory/HashTagPatternParser.java +++ b/core/src/main/java/com/sk89q/worldedit/extension/factory/HashTagPatternParser.java @@ -1,6 +1,7 @@ package com.sk89q.worldedit.extension.factory; import com.boydti.fawe.object.pattern.ExistingPattern; +import com.boydti.fawe.object.pattern.Linear3DBlockPattern; import com.boydti.fawe.object.pattern.LinearBlockPattern; import com.boydti.fawe.object.pattern.NoXPattern; import com.boydti.fawe.object.pattern.NoYPattern; @@ -9,7 +10,6 @@ import com.boydti.fawe.object.pattern.RelativePattern; import com.sk89q.worldedit.EmptyClipboardException; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.extension.input.InputParseException; import com.sk89q.worldedit.extension.input.ParserContext; import com.sk89q.worldedit.extent.clipboard.Clipboard; @@ -32,6 +32,7 @@ public class HashTagPatternParser extends InputParser { switch (input.toLowerCase().charAt(0)) { case '#': { switch (input) { + case "#*": case "#existing": { return new ExistingPattern(context.requireExtent()); } @@ -53,40 +54,55 @@ public class HashTagPatternParser extends InputParser { } String[] split2 = input.split(":"); if (split2.length > 1) { - switch (split2[0]) { + String rest = input.substring(split2[0].length() + 1); + switch (split2[0].toLowerCase()) { + case "#~": + case "#r": case "#relative": case "#rel": { - String rest = input.substring(5); return new RelativePattern(parseFromInput(rest, context)); } + case "#!x": + case "#nx": case "#nox": { - String rest = input.substring(5); return new NoXPattern(parseFromInput(rest, context)); } + case "#!y": + case "#ny": case "#noy": { - String rest = input.substring(5); return new NoYPattern(parseFromInput(rest, context)); } + case "#!z": + case "#nz": case "#noz": { - String rest = input.substring(5); return new NoZPattern(parseFromInput(rest, context)); } + case "#l": + case "#linear": { + ArrayList patterns = new ArrayList<>(); + for (String token : rest.split(",")) { + patterns.add(parseFromInput(token, context)); + } + if (patterns.isEmpty()) { + throw new InputParseException("No blocks provided for linear pattern e.g. [stone,wood"); + } + return new LinearBlockPattern(patterns.toArray(new Pattern[patterns.size()])); + } + case "#l3d": + case "#linear3D": { + ArrayList patterns = new ArrayList<>(); + for (String token : rest.split(",")) { + patterns.add(parseFromInput(token, context)); + } + if (patterns.isEmpty()) { + throw new InputParseException("No blocks provided for linear pattern e.g. [stone,wood"); + } + return new Linear3DBlockPattern(patterns.toArray(new Pattern[patterns.size()])); + } } } throw new InputParseException("Invalid, see: https://github.com/boy0001/FastAsyncWorldedit/wiki/WorldEdit-and-FAWE-patterns"); } - case '[': { - ArrayList blocks = new ArrayList<>(); - for (String token : input.substring(1).split(",")) { - BlockFactory blockRegistry = worldEdit.getBlockFactory(); - BaseBlock block = blockRegistry.parseFromInput(token, context); - blocks.add(block); - } - if (blocks.isEmpty()) { - throw new InputParseException("No blocks provided for linear pattern e.g. [stone,wood"); - } - return new LinearBlockPattern(blocks.toArray(new BaseBlock[blocks.size()])); - } default: String[] items = input.split(","); if (items.length == 1) {