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