Make sure block state is never null, remove now unnecessary null checks

This commit is contained in:
Eclipse 2024-06-20 08:12:58 +00:00
parent 877c941e1f
commit a433c341fc
No known key found for this signature in database
GPG key ID: 441A0B7FDD01D03A
9 changed files with 22 additions and 25 deletions

View file

@ -141,7 +141,7 @@ public class SkullPlayerEntity extends PlayerEntity {
float rotation;
BlockState blockState = skull.getBlockState();
if (blockState != null && blockState.block() instanceof WallSkullBlock) {
if (blockState.block() instanceof WallSkullBlock) {
y += 0.25f;
Direction direction = blockState.getValue(Properties.HORIZONTAL_FACING);
rotation = WallSkullBlock.getDegrees(direction);
@ -152,7 +152,7 @@ public class SkullPlayerEntity extends PlayerEntity {
case EAST -> x -= 0.24f;
}
} else {
rotation = (180f + (blockState != null ? blockState.getValue(Properties.ROTATION_16) * 22.5f : 0.0f)) % 360;
rotation = (180f + blockState.getValue(Properties.ROTATION_16, 0) * 22.5f) % 360;
}
moveAbsolute(Vector3f.from(x, y, z), rotation, 0, rotation, true, true);

View file

@ -33,7 +33,6 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.erosion.util.BlockPositionIterator;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
@ -65,11 +64,7 @@ public abstract class WorldManager {
@NonNull
public BlockState blockAt(GeyserSession session, int x, int y, int z) {
BlockState block = BlockState.of(this.getBlockAt(session, x, y, z));
if (block == null) {
return BlockState.of(Block.JAVA_AIR_ID);
}
return block;
return BlockState.of(this.getBlockAt(session, x, y, z));
}
/**

View file

@ -25,6 +25,7 @@
package org.geysermc.geyser.level.block.type;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.level.block.property.Property;
import org.geysermc.geyser.registry.BlockRegistries;
@ -192,7 +193,14 @@ public final class BlockState {
return builder.toString();
}
/**
* Null-safe method that looks up a Java block state ID in the BLOCK_STATES registry, and defaults to air if not found.
*
* @param javaId the Java block state ID to look up.
* @return the corresponding block state, or air if the given ID wasn't registered and returned null.
*/
@NonNull
public static BlockState of(int javaId) {
return BlockRegistries.BLOCK_STATES.get(javaId);
return BlockRegistries.BLOCK_STATES.getOrDefault(javaId, BlockRegistries.BLOCK_STATES.get(Block.JAVA_AIR_ID));
}
}

View file

@ -254,10 +254,10 @@ public class SkullCache {
CustomSkull customSkull = BlockRegistries.CUSTOM_SKULLS.get(skinHash);
if (customSkull != null) {
CustomBlockState customBlockState;
if (blockState != null && blockState.block() instanceof WallSkullBlock) {
if (blockState.block() instanceof WallSkullBlock) {
customBlockState = customSkull.getWallBlockState(WallSkullBlock.getDegrees(blockState));
} else {
customBlockState = customSkull.getFloorBlockState(blockState != null ? blockState.getValue(Properties.ROTATION_16) : 0);
customBlockState = customSkull.getFloorBlockState(blockState.getValue(Properties.ROTATION_16));
}
return session.getBlockMappings().getCustomBlockStateDefinitions().get(customBlockState);

View file

@ -53,17 +53,13 @@ public class SkullBlockEntityTranslator extends BlockEntityTranslator implements
@Override
public void translateTag(GeyserSession session, NbtMapBuilder bedrockNbt, NbtMap javaNbt, BlockState blockState) {
if (blockState == null) {
bedrockNbt.putByte("SkullType", (byte) 0);
return;
}
Integer rotation = blockState.getValue(Properties.ROTATION_16);
if (rotation != null) {
// Could be a wall skull block otherwise, which has rotation in its Bedrock state
bedrockNbt.putFloat("Rotation", rotation * 22.5f);
}
bedrockNbt.putByte("SkullType", (byte) (blockState.block() instanceof SkullBlock skull ? skull.skullType().bedrockId() : 0));
if (blockState.getValue(Properties.POWERED)) {
if (blockState.getValue(Properties.POWERED, false)) {
bedrockNbt.putBoolean("MouthMoving", true);
}
}

View file

@ -160,7 +160,7 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
LevelEventPacket startBreak = new LevelEventPacket();
startBreak.setType(LevelEvent.BLOCK_START_BREAK);
startBreak.setPosition(vector.toFloat());
double breakTime = BlockUtils.getSessionBreakTime(session, BlockRegistries.BLOCK_STATES.getOrDefault(blockState, BlockState.of(Block.JAVA_AIR_ID)).block()) * 20;
double breakTime = BlockUtils.getSessionBreakTime(session, BlockState.of(blockState).block()) * 20;
// If the block is custom or the breaking item is custom, we must keep track of break time ourselves
GeyserItemStack item = session.getPlayerInventory().getItemInHand();
@ -211,7 +211,7 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
LevelEventPacket updateBreak = new LevelEventPacket();
updateBreak.setType(LevelEvent.BLOCK_UPDATE_BREAK);
updateBreak.setPosition(vectorFloat);
double breakTime = BlockUtils.getSessionBreakTime(session, BlockRegistries.BLOCK_STATES.getOrDefault(breakingBlock, BlockState.of(Block.JAVA_AIR_ID)).block()) * 20;
double breakTime = BlockUtils.getSessionBreakTime(session, BlockState.of(breakingBlock).block()) * 20;
// If the block is custom, we must keep track of when it should break ourselves

View file

@ -27,9 +27,7 @@ package org.geysermc.geyser.translator.protocol.java.level;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventPacket;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
@ -43,7 +41,7 @@ public class JavaBlockDestructionTranslator extends PacketTranslator<Clientbound
@Override
public void translate(GeyserSession session, ClientboundBlockDestructionPacket packet) {
int state = session.getGeyser().getWorldManager().getBlockAt(session, packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ());
int breakTime = (int) (65535 / Math.ceil(BlockUtils.getBreakTime(session, BlockRegistries.BLOCK_STATES.getOrDefault(state, BlockState.of(Block.JAVA_AIR_ID)).block(), ItemMapping.AIR, null, false) * 20));
int breakTime = (int) (65535 / Math.ceil(BlockUtils.getBreakTime(session, BlockState.of(state).block(), ItemMapping.AIR, null, false) * 20));
LevelEventPacket levelEventPacket = new LevelEventPacket();
levelEventPacket.setPosition(packet.getPosition().toFloat());
levelEventPacket.setType(LevelEvent.BLOCK_START_BREAK);

View file

@ -33,8 +33,8 @@ import org.cloudburstmc.protocol.bedrock.data.structure.StructureMirror;
import org.cloudburstmc.protocol.bedrock.data.structure.StructureRotation;
import org.cloudburstmc.protocol.bedrock.packet.ContainerOpenPacket;
import org.cloudburstmc.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.level.block.type.BlockState;
import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.level.block.entity.BlockEntityTranslator;
import org.geysermc.geyser.translator.level.block.entity.RequiresBlockState;
@ -61,9 +61,9 @@ public class JavaBlockEntityDataTranslator extends PacketTranslator<ClientboundB
// between Java block states and Bedrock block entity data
BlockState blockState;
if (translator instanceof RequiresBlockState) {
blockState = BlockRegistries.BLOCK_STATES.get(session.getGeyser().getWorldManager().getBlockAt(session, packet.getPosition()));
blockState = session.getGeyser().getWorldManager().blockAt(session, packet.getPosition());
} else {
blockState = BlockRegistries.BLOCK_STATES.get(0); //TODO
blockState = BlockState.of(Block.JAVA_AIR_ID); //TODO
}
Vector3i position = packet.getPosition();

View file

@ -398,7 +398,7 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
// Get the Java block state ID from block entity position
DataPalette section = javaChunks[(y >> 4) - yOffset];
BlockState blockState = BlockRegistries.BLOCK_STATES.get(section.get(x, y & 0xF, z));
BlockState blockState = BlockState.of(section.get(x, y & 0xF, z));
// Note that, since 1.20.5, tags can be null, but Bedrock still needs a default tag to render the item
// Also, some properties - like banner base colors - are part of the tag and is processed here.