This commit is contained in:
Felix Cravic 2020-04-16 14:51:21 +02:00
parent cb194db4c0
commit 656ccac26d
9 changed files with 2254 additions and 15 deletions

View File

@ -38,4 +38,7 @@ dependencies {
compile 'com.github.TheMode:CommandBuilder:611d16a487'
// https://mvnrepository.com/artifact/javax.vecmath/vecmath
compile group: 'javax.vecmath', name: 'vecmath', version: '1.5.2' // Used for Fastnoise
}

View File

@ -1,6 +1,7 @@
package fr.themode.demo;
import fr.themode.demo.generator.ChunkGeneratorDemo;
import fr.themode.demo.generator.NoiseTestGenerator;
import fr.themode.minestom.MinecraftServer;
import fr.themode.minestom.benchmark.BenchmarkManager;
import fr.themode.minestom.entity.Entity;
@ -27,18 +28,19 @@ public class PlayerInit {
static {
ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo();
NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator();
//instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(new File("chunk_data"));
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer();
instanceContainer.enableAutoChunkLoad(true);
instanceContainer.setChunkGenerator(chunkGeneratorDemo);
instanceContainer.setChunkGenerator(noiseTestGenerator);
// Load some chunks beforehand
int loopStart = -2;
int loopEnd = 2;
long time = System.currentTimeMillis();
for (int x = loopStart; x < loopEnd; x++)
for (int z = loopStart; z < loopEnd; z++) {
instanceContainer.loadChunk(x, z);
}
System.out.println("Time to load all chunks: " + (System.currentTimeMillis() - time) + " ms");
}
public static void init() {
@ -127,8 +129,8 @@ public class PlayerInit {
});
player.setEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.SURVIVAL);
player.teleport(new Position(0, 66, 0));
player.setGameMode(GameMode.CREATIVE);
player.teleport(new Position(0, 75, 0));
/*Random random = new Random();
for (int i = 0; i < 50; i++) {

View File

@ -0,0 +1,41 @@
package fr.themode.demo.generator;
import fr.themode.minestom.instance.Biome;
import fr.themode.minestom.instance.ChunkGenerator;
import fr.themode.minestom.instance.batch.ChunkBatch;
import fr.themode.minestom.utils.noise.FastNoise;
import java.util.Random;
public class NoiseTestGenerator extends ChunkGenerator {
private Random random = new Random();
private FastNoise fastNoise = new FastNoise();
private int totalChunk = 15;
{
fastNoise.SetNoiseType(FastNoise.NoiseType.Simplex);
fastNoise.SetInterp(FastNoise.Interp.Linear);
}
@Override
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
for (byte x = 0; x < 16; x++)
for (byte z = 0; z < 16; z++) {
float height = fastNoise.GetSimplex(x + 16 * chunkX, z + 16 * chunkZ) * 135;
height = Math.max(height, 70);
for (byte y = 0; y < height; y++) {
if (random.nextInt(100) > 10) {
batch.setCustomBlock(x, y, z, "custom_block");
} else {
batch.setBlock(x, y, z, (short) 10);
}
}
}
}
@Override
public Biome getBiome(int chunkX, int chunkZ) {
return Biome.PLAINS;
}
}

View File

@ -54,8 +54,8 @@ public class MinecraftServer {
public static final int THREAD_COUNT_SCHEDULER = 1;
// Config
public static final int CHUNK_VIEW_DISTANCE = 5;
public static final int ENTITY_VIEW_DISTANCE = 2;
public static final int CHUNK_VIEW_DISTANCE = 10;
public static final int ENTITY_VIEW_DISTANCE = 5;
// Can be modified at performance cost when decreased
private static final int MS_TO_SEC = 1000;
public static final int TICK_MS = MS_TO_SEC / 20;

View File

@ -319,7 +319,7 @@ public class Player extends LivingEntity {
}
boolean isLast = counter.get() == length - 1;
if (isLast) {
// This is the last chunk to be loaded, spawn player
// This is the last chunk to be loaded , spawn player
super.setInstance(instance);
PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(instance);
callEvent(PlayerSpawnEvent.class, spawnEvent);
@ -886,9 +886,9 @@ public class Player extends LivingEntity {
int serverRange = MinecraftServer.CHUNK_VIEW_DISTANCE;
int playerRange = getSettings().viewDistance;
if (playerRange == 0) {
return serverRange; // Didn't receive settings packet yet
return serverRange; // Didn't receive settings packet yet (is the case on login)
} else {
return playerRange < serverRange ? playerRange : serverRange;
return Math.min(playerRange, serverRange);
}
}
@ -901,6 +901,8 @@ public class Player extends LivingEntity {
OFF
}
// Settings enum
public enum MainHand {
LEFT,
RIGHT

View File

@ -141,11 +141,17 @@ public abstract class Instance implements BlockModifier, DataContainer {
}
public void loadChunk(Position position, Consumer<Chunk> callback) {
int chunkX = Math.floorDiv((int) position.getX(), 16);
int chunkZ = Math.floorDiv((int) position.getY(), 16);
int chunkX = ChunkUtils.getChunkCoordinate((int) position.getX());
int chunkZ = ChunkUtils.getChunkCoordinate((int) position.getZ());
loadChunk(chunkX, chunkZ, callback);
}
public void loadOptionalChunk(Position position, Consumer<Chunk> callback) {
int chunkX = ChunkUtils.getChunkCoordinate((int) position.getX());
int chunkZ = ChunkUtils.getChunkCoordinate((int) position.getZ());
loadOptionalChunk(chunkX, chunkZ, callback);
}
public short getBlockId(int x, int y, int z) {
Chunk chunk = getChunkAt(x, z);
return chunk.getBlockId((byte) (x % 16), (byte) y, (byte) (z % 16));

View File

@ -35,7 +35,7 @@ public class InstanceContainer extends Instance {
private ChunkGenerator chunkGenerator;
private Map<Long, Chunk> chunks = new ConcurrentHashMap<>();
private boolean autoChunkLoad;
private volatile boolean autoChunkLoad;
protected InstanceContainer(UUID uniqueId, File folder) {
super(uniqueId);
@ -207,6 +207,7 @@ public class InstanceContainer extends Instance {
@Override
public void loadOptionalChunk(int chunkX, int chunkZ, Consumer<Chunk> callback) {
Chunk chunk = getChunk(chunkX, chunkZ);
System.out.println("test load: " + chunk + " : " + hasEnabledAutoChunkLoad());
if (chunk != null) {
if (callback != null)
callback.accept(chunk);

View File

@ -46,6 +46,8 @@ public class LoginStartPacket implements ClientPreplayPacket {
connectionManager.createPlayer(playerUuid, username, connection);
Player player = connectionManager.getPlayer(connection);
MinecraftServer.getEntityManager().addWaitingPlayer(player);
GameMode gameMode = GameMode.SURVIVAL;
Dimension dimension = Dimension.OVERWORLD;
LevelType levelType = LevelType.DEFAULT;
@ -94,8 +96,6 @@ public class LoginStartPacket implements ClientPreplayPacket {
if (playerInitialization != null)
playerInitialization.accept(player);
MinecraftServer.getEntityManager().addWaitingPlayer(player);
{
CommandManager commandManager = MinecraftServer.getCommandManager();

File diff suppressed because it is too large Load Diff