Первые намётки событийной модели
This commit is contained in:
@@ -12,4 +12,5 @@ ext {
|
||||
dependencies {
|
||||
/* Components */
|
||||
compile (group: 'commons-io', name: 'commons-io', version: '2.6')
|
||||
compile (group: 'com.google.guava', name: 'guava', version: '24.1-jre')
|
||||
}
|
||||
|
||||
@@ -24,15 +24,15 @@ public enum ChatStyle {
|
||||
YELLOW('e'),
|
||||
WHITE ('f');
|
||||
|
||||
private static final char COLOR_CHAR = '\u00a7'; // §
|
||||
public static final char SPECIAL_CHAR = '\u00a7'; // §
|
||||
private static final String codes = "0123456789aAbBcCdDeEfF";
|
||||
private static final Pattern EXCAPE_PATTERN = Pattern.compile(COLOR_CHAR + "[0-9a-f]", Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern EXCAPE_PATTERN = Pattern.compile(SPECIAL_CHAR + "[0-9a-f]", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
public static String format(char colorChar, String message) {
|
||||
char[] chars = message.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
if (chars[i] == colorChar && codes.indexOf(chars[i+1]) > -1) {
|
||||
chars[i] = COLOR_CHAR;
|
||||
chars[i] = SPECIAL_CHAR;
|
||||
chars[i+1] = Character.toLowerCase(chars[i+1]);
|
||||
i++;
|
||||
}
|
||||
@@ -48,7 +48,7 @@ public enum ChatStyle {
|
||||
private char[] toString;
|
||||
|
||||
ChatStyle(char ch) {
|
||||
toString = new char[]{ COLOR_CHAR, ch };
|
||||
toString = new char[]{SPECIAL_CHAR, ch };
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,5 +15,6 @@ public interface PlayerManager {
|
||||
void leftServer(Player player);
|
||||
Optional<Player> getPlayer(String name);
|
||||
List<Player> getPlayers();
|
||||
int getCountOnlinePlayers();
|
||||
NetChannel getBroadcastChannel();
|
||||
}
|
||||
|
||||
@@ -66,6 +66,11 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
return players.stream().filter(Player::isOnline).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCountOnlinePlayers() {
|
||||
return players.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetChannel getBroadcastChannel() {
|
||||
return new BroadcastNetChannel(players.stream().filter(Player::isOnline));
|
||||
|
||||
13
core/src/main/java/mc/core/events/Event.java
Normal file
13
core/src/main/java/mc/core/events/Event.java
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.events;
|
||||
|
||||
public interface Event {
|
||||
void setCanceled(boolean value);
|
||||
boolean isCanceled();
|
||||
|
||||
void setLastProcess(boolean value);
|
||||
boolean isLastProcess();
|
||||
}
|
||||
17
core/src/main/java/mc/core/events/EventBase.java
Normal file
17
core/src/main/java/mc/core/events/EventBase.java
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public abstract class EventBase implements Event {
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean canceled;
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean lastProcess;
|
||||
}
|
||||
14
core/src/main/java/mc/core/events/EventBusGetter.java
Normal file
14
core/src/main/java/mc/core/events/EventBusGetter.java
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.events;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
public final class EventBusGetter {
|
||||
public static final EventBus INSTANCE = new EventBus();
|
||||
|
||||
private EventBusGetter() {
|
||||
}
|
||||
}
|
||||
21
core/src/main/java/mc/core/events/LoginEvent.java
Normal file
21
core/src/main/java/mc/core/events/LoginEvent.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class LoginEvent extends EventBase {
|
||||
private String playerName;
|
||||
private final SocketAddress remoteAddress;
|
||||
private boolean deny;
|
||||
private String denyReason;
|
||||
}
|
||||
19
core/src/main/java/mc/core/events/PlayerLookEvent.java
Normal file
19
core/src/main/java/mc/core/events/PlayerLookEvent.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import mc.core.Look;
|
||||
import mc.core.Player;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class PlayerLookEvent extends EventBase {
|
||||
private final Player player;
|
||||
private Look newLook;
|
||||
}
|
||||
19
core/src/main/java/mc/core/events/PlayerPositionEvent.java
Normal file
19
core/src/main/java/mc/core/events/PlayerPositionEvent.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import mc.core.Location;
|
||||
import mc.core.Player;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class PlayerPositionEvent extends EventBase {
|
||||
private final Player player;
|
||||
private Location newPosition;
|
||||
}
|
||||
21
core/src/main/java/mc/core/events/ServerPingEvent.java
Normal file
21
core/src/main/java/mc/core/events/ServerPingEvent.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class ServerPingEvent extends EventBase {
|
||||
private final SocketAddress remoteAddress;
|
||||
private String description;
|
||||
private int online;
|
||||
private int maxOnline;
|
||||
}
|
||||
@@ -22,10 +22,6 @@ import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
public class KickPacket implements SCPacket, CSPacket {
|
||||
private String reason;
|
||||
|
||||
public void setPongMessage(String description, int online, int maxOnline) {
|
||||
reason = String.format("%s§%d§%d", description, online, maxOnline);
|
||||
}
|
||||
|
||||
public String getReason() {
|
||||
return (reason == null ? "" : reason);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-05-02
|
||||
*/
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.Config;
|
||||
import mc.core.Player;
|
||||
import mc.core.PlayerManager;
|
||||
import mc.core.events.LoginEvent;
|
||||
import mc.core.events.ServerPingEvent;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class EventListener {
|
||||
private final Config config;
|
||||
private final PlayerManager playerManager;
|
||||
|
||||
@Subscribe
|
||||
public void onServerPingEvent(ServerPingEvent event) {
|
||||
if (event.isLastProcess() || event.isCanceled()) return;
|
||||
|
||||
event.setDescription(config.getDescriptionServer());
|
||||
event.setOnline(playerManager.getCountOnlinePlayers());
|
||||
event.setMaxOnline(config.getMaxPlayers());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onLoginEvent(LoginEvent event) {
|
||||
if (event.isLastProcess()) return;
|
||||
|
||||
Optional<Player> optPlayer = playerManager.getPlayer(event.getPlayerName());
|
||||
|
||||
if (optPlayer.isPresent() && optPlayer.get().isOnline()) {
|
||||
event.setDeny(true);
|
||||
event.setDenyReason("Player is exists in server");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,11 +13,16 @@ import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.Config;
|
||||
import mc.core.PlayerManager;
|
||||
import mc.core.events.EventBusGetter;
|
||||
import mc.core.network.Server;
|
||||
import mc.core.network.StartServerException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@@ -31,6 +36,20 @@ public class NettyServer implements Server {
|
||||
@Setter
|
||||
private int workerGroupCount = 0;
|
||||
private EventLoopGroup bossGroup, workerGroup;
|
||||
private EventListener eventListener;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
eventListener = new EventListener(
|
||||
applicationContext.getBean(Config.class),
|
||||
applicationContext.getBean(PlayerManager.class));
|
||||
EventBusGetter.INSTANCE.register(eventListener);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destruct() {
|
||||
EventBusGetter.INSTANCE.unregister(eventListener);
|
||||
}
|
||||
|
||||
private ChannelInitializer buildChannelInitializer() {
|
||||
return new ChannelInitializer<SocketChannel>() {
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.util.AttributeKey;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.*;
|
||||
import mc.core.events.*;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
|
||||
import mc.core.network.proto_125.packets.*;
|
||||
@@ -61,33 +62,41 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
}
|
||||
}
|
||||
|
||||
public void onPingPacket(Channel channel, PingPacket packet) {
|
||||
private void onPingPacket(Channel channel, PingPacket packet) {
|
||||
ServerPingEvent event = new ServerPingEvent(channel.remoteAddress());
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (event.isCanceled()) {
|
||||
channel.disconnect();
|
||||
} else {
|
||||
String response = String.format("%s%s%d%s%d",
|
||||
event.getDescription(), ChatStyle.SPECIAL_CHAR,
|
||||
event.getOnline(), ChatStyle.SPECIAL_CHAR,
|
||||
event.getMaxOnline()
|
||||
);
|
||||
|
||||
KickPacket pkt = new KickPacket();
|
||||
pkt.setPongMessage(config.getDescriptionServer(), 0, config.getMaxPlayers());
|
||||
pkt.setReason(response);
|
||||
channel.writeAndFlush(pkt);
|
||||
}
|
||||
}
|
||||
|
||||
public void onHandshakePacket(Channel channel, HandshakePacket packet) {
|
||||
private void onHandshakePacket(Channel channel, HandshakePacket packet) {
|
||||
channel.writeAndFlush(packet);
|
||||
}
|
||||
|
||||
public void onLoginPacket(Channel channel, LoginPacket packet) {
|
||||
Player player;
|
||||
private void onLoginPacket(Channel channel, LoginPacket packet) {
|
||||
LoginEvent event = new LoginEvent(channel.remoteAddress());
|
||||
event.setPlayerName(packet.getPlayerName());
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
Optional<Player> optPlayer = playerManager.getPlayer(packet.getPlayerName());
|
||||
if (optPlayer.isPresent()) {
|
||||
player = optPlayer.get();
|
||||
|
||||
if (player.isOnline()) {
|
||||
channel.writeAndFlush(new KickPacket("Player is exists in server"))
|
||||
if (event.isDeny()) {
|
||||
channel.writeAndFlush(new KickPacket(event.getDenyReason()))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
player = playerManager.createPlayer(packet.getPlayerName());
|
||||
Player player = playerManager.createPlayer(packet.getPlayerName());
|
||||
player.setLocation(world.getSpawn());
|
||||
player.setLook(new Look(0f, 0f));
|
||||
}
|
||||
|
||||
// Response login
|
||||
packet.setPlayerId(player.getId());
|
||||
@@ -149,33 +158,49 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
player.setChannel(new WrapperNetChannel(channel));
|
||||
playerManager.joinServer(player);
|
||||
}
|
||||
}
|
||||
|
||||
public void onKickPacket(Channel channel, KickPacket packet) {
|
||||
private void onKickPacket(Channel channel, KickPacket packet) {
|
||||
if (packet.getReason().equals("Quitting")) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void onPlayerPositionPacket(Channel channel, PlayerPositionPacket packet) {
|
||||
private void onPlayerPositionPacket(Channel channel, PlayerPositionPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
player.getLocation().setX(packet.getX());
|
||||
player.getLocation().setY(packet.getY());
|
||||
player.getLocation().setZ(packet.getZ());
|
||||
PlayerPositionEvent event = new PlayerPositionEvent(player);
|
||||
event.setNewPosition(new Location(packet.getX(), packet.getY(), packet.getZ()));
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (!event.isCanceled()) {
|
||||
player.getLocation().setX(event.getNewPosition().getX());
|
||||
player.getLocation().setY(event.getNewPosition().getY());
|
||||
player.getLocation().setZ(event.getNewPosition().getZ());
|
||||
|
||||
//TODO если позиция была изменена, нужно оповестить клиент
|
||||
}
|
||||
}
|
||||
|
||||
public void onPlayerLookPacket(Channel channel, PlayerLookPacket packet) {
|
||||
private void onPlayerLookPacket(Channel channel, PlayerLookPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
player.getLook().setYaw(packet.getYaw());
|
||||
player.getLook().setPitch(packet.getPitch());
|
||||
PlayerLookEvent event = new PlayerLookEvent(player);
|
||||
event.setNewLook(new Look(packet.getYaw(), packet.getPitch()));
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (!event.isCanceled()) {
|
||||
player.getLook().setYaw(event.getNewLook().getYaw());
|
||||
player.getLook().setPitch(event.getNewLook().getPitch());
|
||||
|
||||
//TODO если обзор был изменен, нужно оповестить клиент
|
||||
}
|
||||
}
|
||||
|
||||
public void onPlayerAbilitiesPacket(Channel channel, PlayerAbilitiesPacket packet) {
|
||||
log.debug("Player new sets: {}", packet.toString());
|
||||
private void onPlayerAbilitiesPacket(Channel channel, PlayerAbilitiesPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
player.setFlying(packet.isFlying());
|
||||
}
|
||||
|
||||
public void onChatMessagePacket(Channel channel, ChatMessagePacket packet) {
|
||||
private void onChatMessagePacket(Channel channel, ChatMessagePacket packet) {
|
||||
log.info(CHAT_MARKER, "<{}>: {}", channel.attr(ATTR_PLAYER).get().getName(), ChatStyle.escapeStyle(packet.getMessage()));
|
||||
playerManager.getBroadcastChannel().writeAndFlush(packet);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user