Skip to content

Commit

Permalink
refactor: refactor world generator and fix issue #270
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 21, 2024
1 parent db5eb73 commit 7b951c3
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 155 deletions.
11 changes: 10 additions & 1 deletion Allay-API/src/main/java/org/allaymc/api/world/chunk/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface Chunk extends UnsafeChunk {
@ApiStatus.Internal
void tick();

boolean isLoaded();

@UnmodifiableView
Set<ChunkLoader> getChunkLoaders();

Expand All @@ -32,11 +34,18 @@ default Set<EntityPlayer> getPlayerChunkLoaders() {
return getChunkLoaders().stream().filter(EntityPlayer.class::isInstance).map(EntityPlayer.class::cast).collect(Collectors.toSet());
}

/**
* Set the callback to be called when the chunk is loaded into the world
* @param callback the callback
*/
@ApiStatus.Internal
void setChunkSetCallback(Runnable callback);

/**
* Called when the chunk is loaded into the world
*/
@ApiStatus.Internal
Runnable getChunkSetCallback();
void onChunkSet();

void addChunkLoader(ChunkLoader chunkLoader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.chunk.Chunk;
import org.allaymc.api.world.generator.function.EntitySpawner;
import org.allaymc.api.world.generator.function.Noiser;
import org.allaymc.api.world.generator.function.Lighter;
import org.allaymc.api.world.generator.function.Noiser;
import org.allaymc.api.world.generator.function.Populator;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

/**
Expand All @@ -23,7 +24,14 @@ static WorldGeneratorBuilder builder() {
return BUILDER_FACTORY.get().create();
}

Chunk generateChunk(int x, int z);
/**
* 生成一个区块 <br>
* 此方法可能被任意线程调用,故实现应确保线程安全
* @param x 区块x坐标
* @param z 区块z坐标
* @return 生成的区块
*/
CompletableFuture<Chunk> generateChunk(int x, int z);

String getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Getter;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.block.type.BlockTypes;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk;

Expand All @@ -13,6 +14,8 @@
*/
public abstract class OtherChunkAccessibleContext extends Context {

private static BlockState AIR = BlockTypes.AIR_TYPE.getDefaultState();

@Getter
protected ChunkAccessible chunkAccessor;

Expand All @@ -35,7 +38,12 @@ public void setBlockState(int x, int y, int z, BlockState blockState, int layer)

private void setBlockStateInOtherChunk(int x, int y, int z, BlockState blockState, int layer) {
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4);
if (chunk == null) return;
chunk.setBlockState(x & 15, y, z & 15, blockState, layer);
if (chunk.isLoaded()) {
// 区块已经载入世界了,所以需要发送方块更新包
chunk.addChunkPacket(Dimension.createBlockUpdatePacket(blockState, x, y, z, layer));
}
}

public BlockState getBlockState(int x, int y, int z) {
Expand All @@ -51,6 +59,7 @@ public BlockState getBlockState(int x, int y, int z, int layer) {

private BlockState getBlockStateInOtherChunk(int x, int y, int z, int layer) {
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4);
if (chunk == null) return AIR;
return chunk.getBlockState(x & 15, y, z & 15);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.allaymc.server.block.component.common;

import lombok.Getter;
import lombok.Getter;
import org.allaymc.api.block.BlockBehavior;
import org.allaymc.api.block.component.common.BlockBaseComponent;
import org.allaymc.api.block.component.common.PlayerInteractInfo;
import org.allaymc.api.block.component.event.*;
import org.allaymc.api.block.data.BlockFace;
import org.allaymc.api.block.component.common.PlayerInteractInfo;
import org.allaymc.api.block.component.event.BlockOnInteractEvent;
import org.allaymc.api.block.component.event.BlockOnNeighborUpdateEvent;
import org.allaymc.api.block.component.event.BlockOnPlaceEvent;
import org.allaymc.api.block.component.event.BlockOnReplaceEvent;
import org.allaymc.api.block.data.BlockFace;
import org.allaymc.api.block.data.BlockStateWithPos;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.block.type.BlockType;
Expand All @@ -17,9 +20,9 @@
import org.allaymc.api.item.enchantment.type.EnchantmentSilkTouchType;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.world.Dimension;
import org.cloudburstmc.protocol.bedrock.data.GameType;
import org.joml.Vector3f;
import org.joml.Vector3ic;
import org.cloudburstmc.protocol.bedrock.data.GameType;
import org.joml.Vector3f;
import org.joml.Vector3ic;

/**
* Allay Project 2023/4/8
Expand Down Expand Up @@ -91,7 +94,7 @@ public void onBreak(BlockStateWithPos blockState, ItemStack usedItem, EntityPlay

@Override
public boolean isDroppable(BlockStateWithPos blockState, ItemStack usedItem, EntityPlayer player) {
if (player.getGameType() == GameType.CREATIVE) return false;
if (player != null && player.getGameType() == GameType.CREATIVE) return false;
return blockState.blockState().getBlockAttributes().canHarvestWithHand() || usedItem.isCorrectToolFor(blockState.blockState());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
import io.netty.buffer.Unpooled;
import io.netty.util.internal.PlatformDependent;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.blockentity.BlockEntity;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.DimensionInfo;
import org.allaymc.api.world.biome.BiomeType;
import org.allaymc.api.world.chunk.Chunk;
import org.allaymc.api.world.chunk.ChunkLoader;
import org.allaymc.api.world.chunk.ChunkSection;
import org.allaymc.api.world.chunk.ChunkState;
import org.allaymc.api.world.chunk.UnsafeChunk;
import org.allaymc.api.world.chunk.UnsafeChunkOperate;
import org.allaymc.api.world.chunk.*;
import org.cloudburstmc.nbt.NbtUtils;
import org.cloudburstmc.protocol.bedrock.packet.BedrockPacket;
import org.cloudburstmc.protocol.bedrock.packet.LevelChunkPacket;
Expand All @@ -28,12 +24,7 @@

import javax.annotation.concurrent.ThreadSafe;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.*;
import java.util.concurrent.locks.StampedLock;
import java.util.function.Predicate;

Expand All @@ -51,6 +42,9 @@ public class AllayChunk implements Chunk {
protected final StampedLock lightLock;
protected final Set<ChunkLoader> chunkLoaders;
protected final Queue<ChunkPacketEntry> chunkPacketQueue;
// 区块是否已载入世界
@Getter
protected boolean loaded = false;
protected Runnable chunkSetCallback = () -> {};

public AllayChunk(AllayUnsafeChunk unsafeChunk) {
Expand Down Expand Up @@ -591,8 +585,9 @@ public void setChunkSetCallback(Runnable callback) {
}

@Override
public Runnable getChunkSetCallback() {
return chunkSetCallback;
public void onChunkSet() {
chunkSetCallback.run();
loaded = true;
}

@Override
Expand Down
Loading

0 comments on commit 7b951c3

Please sign in to comment.