mirror of https://github.com/Minestom/Minestom.git
Add `Area#fromList`
This commit is contained in:
parent
e3cc166e5d
commit
cc1b0d894f
|
@ -7,6 +7,10 @@ import java.util.List;
|
|||
|
||||
public interface Area extends Iterable<Point> {
|
||||
|
||||
static @NotNull Area fromList(@NotNull List<? extends Point> list) {
|
||||
return AreaImpl.fromList(list);
|
||||
}
|
||||
|
||||
static @NotNull Area fill(@NotNull Point point1, @NotNull Point point2) {
|
||||
return new AreaImpl.Fill(point1, point2);
|
||||
}
|
||||
|
@ -16,7 +20,7 @@ public interface Area extends Iterable<Point> {
|
|||
}
|
||||
|
||||
static @NotNull Area randomizer(@NotNull Area area, double probability) {
|
||||
return new AreaImpl.Randomizer(area, probability);
|
||||
return AreaImpl.Randomize(area, probability);
|
||||
}
|
||||
|
||||
@NotNull List<@NotNull Point> asList();
|
||||
|
|
|
@ -12,6 +12,23 @@ import java.util.function.UnaryOperator;
|
|||
|
||||
class AreaImpl {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static Area fromList(List<? extends Point> list) {
|
||||
final var copy = List.copyOf(list); // Defensive copy
|
||||
return new Area() {
|
||||
@Override
|
||||
public @NotNull List<@NotNull Point> asList() {
|
||||
return (List<Point>) copy;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<Point> iterator() {
|
||||
return (Iterator<Point>) copy.iterator();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static class Fill implements Area {
|
||||
private final Point min, max;
|
||||
|
||||
|
@ -85,19 +102,7 @@ class AreaImpl {
|
|||
|
||||
@Override
|
||||
public @NotNull Area end() {
|
||||
final List<Point> list = List.copyOf(positions);
|
||||
return new Area() {
|
||||
@Override
|
||||
public @NotNull List<@NotNull Point> asList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<Point> iterator() {
|
||||
return list.iterator();
|
||||
}
|
||||
};
|
||||
return fromList(positions);
|
||||
}
|
||||
|
||||
private Area.Path with(UnaryOperator<Point> operator) {
|
||||
|
@ -107,30 +112,15 @@ class AreaImpl {
|
|||
}
|
||||
}
|
||||
|
||||
static class Randomizer implements Area {
|
||||
private final List<Point> points;
|
||||
|
||||
protected Randomizer(Area area, double probability) {
|
||||
var random = ThreadLocalRandom.current();
|
||||
List<Point> positions = new ArrayList<>();
|
||||
area.forEach(blockPosition -> {
|
||||
final double value = random.nextDouble();
|
||||
if (value <= probability) {
|
||||
positions.add(blockPosition);
|
||||
}
|
||||
});
|
||||
this.points = List.copyOf(positions);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<Point> iterator() {
|
||||
return points.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<@NotNull Point> asList() {
|
||||
return points;
|
||||
}
|
||||
static Area Randomize(Area area, double probability) {
|
||||
List<Point> points = new ArrayList<>();
|
||||
var localRandom = ThreadLocalRandom.current();
|
||||
area.forEach(blockPosition -> {
|
||||
final double value = localRandom.nextDouble();
|
||||
if (value <= probability) {
|
||||
points.add(blockPosition);
|
||||
}
|
||||
});
|
||||
return fromList(points);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue