use of com.sk89q.worldedit.function.pattern.Pattern in project FastAsyncWorldEdit by IntellectualSites.
the class RichPatternParser method parseFromInput.
@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.isEmpty()) {
throw new SuggestInputParseException("No input provided", "", () -> Stream.concat(Stream.of("#", ",", "&"), BlockTypes.getNameSpaces().stream().map(n -> n + ":")).collect(Collectors.toList()));
}
List<Double> chances = new ArrayList<>();
List<Pattern> patterns = new ArrayList<>();
final CommandLocals locals = new CommandLocals();
Actor actor = context != null ? context.getActor() : null;
if (actor != null) {
locals.put(Actor.class, actor);
}
try {
for (Map.Entry<ParseEntry, List<String>> entry : parse(input)) {
ParseEntry pe = entry.getKey();
final String command = pe.getInput();
String full = pe.getFull();
Pattern pattern = null;
double chance = 1;
if (command.isEmpty()) {
pattern = parseFromInput(StringMan.join(entry.getValue(), ','), context);
} else if (!worldEdit.getPatternFactory().containsAlias(command)) {
// Legacy patterns
char char0 = command.charAt(0);
boolean charPattern = input.length() > 1 && input.charAt(1) != '[';
if (charPattern && input.charAt(0) == '=') {
pattern = parseFromInput(char0 + "[" + input.substring(1) + "]", context);
}
if (char0 == '#' && command.length() > 1 && command.charAt(1) != '#') {
throw new SuggestInputParseException(new NoMatchException(Caption.of("fawe.error.parse.unknown-pattern", full, TextComponent.of("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns").clickEvent(ClickEvent.openUrl("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns")))), full, () -> {
if (full.length() == 1) {
return new ArrayList<>(worldEdit.getPatternFactory().getSuggestions(""));
}
return new ArrayList<>(worldEdit.getPatternFactory().getSuggestions(command.toLowerCase(Locale.ROOT)));
});
}
if (charPattern) {
if (char0 == '$' || char0 == '^' || char0 == '*' || (char0 == '#' && input.charAt(1) == '#')) {
pattern = worldEdit.getPatternFactory().parseWithoutRich(full, context);
}
}
if (pattern == null) {
if (command.startsWith("[")) {
int end = command.lastIndexOf(']');
pattern = parseFromInput(command.substring(1, end == -1 ? command.length() : end), context);
} else {
int percentIndex = command.indexOf('%');
if (percentIndex != -1 && percentPatternRegex.matcher(command).matches()) {
// Legacy percent pattern
chance = Expression.compile(command.substring(0, percentIndex)).evaluate();
String value = command.substring(percentIndex + 1);
if (!entry.getValue().isEmpty()) {
boolean addBrackets = !value.isEmpty();
if (addBrackets) {
value += "[";
}
value += StringMan.join(entry.getValue(), " ");
if (addBrackets) {
value += "]";
}
}
pattern = parseFromInput(value, context);
} else {
// legacy block pattern
try {
pattern = worldEdit.getBlockFactory().parseFromInput(pe.getFull(), context);
} catch (NoMatchException e) {
throw new NoMatchException(Caption.of("fawe.error.parse.unknown-pattern", full, TextComponent.of("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns").clickEvent(com.sk89q.worldedit.util.formatting.text.event.ClickEvent.openUrl("https://intellectualsites.github.io/fastasyncworldedit-documentation/patterns/patterns"))));
}
}
}
}
} else {
List<String> args = entry.getValue();
try {
pattern = worldEdit.getPatternFactory().parseWithoutRich(full, context);
} catch (InputParseException rethrow) {
throw rethrow;
} catch (Throwable e) {
throw SuggestInputParseException.of(e, full, () -> {
try {
String cmdArgs = ((args.isEmpty()) ? "" : " " + StringMan.join(args, " "));
List<Substring> split = CommandArgParser.forArgString(cmdArgs).parseArgs().toList();
List<String> argStrings = split.stream().map(Substring::getSubstring).collect(Collectors.toList());
MemoizingValueAccess access = getPlatform().initializeInjectedValues(() -> cmdArgs, actor, null, true);
List<String> suggestions = getPlatform().getCommandManager().getSuggestions(access, argStrings).stream().map(Suggestion::getSuggestion).collect(Collectors.toUnmodifiableList());
List<String> result = new ArrayList<>();
if (suggestions.size() <= 2) {
for (int i = 0; i < suggestions.size(); i++) {
String suggestion = suggestions.get(i);
if (suggestion.indexOf(' ') != 0) {
String[] splitSuggestion = suggestion.split(" ");
suggestion = "[" + StringMan.join(splitSuggestion, "][") + "]";
result.set(i, suggestion);
}
}
}
return result;
} catch (Throwable e2) {
e2.printStackTrace();
throw new InputParseException(TextComponent.of(e2.getMessage()));
}
});
}
}
if (pattern != null) {
patterns.add(pattern);
chances.add(chance);
}
}
} catch (InputParseException rethrow) {
throw rethrow;
} catch (Throwable e) {
e.printStackTrace();
throw new InputParseException(TextComponent.of(e.getMessage()), e);
}
if (patterns.isEmpty()) {
return null;
}
if (patterns.size() == 1) {
return patterns.get(0);
}
RandomPattern random = new RandomPattern(new TrueRandom());
for (int i = 0; i < patterns.size(); i++) {
random.add(patterns.get(i), chances.get(i));
}
return random;
}
use of com.sk89q.worldedit.function.pattern.Pattern in project FastAsyncWorldEdit by IntellectualSites.
the class FaweDelegateRegionManager method handleClear.
public boolean handleClear(@Nonnull Plot plot, @Nullable Runnable whenDone, @Nonnull PlotManager manager) {
TaskManager.taskManager().async(() -> {
synchronized (FaweDelegateRegionManager.class) {
final HybridPlotWorld hybridPlotWorld = ((HybridPlotManager) manager).getHybridPlotWorld();
World world = BukkitAdapter.adapt(getWorld(hybridPlotWorld.getWorldName()));
EditSession editSession = WorldEdit.getInstance().newEditSessionBuilder().world(world).checkMemory(false).fastMode(true).limitUnlimited().changeSetNull().build();
if (!hybridPlotWorld.PLOT_SCHEMATIC || !Settings.Schematics.PASTE_ON_TOP) {
final BlockType bedrock;
final BlockType air = BlockTypes.AIR;
if (hybridPlotWorld.PLOT_BEDROCK) {
bedrock = BlockTypes.BEDROCK;
} else {
bedrock = air;
}
final Pattern filling = hybridPlotWorld.MAIN_BLOCK.toPattern();
final Pattern plotfloor = hybridPlotWorld.TOP_BLOCK.toPattern();
final BiomeType biome = hybridPlotWorld.getPlotBiome();
BlockVector3 pos1 = plot.getBottomAbs().getBlockVector3();
BlockVector3 pos2 = pos1.add(BlockVector3.at(hybridPlotWorld.PLOT_WIDTH - 1, hybridPlotWorld.getMaxGenHeight(), hybridPlotWorld.PLOT_WIDTH - 1));
if (hybridPlotWorld.PLOT_BEDROCK) {
Region bedrockRegion = new CuboidRegion(pos1, pos2.withY(hybridPlotWorld.getMinGenHeight()));
editSession.setBlocks(bedrockRegion, bedrock);
}
Region fillingRegion = new CuboidRegion(pos1.withY(hybridPlotWorld.getMinGenHeight() + 1), pos2.withY(hybridPlotWorld.PLOT_HEIGHT - 1));
Region floorRegion = new CuboidRegion(pos1.withY(hybridPlotWorld.PLOT_HEIGHT), pos2.withY(hybridPlotWorld.PLOT_HEIGHT));
Region airRegion = new CuboidRegion(pos1.withY(hybridPlotWorld.PLOT_HEIGHT + 1), pos2.withY(hybridPlotWorld.getMaxGenHeight()));
editSession.setBlocks(fillingRegion, filling);
editSession.setBlocks(floorRegion, plotfloor);
editSession.setBlocks(airRegion, air);
}
if (hybridPlotWorld.PLOT_SCHEMATIC) {
// We cannot reuse the editsession
EditSession scheditsession = !Settings.Schematics.PASTE_ON_TOP ? editSession : WorldEdit.getInstance().newEditSessionBuilder().world(world).checkMemory(false).fastMode(true).limitUnlimited().changeSetNull().build();
File schematicFile = new File(hybridPlotWorld.getRoot(), "plot.schem");
if (!schematicFile.exists()) {
schematicFile = new File(hybridPlotWorld.getRoot(), "plot.schematic");
}
BlockVector3 to = plot.getBottomAbs().getBlockVector3().withY(Settings.Schematics.PASTE_ON_TOP ? hybridPlotWorld.SCHEM_Y : hybridPlotWorld.getMinBuildHeight());
try {
Clipboard clip = ClipboardFormats.findByFile(schematicFile).getReader(new FileInputStream(schematicFile)).read();
clip.paste(scheditsession, to, true, true, true);
} catch (IOException e) {
e.printStackTrace();
}
// Be verbose in editsession flushing
scheditsession.flushQueue();
}
editSession.flushQueue();
FaweAPI.fixLighting(world, new CuboidRegion(plot.getBottomAbs().getBlockVector3(), plot.getTopAbs().getBlockVector3()), null, RelightMode.valueOf(com.fastasyncworldedit.core.configuration.Settings.settings().LIGHTING.MODE));
if (whenDone != null) {
TaskManager.taskManager().task(whenDone);
}
}
});
return true;
}
use of com.sk89q.worldedit.function.pattern.Pattern in project FastAsyncWorldEdit by IntellectualSites.
the class SplatterBrush method apply.
@Override
public void apply(final EditSession editSession, final LocalBlockVectorSet placed, final BlockVector3 position, Pattern p, double size) throws MaxChangedBlocksException {
final Pattern finalPattern;
if (solid) {
finalPattern = p.applyBlock(position);
} else {
finalPattern = p;
}
final int size2 = (int) (size * size);
SurfaceMask surface = new SurfaceMask(editSession);
RecursiveVisitor visitor = new RecursiveVisitor(new SplatterBrushMask(editSession, position, size2, surface, placed), vector -> editSession.setBlock(vector, finalPattern), recursion, editSession.getMinY(), editSession.getMaxY());
visitor.setMaxBranch(2);
visitor.setDirections(Arrays.asList(BreadthFirstSearch.DIAGONAL_DIRECTIONS));
visitor.visit(position);
Operations.completeBlindly(visitor);
}
use of com.sk89q.worldedit.function.pattern.Pattern in project FastAsyncWorldEdit by IntellectualSites.
the class LayerBrush method build.
@Override
public void build(EditSession editSession, BlockVector3 position, Pattern ignore, double size) throws MaxChangedBlocksException {
final AdjacentAnyMask adjacent = new AdjacentAnyMask(new BlockMask(editSession).add(BlockTypes.AIR, BlockTypes.CAVE_AIR, BlockTypes.VOID_AIR), editSession.getMinY(), editSession.getMaxY());
final SolidBlockMask solid = new SolidBlockMask(editSession);
final RadiusMask radius = new RadiusMask(0, (int) size);
visitor = new RecursiveVisitor(new MaskIntersection(adjacent, solid, radius), funcion -> true, Integer.MAX_VALUE, editSession.getMinY(), editSession.getMaxY());
visitor.visit(position);
visitor.setDirections(Arrays.asList(BreadthFirstSearch.DIAGONAL_DIRECTIONS));
Operations.completeBlindly(visitor);
BlockVectorSet visited = visitor.getVisited();
visitor = new RecursiveVisitor(new LayerBrushMask(editSession, visitor, layers, adjacent), pos -> {
int depth = visitor.getDepth();
Pattern currentPattern = layers[depth];
return currentPattern.apply(editSession, pos, pos);
}, layers.length - 1, editSession.getMinY(), editSession.getMaxY());
for (BlockVector3 pos : visited) {
visitor.visit(pos);
}
Operations.completeBlindly(visitor);
visitor = null;
}
use of com.sk89q.worldedit.function.pattern.Pattern in project FastAsyncWorldEdit by IntellectualSites.
the class NoisePatternParser method parseFromInput.
@Override
protected Pattern parseFromInput(@Nonnull String[] arguments, ParserContext context) {
if (arguments.length != 2) {
throw new InputParseException(Caption.of("fawe.error.command.syntax", TextComponent.of(getPrefix() + "[scale][pattern] (e.g. " + getPrefix() + "[5][dirt,stone])")));
}
double scale = parseScale(arguments[0]);
Pattern inner = worldEdit.getPatternFactory().parseFromInput(arguments[1], context);
if (inner instanceof RandomPattern) {
return new RandomPattern(new NoiseRandom(this.generatorSupplier.get(), scale), (RandomPattern) inner);
} else if (inner instanceof BlockStateHolder) {
// single blocks won't have any impact on how a noise behaves
return inner;
} else {
throw new InputParseException(TextComponent.of("Pattern " + inner.getClass().getSimpleName() + " cannot be used with #" + this.name));
}
}
Aggregations