use of com.fastasyncworldedit.core.extent.transform.RandomTransform in project FastAsyncWorldEdit by IntellectualSites.
the class RichTransformParser method parseFromInput.
@Override
public ResettableExtent parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.isEmpty()) {
return null;
}
List<Double> unionChances = new ArrayList<>();
List<Double> intersectionChances = new ArrayList<>();
List<ResettableExtent> intersection = new ArrayList<>();
List<ResettableExtent> union = new ArrayList<>();
final CommandLocals locals = new CommandLocals();
Actor actor = context != null ? context.getActor() : null;
if (actor != null) {
locals.put(Actor.class, actor);
}
try {
List<Map.Entry<ParseEntry, List<String>>> parsed = parse(input);
for (Map.Entry<ParseEntry, List<String>> entry : parsed) {
ParseEntry pe = entry.getKey();
String command = pe.getInput();
ResettableExtent transform;
double chance = 1;
if (command.isEmpty()) {
transform = parseFromInput(StringMan.join(entry.getValue(), ','), context);
} else if (!worldEdit.getTransformFactory().containsAlias(command)) {
// Legacy syntax
int percentIndex = command.indexOf('%');
if (percentIndex != -1) {
// Legacy percent pattern
chance = Expression.compile(command.substring(0, percentIndex)).evaluate();
command = command.substring(percentIndex + 1);
if (!entry.getValue().isEmpty()) {
if (!command.isEmpty()) {
command += " ";
}
command += StringMan.join(entry.getValue(), " ");
}
transform = parseFromInput(command, context);
} else {
throw new NoMatchException(Caption.of("fawe.error.parse.unknown-transform", pe.getFull(), TextComponent.of("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms").clickEvent(ClickEvent.openUrl("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms"))));
}
} else {
try {
transform = worldEdit.getTransformFactory().parseWithoutRich(pe.getFull(), context);
} catch (SuggestInputParseException rethrow) {
throw rethrow;
} catch (Throwable e) {
throw new NoMatchException(Caption.of("fawe.error.parse.unknown-transform", pe.getFull(), TextComponent.of("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms").clickEvent(ClickEvent.openUrl("https://intellectualsites.github" + ".io/fastasyncworldedit-documentation/transforms/transforms"))));
}
}
if (pe.isAnd()) {
// &
intersectionChances.add(chance);
intersection.add(transform);
} else {
if (!intersection.isEmpty()) {
if (intersection.size() == 1) {
throw new InputParseException(Caption.of("fawe.error.parse.invalid-dangling-character", "&"));
}
MultiTransform multi = new MultiTransform();
double total = 0;
for (int i = 0; i < intersection.size(); i++) {
Double value = intersectionChances.get(i);
total += value;
multi.add(intersection.get(i), value);
}
union.add(multi);
unionChances.add(total);
intersection.clear();
intersectionChances.clear();
}
unionChances.add(chance);
union.add(transform);
}
}
} catch (Throwable e) {
throw new InputParseException(TextComponent.of(e.getMessage()), e);
}
if (!intersection.isEmpty()) {
if (intersection.size() == 1) {
throw new InputParseException(Caption.of("fawe.error.parse.invalid-dangling-character", "&"));
}
MultiTransform multi = new MultiTransform();
double total = 0;
for (int i = 0; i < intersection.size(); i++) {
Double value = intersectionChances.get(i);
total += value;
multi.add(intersection.get(i), value);
}
union.add(multi);
unionChances.add(total);
intersection.clear();
intersectionChances.clear();
}
if (union.isEmpty()) {
throw new NoMatchException(Caption.of("fawe.error.parse.unknown-transform", input, TextComponent.of("https://intellectualsites.github.io/fastasyncworldedit-documentation/transforms/transforms").clickEvent(ClickEvent.openUrl("https://intellectualsites.github.io/fastasyncworldedit-documentation/transforms/transforms"))));
} else if (union.size() == 1) {
return union.get(0);
} else {
RandomTransform random = new RandomTransform(new TrueRandom());
for (int i = 0; i < union.size(); i++) {
random.add(union.get(i), unionChances.get(i));
}
return random;
}
}
use of com.fastasyncworldedit.core.extent.transform.RandomTransform in project FastAsyncWorldEdit by IntellectualSites.
the class RandomTransformParser method parseFromInput.
@Override
public ResettableExtent parseFromInput(String input, ParserContext context) throws InputParseException {
if (input.isEmpty()) {
return null;
}
List<String> split = StringUtil.split(input, ',', '[', ']');
if (split.size() == 1) {
return null;
}
RandomTransform randomTransform = new RandomTransform();
for (String s : split) {
ResettableExtent transform = worldEdit.getTransformFactory().parseFromInput(s, context);
randomTransform.add(transform, 1d);
}
return randomTransform;
}
Aggregations