| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import java.util.function.Predicate;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.network.FriendlyByteBuf;
|
| 6 | import net.minecraft.world.level.biome.Biome;
|
| 7 | import net.minecraft.world.level.biome.BiomeResolver;
|
| 8 | import net.minecraft.world.level.block.state.BlockState;
|
| 9 | import net.minecraft.world.level.material.FluidState;
|
| 10 |
|
| 11 | public class LevelChunkSection {
|
| 12 | public static final int BIOME_CONTAINER_BITS = 2;
|
| 13 | private short nonEmptyBlockCount;
|
| 14 | private short fluidCount;
|
| 15 | private short tickingBlockCount;
|
| 16 | private short tickingFluidCount;
|
| 17 | private final PalettedContainer<BlockState> states;
|
| 18 | private PalettedContainerRO<Holder<Biome>> biomes;
|
| 19 |
|
| 20 | private LevelChunkSection(final LevelChunkSection source) {
|
| 21 | this.nonEmptyBlockCount = source.nonEmptyBlockCount;
|
| 22 | this.fluidCount = source.fluidCount;
|
| 23 | this.tickingBlockCount = source.tickingBlockCount;
|
| 24 | this.tickingFluidCount = source.tickingFluidCount;
|
| 25 | this.states = source.states.copy();
|
| 26 | this.biomes = source.biomes.copy();
|
| 27 | }
|
| 28 |
|
| 29 | public LevelChunkSection(final PalettedContainer<BlockState> states, final PalettedContainerRO<Holder<Biome>> biomes) {
|
| 30 | this.states = states;
|
| 31 | this.biomes = biomes;
|
| 32 | this.recalcBlockCounts();
|
| 33 | }
|
| 34 |
|
| 35 | public LevelChunkSection(final PalettedContainerFactory containerFactory) {
|
| 36 | this.states = containerFactory.createForBlockStates();
|
| 37 | this.biomes = containerFactory.createForBiomes();
|
| 38 | }
|
| 39 |
|
| 40 | public BlockState getBlockState(final int sectionX, final int sectionY, final int sectionZ) {
|
| 41 | return this.states.get(sectionX, sectionY, sectionZ);
|
| 42 | }
|
| 43 |
|
| 44 | public FluidState getFluidState(final int sectionX, final int sectionY, final int sectionZ) {
|
| 45 | return this.states.get(sectionX, sectionY, sectionZ).getFluidState();
|
| 46 | }
|
| 47 |
|
| 48 | public void acquire() {
|
| 49 | this.states.acquire();
|
| 50 | }
|
| 51 |
|
| 52 | public void release() {
|
| 53 | this.states.release();
|
| 54 | }
|
| 55 |
|
| 56 | public BlockState setBlockState(final int sectionX, final int sectionY, final int sectionZ, final BlockState state) {
|
| 57 | return this.setBlockState(sectionX, sectionY, sectionZ, state, true);
|
| 58 | }
|
| 59 |
|
| 60 | public BlockState setBlockState(final int sectionX, final int sectionY, final int sectionZ, final BlockState state, final boolean checkThreading) {
|
| 61 | BlockState previous;
|
| 62 | if (checkThreading) {
|
| 63 | previous = this.states.getAndSet(sectionX, sectionY, sectionZ, state);
|
| 64 | } else {
|
| 65 | previous = this.states.getAndSetUnchecked(sectionX, sectionY, sectionZ, state);
|
| 66 | }
|
| 67 |
|
| 68 | if (!previous.isAir()) {
|
| 69 | this.nonEmptyBlockCount--;
|
| 70 | if (previous.isRandomlyTicking()) {
|
| 71 | this.tickingBlockCount--;
|
| 72 | }
|
| 73 |
|
| 74 | FluidState previousFluid = previous.getFluidState();
|
| 75 | if (!previousFluid.isEmpty()) {
|
| 76 | this.fluidCount--;
|
| 77 | if (previousFluid.isRandomlyTicking()) {
|
| 78 | this.tickingFluidCount--;
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | if (!state.isAir()) {
|
| 84 | this.nonEmptyBlockCount++;
|
| 85 | if (state.isRandomlyTicking()) {
|
| 86 | this.tickingBlockCount++;
|
| 87 | }
|
| 88 |
|
| 89 | FluidState fluid = state.getFluidState();
|
| 90 | if (!fluid.isEmpty()) {
|
| 91 | this.fluidCount++;
|
| 92 | if (fluid.isRandomlyTicking()) {
|
| 93 | this.tickingFluidCount++;
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | return previous;
|
| 99 | }
|
| 100 |
|
| 101 | public boolean hasOnlyAir() {
|
| 102 | return this.nonEmptyBlockCount == 0;
|
| 103 | }
|
| 104 |
|
| 105 | public boolean hasFluid() {
|
| 106 | return this.fluidCount > 0;
|
| 107 | }
|
| 108 |
|
| 109 | public boolean isRandomlyTicking() {
|
| 110 | return this.isRandomlyTickingBlocks() || this.isRandomlyTickingFluids();
|
| 111 | }
|
| 112 |
|
| 113 | public boolean isRandomlyTickingBlocks() {
|
| 114 | return this.tickingBlockCount > 0;
|
| 115 | }
|
| 116 |
|
| 117 | public boolean isRandomlyTickingFluids() {
|
| 118 | return this.tickingFluidCount > 0;
|
| 119 | }
|
| 120 |
|
| 121 | public void recalcBlockCounts() {
|
| 122 | class BlockCounter implements PalettedContainer.CountConsumer<BlockState> {
|
| 123 | public int nonEmptyBlockCount;
|
| 124 | public int fluidCount;
|
| 125 | public int tickingBlockCount;
|
| 126 | public int tickingFluidCount;
|
| 127 |
|
| 128 | public void accept(final BlockState state, final int count) {
|
| 129 | if (!state.isAir()) {
|
| 130 | this.nonEmptyBlockCount += count;
|
| 131 | if (state.isRandomlyTicking()) {
|
| 132 | this.tickingBlockCount += count;
|
| 133 | }
|
| 134 |
|
| 135 | FluidState fluid = state.getFluidState();
|
| 136 | if (!fluid.isEmpty()) {
|
| 137 | this.fluidCount += count;
|
| 138 | if (fluid.isRandomlyTicking()) {
|
| 139 | this.tickingFluidCount += count;
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | BlockCounter blockCounter = new BlockCounter();
|
| 147 | this.states.count(blockCounter);
|
| 148 | this.nonEmptyBlockCount = (short)blockCounter.nonEmptyBlockCount;
|
| 149 | this.fluidCount = (short)blockCounter.fluidCount;
|
| 150 | this.tickingBlockCount = (short)blockCounter.tickingBlockCount;
|
| 151 | this.tickingFluidCount = (short)blockCounter.tickingFluidCount;
|
| 152 | }
|
| 153 |
|
| 154 | public PalettedContainer<BlockState> getStates() {
|
| 155 | return this.states;
|
| 156 | }
|
| 157 |
|
| 158 | public PalettedContainerRO<Holder<Biome>> getBiomes() {
|
| 159 | return this.biomes;
|
| 160 | }
|
| 161 |
|
| 162 | public void read(final FriendlyByteBuf buffer) {
|
| 163 | this.nonEmptyBlockCount = buffer.readShort();
|
| 164 | this.fluidCount = buffer.readShort();
|
| 165 | this.states.read(buffer);
|
| 166 | PalettedContainer<Holder<Biome>> biomes = this.biomes.recreate();
|
| 167 | biomes.read(buffer);
|
| 168 | this.biomes = biomes;
|
| 169 | }
|
| 170 |
|
| 171 | public void readBiomes(final FriendlyByteBuf buffer) {
|
| 172 | PalettedContainer<Holder<Biome>> biomes = this.biomes.recreate();
|
| 173 | biomes.read(buffer);
|
| 174 | this.biomes = biomes;
|
| 175 | }
|
| 176 |
|
| 177 | public void write(final FriendlyByteBuf buffer) {
|
| 178 | buffer.writeShort(this.nonEmptyBlockCount);
|
| 179 | buffer.writeShort(this.fluidCount);
|
| 180 | this.states.write(buffer);
|
| 181 | this.biomes.write(buffer);
|
| 182 | }
|
| 183 |
|
| 184 | public int getSerializedSize() {
|
| 185 | return 4 + this.states.getSerializedSize() + this.biomes.getSerializedSize();
|
| 186 | }
|
| 187 |
|
| 188 | public boolean maybeHas(final Predicate<BlockState> predicate) {
|
| 189 | return this.states.maybeHas(predicate);
|
| 190 | }
|
| 191 |
|
| 192 | public Holder<Biome> getNoiseBiome(final int quartX, final int quartY, final int quartZ) {
|
| 193 | return this.biomes.get(quartX, quartY, quartZ);
|
| 194 | }
|
| 195 |
|
| 196 | public void fillBiomesFromNoise(final BiomeResolver biomeResolver, final int quartMinX, final int quartMinY, final int quartMinZ) {
|
| 197 | PalettedContainer<Holder<Biome>> newBiomes = this.biomes.recreate();
|
| 198 | int size = 4;
|
| 199 |
|
| 200 | for (int x = 0; x < 4; x++) {
|
| 201 | for (int y = 0; y < 4; y++) {
|
| 202 | for (int z = 0; z < 4; z++) {
|
| 203 | newBiomes.getAndSetUnchecked(x, y, z, biomeResolver.getNoiseBiome(quartMinX + x, quartMinY + y, quartMinZ + z));
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | this.biomes = newBiomes;
|
| 209 | }
|
| 210 |
|
| 211 | public LevelChunkSection copy() {
|
| 212 | return new LevelChunkSection(this);
|
| 213 | }
|
| 214 | }
|
| 215 |
|