From 370f3c18ba4e014a8894627da9a2ae662c791b3d Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Wed, 10 Mar 2021 13:54:09 -0500 Subject: [PATCH] Use compute for determining if a lectern if present Reduces having to call both containsKey and get. --- .../world/block/BlockStateValues.java | 9 ++++- .../geysermc/connector/utils/ChunkUtils.java | 37 ++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockStateValues.java b/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockStateValues.java index ebc90b722..ff51562b9 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockStateValues.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockStateValues.java @@ -29,6 +29,7 @@ import com.fasterxml.jackson.databind.JsonNode; import it.unimi.dsi.fastutil.ints.*; import java.util.Map; +import java.util.function.BiFunction; /** * Used for block entities if the Java block state contains Bedrock block information. @@ -199,7 +200,13 @@ public class BlockStateValues { return FLOWER_POT_VALUES; } - public static Int2BooleanMap getLecternBookStates() { + /** + * This returns a Map interface so IntelliJ doesn't complain about {@link Int2BooleanMap#compute(int, BiFunction)} + * not returning null. + * + * @return the lectern book state map pointing to book present state + */ + public static Map getLecternBookStates() { return LECTERN_BOOK_STATES; } diff --git a/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java b/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java index b3a31e1ab..7d5b56615 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/ChunkUtils.java @@ -372,27 +372,30 @@ public class ChunkUtils { } session.sendUpstreamPacket(waterPacket); - if (BlockStateValues.getLecternBookStates().containsKey(blockState)) { - boolean lecternCachedHasBook = session.getLecternCache().contains(position); - boolean newLecternHasBook = BlockStateValues.getLecternBookStates().get(blockState); - if (!session.getConnector().getWorldManager().shouldExpectLecternHandled() && lecternCachedHasBook != newLecternHasBook) { - // Refresh the block entirely - it either has a book or no longer has a book - NbtMap newLecternTag; - if (newLecternHasBook) { - newLecternTag = session.getConnector().getWorldManager().getLecternDataAt(session, position.getX(), position.getY(), position.getZ(), false); + BlockStateValues.getLecternBookStates().compute(blockState, (key, newLecternHasBook) -> { + // Determine if this block is a lectern + if (newLecternHasBook != null) { + boolean lecternCachedHasBook = session.getLecternCache().contains(position); + if (!session.getConnector().getWorldManager().shouldExpectLecternHandled() && lecternCachedHasBook != newLecternHasBook) { + // Refresh the block entirely - it either has a book or no longer has a book + NbtMap newLecternTag; + if (newLecternHasBook) { + newLecternTag = session.getConnector().getWorldManager().getLecternDataAt(session, position.getX(), position.getY(), position.getZ(), false); + } else { + session.getLecternCache().remove(position); + newLecternTag = LecternInventoryTranslator.getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 0).build(); + } + BlockEntityUtils.updateBlockEntity(session, newLecternTag, position); } else { - session.getLecternCache().remove(position); - newLecternTag = LecternInventoryTranslator.getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 0).build(); + // As of right now, no tag can be added asynchronously + session.getConnector().getWorldManager().getLecternDataAt(session, position.getX(), position.getY(), position.getZ(), false); } - BlockEntityUtils.updateBlockEntity(session, newLecternTag, position); } else { - // As of right now, no tag can be added asynchronously - session.getConnector().getWorldManager().getLecternDataAt(session, position.getX(), position.getY(), position.getZ(), false); + // Lectern has been destroyed, if it existed + session.getLecternCache().remove(position); } - } else { - // Lectern has been destroyed, if it existed - session.getLecternCache().remove(position); - } + return newLecternHasBook; + }); // Since Java stores bed colors/skull information as part of the namespaced ID and Bedrock stores it as a tag // This is the only place I could find that interacts with the Java block state and block updates