mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-10 06:38:35 +00:00
Start reimplementation of simplex pattern (#520)
* Start reimplementation of simplex pattern * Fix suggestions * Allow nested weighted patterns * Add documentation and improve error handling * Remove unnecessary code and obsolete TODOs
This commit is contained in:
@ -38,8 +38,9 @@ public class RandomPatternParser extends InputParser<Pattern> {
|
||||
|
||||
@Override
|
||||
public Stream<String> getSuggestions(String input) {
|
||||
String[] splits = input.split(",", -1);
|
||||
List<String> patterns = StringUtil.parseListInQuotes(splits, ',', '[', ']', true);
|
||||
List<String> patterns = StringUtil.split(input, ',', '[', ']');
|
||||
/*String[] splits = input.split(",", -1);
|
||||
List<String> patterns = StringUtil.parseListInQuotes(splits, ',', '[', ']', true);*/
|
||||
if (patterns.size() == 1) {
|
||||
return Stream.empty();
|
||||
}
|
||||
@ -63,8 +64,9 @@ public class RandomPatternParser extends InputParser<Pattern> {
|
||||
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
|
||||
RandomPattern randomPattern = new RandomPattern();
|
||||
|
||||
String[] splits = input.split(",", -1);
|
||||
List<String> patterns = StringUtil.parseListInQuotes(splits, ',', '[', ']', true);
|
||||
List<String> patterns = StringUtil.split(input, ',', '[', ']');
|
||||
/*String[] splits = input.split(",", -1);
|
||||
List<String> patterns = StringUtil.parseListInQuotes(splits, ',', '[', ']', true);*/
|
||||
if (patterns.size() == 1) {
|
||||
return null; // let a 'single'-pattern parser handle it
|
||||
}
|
||||
@ -74,7 +76,7 @@ public class RandomPatternParser extends InputParser<Pattern> {
|
||||
|
||||
// Parse special percentage syntax
|
||||
if (token.matches("[0-9]+(\\.[0-9]*)?%.*")) {
|
||||
String[] p = token.split("%");
|
||||
String[] p = token.split("%", 2);
|
||||
|
||||
if (p.length < 2) {
|
||||
throw new InputParseException("Missing the type after the % symbol for '" + input + "'");
|
||||
|
@ -0,0 +1,75 @@
|
||||
package com.sk89q.worldedit.extension.factory.parser.pattern;
|
||||
|
||||
import com.boydti.fawe.object.random.SimplexRandom;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.extension.factory.parser.RichParser;
|
||||
import com.sk89q.worldedit.extension.input.InputParseException;
|
||||
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||
import com.sk89q.worldedit.function.pattern.Pattern;
|
||||
import com.sk89q.worldedit.function.pattern.RandomPattern;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SimplexPatternParser extends RichParser<Pattern> {
|
||||
private static final String SIMPLEX_PREFIX = "#simplex";
|
||||
|
||||
public SimplexPatternParser(WorldEdit worldEdit) {
|
||||
super(worldEdit, SIMPLEX_PREFIX);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<String> getSuggestions(String argumentInput, int index) {
|
||||
if (index == 0) {
|
||||
if (argumentInput.isEmpty()) {
|
||||
return Stream.of("1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||
}
|
||||
// if already a valid number, suggest more digits
|
||||
if (isDouble(argumentInput)) {
|
||||
Stream<String> numbers = Stream.of("", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
|
||||
if (argumentInput.indexOf('.') == -1) {
|
||||
numbers = Stream.concat(numbers, Stream.of("."));
|
||||
}
|
||||
return numbers.map(s -> argumentInput + s);
|
||||
}
|
||||
// no valid input anymore
|
||||
return Stream.empty();
|
||||
}
|
||||
if (index == 1) {
|
||||
return worldEdit.getPatternFactory().getSuggestions(argumentInput).stream();
|
||||
}
|
||||
return Stream.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Pattern parseFromInput(@NotNull String[] arguments, ParserContext context) {
|
||||
if (arguments.length != 2) {
|
||||
throw new InputParseException("Simplex requires a scale and a pattern, e.g. #simplex[5][dirt,stone]");
|
||||
}
|
||||
double scale = Double.parseDouble(arguments[0]);
|
||||
scale = 1d / Math.max(1, scale);
|
||||
Pattern inner = worldEdit.getPatternFactory().parseFromInput(arguments[1], context);
|
||||
if (inner instanceof RandomPattern) {
|
||||
return new RandomPattern(new SimplexRandom(scale), (RandomPattern) inner);
|
||||
} else if (inner instanceof BlockStateHolder) {
|
||||
return inner; // single blocks won't have any impact on how simplex behaves
|
||||
} else {
|
||||
throw new InputParseException("Pattern " + inner.getClass().getSimpleName() + " cannot be used with #simplex");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isDouble(String input) {
|
||||
boolean point = false;
|
||||
for (char c : input.toCharArray()) {
|
||||
if (!Character.isDigit(c)) {
|
||||
if (c == '.' && !point) {
|
||||
point = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user