| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.SectionPos;
|
| 5 | import net.minecraft.world.level.ChunkPos;
|
| 6 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 7 | import net.minecraft.world.level.LightLayer;
|
| 8 | import net.minecraft.world.level.chunk.DataLayer;
|
| 9 | import net.minecraft.world.level.chunk.LightChunkGetter;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class LevelLightEngine implements LightEventListener {
|
| 13 | public static final int LIGHT_SECTION_PADDING = 1;
|
| 14 | public static final LevelLightEngine EMPTY = new LevelLightEngine();
|
| 15 | protected final LevelHeightAccessor levelHeightAccessor;
|
| 16 | private final @Nullable LightEngine<?, ?> blockEngine;
|
| 17 | private final @Nullable LightEngine<?, ?> skyEngine;
|
| 18 |
|
| 19 | public LevelLightEngine(final LightChunkGetter chunkSource, final boolean hasBlockLight, final boolean hasSkyLight) {
|
| 20 | this.levelHeightAccessor = chunkSource.getLevel();
|
| 21 | this.blockEngine = hasBlockLight ? new BlockLightEngine(chunkSource) : null;
|
| 22 | this.skyEngine = hasSkyLight ? new SkyLightEngine(chunkSource) : null;
|
| 23 | }
|
| 24 |
|
| 25 | private LevelLightEngine() {
|
| 26 | this.levelHeightAccessor = LevelHeightAccessor.create(0, 0);
|
| 27 | this.blockEngine = null;
|
| 28 | this.skyEngine = null;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public void checkBlock(final BlockPos pos) {
|
| 33 | if (this.blockEngine != null) {
|
| 34 | this.blockEngine.checkBlock(pos);
|
| 35 | }
|
| 36 |
|
| 37 | if (this.skyEngine != null) {
|
| 38 | this.skyEngine.checkBlock(pos);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public boolean hasLightWork() {
|
| 44 | return this.skyEngine != null && this.skyEngine.hasLightWork() ? true : this.blockEngine != null && this.blockEngine.hasLightWork();
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public int runLightUpdates() {
|
| 49 | int count = 0;
|
| 50 | if (this.blockEngine != null) {
|
| 51 | count += this.blockEngine.runLightUpdates();
|
| 52 | }
|
| 53 |
|
| 54 | if (this.skyEngine != null) {
|
| 55 | count += this.skyEngine.runLightUpdates();
|
| 56 | }
|
| 57 |
|
| 58 | return count;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void updateSectionStatus(final SectionPos pos, final boolean sectionEmpty) {
|
| 63 | if (this.blockEngine != null) {
|
| 64 | this.blockEngine.updateSectionStatus(pos, sectionEmpty);
|
| 65 | }
|
| 66 |
|
| 67 | if (this.skyEngine != null) {
|
| 68 | this.skyEngine.updateSectionStatus(pos, sectionEmpty);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void setLightEnabled(final ChunkPos pos, final boolean enable) {
|
| 74 | if (this.blockEngine != null) {
|
| 75 | this.blockEngine.setLightEnabled(pos, enable);
|
| 76 | }
|
| 77 |
|
| 78 | if (this.skyEngine != null) {
|
| 79 | this.skyEngine.setLightEnabled(pos, enable);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void propagateLightSources(final ChunkPos pos) {
|
| 85 | if (this.blockEngine != null) {
|
| 86 | this.blockEngine.propagateLightSources(pos);
|
| 87 | }
|
| 88 |
|
| 89 | if (this.skyEngine != null) {
|
| 90 | this.skyEngine.propagateLightSources(pos);
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | public LayerLightEventListener getLayerListener(final LightLayer layer) {
|
| 95 | if (layer == LightLayer.BLOCK) {
|
| 96 | return this.blockEngine == null ? LayerLightEventListener.DummyLightLayerEventListener.INSTANCE : this.blockEngine;
|
| 97 | } else {
|
| 98 | return this.skyEngine == null ? LayerLightEventListener.DummyLightLayerEventListener.INSTANCE : this.skyEngine;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | public String getDebugData(final LightLayer layer, final SectionPos pos) {
|
| 103 | if (layer == LightLayer.BLOCK) {
|
| 104 | if (this.blockEngine != null) {
|
| 105 | return this.blockEngine.getDebugData(pos.asLong());
|
| 106 | }
|
| 107 | } else if (this.skyEngine != null) {
|
| 108 | return this.skyEngine.getDebugData(pos.asLong());
|
| 109 | }
|
| 110 |
|
| 111 | return "n/a";
|
| 112 | }
|
| 113 |
|
| 114 | public LayerLightSectionStorage.SectionType getDebugSectionType(final LightLayer layer, final SectionPos pos) {
|
| 115 | if (layer == LightLayer.BLOCK) {
|
| 116 | if (this.blockEngine != null) {
|
| 117 | return this.blockEngine.getDebugSectionType(pos.asLong());
|
| 118 | }
|
| 119 | } else if (this.skyEngine != null) {
|
| 120 | return this.skyEngine.getDebugSectionType(pos.asLong());
|
| 121 | }
|
| 122 |
|
| 123 | return LayerLightSectionStorage.SectionType.EMPTY;
|
| 124 | }
|
| 125 |
|
| 126 | public void queueSectionData(final LightLayer layer, final SectionPos pos, final @Nullable DataLayer data) {
|
| 127 | if (layer == LightLayer.BLOCK) {
|
| 128 | if (this.blockEngine != null) {
|
| 129 | this.blockEngine.queueSectionData(pos.asLong(), data);
|
| 130 | }
|
| 131 | } else if (this.skyEngine != null) {
|
| 132 | this.skyEngine.queueSectionData(pos.asLong(), data);
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | public void retainData(final ChunkPos pos, final boolean retain) {
|
| 137 | if (this.blockEngine != null) {
|
| 138 | this.blockEngine.retainData(pos, retain);
|
| 139 | }
|
| 140 |
|
| 141 | if (this.skyEngine != null) {
|
| 142 | this.skyEngine.retainData(pos, retain);
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | public int getRawBrightness(final BlockPos pos, final int skyDampen) {
|
| 147 | int skyLight = this.skyEngine == null ? 0 : this.skyEngine.getLightValue(pos) - skyDampen;
|
| 148 | int blockLight = this.blockEngine == null ? 0 : this.blockEngine.getLightValue(pos);
|
| 149 | return Math.max(blockLight, skyLight);
|
| 150 | }
|
| 151 |
|
| 152 | public boolean lightOnInColumn(final long sectionZeroNode) {
|
| 153 | return this.blockEngine == null
|
| 154 | || this.blockEngine.storage.lightOnInColumn(sectionZeroNode) && (this.skyEngine == null || this.skyEngine.storage.lightOnInColumn(sectionZeroNode));
|
| 155 | }
|
| 156 |
|
| 157 | public int getLightSectionCount() {
|
| 158 | return this.levelHeightAccessor.getSectionsCount() + 2;
|
| 159 | }
|
| 160 |
|
| 161 | public int getMinLightSection() {
|
| 162 | return this.levelHeightAccessor.getMinSectionY() - 1;
|
| 163 | }
|
| 164 |
|
| 165 | public int getMaxLightSection() {
|
| 166 | return this.getMinLightSection() + this.getLightSectionCount();
|
| 167 | }
|
| 168 | }
|
| 169 |
|