mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-12 12:33:54 +00:00
Merge branch '1.16' of https://github.com/IntellectualSites/FastAsyncWorldEdit into 1.16
Conflicts: worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/wrapper/AsyncWorld.java worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/wrapper/state/AsyncDataContainer.java
This commit is contained in:
@ -305,7 +305,7 @@ public class Fawe {
|
||||
br.close();
|
||||
this.version = FaweVersion.tryParse(versionString, commitString, dateString);
|
||||
Settings.IMP.DATE = new Date(100 + version.year, version.month, version.day).toGMTString();
|
||||
Settings.IMP.BUILD = "https://ci.athion.net/job/FastAsyncWorldEdit-1.15/" + version.build;
|
||||
Settings.IMP.BUILD = "https://ci.athion.net/job/FastAsyncWorldEdit-1.16/" + version.build;
|
||||
Settings.IMP.COMMIT = "https://github.com/IntellectualSites/FastAsyncWorldEdit/commit/" + Integer.toHexString(version.hash);
|
||||
} catch (Throwable ignore) {}
|
||||
try {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.boydti.fawe;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.slf4j.LoggerFactory.getLogger;
|
||||
|
||||
import com.boydti.fawe.beta.IChunkSet;
|
||||
import com.boydti.fawe.beta.Trimable;
|
||||
@ -9,6 +8,7 @@ import com.boydti.fawe.beta.implementation.queue.Pool;
|
||||
import com.boydti.fawe.beta.implementation.queue.QueuePool;
|
||||
import com.boydti.fawe.config.Settings;
|
||||
import com.boydti.fawe.object.collection.BitArray;
|
||||
import com.boydti.fawe.object.collection.BitArrayUnstretched;
|
||||
import com.boydti.fawe.object.collection.CleanableThreadLocal;
|
||||
import com.boydti.fawe.object.collection.VariableThreadLocal;
|
||||
import com.boydti.fawe.object.exception.FaweBlockBagException;
|
||||
@ -299,6 +299,102 @@ public enum FaweCache implements Trimable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert raw int array to unstretched palette (1.16)
|
||||
* @param layerOffset
|
||||
* @param blocks
|
||||
* @return palette
|
||||
*/
|
||||
public Palette toPaletteUnstretched(int layerOffset, char[] blocks) {
|
||||
return toPaletteUnstretched(layerOffset, null, blocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert raw int array to unstretched palette (1.16)
|
||||
* @param layerOffset
|
||||
* @param blocks
|
||||
* @return palette
|
||||
*/
|
||||
public Palette toPaletteUnstretched(int layerOffset, int[] blocks) {
|
||||
return toPaletteUnstretched(layerOffset, blocks, null);
|
||||
}
|
||||
|
||||
private Palette toPaletteUnstretched(int layerOffset, int[] blocksInts, char[] blocksChars) {
|
||||
int[] blockToPalette = BLOCK_TO_PALETTE.get();
|
||||
int[] paletteToBlock = PALETTE_TO_BLOCK.get();
|
||||
long[] blockStates = BLOCK_STATES.get();
|
||||
int[] blocksCopy = SECTION_BLOCKS.get();
|
||||
|
||||
try {
|
||||
int num_palette = 0;
|
||||
int blockIndexStart = layerOffset << 12;
|
||||
int blockIndexEnd = blockIndexStart + 4096;
|
||||
if (blocksChars != null) {
|
||||
for (int i = blockIndexStart, j = 0; i < blockIndexEnd; i++, j++) {
|
||||
int ordinal = blocksChars[i];
|
||||
int palette = blockToPalette[ordinal];
|
||||
if (palette == Integer.MAX_VALUE) {
|
||||
blockToPalette[ordinal] = palette = num_palette;
|
||||
paletteToBlock[num_palette] = ordinal;
|
||||
num_palette++;
|
||||
}
|
||||
blocksCopy[j] = palette;
|
||||
}
|
||||
} else if (blocksInts != null) {
|
||||
for (int i = blockIndexStart, j = 0; i < blockIndexEnd; i++, j++) {
|
||||
int ordinal = blocksInts[i];
|
||||
int palette = blockToPalette[ordinal];
|
||||
if (palette == Integer.MAX_VALUE) {
|
||||
// BlockState state = BlockTypesCache.states[ordinal];
|
||||
blockToPalette[ordinal] = palette = num_palette;
|
||||
paletteToBlock[num_palette] = ordinal;
|
||||
num_palette++;
|
||||
}
|
||||
blocksCopy[j] = palette;
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
for (int i = 0; i < num_palette; i++) {
|
||||
blockToPalette[paletteToBlock[i]] = Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
// BlockStates
|
||||
int bitsPerEntry = MathMan.log2nlz(num_palette - 1);
|
||||
if (Settings.IMP.PROTOCOL_SUPPORT_FIX || num_palette != 1) {
|
||||
bitsPerEntry = Math.max(bitsPerEntry, 4); // Protocol support breaks <4 bits per entry
|
||||
} else {
|
||||
bitsPerEntry = Math.max(bitsPerEntry, 1); // For some reason minecraft needs 4096 bits to store 0 entries
|
||||
}
|
||||
int blocksPerLong = MathMan.floorZero((double) 64 / bitsPerEntry);
|
||||
int blockBitArrayEnd = MathMan.ceilZero((float) 4096 / blocksPerLong);
|
||||
if (num_palette == 1) {
|
||||
// Set a value, because minecraft needs it for some reason
|
||||
blockStates[0] = 0;
|
||||
blockBitArrayEnd = 1;
|
||||
} else {
|
||||
BitArrayUnstretched bitArray = new BitArrayUnstretched(bitsPerEntry, blockStates);
|
||||
bitArray.fromRaw(blocksCopy);
|
||||
}
|
||||
|
||||
// Construct palette
|
||||
Palette palette = PALETTE_CACHE.get();
|
||||
palette.bitsPerEntry = bitsPerEntry;
|
||||
palette.paletteToBlockLength = num_palette;
|
||||
palette.paletteToBlock = paletteToBlock;
|
||||
|
||||
palette.blockStatesLength = blockBitArrayEnd;
|
||||
palette.blockStates = blockStates;
|
||||
|
||||
return palette;
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
Arrays.fill(blockToPalette, Integer.MAX_VALUE);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Vector cache
|
||||
*/
|
||||
|
@ -32,9 +32,9 @@ public class FaweVersion {
|
||||
|
||||
@Override public String toString() {
|
||||
if (hash == 0 && build == 0) {
|
||||
return "FastAsyncWorldEdit-1.15-NoVer-SNAPSHOT";
|
||||
return "FastAsyncWorldEdit-1.16-NoVer-SNAPSHOT";
|
||||
} else {
|
||||
return "FastAsyncWorldEdit-1.15" + build;
|
||||
return "FastAsyncWorldEdit-1.16" + build;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,4 +41,8 @@ public interface IFawe {
|
||||
|
||||
Preloader getPreloader();
|
||||
|
||||
default boolean isChunksStretched() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -45,11 +45,11 @@ public interface IBlocks extends Trimable {
|
||||
|
||||
IBlocks reset();
|
||||
|
||||
default byte[] toByteArray(boolean full) {
|
||||
return toByteArray(null, getBitMask(), full);
|
||||
default byte[] toByteArray(boolean full, boolean stretched) {
|
||||
return toByteArray(null, getBitMask(), full, stretched);
|
||||
}
|
||||
|
||||
default byte[] toByteArray(byte[] buffer, int bitMask, boolean full) {
|
||||
default byte[] toByteArray(byte[] buffer, int bitMask, boolean full, boolean stretched) {
|
||||
if (buffer == null) {
|
||||
buffer = new byte[1024];
|
||||
}
|
||||
@ -81,7 +81,12 @@ public interface IBlocks extends Trimable {
|
||||
}
|
||||
|
||||
sectionWriter.writeShort(nonEmpty); // non empty
|
||||
FaweCache.Palette palette = FaweCache.IMP.toPalette(0, ids);
|
||||
FaweCache.Palette palette;
|
||||
if (stretched) {
|
||||
palette = FaweCache.IMP.toPalette(0, ids);
|
||||
} else {
|
||||
palette = FaweCache.IMP.toPaletteUnstretched(0, ids);
|
||||
}
|
||||
|
||||
sectionWriter.writeByte(palette.bitsPerEntry); // bits per block
|
||||
sectionWriter.writeVarInt(palette.paletteToBlockLength);
|
||||
|
@ -181,6 +181,18 @@ public class CharSetBlocks extends CharBlocks implements IChunkSet {
|
||||
}
|
||||
|
||||
@Override public void setFullBright(int layer) {
|
||||
if (light == null) {
|
||||
light = new char[16][];
|
||||
}
|
||||
if (light[layer] == null) {
|
||||
light[layer] = new char[4096];
|
||||
}
|
||||
if (skyLight == null) {
|
||||
skyLight = new char[16][];
|
||||
}
|
||||
if (skyLight[layer] == null) {
|
||||
skyLight[layer] = new char[4096];
|
||||
}
|
||||
Arrays.fill(light[layer], (char) 15);
|
||||
Arrays.fill(skyLight[layer], (char) 15);
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.boydti.fawe.beta.implementation.packet;
|
||||
|
||||
import com.boydti.fawe.Fawe;
|
||||
import com.boydti.fawe.FaweCache;
|
||||
import com.boydti.fawe.beta.IBlocks;
|
||||
import com.boydti.fawe.object.FaweOutputStream;
|
||||
import com.boydti.fawe.object.io.FastByteArrayOutputStream;
|
||||
import com.sk89q.jnbt.CompoundTag;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Function;
|
||||
@ -64,7 +66,7 @@ public class ChunkPacket implements Function<byte[], byte[]>, Supplier<byte[]> {
|
||||
if (sectionBytes == null) {
|
||||
IBlocks tmpChunk = getChunk();
|
||||
byte[] buf = FaweCache.IMP.BYTE_BUFFER_8192.get();
|
||||
sectionBytes = tmpChunk.toByteArray(buf, tmpChunk.getBitMask(), this.full);
|
||||
sectionBytes = tmpChunk.toByteArray(buf, tmpChunk.getBitMask(), this.full, Fawe.imp().isChunksStretched());
|
||||
}
|
||||
tmp = sectionBytes;
|
||||
}
|
||||
@ -72,6 +74,7 @@ public class ChunkPacket implements Function<byte[], byte[]>, Supplier<byte[]> {
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
public Object getNativePacket() {
|
||||
return nativePacket;
|
||||
}
|
||||
|
@ -0,0 +1,115 @@
|
||||
package com.boydti.fawe.object.collection;
|
||||
|
||||
import com.boydti.fawe.util.MathMan;
|
||||
|
||||
public final class BitArrayUnstretched {
|
||||
|
||||
private final long[] data;
|
||||
private final int bitsPerEntry;
|
||||
private final int maxSeqLocIndex;
|
||||
private final int emptyBitCount;
|
||||
private final long mask;
|
||||
private final int longLen;
|
||||
|
||||
public BitArrayUnstretched(int bitsPerEntry, long[] buffer) {
|
||||
this.bitsPerEntry = bitsPerEntry;
|
||||
this.mask = (1L << bitsPerEntry) - 1L;
|
||||
this.emptyBitCount = 64 % bitsPerEntry;
|
||||
this.maxSeqLocIndex = 64 - (bitsPerEntry + emptyBitCount);
|
||||
final int blocksPerLong = MathMan.floorZero((double) 64 / bitsPerEntry);
|
||||
this.longLen = MathMan.ceilZero((float) 4096 / blocksPerLong);
|
||||
if (buffer.length < longLen) {
|
||||
this.data = new long[longLen];
|
||||
} else {
|
||||
this.data = buffer;
|
||||
}
|
||||
}
|
||||
|
||||
public long[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public final void set(int index, int value) {
|
||||
if (longLen == 0) return;
|
||||
int bitIndexStart = index * bitsPerEntry + MathMan.floorZero((double) index / longLen) * emptyBitCount;
|
||||
int longIndexStart = bitIndexStart >> 6;
|
||||
int localBitIndexStart = bitIndexStart & 63;
|
||||
this.data[longIndexStart] = this.data[longIndexStart] & ~(mask << localBitIndexStart) | (long) value << localBitIndexStart;
|
||||
}
|
||||
|
||||
public final int get(int index) {
|
||||
if (longLen == 0) return 0;
|
||||
int bitIndexStart = index * bitsPerEntry + MathMan.floorZero((double) index / longLen) * emptyBitCount;
|
||||
|
||||
int longIndexStart = bitIndexStart >> 6;
|
||||
|
||||
int localBitIndexStart = bitIndexStart & 63;
|
||||
return (int)(this.data[longIndexStart] >>> localBitIndexStart & mask);
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return longLen;
|
||||
}
|
||||
|
||||
public final void fromRaw(int[] arr) {
|
||||
final long[] data = this.data;
|
||||
final int bitsPerEntry = this.bitsPerEntry;
|
||||
final int maxSeqLocIndex = this.maxSeqLocIndex;
|
||||
|
||||
int localStart = 0;
|
||||
int arrI = 0;
|
||||
long l = 0;
|
||||
for (int i = 0; i < longLen; i++) {
|
||||
int lastVal;
|
||||
for (; localStart <= maxSeqLocIndex && arrI < 4096; localStart += bitsPerEntry) {
|
||||
lastVal = arr[arrI++];
|
||||
l |= ((long) lastVal << localStart);
|
||||
}
|
||||
localStart = 0;
|
||||
data[i] = l;
|
||||
l = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public final int[] toRaw() {
|
||||
return toRaw(new int[4096]);
|
||||
}
|
||||
|
||||
public final int[] toRaw(int[] buffer) {
|
||||
final long[] data = this.data;
|
||||
final int bitsPerEntry = this.bitsPerEntry;
|
||||
final int maxSeqLocIndex = this.maxSeqLocIndex;
|
||||
|
||||
int localStart = 0;
|
||||
int arrI = 0;
|
||||
for (int i = 0; i < longLen; i++) {
|
||||
long l = data[i];
|
||||
char lastVal;
|
||||
for (; localStart <= maxSeqLocIndex && arrI < 4096; localStart += bitsPerEntry) {
|
||||
lastVal = (char) (l >>> localStart & this.mask);
|
||||
buffer[arrI++] = lastVal;
|
||||
}
|
||||
localStart = 0;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public final char[] toRaw(char[] buffer) {
|
||||
final long[] data = this.data;
|
||||
final int bitsPerEntry = this.bitsPerEntry;
|
||||
final int maxSeqLocIndex = this.maxSeqLocIndex;
|
||||
|
||||
int localStart = 0;
|
||||
int arrI = 0;
|
||||
for (int i = 0; i < longLen; i++) {
|
||||
long l = data[i];
|
||||
char lastVal;
|
||||
for (; localStart <= maxSeqLocIndex && arrI < 4096; localStart += bitsPerEntry) {
|
||||
lastVal = (char) (l >>> localStart & this.mask);
|
||||
buffer[arrI++] = lastVal;
|
||||
}
|
||||
localStart = 0;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
package com.sk89q.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -331,4 +332,37 @@ public final class StringUtil {
|
||||
|
||||
return parsableBlocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a string respecting enclosing quotes.
|
||||
*
|
||||
* @param input the input to split.
|
||||
* @param delimiter the delimiter to split on.
|
||||
* @param open the opening quote character.
|
||||
* @param close the closing quote character.
|
||||
* @return a list of split strings.
|
||||
*/
|
||||
public static List<String> split(String input, char delimiter, char open, char close) {
|
||||
if (input.indexOf(open) == -1 && input.indexOf(close) == -1) {
|
||||
return Arrays.asList(input.split(String.valueOf(delimiter)));
|
||||
}
|
||||
int level = 0;
|
||||
int begin = 0;
|
||||
List<String> split = new ArrayList<>();
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
if (c == delimiter && level == 0) {
|
||||
split.add(input.substring(begin, i));
|
||||
begin = i + 1;
|
||||
} else if (c == open) {
|
||||
level++;
|
||||
} else if (c == close) {
|
||||
level--;
|
||||
}
|
||||
}
|
||||
if (begin < input.length()) {
|
||||
split.add(input.substring(begin));
|
||||
}
|
||||
return split;
|
||||
}
|
||||
}
|
||||
|
@ -58,8 +58,8 @@ public class BlockFactory extends AbstractFactory<BaseBlock> {
|
||||
*/
|
||||
public Set<BaseBlock> parseFromListInput(String input, ParserContext context) throws InputParseException {
|
||||
Set<BaseBlock> blocks = new HashSet<>();
|
||||
String[] splits = input.split(",");
|
||||
for (String token : StringUtil.parseListInQuotes(splits, ',', '[', ']', true)) {
|
||||
// String[] splits = input.split(",");
|
||||
for (String token : StringUtil.split(input, ',', '[', ']')) {
|
||||
blocks.add(parseFromInput(token, context));
|
||||
}
|
||||
return blocks;
|
||||
|
@ -24,6 +24,7 @@ import com.sk89q.worldedit.extension.factory.parser.pattern.BlockCategoryPattern
|
||||
import com.sk89q.worldedit.extension.factory.parser.pattern.ClipboardPatternParser;
|
||||
import com.sk89q.worldedit.extension.factory.parser.pattern.RandomPatternParser;
|
||||
import com.sk89q.worldedit.extension.factory.parser.pattern.RandomStatePatternParser;
|
||||
import com.sk89q.worldedit.extension.factory.parser.pattern.SimplexPatternParser;
|
||||
import com.sk89q.worldedit.extension.factory.parser.pattern.SingleBlockPatternParser;
|
||||
import com.sk89q.worldedit.extension.factory.parser.pattern.TypeOrStateApplyingPatternParser;
|
||||
import com.sk89q.worldedit.function.pattern.Pattern;
|
||||
@ -54,6 +55,9 @@ public final class PatternFactory extends AbstractFactory<Pattern> {
|
||||
register(new TypeOrStateApplyingPatternParser(worldEdit));
|
||||
register(new RandomStatePatternParser(worldEdit));
|
||||
register(new BlockCategoryPatternParser(worldEdit));
|
||||
|
||||
// FAWE
|
||||
register(new SimplexPatternParser(worldEdit));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,118 @@
|
||||
package com.sk89q.worldedit.extension.factory.parser;
|
||||
|
||||
import com.sk89q.util.StringUtil;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.extension.input.InputParseException;
|
||||
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||
import com.sk89q.worldedit.internal.registry.InputParser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A rich parser allows parsing of patterns and masks with extra arguments,
|
||||
* e.g. #simplex[scale][pattern].
|
||||
*
|
||||
* @param <E> the parse result.
|
||||
*/
|
||||
public abstract class RichParser<E> extends InputParser<E> {
|
||||
private final String prefix;
|
||||
private final String required;
|
||||
|
||||
/**
|
||||
* Create a new rich parser with a defined prefix for the result, e.g. {@code #simplex}.
|
||||
*
|
||||
* @param worldEdit the worldedit instance.
|
||||
* @param prefix the prefix of this parser result.
|
||||
*/
|
||||
protected RichParser(WorldEdit worldEdit, String prefix) {
|
||||
super(worldEdit);
|
||||
this.prefix = prefix;
|
||||
this.required = prefix + "[";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<String> getSuggestions(String input) {
|
||||
// we don't even want to start suggesting if it's not meant to be this parser result
|
||||
if (input.length() > this.required.length() && !input.startsWith(this.required)) {
|
||||
return Stream.empty();
|
||||
}
|
||||
// suggest until the first [ as long as it isn't fully typed
|
||||
if (input.length() < this.required.length()) {
|
||||
return Stream.of(this.required).filter(s -> s.startsWith(input));
|
||||
}
|
||||
// we know that it is at least "<required>"
|
||||
String[] strings = extractArguments(input.substring(this.prefix.length()), false);
|
||||
StringJoiner joiner = new StringJoiner(",");
|
||||
for (int i = 0; i < strings.length - 1; i++) {
|
||||
joiner.add("[" + strings[i] + "]");
|
||||
}
|
||||
String previous = this.prefix + joiner;
|
||||
return getSuggestions(strings[strings.length - 1], strings.length - 1).map(s -> previous + "[" + s + "]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public E parseFromInput(String input, ParserContext context) throws InputParseException {
|
||||
if (!input.startsWith(this.prefix)) return null;
|
||||
if (input.length() < this.prefix.length()) return null;
|
||||
String[] arguments = extractArguments(input.substring(prefix.length()), true);
|
||||
return parseFromInput(arguments, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a stream of suggestions for the argument at the given index.
|
||||
*
|
||||
* @param argumentInput the already provided input for the argument at the given index.
|
||||
* @param index the index of the argument to get suggestions for.
|
||||
* @return a stream of suggestions matching the given input for the argument at the given index.
|
||||
*/
|
||||
protected abstract Stream<String> getSuggestions(String argumentInput, int index);
|
||||
|
||||
/**
|
||||
* Parses the already split arguments.
|
||||
*
|
||||
* @param arguments the array of arguments that were split (can be empty).
|
||||
* @param context the context of this parsing process.
|
||||
* @return the resulting parsed type.
|
||||
* @throws InputParseException if the input couldn't be parsed correctly.
|
||||
*/
|
||||
protected abstract E parseFromInput(@NotNull String[] arguments, ParserContext context) throws InputParseException;
|
||||
|
||||
/**
|
||||
* Extracts arguments enclosed by {@code []} into an array.
|
||||
* Example: {@code [Hello][World]} results in a list containing {@code Hello} and {@code World}.
|
||||
*
|
||||
* @param input the input to extract arguments from.
|
||||
* @param requireClosing whether or not the extraction requires valid bracketing.
|
||||
* @return an array of extracted arguments.
|
||||
* @throws InputParseException if {@code requireClosing == true} and the count of [ != the count of ]
|
||||
*/
|
||||
protected String[] extractArguments(String input, boolean requireClosing) throws InputParseException {
|
||||
int open = 0; // the "level"
|
||||
int openIndex = 0;
|
||||
int i = 0;
|
||||
List<String> arguments = new ArrayList<>();
|
||||
for (; i < input.length(); i++) {
|
||||
if (input.charAt(i) == '[') {
|
||||
if (open++ == 0) {
|
||||
openIndex = i;
|
||||
}
|
||||
}
|
||||
if (input.charAt(i) == ']') {
|
||||
if (--open == 0) {
|
||||
arguments.add(input.substring(openIndex + 1, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!requireClosing && open > 0) {
|
||||
arguments.add(input.substring(openIndex + 1));
|
||||
}
|
||||
if (requireClosing && open != 0) {
|
||||
throw new InputParseException("Invalid bracketing, are you missing a '[' or ']'?");
|
||||
}
|
||||
return arguments.toArray(new String[0]);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -52,6 +52,19 @@ public class RandomPattern extends AbstractPattern {
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a random pattern from an existing one but with a different random.
|
||||
*
|
||||
* @param random the new random to use.
|
||||
* @param parent the existing random pattern.
|
||||
*/
|
||||
public RandomPattern(SimpleRandom random, RandomPattern parent) {
|
||||
this.random = random;
|
||||
this.weights = parent.weights;
|
||||
this.collection = RandomCollection.of(weights, random);
|
||||
this.patterns = parent.patterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a pattern to the weight list of patterns.
|
||||
*
|
||||
|
Reference in New Issue
Block a user