mirror of https://github.com/Minestom/Minestom.git
Entity#getPosition is now fully synchronized with the entity position
This commit is contained in:
parent
eb4e47df52
commit
af430ebc3f
|
@ -0,0 +1,71 @@
|
||||||
|
package fr.themode.demo;
|
||||||
|
|
||||||
|
import net.minestom.server.MinecraftServer;
|
||||||
|
import net.minestom.server.event.player.PlayerLoginEvent;
|
||||||
|
import net.minestom.server.event.player.PlayerSpawnEvent;
|
||||||
|
import net.minestom.server.instance.*;
|
||||||
|
import net.minestom.server.instance.batch.ChunkBatch;
|
||||||
|
import net.minestom.server.instance.block.Block;
|
||||||
|
import net.minestom.server.network.ConnectionManager;
|
||||||
|
import net.minestom.server.utils.Position;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MainDemo {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Initialization
|
||||||
|
MinecraftServer minecraftServer = MinecraftServer.init();
|
||||||
|
|
||||||
|
InstanceManager instanceManager = MinecraftServer.getInstanceManager();
|
||||||
|
// Create the instance
|
||||||
|
InstanceContainer instanceContainer = instanceManager.createInstanceContainer();
|
||||||
|
// Set the ChunkGenerator
|
||||||
|
instanceContainer.setChunkGenerator(new GeneratorDemo());
|
||||||
|
// Enable the auto chunk loading (when players come close)
|
||||||
|
instanceContainer.enableAutoChunkLoad(true);
|
||||||
|
|
||||||
|
// Add event listeners
|
||||||
|
ConnectionManager connectionManager = MinecraftServer.getConnectionManager();
|
||||||
|
connectionManager.addPlayerInitialization(player -> {
|
||||||
|
// Set the spawning instance
|
||||||
|
player.addEventCallback(PlayerLoginEvent.class, event -> {
|
||||||
|
event.setSpawningInstance(instanceContainer);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Teleport the player at spawn
|
||||||
|
player.addEventCallback(PlayerSpawnEvent.class, event -> {
|
||||||
|
player.teleport(new Position(0, 45, 0));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start the server
|
||||||
|
minecraftServer.start("localhost", 55555);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class GeneratorDemo extends ChunkGenerator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
|
||||||
|
// Set chunk blocks
|
||||||
|
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++)
|
||||||
|
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
|
||||||
|
for (byte y = 0; y < 40; y++) {
|
||||||
|
batch.setBlock(x, y, z, Block.STONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fillBiomes(Biome[] biomes, int chunkX, int chunkZ) {
|
||||||
|
Arrays.fill(biomes, Biome.PLAINS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ChunkPopulator> getPopulators() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,7 +16,7 @@ public class ChunkGeneratorDemo extends ChunkGenerator {
|
||||||
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
|
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
|
||||||
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++)
|
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++)
|
||||||
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
|
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
|
||||||
for (byte y = 0; y < 65; y++) {
|
for (byte y = 0; y < 40; y++) {
|
||||||
batch.setBlock(x, y, z, Block.STONE);
|
batch.setBlock(x, y, z, Block.STONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
protected Instance instance;
|
protected Instance instance;
|
||||||
protected Position position;
|
protected Position position;
|
||||||
protected float lastX, lastY, lastZ;
|
protected float lastX, lastY, lastZ;
|
||||||
|
protected float cacheX, cacheY, cacheZ; // Used to synchronize with #getPosition
|
||||||
protected float lastYaw, lastPitch;
|
protected float lastYaw, lastPitch;
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
@ -286,6 +287,12 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (cacheX != position.getX() ||
|
||||||
|
cacheY != position.getY() ||
|
||||||
|
cacheZ != position.getZ()) {
|
||||||
|
teleport(position);
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldUpdate(time)) {
|
if (shouldUpdate(time)) {
|
||||||
this.lastUpdate = time;
|
this.lastUpdate = time;
|
||||||
|
|
||||||
|
@ -689,6 +696,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
||||||
position.setX(x);
|
position.setX(x);
|
||||||
position.setY(y);
|
position.setY(y);
|
||||||
position.setZ(z);
|
position.setZ(z);
|
||||||
|
this.cacheX = x;
|
||||||
|
this.cacheY = y;
|
||||||
|
this.cacheZ = z;
|
||||||
|
|
||||||
if (hasPassenger()) {
|
if (hasPassenger()) {
|
||||||
for (Entity passenger : getPassengers()) {
|
for (Entity passenger : getPassengers()) {
|
||||||
|
|
Loading…
Reference in New Issue