mirror of https://github.com/Minestom/Minestom.git
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/fr/themode/minestom/entity/Player.java # src/main/java/fr/themode/minestom/net/ConnectionManager.java # src/main/java/fr/themode/minestom/net/packet/client/login/LoginStartPacket.java
This commit is contained in:
commit
c8b5e13175
|
@ -19,19 +19,19 @@ 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;
|
||||
private static Server server;
|
||||
|
||||
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 = new TCPServer(new MinecraftProtocol()).addHandler(new ServerHandler() {
|
||||
@Override
|
||||
|
@ -87,10 +87,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
|
||||
|
@ -103,6 +105,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,36 +1,47 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -7,6 +7,7 @@ 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) {
|
||||
|
@ -14,15 +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(UUID uuid, String username, PlayerConnection connection) {
|
||||
this.connectionPlayerMap.put(connection, new Player(uuid, username, connection));
|
||||
}
|
||||
|
||||
public void removePlayer(PlayerConnection connection) {
|
||||
this.connectionPlayerMap.remove(connection);
|
||||
public void createPlayer(PlayerConnection 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();
|
||||
|
|
Loading…
Reference in New Issue