structure gen test.

This commit is contained in:
Eoghanmc22 2020-08-17 21:11:48 -04:00
parent a589b21c9b
commit efbe80a748
8 changed files with 218 additions and 38 deletions

View File

@ -35,12 +35,15 @@ import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.world.DimensionType;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
public class PlayerInit {
private static volatile InstanceContainer instanceContainer;
//private static volatile InstanceContainer netherTest;
private static Random r = new Random();
private static volatile Inventory inventory;
static {
@ -258,7 +261,7 @@ public class PlayerInit {
player.addEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.CREATIVE);
player.teleport(new Position(0, 41f, 0));
player.teleport(new Position(r.nextInt(200)-100, 73, r.nextInt(200)-100));
//player.setHeldItemSlot((byte) 5);

View File

@ -2,46 +2,188 @@ package fr.themode.demo.generator;
import de.articdive.jnoise.JNoise;
import de.articdive.jnoise.interpolation.InterpolationType;
import lombok.Data;
import net.minestom.server.MinecraftServer;
import net.minestom.server.world.biomes.Biome;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.ChunkGenerator;
import net.minestom.server.instance.ChunkPopulator;
import net.minestom.server.instance.batch.ChunkBatch;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.world.biomes.Biome;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.*;
public class NoiseTestGenerator extends ChunkGenerator {
private Random random = new Random();
private JNoise jNoise = JNoise.newBuilder().perlin().setInterpolation(InterpolationType.LINEAR).setSeed(141414).setFrequency(0.5).build();
private Random random = new Random();
private JNoise jNoise = JNoise.newBuilder().perlin().setInterpolation(InterpolationType.LINEAR).setSeed(random.nextInt()).setFrequency(0.4).build();
private JNoise jNoise2 = JNoise.newBuilder().perlin().setInterpolation(InterpolationType.LINEAR).setSeed(random.nextInt()).setFrequency(0.6).build();
private TreePopulator treeGen = new TreePopulator();
@Override
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
for (int x = 0; x < Chunk.CHUNK_SIZE_X; x++) {
for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
double height = jNoise.getNoise((x + chunkX * 16) / 16.0, (z + chunkZ * 16) / 16.0) * 15 + 40;
for (int y = 0; y < height; y++) {
if (random.nextInt(100) > 10) {
batch.setBlock(x, y, z, Block.DIAMOND_BLOCK);
} else {
batch.setBlock(x, y, z, Block.GOLD_BLOCK);
}
}
}
}
}
public int getHeight(int x, int z) {
double preHeight = jNoise.getNoise(x / 16.0, z / 16.0);
return (int) ((preHeight > 0 ? preHeight * 6 : preHeight * 4) + 64);
}
@Override
public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
Arrays.fill(biomes, MinecraftServer.getBiomeManager().getById(0));
}
@Override
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
for (int x = 0; x < Chunk.CHUNK_SIZE_X; x++) {
for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
int height = getHeight(x + chunkX * 16, z + chunkZ * 16);
for (int y = 0; y < height; y++) {
//if (random.nextInt(100) > 10) {
// batch.setBlock(x, y, z, Block.DIAMOND_BLOCK);
//} else {
// batch.setBlock(x, y, z, Block.GOLD_BLOCK);
//}
batch.setBlock(x, y, z, Block.GRASS_BLOCK);
}
if (height < 61) {
for (int y = 0; y < 61 - height; y++) {
batch.setBlock(x, y + height, z, Block.WATER);
}
}
}
}
}
@Override
public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
Arrays.fill(biomes, MinecraftServer.getBiomeManager().getById(0));
}
@Override
public List<ChunkPopulator> getPopulators() {
List<ChunkPopulator> list = new ArrayList<>();
list.add(treeGen);
return list;
}
@Data
public static class Structure {
private final Map<BlockPosition, Block> blocks = new HashMap<>();
public void build(ChunkBatch batch, BlockPosition pos) {
blocks.forEach((bPos, block) -> {
if (bPos.getX() + pos.getX() >= Chunk.CHUNK_SIZE_X || bPos.getX() + pos.getX() < 0)
return;
if (bPos.getZ() + pos.getZ() >= Chunk.CHUNK_SIZE_Z || bPos.getZ() + pos.getZ() < 0)
return;
batch.setBlock(bPos.clone().add(pos), block);
});
}
public void addBlock(Block block, int localX, int localY, int localZ) {
blocks.put(new BlockPosition(localX, localY, localZ), block);
}
}
private class TreePopulator implements ChunkPopulator {
final Structure tree;
public TreePopulator() {
tree = new Structure();
tree.addBlock(Block.OAK_LOG, 0, 0, 0);
tree.addBlock(Block.OAK_LOG, 0, 1, 0);
tree.addBlock(Block.OAK_LOG, 0, 2, 0);
tree.addBlock(Block.OAK_LOG, 0, 3, 0);
tree.addBlock(Block.OAK_LEAVES, 1, 1, 0);
tree.addBlock(Block.OAK_LEAVES, 2, 1, 0);
tree.addBlock(Block.OAK_LEAVES, -1, 1, 0);
tree.addBlock(Block.OAK_LEAVES, -2, 1, 0);
tree.addBlock(Block.OAK_LEAVES, 1, 1, 1);
tree.addBlock(Block.OAK_LEAVES, 2, 1, 1);
tree.addBlock(Block.OAK_LEAVES, 0, 1, 1);
tree.addBlock(Block.OAK_LEAVES, -1, 1, 1);
tree.addBlock(Block.OAK_LEAVES, -2, 1, 1);
tree.addBlock(Block.OAK_LEAVES, 1, 1, 2);
tree.addBlock(Block.OAK_LEAVES, 2, 1, 2);
tree.addBlock(Block.OAK_LEAVES, 0, 1, 2);
tree.addBlock(Block.OAK_LEAVES, -1, 1, 2);
tree.addBlock(Block.OAK_LEAVES, -2, 1, 2);
tree.addBlock(Block.OAK_LEAVES, 1, 1, -1);
tree.addBlock(Block.OAK_LEAVES, 2, 1, -1);
tree.addBlock(Block.OAK_LEAVES, 0, 1, -1);
tree.addBlock(Block.OAK_LEAVES, -1, 1, -1);
tree.addBlock(Block.OAK_LEAVES, -2, 1, -1);
tree.addBlock(Block.OAK_LEAVES, 1, 1, -2);
tree.addBlock(Block.OAK_LEAVES, 2, 1, -2);
tree.addBlock(Block.OAK_LEAVES, 0, 1, -2);
tree.addBlock(Block.OAK_LEAVES, -1, 1, -2);
tree.addBlock(Block.OAK_LEAVES, -2, 1, -2);
tree.addBlock(Block.OAK_LEAVES, 1, 2, 0);
tree.addBlock(Block.OAK_LEAVES, 2, 2, 0);
tree.addBlock(Block.OAK_LEAVES, -1, 2, 0);
tree.addBlock(Block.OAK_LEAVES, -2, 2, 0);
tree.addBlock(Block.OAK_LEAVES, 1, 2, 1);
tree.addBlock(Block.OAK_LEAVES, 2, 2, 1);
tree.addBlock(Block.OAK_LEAVES, 0, 2, 1);
tree.addBlock(Block.OAK_LEAVES, -1, 2, 1);
tree.addBlock(Block.OAK_LEAVES, -2, 2, 1);
tree.addBlock(Block.OAK_LEAVES, 1, 2, 2);
tree.addBlock(Block.OAK_LEAVES, 2, 2, 2);
tree.addBlock(Block.OAK_LEAVES, 0, 2, 2);
tree.addBlock(Block.OAK_LEAVES, -1, 2, 2);
tree.addBlock(Block.OAK_LEAVES, -2, 2, 2);
tree.addBlock(Block.OAK_LEAVES, 1, 2, -1);
tree.addBlock(Block.OAK_LEAVES, 2, 2, -1);
tree.addBlock(Block.OAK_LEAVES, 0, 2, -1);
tree.addBlock(Block.OAK_LEAVES, -1, 2, -1);
tree.addBlock(Block.OAK_LEAVES, -2, 2, -1);
tree.addBlock(Block.OAK_LEAVES, 1, 2, -2);
tree.addBlock(Block.OAK_LEAVES, 2, 2, -2);
tree.addBlock(Block.OAK_LEAVES, 0, 2, -2);
tree.addBlock(Block.OAK_LEAVES, -1, 2, -2);
tree.addBlock(Block.OAK_LEAVES, -2, 2, -2);
tree.addBlock(Block.OAK_LEAVES, 1, 3, 0);
tree.addBlock(Block.OAK_LEAVES, -1, 3, 0);
tree.addBlock(Block.OAK_LEAVES, 1, 3, 1);
tree.addBlock(Block.OAK_LEAVES, 0, 3, 1);
tree.addBlock(Block.OAK_LEAVES, -1, 3, 1);
tree.addBlock(Block.OAK_LEAVES, 1, 3, -1);
tree.addBlock(Block.OAK_LEAVES, 0, 3, -1);
tree.addBlock(Block.OAK_LEAVES, -1, 3, -1);
tree.addBlock(Block.OAK_LEAVES, 1, 4, 0);
tree.addBlock(Block.OAK_LEAVES, 0, 4, 0);
tree.addBlock(Block.OAK_LEAVES, -1, 4, 0);
tree.addBlock(Block.OAK_LEAVES, 0, 4, 1);
tree.addBlock(Block.OAK_LEAVES, 0, 4, -1);
tree.addBlock(Block.OAK_LEAVES, -1, 4, -1);
}
@Override
public void populateChunk(ChunkBatch batch, Chunk chunk) {
for (int i = -2; i < 18; i++) {
for (int j = -2; j < 18; j++) {
if (jNoise2.getNoise(i + chunk.getChunkX() * 16, j + chunk.getChunkZ() * 16) > 0.75) {
int y = getHeight(i + chunk.getChunkX() * 16, j + chunk.getChunkZ() * 16);
tree.build(batch, new BlockPosition(i, y, j));
}
}
}
}
}
@Override
public List<ChunkPopulator> getPopulators() {
return null;
}
}

View File

@ -72,14 +72,13 @@ public class ChunkBatch implements InstanceBatch {
clearData(); // So the populators won't place those blocks again
if (hasPopulator) {
Iterator<ChunkPopulator> populatorIterator = populators.iterator();
while (populatorIterator.hasNext()) {
final ChunkPopulator chunkPopulator = populatorIterator.next();
for (ChunkPopulator chunkPopulator : populators) {
chunkPopulator.populateChunk(this, chunk);
}
singleThreadFlush(callback);
clearData(); // Clear populators blocks
}
clearData(); // Clear populators blocks
});
}

View File

@ -2,6 +2,7 @@ package net.minestom.server.network.netty.channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.network.ConnectionManager;
@ -9,6 +10,7 @@ import net.minestom.server.network.PacketProcessor;
import net.minestom.server.network.netty.packet.InboundPacket;
import net.minestom.server.network.player.PlayerConnection;
@Slf4j
public class ClientChannel extends SimpleChannelInboundHandler<InboundPacket> {
private final ConnectionManager connectionManager = MinecraftServer.getConnectionManager();
@ -57,7 +59,7 @@ public class ClientChannel extends SimpleChannelInboundHandler<InboundPacket> {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
log.info(cause.getMessage());
ctx.close();
}
}

View File

@ -55,6 +55,32 @@ public class BlockPosition {
return this;
}
/**
* Add offsets to this block position
*
* @param pos the pos to add
* @return the instance of this block position
*/
public BlockPosition add(BlockPosition pos) {
this.x += pos.getX();
this.y += pos.getY();
this.z += pos.getZ();
return this;
}
/**
* Subtract offsets to this block position
*
* @param pos the pos to subtract
* @return the instance of this block position
*/
public BlockPosition subtract(BlockPosition pos) {
this.x -= pos.getX();
this.y -= pos.getY();
this.z -= pos.getZ();
return this;
}
/**
* Get the block X
*

View File

@ -78,4 +78,8 @@ public final class MathUtils {
return Math.min(value, max);
}
}
public static double mod(final double a, final double b) {
return (a % b + b) % b;
}
}

View File

@ -85,8 +85,8 @@ public final class ChunkUtils {
* @return the chunk Z based on the index
*/
public static int getChunkCoordZ(long index) {
final int chunkX = (int) (index >> 32);
final int chunkZ = (int) index;
//final int chunkX = (int) (index >> 32);
//final int chunkZ = (int) index;
return (int) index;
}

View File

@ -19,8 +19,12 @@ public class Biome {
//A plains biome has to be registered or else minecraft will crash
public static final Biome PLAINS = Biome.builder()
.category(Biome.Category.NONE)
.category(Category.NONE)
.name(NamespaceID.from("minecraft:plains"))
.temperature(0.8F)
.downfall(0.4F)
.depth(0.125F)
.scale(0.05F)
.effects(BiomeEffects.builder()
.fog_color(0xC0D8FF)
.sky_color(0x78A7FF)