feat: make it works!

This commit is contained in:
daoge_cmd 2024-06-17 00:37:16 +08:00
parent 1f937a2ae0
commit 133df45968
4 changed files with 114 additions and 73 deletions

View File

@ -36,7 +36,7 @@ public class TerraAllayPlugin extends Plugin {
PLATFORM.getEventManager().callEvent(new PlatformInitializationEvent());
log.info("Registering generator...");
WorldGeneratorFactory.getFactory().register("TERRA", AllayGeneratorWrapper::new);
WorldGeneratorFactory.getFactory().register("TERRA", preset -> new AllayGeneratorWrapper(preset).getAllayWorldGenerator());
log.info("Terra started");
}

View File

@ -2,6 +2,7 @@ package org.allaymc.terra.allay.delegate;
import com.dfsek.terra.api.util.vector.Vector3;
import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk;
import org.allaymc.terra.allay.Mapping;
@ -15,13 +16,12 @@ import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
import com.dfsek.terra.api.world.chunk.generation.ProtoWorld;
/**
* Terra Project 2024/6/16
*
* @author daoge_cmd
*/
public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk centerChunk) implements ProtoWorld {
public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk centerChunk, ChunkAccessible chunkAccessor) implements ProtoWorld {
@Override
public int centerChunkX() {
@ -40,11 +40,18 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
@Override
public void setBlockState(int x, int y, int z, BlockState data, boolean physics) {
if(isInRegin(x, y, z)) {
if(isInCurrentChunk(x, y, z)) {
centerChunk.setBlockState(x & 15, y, z & 15, ((AllayBlockState)data).allayBlockState());
} else {
setBlockStateInOtherChunk(x, y, z, data, physics);
}
}
private void setBlockStateInOtherChunk(int x, int y, int z, BlockState data, boolean physics) {
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4);
chunk.setBlockState(x & 15, y, z & 15, ((AllayBlockState)data).allayBlockState());
}
@Override
public Entity spawnEntity(double x, double y, double z, EntityType entityType) {
return new AllayFakeEntity(Vector3.of(x, y, z), allayServerWorld);
@ -52,11 +59,16 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
@Override
public BlockState getBlockState(int x, int y, int z) {
if(isInRegin(x, y, z)) {
if(isInCurrentChunk(x, y, z)) {
var blockState = centerChunk.getBlockState(x & 15, y, z & 15);
return new AllayBlockState(blockState, Mapping.blockStateBeToJe(blockState));
}
return AllayBlockState.AIR;
return getBlockStateInOtherChunk(x, y, z);
}
private BlockState getBlockStateInOtherChunk(int x, int y, int z) {
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4);
return new AllayBlockState(chunk.getBlockState(x & 15, y, z & 15), Mapping.blockStateBeToJe(chunk.getBlockState(x & 15, y, z & 15)));
}
@Override
@ -100,7 +112,7 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
return allayServerWorld;
}
private boolean isInRegin(int x, int y, int z) {
private boolean isInCurrentChunk(int x, int y, int z) {
return
x >= centerChunkX() * 16 && x < centerChunkX() * 16 + 16 &&
z >= centerChunkZ() * 16 && z < centerChunkZ() * 16 + 16 &&

View File

@ -2,11 +2,14 @@ package org.allaymc.terra.allay.generator;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.world.Dimension;
import org.allaymc.api.utils.AllayStringUtils;
import org.allaymc.api.world.biome.BiomeType;
import org.allaymc.api.world.generator.ChunkGenerateContext;
import org.allaymc.api.world.generator.WorldGenerator;
import org.allaymc.api.world.generator.WorldGeneratorType;
import org.allaymc.api.world.generator.context.NoiseContext;
import org.allaymc.api.world.generator.context.PopulateContext;
import org.allaymc.api.world.generator.function.Noiser;
import org.allaymc.api.world.generator.function.Populator;
import org.allaymc.terra.allay.TerraAllayPlugin;
import org.allaymc.terra.allay.delegate.AllayProtoChunk;
import org.allaymc.terra.allay.delegate.AllayProtoWorld;
@ -27,7 +30,7 @@ import com.dfsek.terra.api.world.info.WorldProperties;
* @author daoge_cmd
*/
@Slf4j
public class AllayGeneratorWrapper extends WorldGenerator implements GeneratorWrapper {
public class AllayGeneratorWrapper implements GeneratorWrapper {
protected static final String DEFAULT_PACK_NAME = "overworld";
protected static final String OPTION_PACK_NAME = "pack";
protected static final String OPTION_SEED = "pack";
@ -37,77 +40,107 @@ public class AllayGeneratorWrapper extends WorldGenerator implements GeneratorWr
@Getter
protected final ConfigPack configPack;
protected final ChunkGenerator chunkGenerator;
protected WorldProperties worldProperties;
@Getter
protected long seed;
protected final long seed;
@Getter
protected final WorldGenerator allayWorldGenerator;
protected WorldProperties worldProperties;
protected AllayServerWorld allayServerWorld;
public AllayGeneratorWrapper(String preset) {
super(preset);
var options = parseOptions(preset);
var options = AllayStringUtils.parseOptions(preset);
var packName = options.getOrDefault(OPTION_PACK_NAME, DEFAULT_PACK_NAME);
this.seed = Long.parseLong(options.getOrDefault(OPTION_SEED, "0"));
this.configPack = createConfigPack(packName);
this.chunkGenerator = createGenerator(this.configPack);
this.biomeProvider = this.configPack.getBiomeProvider();
this.allayWorldGenerator = WorldGenerator
.builder()
.name("TERRA")
.preset("")// preset已经在构造函数读取完了这边不需要传preset
.noisers(new AllayNoiser())
.populators(new AllayPopulator())
.onDimensionSet(dimension -> {
this.allayServerWorld = new AllayServerWorld(this, dimension);
this.worldProperties = new WorldProperties() {
@Override
public long getSeed() {
return seed;
}
@Override
public int getMaxHeight() {
return dimension.getDimensionInfo().maxHeight();
}
@Override
public int getMinHeight() {
return dimension.getDimensionInfo().minHeight();
}
@Override
public Object getHandle() {
// 这里留null就行没啥用
return null;
}
};
})
.build();
}
@Override
public void generate(ChunkGenerateContext context) {
var chunk = context.chunk();
var chunkX = chunk.getX();
var chunkZ = chunk.getZ();
chunkGenerator.generateChunkData(
new AllayProtoChunk(chunk),
worldProperties, biomeProvider,
chunkX, chunkZ
);
var minHeight = dimension.getDimensionInfo().minHeight();
var maxHeight = dimension.getDimensionInfo().maxHeight();
for (int x = 0; x < 16; x++) {
for (int y = minHeight; y < maxHeight; y++) {
for (int z = 0; z < 16; z++) {
chunk.setBiome(
x, y, z,
(BiomeType) biomeProvider.getBiome(chunkX * 16 + x, y, chunkZ * 16 + z, seed).getPlatformBiome().getHandle()
);
public class AllayNoiser implements Noiser {
@Override
public Boolean apply(NoiseContext context) {
var chunk = context.getCurrentChunk();
var chunkX = chunk.getX();
var chunkZ = chunk.getZ();
chunkGenerator.generateChunkData(
new AllayProtoChunk(chunk),
worldProperties, biomeProvider,
chunkX, chunkZ
);
var minHeight = context.getDimensionInfo().minHeight();
var maxHeight = context.getDimensionInfo().maxHeight();
for (int x = 0; x < 16; x++) {
for (int y = minHeight; y < maxHeight; y++) {
for (int z = 0; z < 16; z++) {
chunk.setBiome(
x, y, z,
(BiomeType) biomeProvider.getBiome(chunkX * 16 + x, y, chunkZ * 16 + z, seed).getPlatformBiome().getHandle()
);
}
}
}
return true;
}
var tmp = new AllayProtoWorld(new AllayServerWorld(this, dimension), chunk);
try {
for (var generationStage : configPack.getStages()) {
generationStage.populate(tmp);
}
} catch (Exception e) {
log.error("Error while populating chunk", e);
@Override
public String getName() {
return "TERRA_NOISER";
}
}
@Override
public void setDimension(Dimension dimension) {
super.setDimension(dimension);
this.worldProperties = new WorldProperties() {
@Override
public long getSeed() {
return seed;
}
public class AllayPopulator implements Populator {
@Override
public int getMaxHeight() {
return dimension.getDimensionInfo().maxHeight();
@Override
public Boolean apply(PopulateContext context) {
var chunk = context.getCurrentChunk();
var tmp = new AllayProtoWorld(allayServerWorld, chunk, context.getChunkAccessor());
try {
for (var generationStage : configPack.getStages()) {
generationStage.populate(tmp);
}
} catch (Exception e) {
log.error("Error while populating chunk", e);
}
return true;
}
@Override
public int getMinHeight() {
return dimension.getDimensionInfo().minHeight();
}
@Override
public Object getHandle() {
// 这里留null就行没啥用
return null;
}
};
@Override
public String getName() {
return "TERRA_POPULATOR";
}
}
protected static ConfigPack createConfigPack(String packName) {
@ -126,14 +159,4 @@ public class AllayGeneratorWrapper extends WorldGenerator implements GeneratorWr
public ChunkGenerator getHandle() {
return chunkGenerator;
}
@Override
public String getGeneratorName() {
return "TERRA";
}
@Override
public WorldGeneratorType getType() {
return WorldGeneratorType.INFINITE;
}
}

View File

@ -31,6 +31,12 @@ public class AllayWorldHandle implements WorldHandle {
@Override
public @NotNull EntityType getEntity(@NotNull String id) {
// TODO: 我们暂时不支持实体因为端本身都没实体ai生成实体没有意义
return null;
return new EntityType() {
private final Object fakeEntityType = new Object();
@Override
public Object getHandle() {
return fakeEntityType;
}
};
}
}