Use compute for determining if a lectern if present

Reduces having to call both containsKey and get.
This commit is contained in:
Camotoy 2021-03-10 13:54:09 -05:00
parent ded00dfd97
commit 370f3c18ba
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 28 additions and 18 deletions

View file

@ -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<Integer, Boolean> getLecternBookStates() {
return LECTERN_BOOK_STATES;
}

View file

@ -372,9 +372,10 @@ public class ChunkUtils {
}
session.sendUpstreamPacket(waterPacket);
if (BlockStateValues.getLecternBookStates().containsKey(blockState)) {
BlockStateValues.getLecternBookStates().compute(blockState, (key, newLecternHasBook) -> {
// Determine if this block is a lectern
if (newLecternHasBook != null) {
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;
@ -393,6 +394,8 @@ public class ChunkUtils {
// 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