реорганизация работы Player manager
(cherry picked from commit f92b80d0ae10c329477ce39e41147a320301c2d5)
This commit is contained in:
@@ -8,13 +8,8 @@ import mc.core.network.NetChannel;
|
||||
|
||||
public interface Player {
|
||||
int getId();
|
||||
void setId(int value);
|
||||
|
||||
String getName();
|
||||
void setName(String value);
|
||||
|
||||
boolean isOnline();
|
||||
void setOnline(boolean status);
|
||||
|
||||
NetChannel getChannel();
|
||||
void setChannel(NetChannel channel);
|
||||
|
||||
@@ -9,9 +9,9 @@ import mc.core.network.NetChannel;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PlayerManager {
|
||||
void addPlayer(Player player);
|
||||
Player createPlayer(String name);
|
||||
void joinServer(Player player);
|
||||
void leftServer(Player player);
|
||||
Optional<Player> getPlayer(String name);
|
||||
Optional<Player> getPlayerById(int id);
|
||||
void removePlayer(Player player);
|
||||
NetChannel getBroadcastChannel();
|
||||
}
|
||||
|
||||
@@ -13,13 +13,11 @@ import mc.core.network.BroadcastNetChannel;
|
||||
import mc.core.network.NetChannel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
private static final Random rand = new Random();
|
||||
private List<Player> players;
|
||||
private final Object lock = new Object();
|
||||
@Setter
|
||||
@@ -33,12 +31,26 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPlayer(Player player) {
|
||||
public Player createPlayer(String name) {
|
||||
SimplePlayer player = new SimplePlayer();
|
||||
player.setId(rand.nextInt(10000));
|
||||
player.setName(name);
|
||||
|
||||
synchronized (lock) {
|
||||
players.add(player);
|
||||
lock.notify();
|
||||
log.info("Player \"{}\" join server", player.getName());
|
||||
}
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void joinServer(Player player) {
|
||||
((SimplePlayer) player).setOnline(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leftServer(Player player) {
|
||||
((SimplePlayer) player).setOnline(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -48,27 +60,9 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Player> getPlayerById(final int id) {
|
||||
return players.stream()
|
||||
.filter(player -> player.getId() == id)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePlayer(Player player) {
|
||||
player.setOnline(false);
|
||||
player.setChannel(null);
|
||||
log.info("Player \"{}\" left server", player.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetChannel getBroadcastChannel() {
|
||||
return new BroadcastNetChannel(
|
||||
players.stream()
|
||||
.filter(Player::isOnline)
|
||||
.filter(player -> player.getChannel() != null)
|
||||
);
|
||||
return new BroadcastNetChannel(players.stream().filter(Player::isOnline));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
21
core/src/main/java/mc/core/embedded/SimplePlayer.java
Normal file
21
core/src/main/java/mc/core/embedded/SimplePlayer.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-23
|
||||
*/
|
||||
package mc.core.embedded;
|
||||
|
||||
import lombok.Data;
|
||||
import mc.core.Location;
|
||||
import mc.core.Look;
|
||||
import mc.core.Player;
|
||||
import mc.core.network.NetChannel;
|
||||
|
||||
@Data
|
||||
public class SimplePlayer implements Player {
|
||||
private int id;
|
||||
private String name;
|
||||
private boolean online = false;
|
||||
private NetChannel channel;
|
||||
private Location location = new Location(0, 0, 0);
|
||||
private Look look = new Look(0, 0);
|
||||
}
|
||||
Reference in New Issue
Block a user