| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.SectionPos;
|
| 8 | import net.minecraft.world.level.LightLayer;
|
| 9 | import net.minecraft.world.level.chunk.DataLayer;
|
| 10 | import net.minecraft.world.level.chunk.LightChunkGetter;
|
| 11 |
|
| 12 | public class SkyLightSectionStorage extends LayerLightSectionStorage<SkyLightSectionStorage.SkyDataLayerStorageMap> {
|
| 13 | protected SkyLightSectionStorage(final LightChunkGetter chunkSource) {
|
| 14 | super(
|
| 15 | LightLayer.SKY, chunkSource, new SkyLightSectionStorage.SkyDataLayerStorageMap(new Long2ObjectOpenHashMap<>(), new Long2IntOpenHashMap(), Integer.MAX_VALUE)
|
| 16 | );
|
| 17 | }
|
| 18 |
|
| 19 | @Override
|
| 20 | protected int getLightValue(final long blockNode) {
|
| 21 | return this.getLightValue(blockNode, false);
|
| 22 | }
|
| 23 |
|
| 24 | protected int getLightValue(long blockNode, final boolean updating) {
|
| 25 | long sectionNode = SectionPos.blockToSection(blockNode);
|
| 26 | int sectionY = SectionPos.y(sectionNode);
|
| 27 | SkyLightSectionStorage.SkyDataLayerStorageMap sections = updating ? this.updatingSectionData : this.visibleSectionData;
|
| 28 | int topSection = sections.topSections.get(SectionPos.getZeroNode(sectionNode));
|
| 29 | if (topSection != sections.currentLowestY && sectionY < topSection) {
|
| 30 | DataLayer layer = this.getDataLayer(sections, sectionNode);
|
| 31 | if (layer == null) {
|
| 32 | blockNode = BlockPos.getFlatIndex(blockNode);
|
| 33 |
|
| 34 | while (layer == null) {
|
| 35 | if (++sectionY >= topSection) {
|
| 36 | return 15;
|
| 37 | }
|
| 38 |
|
| 39 | sectionNode = SectionPos.offset(sectionNode, Direction.UP);
|
| 40 | layer = this.getDataLayer(sections, sectionNode);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return layer.get(
|
| 45 | SectionPos.sectionRelative(BlockPos.getX(blockNode)),
|
| 46 | SectionPos.sectionRelative(BlockPos.getY(blockNode)),
|
| 47 | SectionPos.sectionRelative(BlockPos.getZ(blockNode))
|
| 48 | );
|
| 49 | } else {
|
| 50 | return updating && !this.lightOnInSection(sectionNode) ? 0 : 15;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected void onNodeAdded(final long sectionNode) {
|
| 56 | int y = SectionPos.y(sectionNode);
|
| 57 | if (this.updatingSectionData.currentLowestY > y) {
|
| 58 | this.updatingSectionData.currentLowestY = y;
|
| 59 | this.updatingSectionData.topSections.defaultReturnValue(this.updatingSectionData.currentLowestY);
|
| 60 | }
|
| 61 |
|
| 62 | long zeroNode = SectionPos.getZeroNode(sectionNode);
|
| 63 | int oldTop = this.updatingSectionData.topSections.get(zeroNode);
|
| 64 | if (oldTop < y + 1) {
|
| 65 | this.updatingSectionData.topSections.put(zeroNode, y + 1);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected void onNodeRemoved(final long sectionNode) {
|
| 71 | long zeroNode = SectionPos.getZeroNode(sectionNode);
|
| 72 | int y = SectionPos.y(sectionNode);
|
| 73 | if (this.updatingSectionData.topSections.get(zeroNode) == y + 1) {
|
| 74 | long newTopSection;
|
| 75 | for (newTopSection = sectionNode;
|
| 76 | !this.storingLightForSection(newTopSection) && this.hasLightDataAtOrBelow(y);
|
| 77 | newTopSection = SectionPos.offset(newTopSection, Direction.DOWN)
|
| 78 | ) {
|
| 79 | y--;
|
| 80 | }
|
| 81 |
|
| 82 | if (this.storingLightForSection(newTopSection)) {
|
| 83 | this.updatingSectionData.topSections.put(zeroNode, y + 1);
|
| 84 | } else {
|
| 85 | this.updatingSectionData.topSections.remove(zeroNode);
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | protected DataLayer createDataLayer(final long sectionNode) {
|
| 92 | DataLayer queuedLayer = this.queuedSections.get(sectionNode);
|
| 93 | if (queuedLayer != null) {
|
| 94 | return queuedLayer;
|
| 95 | }
|
| 96 |
|
| 97 | int topSection = this.updatingSectionData.topSections.get(SectionPos.getZeroNode(sectionNode));
|
| 98 | if (topSection != this.updatingSectionData.currentLowestY && SectionPos.y(sectionNode) < topSection) {
|
| 99 | long aboveSection = SectionPos.offset(sectionNode, Direction.UP);
|
| 100 |
|
| 101 | DataLayer aboveData;
|
| 102 | while ((aboveData = this.getDataLayer(aboveSection, true)) == null) {
|
| 103 | aboveSection = SectionPos.offset(aboveSection, Direction.UP);
|
| 104 | }
|
| 105 |
|
| 106 | return repeatFirstLayer(aboveData);
|
| 107 | } else {
|
| 108 | return this.lightOnInSection(sectionNode) ? new DataLayer(15) : new DataLayer();
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private static DataLayer repeatFirstLayer(final DataLayer data) {
|
| 113 | if (data.isDefinitelyHomogenous()) {
|
| 114 | return data.copy();
|
| 115 | }
|
| 116 |
|
| 117 | byte[] input = data.getData();
|
| 118 | byte[] output = new byte[2048];
|
| 119 |
|
| 120 | for (int i = 0; i < 16; i++) {
|
| 121 | System.arraycopy(input, 0, output, i * 128, 128);
|
| 122 | }
|
| 123 |
|
| 124 | return new DataLayer(output);
|
| 125 | }
|
| 126 |
|
| 127 | protected boolean hasLightDataAtOrBelow(final int sectionY) {
|
| 128 | return sectionY >= this.updatingSectionData.currentLowestY;
|
| 129 | }
|
| 130 |
|
| 131 | protected boolean isAboveData(final long sectionNode) {
|
| 132 | long zeroNode = SectionPos.getZeroNode(sectionNode);
|
| 133 | int topSection = this.updatingSectionData.topSections.get(zeroNode);
|
| 134 | return topSection == this.updatingSectionData.currentLowestY || SectionPos.y(sectionNode) >= topSection;
|
| 135 | }
|
| 136 |
|
| 137 | protected int getTopSectionY(final long zeroNode) {
|
| 138 | return this.updatingSectionData.topSections.get(zeroNode);
|
| 139 | }
|
| 140 |
|
| 141 | protected int getBottomSectionY() {
|
| 142 | return this.updatingSectionData.currentLowestY;
|
| 143 | }
|
| 144 |
|
| 145 | protected static final class SkyDataLayerStorageMap extends DataLayerStorageMap<SkyLightSectionStorage.SkyDataLayerStorageMap> {
|
| 146 | private int currentLowestY;
|
| 147 | private final Long2IntOpenHashMap topSections;
|
| 148 |
|
| 149 | public SkyDataLayerStorageMap(final Long2ObjectOpenHashMap<DataLayer> map, final Long2IntOpenHashMap topSections, final int currentLowestY) {
|
| 150 | super(map);
|
| 151 | this.topSections = topSections;
|
| 152 | topSections.defaultReturnValue(currentLowestY);
|
| 153 | this.currentLowestY = currentLowestY;
|
| 154 | }
|
| 155 |
|
| 156 | public SkyLightSectionStorage.SkyDataLayerStorageMap copy() {
|
| 157 | return new SkyLightSectionStorage.SkyDataLayerStorageMap(this.map.clone(), this.topSections.clone(), this.currentLowestY);
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 |
|