Skip to content

Commit

Permalink
chores: some improvements to WorldGenerator & dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 17, 2024
1 parent b65e368 commit 62f4d01
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static WorldGeneratorBuilder builder() {
return BUILDER_FACTORY.get().create();
}

Chunk generateFinishedChunkSynchronously(int x, int z);
Chunk generateChunk(int x, int z);

String getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ private void updatePluginTable() {
private void createUIComponents() {
// Init the three graph
ramGraph = new GraphPanel();
ramGraph.setVariableExtreme(false);
ramValues = new ArrayList<>();
// Set the ram graph to 0
for (int i = 0; i < RAM_VALUE_COUNT; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public final class GraphPanel extends JPanel {
private final static int MIN_SCORE_THRESHOLD = 0;
private int minScore = MIN_SCORE_THRESHOLD;
private int maxScore = MAX_SCORE_THRESHOLD;

@Setter
private boolean variableExtreme = true;
@Setter
private String xLabel = "";

Expand Down Expand Up @@ -194,6 +195,7 @@ private int getMaxScore() {
}

private void calculateExtremum() {
if (!variableExtreme) return;
minScore = Integer.MAX_VALUE;
maxScore = Integer.MIN_VALUE;
for (int value : values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class AllayWorldGenerator implements WorldGenerator {
private final List<EntitySpawner> entitySpawners;
private final Consumer<Dimension> onDimensionSet;
// 存储基本区块,基本区块的ChunkStatus为NOISED
// 基本区块只会在当一个区块生成时需要访问相邻未生成区块时产生
private final Map<Long, Chunk> basicChunks = new Long2ObjectNonBlockingMap<>();

@Getter
Expand Down Expand Up @@ -94,7 +95,7 @@ public void setDimension(Dimension dimension) {
* 立即在此线程生成完整区块,完整区块的ChunkStatus为FINISHED,即可被载入世界
*/
@Override
public Chunk generateFinishedChunkSynchronously(int x, int z) {
public Chunk generateChunk(int x, int z) {
Chunk chunk = basicChunks.remove(HashUtils.hashXZ(x, z));
if (chunk != null) {
statusNoisedToFinished(chunk);
Expand All @@ -106,20 +107,13 @@ public Chunk generateFinishedChunkSynchronously(int x, int z) {
}

private Chunk getOrGenerateBasicChunk(int x, int z) {
var chunkHash = HashUtils.hashXZ(x, z);
var chunk = basicChunks.get(chunkHash);
if (chunk != null) {
return chunk;
}
chunk = generateBasicChunkImmediately(x, z);
basicChunks.put(chunkHash, chunk);
return chunk;
return basicChunks.computeIfAbsent(HashUtils.hashXZ(x, z), unused -> generateBasicChunk(x, z));
}

/**
* 生成基本区块,基本区块的ChunkStatus为NOISED
*/
private Chunk generateBasicChunkImmediately(int x, int z) {
private Chunk generateBasicChunk(int x, int z) {
var chunk = AllayUnsafeChunk.builder().emptyChunk(x, z, dimension.getDimensionInfo()).toSafeChunk();
statusEmptyToNoised(chunk);
return chunk;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public CompletableFuture<Chunk> loadChunk(int x, int z) {
.thenApplyAsync(chunk -> {
if (chunk.getState() != FINISHED) {
// 只要未完全加载好都重新加载
chunk = getWorldGenerator().generateFinishedChunkSynchronously(x, z);
chunk = getWorldGenerator().generateChunk(x, z);
}
return chunk;
}, Server.getInstance().getComputeThreadPool())
Expand Down Expand Up @@ -228,7 +228,7 @@ private Chunk loadChunkSynchronously(int x, int z, CompletableFuture<Chunk> futu
}
try {
if (chunk.getState() != FINISHED) {
chunk = getWorldGenerator().generateFinishedChunkSynchronously(x, z);
chunk = getWorldGenerator().generateChunk(x, z);
}
} catch (Throwable t) {
log.error("Error while generating chunk ({},{}) !", x, z, t);
Expand Down

0 comments on commit 62f4d01

Please sign in to comment.