mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-04 00:45:57 +00:00
feat: make it works!
This commit is contained in:
parent
1f937a2ae0
commit
133df45968
@ -36,7 +36,7 @@ public class TerraAllayPlugin extends Plugin {
|
|||||||
PLATFORM.getEventManager().callEvent(new PlatformInitializationEvent());
|
PLATFORM.getEventManager().callEvent(new PlatformInitializationEvent());
|
||||||
|
|
||||||
log.info("Registering generator...");
|
log.info("Registering generator...");
|
||||||
WorldGeneratorFactory.getFactory().register("TERRA", AllayGeneratorWrapper::new);
|
WorldGeneratorFactory.getFactory().register("TERRA", preset -> new AllayGeneratorWrapper(preset).getAllayWorldGenerator());
|
||||||
|
|
||||||
log.info("Terra started");
|
log.info("Terra started");
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package org.allaymc.terra.allay.delegate;
|
|||||||
|
|
||||||
import com.dfsek.terra.api.util.vector.Vector3;
|
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.api.world.chunk.UnsafeChunk;
|
||||||
import org.allaymc.terra.allay.Mapping;
|
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.ChunkGenerator;
|
||||||
import com.dfsek.terra.api.world.chunk.generation.ProtoWorld;
|
import com.dfsek.terra.api.world.chunk.generation.ProtoWorld;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Terra Project 2024/6/16
|
* Terra Project 2024/6/16
|
||||||
*
|
*
|
||||||
* @author daoge_cmd
|
* @author daoge_cmd
|
||||||
*/
|
*/
|
||||||
public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk centerChunk) implements ProtoWorld {
|
public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk centerChunk, ChunkAccessible chunkAccessor) implements ProtoWorld {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int centerChunkX() {
|
public int centerChunkX() {
|
||||||
@ -40,11 +40,18 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBlockState(int x, int y, int z, BlockState data, boolean physics) {
|
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());
|
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
|
@Override
|
||||||
public Entity spawnEntity(double x, double y, double z, EntityType entityType) {
|
public Entity spawnEntity(double x, double y, double z, EntityType entityType) {
|
||||||
return new AllayFakeEntity(Vector3.of(x, y, z), allayServerWorld);
|
return new AllayFakeEntity(Vector3.of(x, y, z), allayServerWorld);
|
||||||
@ -52,11 +59,16 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BlockState getBlockState(int x, int y, int z) {
|
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);
|
var blockState = centerChunk.getBlockState(x & 15, y, z & 15);
|
||||||
return new AllayBlockState(blockState, Mapping.blockStateBeToJe(blockState));
|
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
|
@Override
|
||||||
@ -100,7 +112,7 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
|
|||||||
return allayServerWorld;
|
return allayServerWorld;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isInRegin(int x, int y, int z) {
|
private boolean isInCurrentChunk(int x, int y, int z) {
|
||||||
return
|
return
|
||||||
x >= centerChunkX() * 16 && x < centerChunkX() * 16 + 16 &&
|
x >= centerChunkX() * 16 && x < centerChunkX() * 16 + 16 &&
|
||||||
z >= centerChunkZ() * 16 && z < centerChunkZ() * 16 + 16 &&
|
z >= centerChunkZ() * 16 && z < centerChunkZ() * 16 + 16 &&
|
||||||
|
@ -2,11 +2,14 @@ package org.allaymc.terra.allay.generator;
|
|||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.biome.BiomeType;
|
||||||
import org.allaymc.api.world.generator.ChunkGenerateContext;
|
|
||||||
import org.allaymc.api.world.generator.WorldGenerator;
|
import org.allaymc.api.world.generator.WorldGenerator;
|
||||||
import org.allaymc.api.world.generator.WorldGeneratorType;
|
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.TerraAllayPlugin;
|
||||||
import org.allaymc.terra.allay.delegate.AllayProtoChunk;
|
import org.allaymc.terra.allay.delegate.AllayProtoChunk;
|
||||||
import org.allaymc.terra.allay.delegate.AllayProtoWorld;
|
import org.allaymc.terra.allay.delegate.AllayProtoWorld;
|
||||||
@ -27,7 +30,7 @@ import com.dfsek.terra.api.world.info.WorldProperties;
|
|||||||
* @author daoge_cmd
|
* @author daoge_cmd
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@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 DEFAULT_PACK_NAME = "overworld";
|
||||||
protected static final String OPTION_PACK_NAME = "pack";
|
protected static final String OPTION_PACK_NAME = "pack";
|
||||||
protected static final String OPTION_SEED = "pack";
|
protected static final String OPTION_SEED = "pack";
|
||||||
@ -37,55 +40,28 @@ public class AllayGeneratorWrapper extends WorldGenerator implements GeneratorWr
|
|||||||
@Getter
|
@Getter
|
||||||
protected final ConfigPack configPack;
|
protected final ConfigPack configPack;
|
||||||
protected final ChunkGenerator chunkGenerator;
|
protected final ChunkGenerator chunkGenerator;
|
||||||
protected WorldProperties worldProperties;
|
|
||||||
@Getter
|
@Getter
|
||||||
protected long seed;
|
protected final long seed;
|
||||||
|
@Getter
|
||||||
|
protected final WorldGenerator allayWorldGenerator;
|
||||||
|
protected WorldProperties worldProperties;
|
||||||
|
protected AllayServerWorld allayServerWorld;
|
||||||
|
|
||||||
public AllayGeneratorWrapper(String preset) {
|
public AllayGeneratorWrapper(String preset) {
|
||||||
super(preset);
|
var options = AllayStringUtils.parseOptions(preset);
|
||||||
var options = parseOptions(preset);
|
|
||||||
var packName = options.getOrDefault(OPTION_PACK_NAME, DEFAULT_PACK_NAME);
|
var packName = options.getOrDefault(OPTION_PACK_NAME, DEFAULT_PACK_NAME);
|
||||||
this.seed = Long.parseLong(options.getOrDefault(OPTION_SEED, "0"));
|
this.seed = Long.parseLong(options.getOrDefault(OPTION_SEED, "0"));
|
||||||
this.configPack = createConfigPack(packName);
|
this.configPack = createConfigPack(packName);
|
||||||
this.chunkGenerator = createGenerator(this.configPack);
|
this.chunkGenerator = createGenerator(this.configPack);
|
||||||
this.biomeProvider = this.configPack.getBiomeProvider();
|
this.biomeProvider = this.configPack.getBiomeProvider();
|
||||||
}
|
this.allayWorldGenerator = WorldGenerator
|
||||||
|
.builder()
|
||||||
@Override
|
.name("TERRA")
|
||||||
public void generate(ChunkGenerateContext context) {
|
.preset("")// preset已经在构造函数读取完了,这边不需要传preset
|
||||||
var chunk = context.chunk();
|
.noisers(new AllayNoiser())
|
||||||
var chunkX = chunk.getX();
|
.populators(new AllayPopulator())
|
||||||
var chunkZ = chunk.getZ();
|
.onDimensionSet(dimension -> {
|
||||||
chunkGenerator.generateChunkData(
|
this.allayServerWorld = new AllayServerWorld(this, dimension);
|
||||||
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()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 void setDimension(Dimension dimension) {
|
|
||||||
super.setDimension(dimension);
|
|
||||||
this.worldProperties = new WorldProperties() {
|
this.worldProperties = new WorldProperties() {
|
||||||
@Override
|
@Override
|
||||||
public long getSeed() {
|
public long getSeed() {
|
||||||
@ -108,6 +84,63 @@ public class AllayGeneratorWrapper extends WorldGenerator implements GeneratorWr
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "TERRA_NOISER";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AllayPopulator implements Populator {
|
||||||
|
|
||||||
|
@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 String getName() {
|
||||||
|
return "TERRA_POPULATOR";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static ConfigPack createConfigPack(String packName) {
|
protected static ConfigPack createConfigPack(String packName) {
|
||||||
@ -126,14 +159,4 @@ public class AllayGeneratorWrapper extends WorldGenerator implements GeneratorWr
|
|||||||
public ChunkGenerator getHandle() {
|
public ChunkGenerator getHandle() {
|
||||||
return chunkGenerator;
|
return chunkGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getGeneratorName() {
|
|
||||||
return "TERRA";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public WorldGeneratorType getType() {
|
|
||||||
return WorldGeneratorType.INFINITE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,12 @@ public class AllayWorldHandle implements WorldHandle {
|
|||||||
@Override
|
@Override
|
||||||
public @NotNull EntityType getEntity(@NotNull String id) {
|
public @NotNull EntityType getEntity(@NotNull String id) {
|
||||||
// TODO: 我们暂时不支持实体,因为端本身都没实体ai,生成实体没有意义
|
// TODO: 我们暂时不支持实体,因为端本身都没实体ai,生成实体没有意义
|
||||||
return null;
|
return new EntityType() {
|
||||||
|
private final Object fakeEntityType = new Object();
|
||||||
|
@Override
|
||||||
|
public Object getHandle() {
|
||||||
|
return fakeEntityType;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user