Skip to content

Commit

Permalink
reduce memory of loaded lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4722202468 committed May 10, 2024
1 parent 6b8a4e4 commit 667b901
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
18 changes: 14 additions & 4 deletions src/main/java/net/minestom/server/instance/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import net.minestom.server.network.NetworkBuffer;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

import static net.minestom.server.instance.light.LightCompute.contentFullyLit;
import static net.minestom.server.instance.light.LightCompute.emptyContent;
import static net.minestom.server.network.NetworkBuffer.SHORT;

public final class Section implements NetworkBuffer.Writer {
Expand Down Expand Up @@ -49,8 +53,8 @@ public void clear() {
final Light skyLight = Light.sky(blockPalette);
final Light blockLight = Light.block(blockPalette);

skyLight.set(this.skyLight.array());
blockLight.set(this.blockLight.array());
setBlockLight(this.skyLight.array());
setSkyLight(this.blockLight.array());

return new Section(this.blockPalette.clone(), this.biomePalette.clone(), skyLight, blockLight);
}
Expand All @@ -63,11 +67,17 @@ public void write(@NotNull NetworkBuffer writer) {
}

public void setSkyLight(byte[] copyArray) {
this.skyLight.set(copyArray);
if (copyArray == null || copyArray.length == 0) this.skyLight.set(emptyContent);
else if (Arrays.equals(copyArray, emptyContent)) this.skyLight.set(emptyContent);
else if (Arrays.equals(copyArray, contentFullyLit)) this.skyLight.set(contentFullyLit);
else this.skyLight.set(copyArray);
}

public void setBlockLight(byte[] copyArray) {
this.blockLight.set(copyArray);
if (copyArray == null || copyArray.length == 0) this.blockLight.set(emptyContent);
else if (Arrays.equals(copyArray, emptyContent)) this.blockLight.set(emptyContent);
else if (Arrays.equals(copyArray, contentFullyLit)) this.blockLight.set(contentFullyLit);
else this.blockLight.set(copyArray);
}

public Light skyLight() {
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/net/minestom/server/instance/light/BlockLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,12 @@ public boolean requiresUpdate() {
}

@Override
@ApiStatus.Internal
public void set(byte[] copyArray) {
if (copyArray.length == 0) {
this.content = emptyContent;
this.contentPropagation = emptyContent;
} else {
this.content = copyArray.clone();
this.contentPropagation = this.content;
}
this.content = copyArray.clone();
this.contentPropagation = this.content;
this.isValidBorders = true;
this.needsSend = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minestom.server.utils.Direction;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Objects;

import static net.minestom.server.instance.light.BlockLight.buildInternalQueue;
Expand All @@ -17,6 +18,11 @@ public final class LightCompute {
static final int SECTION_SIZE = 16;

public static final byte[] emptyContent = new byte[LIGHT_LENGTH];
public static final byte[] contentFullyLit = new byte[LIGHT_LENGTH];

static {
Arrays.fill(contentFullyLit, (byte) -1);
}

static @NotNull Result compute(Palette blockPalette) {
return LightCompute.compute(blockPalette, buildInternalQueue(blockPalette));
Expand Down
20 changes: 6 additions & 14 deletions src/main/java/net/minestom/server/instance/light/SkyLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.palette.Palette;
import org.jetbrains.annotations.ApiStatus;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -29,13 +29,7 @@ final class SkyLight implements Light {

private Set<Point> toUpdateSet = new HashSet<>();
private final Section[] neighborSections = new Section[BlockFace.values().length];

private boolean fullyLit = false;
private static final byte[] contentFullyLit = new byte[LIGHT_LENGTH];

static {
Arrays.fill(contentFullyLit, (byte) -1);
}

SkyLight(Palette blockPalette) {
this.blockPalette = blockPalette;
Expand Down Expand Up @@ -220,14 +214,12 @@ public boolean requiresUpdate() {
}

@Override
@ApiStatus.Internal
public void set(byte[] copyArray) {
if (copyArray.length == 0) {
this.content = emptyContent;
this.contentPropagation = emptyContent;
} else {
this.content = copyArray.clone();
this.contentPropagation = this.content;
}
this.content = copyArray.clone();
this.contentPropagation = this.content;
this.isValidBorders = true;
this.needsSend = true;
}

@Override
Expand Down

0 comments on commit 667b901

Please sign in to comment.