| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 4 | import net.minecraft.world.level.chunk.DataLayer;
|
| 5 | import org.jspecify.annotations.Nullable;
|
| 6 |
|
| 7 | public abstract class DataLayerStorageMap<M extends DataLayerStorageMap<M>> {
|
| 8 | private static final int CACHE_SIZE = 2;
|
| 9 | private final long[] lastSectionKeys = new long[2];
|
| 10 | private final @Nullable DataLayer[] lastSections = new DataLayer[2];
|
| 11 | private boolean cacheEnabled;
|
| 12 | protected final Long2ObjectOpenHashMap<DataLayer> map;
|
| 13 |
|
| 14 | protected DataLayerStorageMap(final Long2ObjectOpenHashMap<DataLayer> map) {
|
| 15 | this.map = map;
|
| 16 | this.clearCache();
|
| 17 | this.cacheEnabled = true;
|
| 18 | }
|
| 19 |
|
| 20 | public abstract M copy();
|
| 21 |
|
| 22 | public DataLayer copyDataLayer(final long sectionNode) {
|
| 23 | DataLayer newDataLayer = this.map.get(sectionNode).copy();
|
| 24 | this.map.put(sectionNode, newDataLayer);
|
| 25 | this.clearCache();
|
| 26 | return newDataLayer;
|
| 27 | }
|
| 28 |
|
| 29 | public boolean hasLayer(final long sectionNode) {
|
| 30 | return this.map.containsKey(sectionNode);
|
| 31 | }
|
| 32 |
|
| 33 | public @Nullable DataLayer getLayer(final long sectionNode) {
|
| 34 | if (this.cacheEnabled) {
|
| 35 | for (int i = 0; i < 2; i++) {
|
| 36 | if (sectionNode == this.lastSectionKeys[i]) {
|
| 37 | return this.lastSections[i];
|
| 38 | }
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | DataLayer data = this.map.get(sectionNode);
|
| 43 | if (data == null) {
|
| 44 | return null;
|
| 45 | }
|
| 46 |
|
| 47 | if (this.cacheEnabled) {
|
| 48 | for (int i = 1; i > 0; i--) {
|
| 49 | this.lastSectionKeys[i] = this.lastSectionKeys[i - 1];
|
| 50 | this.lastSections[i] = this.lastSections[i - 1];
|
| 51 | }
|
| 52 |
|
| 53 | this.lastSectionKeys[0] = sectionNode;
|
| 54 | this.lastSections[0] = data;
|
| 55 | }
|
| 56 |
|
| 57 | return data;
|
| 58 | }
|
| 59 |
|
| 60 | public @Nullable DataLayer removeLayer(final long sectionNode) {
|
| 61 | return this.map.remove(sectionNode);
|
| 62 | }
|
| 63 |
|
| 64 | public void setLayer(final long sectionNode, final DataLayer layer) {
|
| 65 | this.map.put(sectionNode, layer);
|
| 66 | }
|
| 67 |
|
| 68 | public void clearCache() {
|
| 69 | for (int i = 0; i < 2; i++) {
|
| 70 | this.lastSectionKeys[i] = Long.MAX_VALUE;
|
| 71 | this.lastSections[i] = null;
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public void disableCache() {
|
| 76 | this.cacheEnabled = false;
|
| 77 | }
|
| 78 | }
|
| 79 |
|