From 60f583b179a213e4f1d707cf26e4696f816c9183 Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 13 Mar 2022 21:27:56 +0100 Subject: [PATCH] Cache shapes size --- .../minestom/server/collision/ShapeImpl.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/minestom/server/collision/ShapeImpl.java b/src/main/java/net/minestom/server/collision/ShapeImpl.java index 31e7c34c5..c73dc5fea 100644 --- a/src/main/java/net/minestom/server/collision/ShapeImpl.java +++ b/src/main/java/net/minestom/server/collision/ShapeImpl.java @@ -14,11 +14,29 @@ import java.util.regex.Pattern; final class ShapeImpl implements Shape { private static final Pattern PATTERN = Pattern.compile("\\d.\\d{1,3}", Pattern.MULTILINE); private final BoundingBox[] blockSections; + private final Point relativeStart, relativeEnd; private final Supplier block; private ShapeImpl(BoundingBox[] boundingBoxes, Supplier block) { this.blockSections = boundingBoxes; 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 block) { @@ -51,24 +69,12 @@ final class ShapeImpl implements Shape { @Override public @NotNull Point relativeStart() { - double minX = 1, minY = 1, minZ = 1; - 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); + return relativeStart; } @Override public @NotNull Point relativeEnd() { - double maxX = 1, maxY = 1, maxZ = 1; - 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); + return relativeEnd; } @Override