From b25fcf651620668f22d524d978b536f312cd11b4 Mon Sep 17 00:00:00 2001 From: theEvilReaper Date: Fri, 13 Sep 2024 11:02:02 +0200 Subject: [PATCH 1/2] Cache the result of the values method --- .../server/instance/block/BlockFace.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/block/BlockFace.java b/src/main/java/net/minestom/server/instance/block/BlockFace.java index ec69fe23689..cc5bc2ceca8 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockFace.java +++ b/src/main/java/net/minestom/server/instance/block/BlockFace.java @@ -3,7 +3,13 @@ import net.minestom.server.utils.Direction; import org.jetbrains.annotations.NotNull; +/** + * The enumeration contains all faces which a block can have in the game. + * It's possible that specific blocks doesn't have all faces + * @version 1.0.1 + */ public enum BlockFace { + BOTTOM(Direction.DOWN), TOP(Direction.UP), NORTH(Direction.NORTH), @@ -11,13 +17,25 @@ public enum BlockFace { WEST(Direction.WEST), EAST(Direction.EAST); + private static final BlockFace[] VALUES = values(); + private final Direction direction; - BlockFace(Direction direction) { + /** + * Creates a new enum entry + * + * @param direction the direction for the entry + */ + BlockFace(@NotNull Direction direction) { this.direction = direction; } - public Direction toDirection() { + /** + * Returns the {@link Direction} which correspond with the face. + * + * @return the given direction + */ + public @NotNull Direction toDirection() { return direction; } @@ -77,4 +95,12 @@ public static BlockFace fromDirection(Direction direction) { case EAST -> EAST; }; } + + /** + * Returns the static accessor which caches all entries from the values call. + * @return the given array + */ + public static @NotNull BlockFace[] getValues() { + return VALUES; + } } From 3e5d0cabba84594662f9574981b50f632d138d0c Mon Sep 17 00:00:00 2001 From: theEvilReaper Date: Fri, 13 Sep 2024 11:02:25 +0200 Subject: [PATCH 2/2] Update BlockFace iteration --- .../java/net/minestom/server/collision/ShapeImpl.java | 4 ++-- .../net/minestom/server/instance/light/BlockLight.java | 4 ++-- .../java/net/minestom/server/instance/light/Light.java | 6 ++++-- .../net/minestom/server/instance/light/SkyLight.java | 4 ++-- .../packet/client/play/ClientPlayerDiggingPacket.java | 2 +- .../server/instance/light/BlockIsOccludedTest.java | 10 +++++----- 6 files changed, 16 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 82b1ea497b6..f6c097c40ce 100644 --- a/src/main/java/net/minestom/server/collision/ShapeImpl.java +++ b/src/main/java/net/minestom/server/collision/ShapeImpl.java @@ -208,7 +208,7 @@ private static CollisionData collisionData(List collisionBoundingBo } byte fullCollisionFaces = 0; - for (BlockFace f : BlockFace.values()) { + for (BlockFace f : BlockFace.getValues()) { final byte res = isFaceCovered(computeOcclusionSet(f, collisionBoundingBoxes)); fullCollisionFaces |= ((res == 2) ? 0b1 : 0b0) << (byte) f.ordinal(); } @@ -219,7 +219,7 @@ private static CollisionData collisionData(List collisionBoundingBo private static LightData lightData(List occlusionBoundingBoxes, int lightEmission) { byte fullFaces = 0; byte airFaces = 0; - for (BlockFace f : BlockFace.values()) { + for (BlockFace f : BlockFace.getValues()) { final byte res = isFaceCovered(computeOcclusionSet(f, occlusionBoundingBoxes)); fullFaces |= ((res == 2) ? 0b1 : 0b0) << (byte) f.ordinal(); airFaces |= ((res == 0) ? 0b1 : 0b0) << (byte) f.ordinal(); diff --git a/src/main/java/net/minestom/server/instance/light/BlockLight.java b/src/main/java/net/minestom/server/instance/light/BlockLight.java index 244940fc430..f0b5109ae46 100644 --- a/src/main/java/net/minestom/server/instance/light/BlockLight.java +++ b/src/main/java/net/minestom/server/instance/light/BlockLight.java @@ -52,7 +52,7 @@ private ShortArrayFIFOQueue buildExternalQueue(Palette blockPalette, ShortArrayFIFOQueue lightSources = new ShortArrayFIFOQueue(); for (int i = 0; i < neighbors.length; i++) { - final BlockFace face = BlockFace.values()[i]; + final BlockFace face = BlockFace.getValues()[i]; Point neighborSection = neighbors[i]; if (neighborSection == null) continue; @@ -203,7 +203,7 @@ public Set calculateExternal(Palette blockPalette, for (int i = 0; i < neighbors.length; i++) { final Point neighbor = neighbors[i]; if (neighbor == null) continue; - final BlockFace face = BlockFace.values()[i]; + final BlockFace face = BlockFace.getValues()[i]; if (!LightCompute.compareBorders(content, contentPropagation, contentPropagationTemp, face)) { toUpdate.add(neighbor); } diff --git a/src/main/java/net/minestom/server/instance/light/Light.java b/src/main/java/net/minestom/server/instance/light/Light.java index 45216c834a7..add0d2f1a4c 100644 --- a/src/main/java/net/minestom/server/instance/light/Light.java +++ b/src/main/java/net/minestom/server/instance/light/Light.java @@ -51,8 +51,10 @@ static Point[] getNeighbors(Chunk chunk, int sectionY) { final int chunkX = chunk.getChunkX(); final int chunkZ = chunk.getChunkZ(); - Point[] links = new Vec[BlockFace.values().length]; - for (BlockFace face : BlockFace.values()) { + final BlockFace[] faces = BlockFace.getValues(); + + Point[] links = new Vec[faces.length]; + for (BlockFace face : faces) { final Direction direction = face.toDirection(); final int x = chunkX + direction.normalX(); final int z = chunkZ + direction.normalZ(); diff --git a/src/main/java/net/minestom/server/instance/light/SkyLight.java b/src/main/java/net/minestom/server/instance/light/SkyLight.java index 6a79b672048..e44291d9c85 100644 --- a/src/main/java/net/minestom/server/instance/light/SkyLight.java +++ b/src/main/java/net/minestom/server/instance/light/SkyLight.java @@ -54,7 +54,7 @@ private ShortArrayFIFOQueue buildExternalQueue(Palette blockPalette, ShortArrayFIFOQueue lightSources = new ShortArrayFIFOQueue(); for (int i = 0; i < neighbors.length; i++) { - final BlockFace face = BlockFace.values()[i]; + final BlockFace face = BlockFace.getValues()[i]; Point neighborSection = neighbors[i]; if (neighborSection == null) continue; @@ -225,7 +225,7 @@ public Set calculateExternal(Palette blockPalette, for (int i = 0; i < neighbors.length; i++) { final Point neighbor = neighbors[i]; if (neighbor == null) continue; - final BlockFace face = BlockFace.values()[i]; + final BlockFace face = BlockFace.getValues()[i]; if (!LightCompute.compareBorders(content, contentPropagation, contentPropagationTemp, face)) { toUpdate.add(neighbor); } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java index c6c8c08b18a..a39026f5327 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java @@ -12,7 +12,7 @@ public record ClientPlayerDiggingPacket(@NotNull Status status, @NotNull Point b @NotNull BlockFace blockFace, int sequence) implements ClientPacket { public ClientPlayerDiggingPacket(@NotNull NetworkBuffer reader) { this(reader.readEnum(Status.class), reader.read(BLOCK_POSITION), - BlockFace.values()[reader.read(BYTE)], reader.read(VAR_INT)); + BlockFace.getValues()[reader.read(BYTE)], reader.read(VAR_INT)); } @Override diff --git a/src/test/java/net/minestom/server/instance/light/BlockIsOccludedTest.java b/src/test/java/net/minestom/server/instance/light/BlockIsOccludedTest.java index 0160c531571..c2457c6b128 100644 --- a/src/test/java/net/minestom/server/instance/light/BlockIsOccludedTest.java +++ b/src/test/java/net/minestom/server/instance/light/BlockIsOccludedTest.java @@ -15,7 +15,7 @@ class BlockIsOccludedTest { void blockAir() { Shape airBlock = Block.AIR.registry().collisionShape(); - for (BlockFace face : BlockFace.values()) { + for (BlockFace face : BlockFace.getValues()) { assertFalse(airBlock.isOccluded(airBlock, face)); } } @@ -25,7 +25,7 @@ void blockLantern() { Shape shape = Block.LANTERN.registry().collisionShape(); Shape airBlock = Block.AIR.registry().collisionShape(); - for (BlockFace face : BlockFace.values()) { + for (BlockFace face : BlockFace.getValues()) { assertFalse(shape.isOccluded(airBlock, face)); } } @@ -35,7 +35,7 @@ void blockSpruceLeaves() { Shape shape = Block.SPRUCE_LEAVES.registry().collisionShape(); Shape airBlock = Block.AIR.registry().collisionShape(); - for (BlockFace face : BlockFace.values()) { + for (BlockFace face : BlockFace.getValues()) { assertFalse(shape.isOccluded(airBlock, face)); } } @@ -45,7 +45,7 @@ void blockCauldron() { Shape shape = Block.CAULDRON.registry().collisionShape(); Shape airBlock = Block.AIR.registry().collisionShape(); - for (BlockFace face : BlockFace.values()) { + for (BlockFace face : BlockFace.getValues()) { assertFalse(shape.isOccluded(airBlock, face)); } } @@ -114,7 +114,7 @@ void blockStone() { Shape shape = Block.STONE.registry().collisionShape(); Shape airBlock = Block.AIR.registry().collisionShape(); - for (BlockFace face : BlockFace.values()) { + for (BlockFace face : BlockFace.getValues()) { assertTrue(shape.isOccluded(airBlock, face)); } }