Cache shapes size

This commit is contained in:
themode 2022-03-13 21:27:56 +01:00
parent 11708a1c1a
commit 60f583b179

View File

@ -14,11 +14,29 @@ import java.util.regex.Pattern;
final class ShapeImpl implements Shape { final class ShapeImpl implements Shape {
private static final Pattern PATTERN = Pattern.compile("\\d.\\d{1,3}", Pattern.MULTILINE); private static final Pattern PATTERN = Pattern.compile("\\d.\\d{1,3}", Pattern.MULTILINE);
private final BoundingBox[] blockSections; private final BoundingBox[] blockSections;
private final Point relativeStart, relativeEnd;
private final Supplier<Material> block; private final Supplier<Material> block;
private ShapeImpl(BoundingBox[] boundingBoxes, Supplier<Material> block) { private ShapeImpl(BoundingBox[] boundingBoxes, Supplier<Material> block) {
this.blockSections = boundingBoxes; this.blockSections = boundingBoxes;
this.block = block; this.block = block;
// Find bounds
{
double minX = 1, minY = 1, minZ = 1;
double maxX = 1, maxY = 1, maxZ = 1;
for (BoundingBox blockSection : blockSections) {
// Min
if (blockSection.minX() < minX) minX = blockSection.minX();
if (blockSection.minY() < minY) minY = blockSection.minY();
if (blockSection.minZ() < minZ) minZ = blockSection.minZ();
// Max
if (blockSection.maxX() < maxX) maxX = blockSection.maxX();
if (blockSection.maxY() < maxY) maxY = blockSection.maxY();
if (blockSection.maxZ() < maxZ) maxZ = blockSection.maxZ();
}
this.relativeStart = new Vec(minX, minY, minZ);
this.relativeEnd = new Vec(maxX, maxY, maxZ);
}
} }
static ShapeImpl parseBlockFromRegistry(String str, Supplier<Material> block) { static ShapeImpl parseBlockFromRegistry(String str, Supplier<Material> block) {
@ -51,24 +69,12 @@ final class ShapeImpl implements Shape {
@Override @Override
public @NotNull Point relativeStart() { public @NotNull Point relativeStart() {
double minX = 1, minY = 1, minZ = 1; return relativeStart;
for (BoundingBox blockSection : blockSections) {
if (blockSection.minX() < minX) minX = blockSection.minX();
if (blockSection.minY() < minY) minY = blockSection.minY();
if (blockSection.minZ() < minZ) minZ = blockSection.minZ();
}
return new Vec(minX, minY, minZ);
} }
@Override @Override
public @NotNull Point relativeEnd() { public @NotNull Point relativeEnd() {
double maxX = 1, maxY = 1, maxZ = 1; return relativeEnd;
for (BoundingBox blockSection : blockSections) {
if (blockSection.maxX() < maxX) maxX = blockSection.maxX();
if (blockSection.maxY() < maxY) maxY = blockSection.maxY();
if (blockSection.maxZ() < maxZ) maxZ = blockSection.maxZ();
}
return new Vec(maxX, maxY, maxZ);
} }
@Override @Override