import source
This commit is contained in:
29
proto125_netty/README.MD
Normal file
29
proto125_netty/README.MD
Normal file
@@ -0,0 +1,29 @@
|
||||
# Protocol 1.2.5 (Netty impl.)
|
||||
|
||||
Реализация протокола "1.2.5" на сетевом движке Netty.
|
||||
|
||||
## Spring beans
|
||||
|
||||
### NettyServer
|
||||
|
||||
Bean:
|
||||
|
||||
```xml
|
||||
<bean id="pipeline.decoder" class="mc.core.network.proto_125.netty.PacketDecoder" scope="prototype"/>
|
||||
<bean id="pipeline.encoder" class="mc.core.network.proto_125.netty.PacketEncoder" scope="prototype"/>
|
||||
<bean id="pipeline.handler" class="mc.core.network.proto_125.netty.PacketHandler" scope="prototype"/>
|
||||
|
||||
<bean id="server" class="mc.core.network.proto_125.netty.NettyServer">
|
||||
<property name="host" value="127.0.0.1"/>
|
||||
<property name="port" value="25565"/>
|
||||
<property name="workerGroupCount" value="2"/>
|
||||
</bean>
|
||||
```
|
||||
|
||||
`workerGroupCount` - максимальное количество потоков для обработки соединений
|
||||
|
||||
Для логирования содержимого пакетов, можно добавить следующий bean:
|
||||
|
||||
```xml
|
||||
<bean id="pipeline.log" class="io.netty.handler.logging.LoggingHandler" scope="prototype"/>
|
||||
```
|
||||
14
proto125_netty/build.gradle
Normal file
14
proto125_netty/build.gradle
Normal file
@@ -0,0 +1,14 @@
|
||||
group 'mc'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
ext {
|
||||
netty_version = '4.1.22.Final'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
/* Protocol 1.2.5 */
|
||||
compile_excludeCopy project(':proto125')
|
||||
|
||||
/* Netty */
|
||||
compile (group: 'io.netty', name: 'netty-all', version: netty_version)
|
||||
}
|
||||
@@ -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.Player;
|
||||
import mc.core.player.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
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.player.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.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class NettyServer implements Server {
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
@Setter
|
||||
private String host;
|
||||
@Setter
|
||||
private int port;
|
||||
@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>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
Map<String, ChannelHandler> beans = applicationContext.getBeansOfType(ChannelHandler.class);
|
||||
beans.entrySet().stream()
|
||||
.sorted((e1, e2) -> e1.getKey().compareToIgnoreCase(e2.getKey()))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
|
||||
.forEach(socketChannel.pipeline()::addLast);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private ServerBootstrap buildServerBootstrap() {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
|
||||
bootstrap.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(buildChannelInitializer());
|
||||
|
||||
return bootstrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws StartServerException {
|
||||
log.info("Use protocol 1.2.5");
|
||||
bossGroup = new NioEventLoopGroup(1);
|
||||
workerGroup = new NioEventLoopGroup(workerGroupCount);
|
||||
|
||||
ServerBootstrap serverBootstrap = buildServerBootstrap();
|
||||
|
||||
log.info("Start server: {}:{}", host, port);
|
||||
try {
|
||||
serverBootstrap.bind(host, port).sync().channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
throw new StartServerException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
log.info("Server shutdown");
|
||||
workerGroup.shutdownGracefully();
|
||||
bossGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-03-25
|
||||
*/
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.proto_125.netty.wrappers.WrapperNetStream;
|
||||
import mc.core.network.proto_125.packets.PacketManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class PacketDecoder extends ByteToMessageDecoder {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
log.debug("ByteBuf readableBytes: {}", in.readableBytes());
|
||||
int id = in.readUnsignedByte();
|
||||
log.debug("Pkt-Id: {} / 0x{}", id, Integer.toHexString(id).toUpperCase());
|
||||
|
||||
Class<? extends CSPacket> packetClass = PacketManager.getClientSidePacket(id);
|
||||
if (packetClass != null) {
|
||||
NetStream netStream = new WrapperNetStream(in);
|
||||
CSPacket packet = packetClass.newInstance();
|
||||
packet.readSelf(netStream);
|
||||
|
||||
out.add(packet);
|
||||
log.debug("{}: {}", packet.getClass().getSimpleName(), packet.toString());
|
||||
} else {
|
||||
log.debug("Unknown packet");
|
||||
}
|
||||
|
||||
if (in.readableBytes() > 0)
|
||||
in.skipBytes(in.readableBytes());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.packets.PacketManager;
|
||||
|
||||
@Slf4j
|
||||
public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, SCPacket pkt, ByteBuf out) throws Exception {
|
||||
log.debug("{}: {}", pkt.getClass().getSimpleName(), pkt.toString());
|
||||
Integer id = PacketManager.getServirSidePacket(pkt.getClass());
|
||||
if (id == null) {
|
||||
log.warn("Not defined ID packet \"{}\"", pkt.getClass().getSimpleName());
|
||||
return;
|
||||
}
|
||||
byte[] bytes = pkt.toByteArray();
|
||||
|
||||
out.writeByte(id);
|
||||
out.writeBytes(bytes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.util.AttributeKey;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.*;
|
||||
import mc.core.chat.ChatProcessor;
|
||||
import mc.core.chat.ChatStyle;
|
||||
import mc.core.events.*;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
|
||||
import mc.core.network.proto_125.packets.*;
|
||||
import mc.core.player.Look;
|
||||
import mc.core.player.Player;
|
||||
import mc.core.player.PlayerManager;
|
||||
import mc.core.player.PlayerMode;
|
||||
import mc.core.world.World;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Slf4j
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
private static final AttributeKey<Player> ATTR_PLAYER = AttributeKey.newInstance("ATTR_PLAYER");
|
||||
@Autowired
|
||||
private Config config;
|
||||
@Autowired
|
||||
private PlayerManager playerManager;
|
||||
@Autowired
|
||||
private World world;
|
||||
@Autowired
|
||||
private ChatProcessor chatProcessor;
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext context) throws Exception {
|
||||
super.channelInactive(context);
|
||||
Player player = context.channel().attr(ATTR_PLAYER).get();
|
||||
if (player != null) {
|
||||
playerManager.leftServer(player);
|
||||
player.setChannel(null);
|
||||
playerManager.getBroadcastChannel().writeAndFlush(new DestroyEntityPacket(player.getId()));
|
||||
}
|
||||
context.channel().attr(ATTR_PLAYER).set(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception {
|
||||
Optional<Method> optionalMethod = Arrays.stream(this.getClass().getDeclaredMethods())
|
||||
.filter(method -> method.getName().equals("on" + packet.getClass().getSimpleName())
|
||||
&& method.getParameterCount() == 2
|
||||
&& method.getParameterTypes()[0].isAssignableFrom(Channel.class)
|
||||
&& method.getParameterTypes()[1].isAssignableFrom(packet.getClass()))
|
||||
.findFirst();
|
||||
|
||||
if (optionalMethod.isPresent()) {
|
||||
Method method = optionalMethod.get();
|
||||
method.invoke(this, ctx.channel(), 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.setReason(response);
|
||||
channel.writeAndFlush(pkt);
|
||||
}
|
||||
}
|
||||
|
||||
private void onHandshakePacket(Channel channel, HandshakePacket packet) {
|
||||
channel.writeAndFlush(packet);
|
||||
}
|
||||
|
||||
private void onLoginPacket(Channel channel, LoginPacket packet) {
|
||||
LoginEvent event = new LoginEvent(channel.remoteAddress());
|
||||
event.setPlayerName(packet.getPlayerName());
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (event.isDeny()) {
|
||||
channel.writeAndFlush(new KickPacket(event.getDenyReason()))
|
||||
.addListener(ChannelFutureListener.CLOSE);
|
||||
} else {
|
||||
Player player = playerManager.getPlayer(packet.getPlayerName())
|
||||
.orElseGet(() -> playerManager.createPlayer(
|
||||
packet.getPlayerName(),
|
||||
world.getSpawn(),
|
||||
new Look(0f, 0f)));
|
||||
|
||||
// Response login
|
||||
packet.setPlayerId(player.getId());
|
||||
packet.setLevelType("flat");
|
||||
packet.setDefaultPlayerMode(PlayerMode.CREATIVE);
|
||||
packet.setDimension(0/*Overworld*/);
|
||||
packet.setDifficulty(0/*Peaceful*/);
|
||||
packet.setMaxPlayers(config.getMaxPlayers());
|
||||
channel.write(packet);
|
||||
|
||||
// send Spawn position
|
||||
SpawnPositionPacket spawnPkt = new SpawnPositionPacket();
|
||||
spawnPkt.setLocation(world.getSpawn());
|
||||
channel.write(spawnPkt);
|
||||
|
||||
// send Player abilities
|
||||
PlayerAbilitiesPacket abilitiesPkt = new PlayerAbilitiesPacket();
|
||||
abilitiesPkt.setCanFly(true);
|
||||
abilitiesPkt.setFlying(true);
|
||||
abilitiesPkt.setGodMode(true);
|
||||
abilitiesPkt.setInstantDestroyBlocks(true);
|
||||
channel.write(abilitiesPkt);
|
||||
|
||||
// send Chunk allocation
|
||||
ChunkAllocationPacket chInitPkt = new ChunkAllocationPacket();
|
||||
chInitPkt.setX(0);
|
||||
chInitPkt.setZ(0);
|
||||
chInitPkt.setInitChunk(true);
|
||||
channel.write(chInitPkt);
|
||||
|
||||
// send Chunk data
|
||||
ChunkDataPacket chDataPkt = new ChunkDataPacket();
|
||||
chDataPkt.setX(0);
|
||||
chDataPkt.setZ(0);
|
||||
chDataPkt.setChunk(world.getChunk(0, 0));
|
||||
chDataPkt.setNeedInitChunk(true);
|
||||
chDataPkt.setYMin(1);
|
||||
chDataPkt.setYMax(0);
|
||||
channel.write(chDataPkt);
|
||||
|
||||
// send Position and look
|
||||
PositionAndLookPacket posLookPkt = new PositionAndLookPacket();
|
||||
posLookPkt.setLocation(player.getLocation());
|
||||
posLookPkt.setStance(player.getLocation().getY() + 1.64d);
|
||||
posLookPkt.setLook(player.getLook());
|
||||
posLookPkt.setOnGround(false);
|
||||
channel.write(posLookPkt);
|
||||
channel.flush();
|
||||
|
||||
// send Spawn named entity
|
||||
SpawnNamedEntityPacket spawnPlayer = new SpawnNamedEntityPacket();
|
||||
spawnPlayer.setId(player.getId());
|
||||
spawnPlayer.setEntityName(player.getName());
|
||||
spawnPlayer.setPosition(player.getLocation());
|
||||
spawnPlayer.setLook(player.getLook());
|
||||
playerManager.getBroadcastChannel().writeAndFlush(spawnPlayer);
|
||||
|
||||
// send Spawn named entity (another players)
|
||||
List<Player> players = playerManager.getPlayers();
|
||||
players.forEach(pl -> {
|
||||
SpawnNamedEntityPacket spawnAnotherPlayer = new SpawnNamedEntityPacket();
|
||||
spawnAnotherPlayer.setId(pl.getId());
|
||||
spawnAnotherPlayer.setEntityName(pl.getName());
|
||||
spawnAnotherPlayer.setPosition(pl.getLocation());
|
||||
spawnAnotherPlayer.setLook(pl.getLook());
|
||||
channel.write(spawnAnotherPlayer);
|
||||
});
|
||||
channel.flush();
|
||||
|
||||
// join server
|
||||
channel.attr(ATTR_PLAYER).set(player);
|
||||
player.setChannel(new WrapperNetChannel(channel));
|
||||
playerManager.joinServer(player);
|
||||
|
||||
// send Player info
|
||||
players.forEach(pl -> {
|
||||
PlayerInfoPacket infoPkt = new PlayerInfoPacket();
|
||||
infoPkt.setPlayerName(pl.getName());
|
||||
infoPkt.setOnline(true);
|
||||
infoPkt.setPing(4);
|
||||
playerManager.getBroadcastChannel().writeAndFlush(infoPkt);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void onKickPacket(Channel channel, KickPacket packet) {
|
||||
if (packet.getReason().equals("Quitting")) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
private void onPlayerPositionPacket(Channel channel, PlayerPositionPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
PlayerPositionEvent event = new PlayerPositionEvent(player);
|
||||
event.setNewPosition(new Location(packet.getX(), packet.getY(), packet.getZ()));
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (!event.isCanceled()) {
|
||||
Location diffLoc = event.getNewPosition().diff(player.getLocation());
|
||||
player.getLocation().set(event.getNewPosition());
|
||||
|
||||
//TODO если позиция была изменена, нужно оповестить клиент
|
||||
|
||||
final SCPacket pkt;
|
||||
if ((diffLoc.getBlockX() >= 4 || diffLoc.getBlockX() <= -4)
|
||||
|| (diffLoc.getBlockY() >= 4 || diffLoc.getBlockY() <= -4)
|
||||
|| (diffLoc.getBlockZ() >= 4 || diffLoc.getBlockZ() <= -4)) {
|
||||
pkt = new EntityTeleportPacket(player.getId(), player.getLocation(), player.getLook());
|
||||
} else {
|
||||
pkt = new EntityRelativeMovePacket(player.getId(), diffLoc);
|
||||
}
|
||||
playerManager.getPlayers().stream()
|
||||
.filter(pl -> pl.getId() != player.getId())
|
||||
.forEach(pl -> pl.getChannel().writeAndFlush(pkt));
|
||||
}
|
||||
}
|
||||
|
||||
private void onPlayerLookPacket(Channel channel, PlayerLookPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
PlayerLookEvent event = new PlayerLookEvent(player);
|
||||
event.setNewLook(new Look(packet.getYaw(), packet.getPitch()));
|
||||
EventBusGetter.INSTANCE.post(event);
|
||||
|
||||
if (!event.isCanceled()) {
|
||||
player.getLook().set(event.getNewLook());
|
||||
|
||||
//TODO если обзор был изменен, нужно оповестить клиент
|
||||
|
||||
final SCPacket pkt1 = new EntityLookPacket(player.getId(), player.getLook());
|
||||
final SCPacket pkt2 = new EntityLookHeadPacket(player.getId(), player.getLook().getYaw());
|
||||
playerManager.getPlayers().stream()
|
||||
.filter(pl -> pl.getId() != player.getId())
|
||||
.forEach(pl -> {
|
||||
pl.getChannel().write(pkt1);
|
||||
pl.getChannel().write(pkt2);
|
||||
pl.getChannel().flush();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void onPositionAndLookPacket(Channel channel, PositionAndLookPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
|
||||
Location diffLoc = packet.getLocation().diff(player.getLocation());
|
||||
player.getLocation().set(packet.getLocation());
|
||||
player.getLook().set(packet.getLook());
|
||||
|
||||
Stream<Player> stream = playerManager.getPlayers().stream()
|
||||
.filter(pl -> pl.getId() != player.getId());
|
||||
|
||||
if ((diffLoc.getBlockX() >= 4 || diffLoc.getBlockX() <= -4)
|
||||
|| (diffLoc.getBlockY() >= 4 || diffLoc.getBlockY() <= -4)
|
||||
|| (diffLoc.getBlockZ() >= 4 || diffLoc.getBlockZ() <= -4)) {
|
||||
final SCPacket pkt = new EntityTeleportPacket(player.getId(), player.getLocation(), player.getLook());
|
||||
stream.forEach(pl -> pl.getChannel().writeAndFlush(pkt));
|
||||
} else {
|
||||
final SCPacket pkt1 = new EntityLookRelativeMovePacket(player.getId(), diffLoc, player.getLook());
|
||||
final SCPacket pkt2 = new EntityLookHeadPacket(player.getId(), player.getLook().getYaw());
|
||||
stream.forEach(pl -> {
|
||||
pl.getChannel().write(pkt1);
|
||||
pl.getChannel().write(pkt2);
|
||||
pl.getChannel().flush();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void onPlayerAbilitiesPacket(Channel channel, PlayerAbilitiesPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
player.setFlying(packet.isFlying());
|
||||
}
|
||||
|
||||
private void onChatMessagePacket(Channel channel, ChatMessagePacket packet) {
|
||||
chatProcessor.process(
|
||||
channel.attr(ATTR_PLAYER).get(),
|
||||
packet.getMessage()
|
||||
);
|
||||
}
|
||||
|
||||
private void onAnimationPacket(Channel channel, AnimationPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
playerManager.getPlayers().stream().filter(pl -> !pl.equals(player)).forEach(pl -> {
|
||||
pl.getChannel().writeAndFlush(packet);
|
||||
});
|
||||
}
|
||||
|
||||
private void onUseEntityPacket(Channel channel, UseEntityPacket packet) {
|
||||
Optional<Player> optPlayer = playerManager.getPlayerById(packet.getPlayerId());
|
||||
if (!optPlayer.isPresent()) {
|
||||
log.debug("Player id {} not found");
|
||||
return;
|
||||
}
|
||||
Player player = optPlayer.get();
|
||||
|
||||
optPlayer = playerManager.getPlayerById(packet.getTargetId());
|
||||
if (!optPlayer.isPresent()) {
|
||||
log.debug("Target id {} not found");
|
||||
return;
|
||||
}
|
||||
Player target = optPlayer.get();
|
||||
|
||||
log.info("<{}> {} clicked <{}>", player.getName(), (packet.isLeftMouseButton() ? "left" : "right"), target.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-13
|
||||
*/
|
||||
package mc.core.network.proto_125.netty.wrappers;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.network.NetChannel;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.packets.ChatMessagePacket;
|
||||
import mc.core.network.proto_125.packets.KeepAlivePacket;
|
||||
import mc.core.network.proto_125.packets.TimeUpdatePacket;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class WrapperNetChannel implements NetChannel {
|
||||
private final Channel channel;
|
||||
|
||||
@Override
|
||||
public void sendKeepAlive() {
|
||||
channel.writeAndFlush(new KeepAlivePacket());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTimeUpdate(long value) {
|
||||
channel.writeAndFlush(new TimeUpdatePacket(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendChatMessage(String message) {
|
||||
channel.writeAndFlush(new ChatMessagePacket(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeAndFlush(SCPacket pkt) {
|
||||
channel.writeAndFlush(pkt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(SCPacket pkt) {
|
||||
channel.write(pkt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
channel.flush();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.network.proto_125.netty.wrappers;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.network.proto_125.NetStream_p125;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class WrapperNetStream extends NetStream_p125 {
|
||||
private final ByteBuf byteBuf;
|
||||
|
||||
@Override
|
||||
public boolean readBoolean() {
|
||||
return byteBuf.readBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte readByte() {
|
||||
return byteBuf.readByte();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] buffer) {
|
||||
byteBuf.readBytes(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedByte() {
|
||||
return byteBuf.readUnsignedByte();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedShort() {
|
||||
return byteBuf.readUnsignedShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public short readShort() {
|
||||
return byteBuf.readShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readInt() {
|
||||
return byteBuf.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float readFloat() {
|
||||
return byteBuf.readFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDouble() {
|
||||
return byteBuf.readDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBoolean(boolean value) {
|
||||
byteBuf.writeBoolean(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(int value) {
|
||||
byteBuf.writeByte(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(byte[] buffer) {
|
||||
byteBuf.writeBytes(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeShort(int value) {
|
||||
byteBuf.writeShort(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
byteBuf.writeInt(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
byteBuf.writeLong(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeFloat(float value) {
|
||||
byteBuf.writeFloat(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDouble(double value) {
|
||||
byteBuf.writeDouble(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skipBytes(int count) {
|
||||
byteBuf.skipBytes(count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user