mirror of https://github.com/Minestom/Minestom.git
Updated tick manager & players update
This commit is contained in:
parent
cc203b1a1a
commit
7271636aa8
|
@ -15,18 +15,18 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
// In-Game Manager
|
// Networking
|
||||||
private static EntityManager entityManager;
|
|
||||||
|
|
||||||
// Others
|
|
||||||
private static ConnectionManager connectionManager;
|
private static ConnectionManager connectionManager;
|
||||||
private static PacketProcessor packetProcessor;
|
private static PacketProcessor packetProcessor;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
// In-Game Manager
|
||||||
entityManager = new EntityManager();
|
private static EntityManager entityManager;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
connectionManager = new ConnectionManager();
|
connectionManager = new ConnectionManager();
|
||||||
packetProcessor = new PacketProcessor(connectionManager);
|
packetProcessor = new PacketProcessor();
|
||||||
|
|
||||||
|
entityManager = new EntityManager();
|
||||||
|
|
||||||
Server server = new TCPServer(new MinecraftProtocol()).addHandler(new ServerHandler() {
|
Server server = new TCPServer(new MinecraftProtocol()).addHandler(new ServerHandler() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -63,10 +63,12 @@ public class Main {
|
||||||
server.bind(25565);
|
server.bind(25565);
|
||||||
System.out.println("Server started");
|
System.out.println("Server started");
|
||||||
|
|
||||||
long lastTime = System.currentTimeMillis();
|
long tickDistance = 50 * 1000000; // 50 ms
|
||||||
|
long nextTick = System.nanoTime();
|
||||||
|
long currentTime;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (System.currentTimeMillis() - lastTime >= 50) {
|
currentTime = System.nanoTime();
|
||||||
lastTime = System.currentTimeMillis();
|
if (currentTime >= nextTick) {
|
||||||
// Tick
|
// Tick
|
||||||
|
|
||||||
// Keep Alive Handling
|
// Keep Alive Handling
|
||||||
|
@ -79,6 +81,10 @@ public class Main {
|
||||||
|
|
||||||
// Entities update
|
// Entities update
|
||||||
entityManager.update();
|
entityManager.update();
|
||||||
|
|
||||||
|
// Set next tick update time
|
||||||
|
currentTime = System.nanoTime();
|
||||||
|
nextTick = currentTime + tickDistance - (currentTime - nextTick);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,8 +37,8 @@ public class Entity {
|
||||||
public void addToWorld() {
|
public void addToWorld() {
|
||||||
this.isActive = true;
|
this.isActive = true;
|
||||||
EntityManager entityManager = Main.getEntityManager();
|
EntityManager entityManager = Main.getEntityManager();
|
||||||
if (this instanceof LivingEntity) {
|
if (this instanceof EntityCreature) {
|
||||||
entityManager.addLivingEntity((LivingEntity) this);
|
entityManager.addCreature((EntityCreature) this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,35 +1,46 @@
|
||||||
package fr.themode.minestom.entity;
|
package fr.themode.minestom.entity;
|
||||||
|
|
||||||
import java.util.Collections;
|
import fr.themode.minestom.Main;
|
||||||
import java.util.HashSet;
|
import fr.themode.minestom.net.ConnectionManager;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Set;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public class EntityManager {
|
public class EntityManager {
|
||||||
|
|
||||||
private Set<LivingEntity> livingEntities = Collections.synchronizedSet(new HashSet<>());
|
private Set<EntityCreature> creatures = Collections.synchronizedSet(new HashSet<>());
|
||||||
|
|
||||||
private ExecutorService pool = Executors.newFixedThreadPool(50);
|
private ExecutorService creaturesPool = Executors.newFixedThreadPool(3);
|
||||||
|
private ExecutorService playersPool = Executors.newFixedThreadPool(2);
|
||||||
|
|
||||||
|
private ConnectionManager connectionManager = Main.getConnectionManager();
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
livingEntities.removeIf(livingEntity -> livingEntity.shouldRemove());
|
creatures.removeIf(creature -> creature.shouldRemove());
|
||||||
|
|
||||||
synchronized (livingEntities) {
|
synchronized (creatures) {
|
||||||
Iterator<LivingEntity> iterator = livingEntities.iterator();
|
Iterator<EntityCreature> iterator = creatures.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
LivingEntity entity = iterator.next();
|
EntityCreature creature = iterator.next();
|
||||||
pool.submit(entity::update);
|
creaturesPool.submit(creature::update);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Collection<Player> players = connectionManager.getOnlinePlayers();
|
||||||
|
Iterator<Player> iterator = players.iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Player player = iterator.next();
|
||||||
|
playersPool.submit(player::update);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<LivingEntity> getEntities() {
|
public Set<EntityCreature> getCreatures() {
|
||||||
return Collections.unmodifiableSet(livingEntities);
|
return Collections.unmodifiableSet(creatures);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addLivingEntity(LivingEntity livingEntity) {
|
protected void addCreature(EntityCreature creature) {
|
||||||
this.livingEntities.add(livingEntity);
|
this.creatures.add(creature);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ public class Player extends LivingEntity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
// System.out.println("Je suis l'update");
|
//System.out.println("Je suis l'update");
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlayerConnection getPlayerConnection() {
|
public PlayerConnection getPlayerConnection() {
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class ChickenCreature extends EntityCreature {
|
||||||
public void update() {
|
public void update() {
|
||||||
onGround = true;
|
onGround = true;
|
||||||
|
|
||||||
double speed = 0.01;
|
double speed = 0.05;
|
||||||
double newPos = getZ() + speed;
|
double newPos = getZ() + speed;
|
||||||
|
|
||||||
EntityRelativeMovePacket entityRelativeMovePacket = new EntityRelativeMovePacket();
|
EntityRelativeMovePacket entityRelativeMovePacket = new EntityRelativeMovePacket();
|
||||||
|
|
|
@ -3,13 +3,11 @@ package fr.themode.minestom.net;
|
||||||
import fr.themode.minestom.entity.Player;
|
import fr.themode.minestom.entity.Player;
|
||||||
import fr.themode.minestom.net.player.PlayerConnection;
|
import fr.themode.minestom.net.player.PlayerConnection;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class ConnectionManager {
|
public class ConnectionManager {
|
||||||
|
|
||||||
|
private volatile Set<Player> players = new HashSet<>();
|
||||||
private volatile Map<PlayerConnection, Player> connectionPlayerMap = new HashMap<>();
|
private volatile Map<PlayerConnection, Player> connectionPlayerMap = new HashMap<>();
|
||||||
|
|
||||||
public Player getPlayer(PlayerConnection connection) {
|
public Player getPlayer(PlayerConnection connection) {
|
||||||
|
@ -17,11 +15,13 @@ public class ConnectionManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<Player> getOnlinePlayers() {
|
public Collection<Player> getOnlinePlayers() {
|
||||||
return Collections.unmodifiableCollection(connectionPlayerMap.values());
|
return Collections.unmodifiableCollection(players);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is only used at LoginStartPacket#process
|
// Is only used at LoginStartPacket#process
|
||||||
public void createPlayer(PlayerConnection connection) {
|
public void createPlayer(PlayerConnection connection) {
|
||||||
this.connectionPlayerMap.put(connection, new Player(connection));
|
Player player = new Player(connection);
|
||||||
|
this.players.add(player);
|
||||||
|
this.connectionPlayerMap.put(connection, player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package fr.themode.minestom.net;
|
||||||
import fr.adamaq01.ozao.net.Buffer;
|
import fr.adamaq01.ozao.net.Buffer;
|
||||||
import fr.adamaq01.ozao.net.packet.Packet;
|
import fr.adamaq01.ozao.net.packet.Packet;
|
||||||
import fr.adamaq01.ozao.net.server.Connection;
|
import fr.adamaq01.ozao.net.server.Connection;
|
||||||
|
import fr.themode.minestom.Main;
|
||||||
import fr.themode.minestom.entity.Player;
|
import fr.themode.minestom.entity.Player;
|
||||||
import fr.themode.minestom.net.packet.client.ClientPlayPacket;
|
import fr.themode.minestom.net.packet.client.ClientPlayPacket;
|
||||||
import fr.themode.minestom.net.packet.client.ClientPreplayPacket;
|
import fr.themode.minestom.net.packet.client.ClientPreplayPacket;
|
||||||
|
@ -29,8 +30,8 @@ public class PacketProcessor {
|
||||||
private ClientLoginPacketsHandler loginPacketsHandler;
|
private ClientLoginPacketsHandler loginPacketsHandler;
|
||||||
private ClientPlayPacketsHandler playPacketsHandler;
|
private ClientPlayPacketsHandler playPacketsHandler;
|
||||||
|
|
||||||
public PacketProcessor(ConnectionManager connectionManager) {
|
public PacketProcessor() {
|
||||||
this.connectionManager = connectionManager;
|
this.connectionManager = Main.getConnectionManager();
|
||||||
|
|
||||||
this.statusPacketsHandler = new ClientStatusPacketsHandler();
|
this.statusPacketsHandler = new ClientStatusPacketsHandler();
|
||||||
this.loginPacketsHandler = new ClientLoginPacketsHandler();
|
this.loginPacketsHandler = new ClientLoginPacketsHandler();
|
||||||
|
|
|
@ -67,9 +67,9 @@ public class LoginStartPacket implements ClientPreplayPacket {
|
||||||
connection.sendPacket(spawnPositionPacket);
|
connection.sendPacket(spawnPositionPacket);
|
||||||
|
|
||||||
PlayerPositionAndLookPacket playerPositionAndLookPacket = new PlayerPositionAndLookPacket();
|
PlayerPositionAndLookPacket playerPositionAndLookPacket = new PlayerPositionAndLookPacket();
|
||||||
playerPositionAndLookPacket.x = 50;
|
playerPositionAndLookPacket.x = 0;
|
||||||
playerPositionAndLookPacket.y = 5;
|
playerPositionAndLookPacket.y = 5;
|
||||||
playerPositionAndLookPacket.z = 50;
|
playerPositionAndLookPacket.z = 0;
|
||||||
playerPositionAndLookPacket.yaw = 0;
|
playerPositionAndLookPacket.yaw = 0;
|
||||||
playerPositionAndLookPacket.pitch = 0;
|
playerPositionAndLookPacket.pitch = 0;
|
||||||
playerPositionAndLookPacket.flags = 0;
|
playerPositionAndLookPacket.flags = 0;
|
||||||
|
@ -80,7 +80,7 @@ public class LoginStartPacket implements ClientPreplayPacket {
|
||||||
for (int z = -20; z < 20; z++) {
|
for (int z = -20; z < 20; z++) {
|
||||||
// TODO test entity
|
// TODO test entity
|
||||||
ChickenCreature chickenCreature = new ChickenCreature();
|
ChickenCreature chickenCreature = new ChickenCreature();
|
||||||
chickenCreature.setPosition(50 + (double) x * 1, 5, 50 + (double) z * 1);
|
chickenCreature.setPosition((double) x * 1, 5, (double) z * 1);
|
||||||
connectionManager.getOnlinePlayers().forEach(p -> chickenCreature.addViewer(p));
|
connectionManager.getOnlinePlayers().forEach(p -> chickenCreature.addViewer(p));
|
||||||
chickenCreature.addToWorld();
|
chickenCreature.addToWorld();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue