Skip to content

Commit

Permalink
Allow combinations of patterns.
Browse files Browse the repository at this point in the history
The RandomPatternParser, which allowed multiple blocks to be specified,
now allows multiple patterns by re-parsing each token as a pattern
instead of as a block.
  • Loading branch information
wizjany committed Feb 12, 2019
1 parent 142b86d commit 898ab17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Expand Up @@ -44,10 +44,14 @@ public final class PatternFactory extends AbstractFactory<Pattern> {
public PatternFactory(WorldEdit worldEdit) {
super(worldEdit);

// split and parse each sub-pattern
register(new RandomPatternParser(worldEdit));

// individual patterns
register(new BlockCategoryPatternParser(worldEdit));
register(new ClipboardPatternParser(worldEdit));
register(new BlockCategoryPatternParser(worldEdit));
register(new SingleBlockPatternParser(worldEdit));
register(new RandomPatternParser(worldEdit));
}

}
Expand Up @@ -48,7 +48,7 @@ public List<String> getSuggestions() {

@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
if(!input.startsWith("##")) {
if (!input.startsWith("##")) {
return null;
}
String tag = input.substring(2).toLowerCase();
Expand Down
Expand Up @@ -30,6 +30,8 @@
import com.sk89q.worldedit.internal.registry.InputParser;
import com.sk89q.worldedit.world.block.BaseBlock;

import java.util.List;

public class RandomPatternParser extends InputParser<Pattern> {

public RandomPatternParser(WorldEdit worldEdit) {
Expand All @@ -42,10 +44,13 @@ public Pattern parseFromInput(String input, ParserContext context) throws InputP
RandomPattern randomPattern = new RandomPattern();

String[] splits = input.split(",");
for (String token : StringUtil.parseListInQuotes(splits, ',', '[', ']')) {
BaseBlock block;

List<String> patterns = StringUtil.parseListInQuotes(splits, ',', '[', ']');
if (patterns.size() == 1) {
return null; // let a 'single'-pattern parser handle it
}
for (String token : patterns) {
double chance;
Pattern innerPattern;

// Parse special percentage syntax
if (token.matches("[0-9]+(\\.[0-9]*)?%.*")) {
Expand All @@ -55,14 +60,14 @@ public Pattern parseFromInput(String input, ParserContext context) throws InputP
throw new InputParseException("Missing the type after the % symbol for '" + input + "'");
} else {
chance = Double.parseDouble(p[0]);
block = blockRegistry.parseFromInput(p[1], context);
innerPattern = worldEdit.getPatternFactory().parseFromInput(p[1], context);
}
} else {
chance = 1;
block = blockRegistry.parseFromInput(token, context);
innerPattern = worldEdit.getPatternFactory().parseFromInput(token, context);
}

randomPattern.add(new BlockPattern(block), chance);
randomPattern.add(innerPattern, chance);
}

return randomPattern;
Expand Down

0 comments on commit 898ab17

Please sign in to comment.