| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.SectionPos;
|
| 7 | import net.minecraft.world.level.ChunkPos;
|
| 8 | import net.minecraft.world.level.block.Blocks;
|
| 9 | import net.minecraft.world.level.block.state.BlockState;
|
| 10 | import net.minecraft.world.level.chunk.LightChunk;
|
| 11 | import net.minecraft.world.level.chunk.LightChunkGetter;
|
| 12 |
|
| 13 | public final class BlockLightEngine extends LightEngine<BlockLightSectionStorage.BlockDataLayerStorageMap, BlockLightSectionStorage> {
|
| 14 | private final BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
|
| 15 |
|
| 16 | public BlockLightEngine(final LightChunkGetter chunkSource) {
|
| 17 | this(chunkSource, new BlockLightSectionStorage(chunkSource));
|
| 18 | }
|
| 19 |
|
| 20 | @VisibleForTesting
|
| 21 | public BlockLightEngine(final LightChunkGetter chunkSource, final BlockLightSectionStorage storage) {
|
| 22 | super(chunkSource, storage);
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | protected void checkNode(final long blockNode) {
|
| 27 | long sectionNode = SectionPos.blockToSection(blockNode);
|
| 28 | if (this.storage.storingLightForSection(sectionNode)) {
|
| 29 | BlockState state = this.getState(this.mutablePos.set(blockNode));
|
| 30 | int lightEmission = this.getEmission(blockNode, state);
|
| 31 | int oldLevel = this.storage.getStoredLevel(blockNode);
|
| 32 | if (lightEmission < oldLevel) {
|
| 33 | this.storage.setStoredLevel(blockNode, 0);
|
| 34 | this.enqueueDecrease(blockNode, LightEngine.QueueEntry.decreaseAllDirections(oldLevel));
|
| 35 | } else {
|
| 36 | this.enqueueDecrease(blockNode, PULL_LIGHT_IN_ENTRY);
|
| 37 | }
|
| 38 |
|
| 39 | if (lightEmission > 0) {
|
| 40 | this.enqueueIncrease(blockNode, LightEngine.QueueEntry.increaseLightFromEmission(lightEmission, isEmptyShape(state)));
|
| 41 | }
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected void propagateIncrease(final long fromNode, final long increaseData, final int fromLevel) {
|
| 47 | BlockState fromState = null;
|
| 48 |
|
| 49 | for (Direction propagationDirection : PROPAGATION_DIRECTIONS) {
|
| 50 | if (LightEngine.QueueEntry.shouldPropagateInDirection(increaseData, propagationDirection)) {
|
| 51 | long toNode = BlockPos.offset(fromNode, propagationDirection);
|
| 52 | if (this.storage.storingLightForSection(SectionPos.blockToSection(toNode))) {
|
| 53 | int toLevel = this.storage.getStoredLevel(toNode);
|
| 54 | int maxPossibleNewToLevel = fromLevel - 1;
|
| 55 | if (maxPossibleNewToLevel > toLevel) {
|
| 56 | this.mutablePos.set(toNode);
|
| 57 | BlockState toState = this.getState(this.mutablePos);
|
| 58 | int newToLevel = fromLevel - this.getOpacity(toState);
|
| 59 | if (newToLevel > toLevel) {
|
| 60 | if (fromState == null) {
|
| 61 | fromState = LightEngine.QueueEntry.isFromEmptyShape(increaseData) ? Blocks.AIR.defaultBlockState() : this.getState(this.mutablePos.set(fromNode));
|
| 62 | }
|
| 63 |
|
| 64 | if (!this.shapeOccludes(fromState, toState, propagationDirection)) {
|
| 65 | this.storage.setStoredLevel(toNode, newToLevel);
|
| 66 | if (newToLevel > 1) {
|
| 67 | this.enqueueIncrease(toNode, LightEngine.QueueEntry.increaseSkipOneDirection(newToLevel, isEmptyShape(toState), propagationDirection.getOpposite()));
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | protected void propagateDecrease(final long fromNode, final long decreaseData) {
|
| 79 | int oldFromLevel = LightEngine.QueueEntry.getFromLevel(decreaseData);
|
| 80 |
|
| 81 | for (Direction propagationDirection : PROPAGATION_DIRECTIONS) {
|
| 82 | if (LightEngine.QueueEntry.shouldPropagateInDirection(decreaseData, propagationDirection)) {
|
| 83 | long toNode = BlockPos.offset(fromNode, propagationDirection);
|
| 84 | if (this.storage.storingLightForSection(SectionPos.blockToSection(toNode))) {
|
| 85 | int toLevel = this.storage.getStoredLevel(toNode);
|
| 86 | if (toLevel != 0) {
|
| 87 | if (toLevel <= oldFromLevel - 1) {
|
| 88 | BlockState toState = this.getState(this.mutablePos.set(toNode));
|
| 89 | int toEmission = this.getEmission(toNode, toState);
|
| 90 | this.storage.setStoredLevel(toNode, 0);
|
| 91 | if (toEmission < toLevel) {
|
| 92 | this.enqueueDecrease(toNode, LightEngine.QueueEntry.decreaseSkipOneDirection(toLevel, propagationDirection.getOpposite()));
|
| 93 | }
|
| 94 |
|
| 95 | if (toEmission > 0) {
|
| 96 | this.enqueueIncrease(toNode, LightEngine.QueueEntry.increaseLightFromEmission(toEmission, isEmptyShape(toState)));
|
| 97 | }
|
| 98 | } else {
|
| 99 | this.enqueueIncrease(toNode, LightEngine.QueueEntry.increaseOnlyOneDirection(toLevel, false, propagationDirection.getOpposite()));
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | private int getEmission(final long blockNode, final BlockState state) {
|
| 108 | int emission = state.getLightEmission();
|
| 109 | return emission > 0 && this.storage.lightOnInSection(SectionPos.blockToSection(blockNode)) ? emission : 0;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void propagateLightSources(final ChunkPos pos) {
|
| 114 | this.setLightEnabled(pos, true);
|
| 115 | LightChunk chunk = this.chunkSource.getChunkForLighting(pos.x(), pos.z());
|
| 116 | if (chunk != null) {
|
| 117 | chunk.findBlockLightSources((lightPos, state) -> {
|
| 118 | int lightEmission = state.getLightEmission();
|
| 119 | this.enqueueIncrease(lightPos.asLong(), LightEngine.QueueEntry.increaseLightFromEmission(lightEmission, isEmptyShape(state)));
|
| 120 | });
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 |
|