Skip to content

Commit

Permalink
Optimize Spectator container block scan
Browse files Browse the repository at this point in the history
Fixes #4600. Blindly trying to get a non-existing block entity also is a bit more expensive than getting a block state that would know if there's supposed to be any block entity in the first place.
  • Loading branch information
TheRealWormbo committed Jul 13, 2024
1 parent f4d7e4d commit 30dd003
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.trading.Merchant;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
import net.minecraft.world.phys.AABB;
Expand Down Expand Up @@ -204,7 +205,11 @@ private long[] scanBlockContainers(Player player, ItemStack mainHandStack, ItemS
}

private boolean scanBlock(Player player, BlockPos pos, ItemStack mainHandStack, ItemStack offHandStack) {
BlockEntity blockEntity = player.level().getBlockEntity(pos);
Level level = player.level();
if (!level.isLoaded(pos) || !level.getBlockState(pos).hasBlockEntity()) {
return false;
}
BlockEntity blockEntity = level.getBlockEntity(pos);
return blockEntity instanceof Container inv && (!(blockEntity instanceof RandomizableContainerBlockEntity lootInv)
|| ((RandomizableContainerBlockEntityAccessor) lootInv).getLootTable() == null)
&& scanInventory(inv, mainHandStack, offHandStack);
Expand Down

0 comments on commit 30dd003

Please sign in to comment.