Archived
0

correct update player list

This commit is contained in:
2018-04-30 14:40:45 +03:00
parent 1bbd46771a
commit 1a7f30da5a
3 changed files with 20 additions and 7 deletions

View File

@@ -6,6 +6,7 @@ package mc.core;
import mc.core.network.NetChannel;
import java.util.List;
import java.util.Optional;
public interface PlayerManager {
@@ -13,5 +14,6 @@ public interface PlayerManager {
void joinServer(Player player);
void leftServer(Player player);
Optional<Player> getPlayer(String name);
List<Player> getPlayers();
NetChannel getBroadcastChannel();
}

View File

@@ -14,6 +14,7 @@ import mc.core.network.NetChannel;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
public class InMemoryPlayerManager implements PlayerManager, Runnable {
@@ -60,6 +61,11 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
.findFirst();
}
@Override
public List<Player> getPlayers() {
return players.stream().filter(Player::isOnline).collect(Collectors.toList());
}
@Override
public NetChannel getBroadcastChannel() {
return new BroadcastNetChannel(players.stream().filter(Player::isOnline));

View File

@@ -13,6 +13,7 @@ import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import mc.core.*;
import mc.core.network.CSPacket;
import mc.core.network.NetChannel;
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
import mc.core.network.proto_125.packets.*;
import org.slf4j.Marker;
@@ -21,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
@Slf4j
@@ -109,13 +111,6 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
abilitiesPkt.setInstantDestroyBlocks(true);
channel.write(abilitiesPkt);
// send Player info
PlayerInfoPacket infoPkt = new PlayerInfoPacket();
infoPkt.setPlayerName(player.getName());
infoPkt.setOnline(true);
infoPkt.setPing(4);
channel.write(infoPkt);
// send Chunk allocation
ChunkAllocationPacket chInitPkt = new ChunkAllocationPacket();
chInitPkt.setX(0);
@@ -152,6 +147,16 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
channel.attr(ATTR_PLAYER).set(player);
player.setChannel(new WrapperNetChannel(channel));
playerManager.joinServer(player);
// send Player info
List<Player> players = playerManager.getPlayers();
players.forEach(pl -> {
PlayerInfoPacket infoPkt = new PlayerInfoPacket();
infoPkt.setPlayerName(pl.getName());
infoPkt.setOnline(true);
infoPkt.setPing(4);
playerManager.getBroadcastChannel().writeAndFlush(infoPkt);
});
}
public void onKickPacket(Channel channel, KickPacket packet) {