proto-125 as module
This commit is contained in:
18
proto125/build.gradle
Normal file
18
proto125/build.gradle
Normal file
@@ -0,0 +1,18 @@
|
||||
group 'mc'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
ext {
|
||||
netty_version = '4.1.22.Final'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
/* Core */
|
||||
compile_excludeCopy rootProject
|
||||
|
||||
/* Netty */
|
||||
compile (group: 'io.netty', name: 'netty-all', version: netty_version)
|
||||
|
||||
/* Components */
|
||||
compile (group: 'com.google.guava', name: 'guava', version: '24.1-jre')
|
||||
compile (group: 'com.google.code.gson', name: 'gson', version: '2.8.2')
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.network.proto_125;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Slf4j
|
||||
public class ByteArrayOutputNetStream extends NetStream_p125 {
|
||||
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
@Override
|
||||
public boolean readBoolean() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte readByte() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] buffer) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedByte() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedShort() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public short readShort() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readInt() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float readFloat() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDouble() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBoolean(boolean value) {
|
||||
baos.write(value ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeByte(int value) {
|
||||
baos.write(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(byte[] buffer) {
|
||||
baos.write(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeShort(int value) {
|
||||
baos.write((byte) value >>> 8);
|
||||
baos.write((byte) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeInt(int value) {
|
||||
baos.write((byte)((int)(value >>> 24)));
|
||||
baos.write((byte)((int)(value >>> 16)));
|
||||
baos.write((byte)((int)(value >>> 8)));
|
||||
baos.write((byte)((int)(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
baos.write((byte)((int)(value >>> 56)));
|
||||
baos.write((byte)((int)(value >>> 48)));
|
||||
baos.write((byte)((int)(value >>> 40)));
|
||||
baos.write((byte)((int)(value >>> 32)));
|
||||
baos.write((byte)((int)(value >>> 24)));
|
||||
baos.write((byte)((int)(value >>> 16)));
|
||||
baos.write((byte)((int)(value >>> 8)));
|
||||
baos.write((byte)((int)(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeFloat(float value) {
|
||||
writeInt(Float.floatToIntBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeDouble(double value) {
|
||||
writeLong(Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skipBytes(int count) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-13
|
||||
*/
|
||||
package mc.core.network.proto_125;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Slf4j
|
||||
public abstract class NetStream_p125 extends NetStream {
|
||||
@Override
|
||||
public String readString() {
|
||||
int size = readShort() * 2;
|
||||
if (size == 0) {
|
||||
log.warn("String zero length??");
|
||||
return "";
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[size];
|
||||
readBytes(bytes);
|
||||
return new String(bytes, StandardCharsets.UTF_16BE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeString(String value) {
|
||||
if (value.length() > Short.MAX_VALUE) {
|
||||
log.warn("String \"{}\" too long!", value);
|
||||
byte[] buf = value.substring(0, Short.MAX_VALUE).getBytes(StandardCharsets.UTF_16BE);
|
||||
writeShort(Short.MAX_VALUE);
|
||||
writeBytes(buf);
|
||||
} else {
|
||||
byte[] buf = value.getBytes(StandardCharsets.UTF_16BE);
|
||||
writeShort(value.length());
|
||||
writeBytes(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-15
|
||||
*/
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.Location;
|
||||
import mc.core.Look;
|
||||
import mc.core.Player;
|
||||
import mc.core.network.NetChannel;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class NettyPlayer implements Player {
|
||||
private int id;
|
||||
private String name;
|
||||
private boolean online;
|
||||
private NetChannel channel;
|
||||
private Location location;
|
||||
private Look look;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.ChannelPipeline;
|
||||
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 io.netty.handler.logging.LoggingHandler;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.Server;
|
||||
import mc.core.network.StartServerException;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@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 ChannelInitializer buildChannelInitializer() {
|
||||
return new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
Map<String, ChannelHandler> beans = applicationContext.getBeansOfType(ChannelHandler.class);
|
||||
beans.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,190 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
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.network.CSPacket;
|
||||
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
|
||||
import mc.core.network.proto_125.packets.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@Slf4j
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
private static final Random random = new Random();
|
||||
private static final AttributeKey<Player> ATTR_PLAYER = AttributeKey.newInstance("ATTR_PLAYER");
|
||||
@Autowired
|
||||
private Config config;
|
||||
@Autowired
|
||||
private PlayerManager playerManager;
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext context) throws Exception {
|
||||
super.channelInactive(context);
|
||||
Player player = context.channel().attr(ATTR_PLAYER).get();
|
||||
if (player != null) {
|
||||
playerManager.removePlayer(player);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public void onPingPacket(Channel channel, PingPacket packet) {
|
||||
KickPacket pkt = new KickPacket();
|
||||
pkt.setPongMessage(config.getDescriptionServer(), 0, config.getMaxPlayers());
|
||||
channel.writeAndFlush(pkt);
|
||||
}
|
||||
|
||||
public void onHandshakePacket(Channel channel, HandshakePacket packet) {
|
||||
channel.writeAndFlush(packet);
|
||||
}
|
||||
|
||||
public void onLoginPacket(Channel channel, LoginPacket packet) {
|
||||
final Location spawnLoc = new Location(0, 65, 0);
|
||||
Player player;
|
||||
|
||||
Optional<Player> optPlayer = playerManager.getPlayer(packet.getPlayerName());
|
||||
if (optPlayer.isPresent()) {
|
||||
player = optPlayer.get();
|
||||
} else {
|
||||
int pId = random.nextInt(9999);
|
||||
|
||||
player = new NettyPlayer();
|
||||
player.setId(pId);
|
||||
player.setName(packet.getPlayerName());
|
||||
player.setLocation(spawnLoc);
|
||||
player.setLook(new Look(0f, 0f));
|
||||
player.setOnline(true);
|
||||
playerManager.addPlayer(player);
|
||||
}
|
||||
|
||||
// Response login
|
||||
packet.setPlayerId(player.getId());
|
||||
packet.setLevelType("flat");
|
||||
packet.setServerMode(1/*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(spawnLoc);
|
||||
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 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);
|
||||
chInitPkt.setZ(0);
|
||||
chInitPkt.setInitChunk(true);
|
||||
channel.write(chInitPkt);
|
||||
|
||||
for (int x = -17; x <= 17; x++) {
|
||||
for (int z = -17; z <= 17; z++) {
|
||||
if (x == 0 && z == 0) continue;
|
||||
|
||||
chInitPkt = new ChunkAllocationPacket();
|
||||
chInitPkt.setX(x);
|
||||
chInitPkt.setZ(z);
|
||||
chInitPkt.setInitChunk(true);
|
||||
channel.write(chInitPkt);
|
||||
}
|
||||
}
|
||||
|
||||
// send Chunk data
|
||||
ChunkDataPacket chDataPkt = new ChunkDataPacket();
|
||||
chDataPkt.setX(0);
|
||||
chDataPkt.setZ(0);
|
||||
chDataPkt.setNeedInitChunk(false);
|
||||
chDataPkt.setYMin(0);
|
||||
chDataPkt.setYMax(0);
|
||||
channel.write(chDataPkt);
|
||||
|
||||
for (int x = -17; x <= 17; x++) {
|
||||
for (int z = -17; z <= 17; z++) {
|
||||
if (x == 0 && z == 0) continue;
|
||||
|
||||
chDataPkt = new ChunkDataPacket();
|
||||
chDataPkt.setX(x);
|
||||
chDataPkt.setZ(z);
|
||||
chDataPkt.setNeedInitChunk(false);
|
||||
chDataPkt.setYMin(0);
|
||||
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.attr(ATTR_PLAYER).set(player);
|
||||
player.setChannel(new WrapperNetChannel(channel));
|
||||
player.setOnline(true);
|
||||
channel.flush();
|
||||
}
|
||||
|
||||
public void onKickPacket(Channel channel, KickPacket packet) {
|
||||
if (packet.getReason().equals("Quitting")) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public 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());
|
||||
}
|
||||
|
||||
public void onPlayerLookPacket(Channel channel, PlayerLookPacket packet) {
|
||||
Player player = channel.attr(ATTR_PLAYER).get();
|
||||
player.getLook().setYaw(packet.getYaw());
|
||||
player.getLook().setPitch(packet.getPitch());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.KeepAlivePacket;
|
||||
import mc.core.network.proto_125.packets.PingPacket;
|
||||
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 writeAndFlush(SCPacket pkt) {
|
||||
channel.writeAndFlush(pkt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-08
|
||||
*/
|
||||
package mc.core.network.proto_125.netty.wrappers;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.proto_125.NetStream_p125;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-20
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@Setter
|
||||
@ToString
|
||||
public class ChunkAllocationPacket implements SCPacket {
|
||||
private int x, z;
|
||||
private boolean initChunk;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(x);
|
||||
netStream.writeInt(z);
|
||||
netStream.writeBoolean(initChunk);
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-20
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
@Setter
|
||||
@ToString
|
||||
public class ChunkDataPacket implements SCPacket {
|
||||
private static final byte[] data = new byte[65536 + 32768 + 32768 + 32768/* + 0*/ + 256];
|
||||
private static byte[] compressData = null;
|
||||
|
||||
private int x, z;
|
||||
private boolean needInitChunk;
|
||||
private int yMin,yMax;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
if (compressData == null) {
|
||||
Deflater zlib = new Deflater();
|
||||
zlib.setInput(data);
|
||||
byte[] preCompress = new byte[data.length];
|
||||
int len = zlib.deflate(preCompress);
|
||||
|
||||
compressData = new byte[len];
|
||||
System.arraycopy(preCompress, 0, compressData, 0, len);
|
||||
}
|
||||
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(x);
|
||||
netStream.writeInt(z);
|
||||
netStream.writeBoolean(needInitChunk);
|
||||
netStream.writeShort(yMin);
|
||||
netStream.writeShort(yMax);
|
||||
netStream.writeInt(compressData.length);
|
||||
netStream.writeInt(0);
|
||||
netStream.writeBytes(compressData);
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class HandshakePacket implements CSPacket, SCPacket {
|
||||
private String playerName;
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
String[] str = netStream.readString().split(";");
|
||||
|
||||
playerName = str[0];
|
||||
if (str[1].contains(":")) {
|
||||
str = str[1].split(":");
|
||||
host = str[0];
|
||||
port = Integer.parseInt(str[1]);
|
||||
} else {
|
||||
host = str[1];
|
||||
port = 25565;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeString("-");
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-21
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class KeepAlivePacket implements SCPacket, CSPacket {
|
||||
private static final Random rand = new Random();
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeInt(rand.nextInt(Integer.MAX_VALUE));
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@Slf4j
|
||||
@Setter
|
||||
@ToString
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
try {
|
||||
reason = netStream.readString();
|
||||
} catch (NegativeArraySizeException e) {
|
||||
log.warn("Invalid packet");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeString(reason);
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@ToString
|
||||
public class LoginPacket implements CSPacket, SCPacket {
|
||||
@Getter
|
||||
private int protocol;
|
||||
@Getter
|
||||
private String playerName;
|
||||
|
||||
@Setter
|
||||
private int playerId;
|
||||
@Setter
|
||||
private String levelType;
|
||||
@Setter
|
||||
private int serverMode;
|
||||
@Setter
|
||||
private int dimension;
|
||||
@Setter
|
||||
private int difficulty;
|
||||
@Setter
|
||||
private int maxPlayers;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
protocol = netStream.readInt();
|
||||
playerName = netStream.readString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt(playerId);
|
||||
netStream.writeString("");
|
||||
netStream.writeString(levelType);
|
||||
netStream.writeInt(serverMode);
|
||||
netStream.writeInt(dimension);
|
||||
netStream.writeByte(difficulty);
|
||||
netStream.writeByte(0);
|
||||
netStream.writeByte(maxPlayers);
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.packets.*;
|
||||
|
||||
public class PacketManager {
|
||||
private static final BiMap<Integer, Class<?>> packetMap = ImmutableBiMap.<Integer, Class<?>>builder()
|
||||
.put(0x00, KeepAlivePacket.class)
|
||||
.put(0x01, LoginPacket.class)
|
||||
.put(0x02, HandshakePacket.class)
|
||||
.put(0x04, TimeUpdatePacket.class)
|
||||
.put(0x06, SpawnPositionPacket.class)
|
||||
.put(0x0B, PlayerPositionPacket.class)
|
||||
.put(0x0C, PlayerLookPacket.class)
|
||||
.put(0x0D, PositionAndLookPacket.class)
|
||||
.put(0x32, ChunkAllocationPacket.class)
|
||||
.put(0x33, ChunkDataPacket.class)
|
||||
.put(0xC9, PlayerInfoPacket.class)
|
||||
.put(0xCA, PlayerAbilitiesPacket.class)
|
||||
.put(0xFE, PingPacket.class)
|
||||
.put(0xFF, KickPacket.class)
|
||||
.build();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Class<? extends CSPacket> getClientSidePacket(int id) {
|
||||
return (Class<? extends CSPacket>) packetMap.get(id);
|
||||
}
|
||||
|
||||
public static Integer getServirSidePacket(Class<? extends SCPacket> clazz) {
|
||||
return packetMap.inverse().get(clazz);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
@ToString
|
||||
public class PingPacket implements CSPacket {
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-19
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@Setter
|
||||
@ToString
|
||||
public class PlayerAbilitiesPacket implements SCPacket {
|
||||
private boolean godMode = false;
|
||||
private boolean flying = false;
|
||||
private boolean canFly = false;
|
||||
private boolean instantDestroyBlocks = false;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeBoolean(godMode);
|
||||
netStream.writeBoolean(flying);
|
||||
netStream.writeBoolean(canFly);
|
||||
netStream.writeBoolean(instantDestroyBlocks);
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-19
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@Setter
|
||||
@ToString
|
||||
public class PlayerInfoPacket implements SCPacket {
|
||||
private String playerName;
|
||||
private boolean online;
|
||||
private int ping;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeString(playerName);
|
||||
netStream.writeBoolean(online);
|
||||
netStream.writeShort(ping);
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-22
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class PlayerLookPacket implements CSPacket {
|
||||
private float yaw, pitch;
|
||||
private boolean onGround;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
yaw = netStream.readFloat();
|
||||
pitch = netStream.readFloat();
|
||||
onGround = netStream.readBoolean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-22
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class PlayerPositionPacket implements CSPacket {
|
||||
private double x, y, z;
|
||||
private double stance;
|
||||
private boolean onGround;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
this.x = netStream.readDouble();
|
||||
this.y = netStream.readDouble();
|
||||
this.stance = netStream.readDouble();
|
||||
this.z = netStream.readDouble();
|
||||
this.onGround = netStream.readBoolean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-15
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.Location;
|
||||
import mc.core.Look;
|
||||
import mc.core.network.CSPacket;
|
||||
import mc.core.network.NetStream;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public class PositionAndLookPacket implements SCPacket, CSPacket {
|
||||
private Location location;
|
||||
private double stance;
|
||||
private Look look;
|
||||
private boolean onGround;
|
||||
|
||||
@Override
|
||||
public void readSelf(NetStream netStream) {
|
||||
double x = netStream.readDouble();
|
||||
double y = netStream.readDouble();
|
||||
stance = netStream.readDouble();
|
||||
double z = netStream.readDouble();
|
||||
float yaw = netStream.readFloat();
|
||||
float pitch = netStream.readFloat();
|
||||
onGround = netStream.readBoolean();
|
||||
|
||||
location = new Location(x, y, z);
|
||||
look = new Look(yaw, pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeDouble(location.getX());
|
||||
netStream.writeDouble(location.getY());
|
||||
netStream.writeDouble(stance);
|
||||
netStream.writeDouble(location.getZ());
|
||||
netStream.writeFloat(look.getYaw());
|
||||
netStream.writeFloat(look.getPitch());
|
||||
netStream.writeBoolean(onGround);
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-19
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.Location;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@Setter
|
||||
@ToString
|
||||
public class SpawnPositionPacket implements SCPacket {
|
||||
private Location location;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
|
||||
netStream.writeInt((int) location.getX());
|
||||
netStream.writeInt((int) location.getY());
|
||||
netStream.writeInt((int) location.getZ());
|
||||
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-21
|
||||
*/
|
||||
package mc.core.network.proto_125.packets;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.ByteArrayOutputNetStream;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Setter
|
||||
@ToString
|
||||
public class TimeUpdatePacket implements SCPacket {
|
||||
private long time;
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeLong(time);
|
||||
return netStream.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user