mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-28 14:37:31 +02:00
structure gen test.
This commit is contained in:
parent
a589b21c9b
commit
efbe80a748
@ -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);
|
||||
|
||||
|
@ -2,33 +2,46 @@ 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 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();
|
||||
|
||||
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 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;
|
||||
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);
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -42,6 +55,135 @@ public class NoiseTestGenerator extends ChunkGenerator {
|
||||
|
||||
@Override
|
||||
public List<ChunkPopulator> getPopulators() {
|
||||
return null;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user